mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Add files endpoint
This commit is contained in:
@@ -7,12 +7,14 @@ import { errorHandler } from './error';
|
||||
import itemsRouter from './routes/items';
|
||||
import activityRouter from './routes/activity';
|
||||
import collectionPresetsRouter from './routes/collection-presets';
|
||||
import filesRouter from './routes/files';
|
||||
import notFoundHandler from './routes/not-found';
|
||||
|
||||
const app = express()
|
||||
.disable('x-powered-by')
|
||||
.use(bodyParser.json())
|
||||
.use('/items', itemsRouter)
|
||||
.use('/files', filesRouter)
|
||||
.use('/activity', activityRouter)
|
||||
.use('/collection_presets', collectionPresetsRouter)
|
||||
.use(notFoundHandler)
|
||||
|
||||
57
src/routes/files.ts
Normal file
57
src/routes/files.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import express from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import sanitizeQuery from '../middleware/sanitize-query';
|
||||
import validateQuery from '../middleware/validate-query';
|
||||
import * as FilesService from '../services/files';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/** @TODO This needs to support multipart form-data for file uploads */
|
||||
// router.post(
|
||||
// '/',
|
||||
// asyncHandler(async (req, res) => {
|
||||
// const records = await FilesService.createFile(
|
||||
// req.body,
|
||||
// res.locals.query
|
||||
// );
|
||||
// return res.json({ data: records });
|
||||
// })
|
||||
// );
|
||||
|
||||
router.get(
|
||||
'/',
|
||||
sanitizeQuery,
|
||||
validateQuery,
|
||||
asyncHandler(async (req, res) => {
|
||||
const records = await FilesService.readFiles(res.locals.query);
|
||||
return res.json({ data: records });
|
||||
})
|
||||
);
|
||||
|
||||
router.get(
|
||||
'/:pk',
|
||||
sanitizeQuery,
|
||||
validateQuery,
|
||||
asyncHandler(async (req, res) => {
|
||||
const record = await FilesService.readFile(req.params.pk, res.locals.query);
|
||||
return res.json({ data: record });
|
||||
})
|
||||
);
|
||||
|
||||
router.patch(
|
||||
'/:pk',
|
||||
asyncHandler(async (req, res) => {
|
||||
const records = await FilesService.updateFile(req.params.pk, req.body, res.locals.query);
|
||||
return res.json({ data: records });
|
||||
})
|
||||
);
|
||||
|
||||
router.delete(
|
||||
'/:pk',
|
||||
asyncHandler(async (req, res) => {
|
||||
await FilesService.deleteFile(req.params.pk);
|
||||
return res.status(200).end();
|
||||
})
|
||||
);
|
||||
|
||||
export default router;
|
||||
23
src/services/files.ts
Normal file
23
src/services/files.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Query } from '../types/query';
|
||||
import * as ItemsService from './items';
|
||||
|
||||
/** @TODO This is a little more involved ofc, circling back later */
|
||||
// export const createFile = async (data: Record<string, any>, query: Query) => {
|
||||
// return await ItemsService.createItem('directus_files', data, query);
|
||||
// };
|
||||
|
||||
export const readFiles = async (query: Query) => {
|
||||
return await ItemsService.readItems('directus_files', query);
|
||||
};
|
||||
|
||||
export const readFile = async (pk: string | number, query: Query) => {
|
||||
return await ItemsService.readItem('directus_files', pk, query);
|
||||
};
|
||||
|
||||
export const updateFile = async (pk: string | number, data: Record<string, any>, query: Query) => {
|
||||
return await ItemsService.updateItem('directus_files', pk, data, query);
|
||||
};
|
||||
|
||||
export const deleteFile = async (pk: string | number) => {
|
||||
await ItemsService.deleteItem('directus_files', pk);
|
||||
};
|
||||
Reference in New Issue
Block a user