mirror of
https://github.com/directus/directus.git
synced 2026-01-29 16:28:02 -05:00
csv export
This commit is contained in:
938
api/package-lock.json
generated
938
api/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -79,6 +79,7 @@
|
||||
"commander": "^5.1.0",
|
||||
"cookie-parser": "^1.4.5",
|
||||
"cors": "^2.8.5",
|
||||
"csv-express": "^1.2.2",
|
||||
"dotenv": "^8.2.0",
|
||||
"execa": "^4.0.3",
|
||||
"exif-reader": "^1.0.3",
|
||||
@@ -92,6 +93,7 @@
|
||||
"inquirer": "^7.3.3",
|
||||
"joi": "^17.1.1",
|
||||
"js-yaml": "^3.14.0",
|
||||
"json2csv": "^5.0.1",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"knex": "^0.21.4",
|
||||
"knex-schema-inspector": "0.0.9",
|
||||
|
||||
@@ -10,6 +10,7 @@ import errorHandler from './middleware/error-handler';
|
||||
|
||||
import extractToken from './middleware/extract-token';
|
||||
import authenticate from './middleware/authenticate';
|
||||
import responseManager from './middleware/response-manager';
|
||||
|
||||
import activityRouter from './controllers/activity';
|
||||
import assetsRouter from './controllers/assets';
|
||||
@@ -69,11 +70,9 @@ if (env.NODE_ENV !== 'development') {
|
||||
|
||||
app.use('/auth', authRouter)
|
||||
|
||||
.use(authenticate)
|
||||
|
||||
.use('/activity', activityRouter)
|
||||
.use('/assets', assetsRouter)
|
||||
.use('/collections', collectionsRouter)
|
||||
.use('/collections', collectionsRouter, responseManager)
|
||||
.use('/extensions', extensionsRouter)
|
||||
.use('/fields', fieldsRouter)
|
||||
.use('/files', filesRouter)
|
||||
|
||||
7
api/src/exceptions/export-failed.ts
Normal file
7
api/src/exceptions/export-failed.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { BaseException } from './base';
|
||||
|
||||
export class ExportFailedException extends BaseException {
|
||||
constructor(message: string) {
|
||||
super(message, 500, 'EXPORT_FAILED');
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
export * from './base';
|
||||
export * from './collection-not-found';
|
||||
export * from './export-failed';
|
||||
export * from './field-not-found';
|
||||
export * from './forbidden';
|
||||
export * from './invalid-credentials';
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
import { RequestHandler } from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import fs from 'fs';
|
||||
|
||||
/**
|
||||
* middleware to manage actions on responses such as
|
||||
* export / import and caching
|
||||
* @todo move caching into here.
|
||||
*
|
||||
*/
|
||||
|
||||
const responseManager: RequestHandler = asyncHandler(async (req, res, next) => {
|
||||
if (!req.query.export) return next();
|
||||
|
||||
const exportType = req.query.export;
|
||||
|
||||
if (exportType === 'json') {
|
||||
// have chose to export json
|
||||
}
|
||||
|
||||
if (exportType === 'csv') {
|
||||
// have chosen to export csv
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
export default responseManager;
|
||||
44
api/src/middleware/response-manager.ts
Normal file
44
api/src/middleware/response-manager.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { RequestHandler } from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import { ExportFailedException } from '../exceptions';
|
||||
|
||||
/**
|
||||
* middleware to manage actions on responses such as
|
||||
* export / import and caching
|
||||
* @todo move caching into here.
|
||||
*
|
||||
*/
|
||||
|
||||
const responseManager: RequestHandler = asyncHandler(async (req, res, next) => {
|
||||
if (!req.query.export) return next();
|
||||
// only want to export out on get
|
||||
if (req.method == 'GET') {
|
||||
const exportType = req.query.export;
|
||||
|
||||
if (exportType == 'json') {
|
||||
// have chosen to export json
|
||||
}
|
||||
|
||||
if (exportType == 'csv') {
|
||||
// have chosen to export csv
|
||||
console.log('get here');
|
||||
const { Parser } = require('json2csv');
|
||||
const exportData = res.json;
|
||||
|
||||
const fields = Object.keys(exportData);
|
||||
|
||||
Parser({ data: exportData }, function (err: any, csvStr: string) {
|
||||
if (err) {
|
||||
throw new ExportFailedException('CSV generation failed');
|
||||
}
|
||||
res.type('text/csv');
|
||||
res.attachment('export-file.csv');
|
||||
res.send(csvStr);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
export default responseManager;
|
||||
Reference in New Issue
Block a user