Replace express-async-handler for a local function.

This commit is contained in:
WoLfulus
2020-12-22 17:11:08 -03:00
parent 0cbf5ee418
commit c5f3802da3
30 changed files with 207 additions and 172 deletions

View File

@@ -91,7 +91,6 @@
"execa": "^4.1.0",
"exif-reader": "^1.0.3",
"express": "^4.17.1",
"express-async-handler": "^1.1.4",
"express-graphql": "^0.11.0",
"express-pino-logger": "^5.0.0",
"express-session": "^1.17.1",

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import { ActivityService, MetaService } from '../services';
import { Action } from '../types';
import { ForbiddenException } from '../exceptions';
@@ -12,7 +12,7 @@ router.use(useCollection('directus_activity'));
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new ActivityService({
accountability: req.accountability,
schema: req.schema,
@@ -37,7 +37,7 @@ router.get(
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new ActivityService({
accountability: req.accountability,
schema: req.schema,
@@ -55,7 +55,7 @@ router.get(
router.post(
'/comment',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new ActivityService({
accountability: req.accountability,
schema: req.schema,
@@ -90,7 +90,7 @@ router.post(
router.patch(
'/comment/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new ActivityService({
accountability: req.accountability,
schema: req.schema,
@@ -118,7 +118,7 @@ router.patch(
router.delete(
'/comment/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new ActivityService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { NextFunction, Request, Response, Router } from 'express';
import asyncHandler from '../utils/async-handler';
import database from '../database';
import { SYSTEM_ASSET_ALLOW_LIST, ASSET_TRANSFORM_QUERY_KEYS } from '../constants';
import { InvalidQueryException, ForbiddenException } from '../exceptions';
@@ -20,7 +20,7 @@ router.get(
'/:pk',
// Check if file exists
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const id = req.params.pk;
/**
@@ -44,7 +44,7 @@ router.get(
}),
// Validate query params
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const payloadService = new PayloadService('directus_settings', { schema: req.schema });
const defaults = { storage_asset_presets: [], storage_asset_transform: 'all' };
@@ -92,7 +92,7 @@ router.get(
}),
// Return file
asyncHandler(async (req, res) => {
asyncHandler(async (req: Request, res: Response) => {
const service = new AssetsService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,6 +1,6 @@
import { Router } from 'express';
import { NextFunction, Request, Response, Router } from 'express';
import session from 'express-session';
import asyncHandler from 'express-async-handler';
import asyncHandler from '../utils/async-handler';
import Joi from 'joi';
import grant from 'grant';
import getEmailFromProfile from '../utils/get-email-from-profile';
@@ -25,7 +25,7 @@ const loginSchema = Joi.object({
router.post(
'/login',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const accountability = {
ip: req.ip,
userAgent: req.get('user-agent'),
@@ -81,7 +81,7 @@ router.post(
router.post(
'/refresh',
cookieParser(),
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const accountability = {
ip: req.ip,
userAgent: req.get('user-agent'),
@@ -129,7 +129,7 @@ router.post(
router.post(
'/logout',
cookieParser(),
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const accountability = {
ip: req.ip,
userAgent: req.get('user-agent'),
@@ -155,7 +155,7 @@ router.post(
router.post(
'/password/request',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body.email) {
throw new InvalidPayloadException(`"email" field is required.`);
}
@@ -182,7 +182,7 @@ router.post(
router.post(
'/password/reset',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body.token) {
throw new InvalidPayloadException(`"token" field is required.`);
}
@@ -206,7 +206,7 @@ router.post(
router.get(
'/oauth',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const providers = toArray(env.OAUTH_PROVIDERS);
res.locals.payload = { data: env.OAUTH_PROVIDERS ? providers : null };
return next();
@@ -218,7 +218,7 @@ router.use('/oauth', session({ secret: env.SECRET as string, saveUninitialized:
router.get(
'/oauth/:provider',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const config = { ...grantConfig };
delete config.defaults;
@@ -241,7 +241,7 @@ router.use(grant.express()(grantConfig));
router.get(
'/oauth/:provider/callback',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const redirect = req.session.redirect;
const accountability = {

View File

@@ -1,5 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { NextFunction, Request, Response, Router } from 'express';
import asyncHandler from '../utils/async-handler';
import { CollectionsService, MetaService } from '../services';
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
import { respond } from '../middleware/respond';
@@ -8,7 +8,7 @@ const router = Router();
router.post(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
@@ -25,7 +25,7 @@ router.post(
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
@@ -46,7 +46,7 @@ router.get(
router.get(
'/:collection',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
@@ -73,7 +73,7 @@ router.get(
router.patch(
'/:collection',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
@@ -101,7 +101,7 @@ router.patch(
router.delete(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body || Array.isArray(req.body) === false) {
throw new InvalidPayloadException(`Body has to be an array of primary keys`);
}
@@ -119,7 +119,7 @@ router.delete(
router.delete(
'/:collection',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import express, { Router } from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response, Router } from 'express';
import asyncHandler from '../utils/async-handler';
import { RouteNotFoundException } from '../exceptions';
import { listExtensions } from '../extensions';
import env from '../env';
@@ -12,7 +12,7 @@ router.use(express.static(extensionsPath));
router.get(
'/:type',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const typeAllowList = ['interfaces', 'layouts', 'displays', 'modules'];
if (typeAllowList.includes(req.params.type) === false) {

View File

@@ -1,5 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { NextFunction, Request, Response, Router } from 'express';
import asyncHandler from '../utils/async-handler';
import { FieldsService } from '../services/fields';
import validateCollection from '../middleware/collection-exists';
import { InvalidPayloadException, ForbiddenException } from '../exceptions';
@@ -14,7 +14,7 @@ router.use(useCollection('directus_fields'));
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FieldsService({
accountability: req.accountability,
schema: req.schema,
@@ -30,7 +30,7 @@ router.get(
router.get(
'/:collection',
validateCollection,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FieldsService({
accountability: req.accountability,
schema: req.schema,
@@ -46,7 +46,7 @@ router.get(
router.get(
'/:collection/:field',
validateCollection,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FieldsService({
accountability: req.accountability,
schema: req.schema,
@@ -78,7 +78,7 @@ const newFieldSchema = Joi.object({
router.post(
'/:collection',
validateCollection,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body.schema && !req.body.meta) throw new InvalidPayloadException(`"schema" or "meta" is required`);
const service = new FieldsService({
@@ -115,7 +115,7 @@ router.post(
router.patch(
'/:collection',
validateCollection,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FieldsService({
accountability: req.accountability,
schema: req.schema,
@@ -153,7 +153,7 @@ router.patch(
'/:collection/:field',
validateCollection,
// @todo: validate field
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FieldsService({
accountability: req.accountability,
schema: req.schema,
@@ -183,7 +183,7 @@ router.patch(
router.delete(
'/:collection/:field',
validateCollection,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FieldsService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import Busboy from 'busboy';
import { MetaService, FilesService } from '../services';
import { File, PrimaryKey } from '../types';
@@ -18,7 +18,7 @@ const router = express.Router();
router.use(useCollection('directus_files'));
const multipartHandler = asyncHandler(async (req, res, next) => {
const multipartHandler = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (req.is('multipart/form-data') === false) return next();
const busboy = new Busboy({ headers: req.headers });
@@ -97,7 +97,7 @@ const multipartHandler = asyncHandler(async (req, res, next) => {
router.post(
'/',
multipartHandler,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FilesService({
accountability: req.accountability,
schema: req.schema,
@@ -137,7 +137,7 @@ const importSchema = Joi.object({
router.post(
'/import',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const { error } = importSchema.validate(req.body);
if (error) {
@@ -184,7 +184,7 @@ router.post(
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FilesService({
accountability: req.accountability,
schema: req.schema,
@@ -205,7 +205,7 @@ router.get(
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const keys = req.params.pk.includes(',') ? req.params.pk.split(',') : req.params.pk;
const service = new FilesService({
accountability: req.accountability,
@@ -221,7 +221,7 @@ router.get(
router.patch(
'/:pk',
multipartHandler,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FilesService({
accountability: req.accountability,
schema: req.schema,
@@ -253,7 +253,7 @@ router.patch(
router.delete(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body || Array.isArray(req.body) === false) {
throw new InvalidPayloadException(`Body has to be an array of primary keys`);
}
@@ -270,7 +270,7 @@ router.delete(
router.delete(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const keys = req.params.pk.includes(',') ? req.params.pk.split(',') : req.params.pk;
const service = new FilesService({
accountability: req.accountability,

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import { FoldersService, MetaService } from '../services';
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
import useCollection from '../middleware/use-collection';
@@ -12,7 +12,7 @@ router.use(useCollection('directus_folders'));
router.post(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FoldersService({
accountability: req.accountability,
schema: req.schema,
@@ -37,7 +37,7 @@ router.post(
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FoldersService({
accountability: req.accountability,
schema: req.schema,
@@ -58,7 +58,7 @@ router.get(
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FoldersService({
accountability: req.accountability,
schema: req.schema,
@@ -74,7 +74,7 @@ router.get(
router.patch(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FoldersService({
accountability: req.accountability,
schema: req.schema,
@@ -100,7 +100,7 @@ router.patch(
router.delete(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body || Array.isArray(req.body) === false) {
throw new InvalidPayloadException(`Body has to be an array of primary keys`);
}
@@ -117,7 +117,7 @@ router.delete(
router.delete(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new FoldersService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,12 +1,12 @@
import { Router } from 'express';
import { Request, Response, Router } from 'express';
import { graphqlHTTP } from 'express-graphql';
import { GraphQLService } from '../services';
import asyncHandler from 'express-async-handler';
import asyncHandler from '../utils/async-handler';
const router = Router();
router.use(
asyncHandler(async (req, res) => {
asyncHandler(async (req: Request, res: Response) => {
const service = new GraphQLService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import collectionExists from '../middleware/collection-exists';
import { ItemsService, MetaService } from '../services';
import { RouteNotFoundException, ForbiddenException, FailedValidationException } from '../exceptions';
@@ -13,7 +13,7 @@ const router = express.Router();
router.post(
'/:collection',
collectionExists,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (req.singleton) {
throw new RouteNotFoundException(req.path);
}
@@ -43,7 +43,7 @@ router.post(
router.get(
'/:collection',
collectionExists,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new ItemsService(req.collection, {
accountability: req.accountability,
schema: req.schema,
@@ -73,7 +73,7 @@ router.get(
router.get(
'/:collection/:pk',
collectionExists,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (req.singleton) {
throw new RouteNotFoundException(req.path);
}
@@ -96,7 +96,7 @@ router.get(
router.patch(
'/:collection',
collectionExists,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new ItemsService(req.collection, {
accountability: req.accountability,
schema: req.schema,
@@ -159,7 +159,7 @@ router.patch(
router.patch(
'/:collection/:pk',
collectionExists,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (req.singleton) {
throw new RouteNotFoundException(req.path);
}
@@ -191,7 +191,7 @@ router.patch(
router.delete(
'/:collection',
collectionExists,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body || Array.isArray(req.body) === false) {
throw new InvalidPayloadException(`Body has to be an array of primary keys`);
}
@@ -209,7 +209,7 @@ router.delete(
router.delete(
'/:collection/:pk',
collectionExists,
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new ItemsService(req.collection, {
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import { PermissionsService, MetaService } from '../services';
import { clone } from 'lodash';
import { InvalidCredentialsException, ForbiddenException, InvalidPayloadException } from '../exceptions';
@@ -13,7 +13,7 @@ router.use(useCollection('directus_permissions'));
router.post(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new PermissionsService({
accountability: req.accountability,
schema: req.schema,
@@ -37,7 +37,7 @@ router.post(
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new PermissionsService({
accountability: req.accountability,
schema: req.schema,
@@ -58,7 +58,7 @@ router.get(
router.get(
'/me',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.accountability?.user) {
throw new InvalidCredentialsException();
}
@@ -83,7 +83,7 @@ router.get(
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (req.path.endsWith('me')) return next();
const service = new PermissionsService({
accountability: req.accountability,
@@ -100,7 +100,7 @@ router.get(
router.patch(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new PermissionsService({
accountability: req.accountability,
schema: req.schema,
@@ -126,7 +126,7 @@ router.patch(
router.delete(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body || Array.isArray(req.body) === false) {
throw new InvalidPayloadException(`Body has to be an array of primary keys`);
}
@@ -143,7 +143,7 @@ router.delete(
router.delete(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new PermissionsService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import { PresetsService, MetaService } from '../services';
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
import useCollection from '../middleware/use-collection';
@@ -12,7 +12,7 @@ router.use(useCollection('directus_presets'));
router.post(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new PresetsService({
accountability: req.accountability,
schema: req.schema,
@@ -37,7 +37,7 @@ router.post(
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new PresetsService({
accountability: req.accountability,
schema: req.schema,
@@ -58,7 +58,7 @@ router.get(
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new PresetsService({
accountability: req.accountability,
schema: req.schema,
@@ -74,7 +74,7 @@ router.get(
router.patch(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new PresetsService({
accountability: req.accountability,
schema: req.schema,
@@ -100,7 +100,7 @@ router.patch(
router.delete(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body || Array.isArray(req.body) === false) {
throw new InvalidPayloadException(`Body has to be an array of primary keys`);
}
@@ -117,7 +117,7 @@ router.delete(
router.delete(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new PresetsService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import { RelationsService, MetaService } from '../services';
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
import useCollection from '../middleware/use-collection';
@@ -12,7 +12,7 @@ router.use(useCollection('directus_relations'));
router.post(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RelationsService({
accountability: req.accountability,
schema: req.schema,
@@ -37,7 +37,7 @@ router.post(
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RelationsService({
accountability: req.accountability,
schema: req.schema,
@@ -58,7 +58,7 @@ router.get(
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RelationsService({
accountability: req.accountability,
schema: req.schema,
@@ -73,7 +73,7 @@ router.get(
router.patch(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RelationsService({
accountability: req.accountability,
schema: req.schema,
@@ -99,7 +99,7 @@ router.patch(
router.delete(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body || Array.isArray(req.body) === false) {
throw new InvalidPayloadException(`Body has to be an array of primary keys`);
}
@@ -116,7 +116,7 @@ router.delete(
router.delete(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RelationsService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import { RevisionsService, MetaService } from '../services';
import useCollection from '../middleware/use-collection';
import { respond } from '../middleware/respond';
@@ -10,7 +10,7 @@ router.use(useCollection('directus_revisions'));
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RevisionsService({
accountability: req.accountability,
schema: req.schema,
@@ -31,7 +31,7 @@ router.get(
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RevisionsService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import { RolesService, MetaService } from '../services';
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
import useCollection from '../middleware/use-collection';
@@ -12,7 +12,7 @@ router.use(useCollection('directus_roles'));
router.post(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RolesService({
accountability: req.accountability,
schema: req.schema,
@@ -37,7 +37,7 @@ router.post(
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RolesService({
accountability: req.accountability,
schema: req.schema,
@@ -58,7 +58,7 @@ router.get(
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RolesService({
accountability: req.accountability,
schema: req.schema,
@@ -73,7 +73,7 @@ router.get(
router.patch(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RolesService({
accountability: req.accountability,
schema: req.schema,
@@ -99,7 +99,7 @@ router.patch(
router.delete(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body || Array.isArray(req.body) === false) {
throw new InvalidPayloadException(`Body has to be an array of primary keys`);
}
@@ -116,7 +116,7 @@ router.delete(
router.delete(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RolesService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,14 +1,14 @@
import { Router } from 'express';
import { NextFunction, Request, Response, Router } from 'express';
import { ServerService } from '../services';
import { SpecificationService } from '../services';
import asyncHandler from 'express-async-handler';
import asyncHandler from '../utils/async-handler';
import { respond } from '../middleware/respond';
const router = Router();
router.get(
'/specs/oas',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new SpecificationService({
accountability: req.accountability,
schema: req.schema,
@@ -23,7 +23,7 @@ router.get('/ping', (req, res) => res.send('pong'));
router.get(
'/info',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new ServerService({
accountability: req.accountability,
schema: req.schema,
@@ -37,7 +37,7 @@ router.get(
router.get(
'/health',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new ServerService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import { SettingsService } from '../services';
import { ForbiddenException } from '../exceptions';
import useCollection from '../middleware/use-collection';
@@ -11,7 +11,7 @@ router.use(useCollection('directus_settings'));
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new SettingsService({
accountability: req.accountability,
schema: req.schema,
@@ -25,7 +25,7 @@ router.get(
router.patch(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new SettingsService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import Joi from 'joi';
import { InvalidPayloadException, InvalidCredentialsException, ForbiddenException } from '../exceptions';
import { UsersService, MetaService, AuthenticationService } from '../services';
@@ -13,7 +13,7 @@ router.use(useCollection('directus_users'));
router.post(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new UsersService({
accountability: req.accountability,
schema: req.schema,
@@ -38,7 +38,7 @@ router.post(
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new UsersService({
accountability: req.accountability,
schema: req.schema,
@@ -59,7 +59,7 @@ router.get(
router.get(
'/me',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.accountability?.user) {
throw new InvalidCredentialsException();
}
@@ -88,7 +88,7 @@ router.get(
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (req.path.endsWith('me')) return next();
const service = new UsersService({
accountability: req.accountability,
@@ -104,7 +104,7 @@ router.get(
router.patch(
'/me',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.accountability?.user) {
throw new InvalidCredentialsException();
}
@@ -124,7 +124,7 @@ router.patch(
router.patch(
'/me/track/page',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.accountability?.user) {
throw new InvalidCredentialsException();
}
@@ -143,7 +143,7 @@ router.patch(
router.patch(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new UsersService({
accountability: req.accountability,
schema: req.schema,
@@ -169,7 +169,7 @@ router.patch(
router.delete(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body || Array.isArray(req.body) === false) {
throw new InvalidPayloadException(`Body has to be an array of primary keys`);
}
@@ -187,7 +187,7 @@ router.delete(
router.delete(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new UsersService({
accountability: req.accountability,
schema: req.schema,
@@ -207,7 +207,7 @@ const inviteSchema = Joi.object({
router.post(
'/invite',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const { error } = inviteSchema.validate(req.body);
if (error) throw new InvalidPayloadException(error.message);
@@ -228,7 +228,7 @@ const acceptInviteSchema = Joi.object({
router.post(
'/invite/accept',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const { error } = acceptInviteSchema.validate(req.body);
if (error) throw new InvalidPayloadException(error.message);
const service = new UsersService({
@@ -243,7 +243,7 @@ router.post(
router.post(
'/me/tfa/enable/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.accountability?.user) {
throw new InvalidCredentialsException();
}
@@ -273,7 +273,7 @@ router.post(
router.post(
'/me/tfa/disable',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.accountability?.user) {
throw new InvalidCredentialsException();
}

View File

@@ -1,5 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { NextFunction, Request, Response, Router } from 'express';
import asyncHandler from '../utils/async-handler';
import { nanoid } from 'nanoid';
import { InvalidQueryException, InvalidPayloadException } from '../exceptions';
import argon2 from 'argon2';
@@ -12,7 +12,7 @@ const router = Router();
router.get(
'/random/string',
asyncHandler(async (req, res) => {
asyncHandler(async (req: Request, res: Response) => {
if (req.query && req.query.length && Number(req.query.length) > 500)
throw new InvalidQueryException(`"length" can't be more than 500 characters`);
@@ -25,7 +25,7 @@ router.get(
router.post(
'/hash/generate',
asyncHandler(async (req, res) => {
asyncHandler(async (req: Request, res: Response) => {
if (!req.body?.string) {
throw new InvalidPayloadException(`"string" is required`);
}
@@ -39,7 +39,7 @@ router.post(
router.post(
'/hash/verify',
asyncHandler(async (req, res) => {
asyncHandler(async (req: Request, res: Response) => {
if (!req.body?.string) {
throw new InvalidPayloadException(`"string" is required`);
}
@@ -63,7 +63,7 @@ const SortSchema = Joi.object({
router.post(
'/sort/:collection',
collectionExists,
asyncHandler(async (req, res) => {
asyncHandler(async (req: Request, res: Response) => {
const { error } = SortSchema.validate(req.body);
if (error) throw new InvalidPayloadException(error.message);
@@ -80,7 +80,7 @@ router.post(
router.post(
'/revert/:revision',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new RevisionsService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,5 +1,5 @@
import express from 'express';
import asyncHandler from 'express-async-handler';
import express, { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import { WebhooksService, MetaService } from '../services';
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
import useCollection from '../middleware/use-collection';
@@ -12,7 +12,7 @@ router.use(useCollection('directus_webhooks'));
router.post(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new WebhooksService({
accountability: req.accountability,
schema: req.schema,
@@ -37,7 +37,7 @@ router.post(
router.get(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new WebhooksService({
accountability: req.accountability,
schema: req.schema,
@@ -58,7 +58,7 @@ router.get(
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new WebhooksService({
accountability: req.accountability,
schema: req.schema,
@@ -74,7 +74,7 @@ router.get(
router.patch(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new WebhooksService({
accountability: req.accountability,
schema: req.schema,
@@ -100,7 +100,7 @@ router.patch(
router.delete(
'/',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.body || Array.isArray(req.body) === false) {
throw new InvalidPayloadException(`Body has to be an array of primary keys`);
}
@@ -118,7 +118,7 @@ router.delete(
router.delete(
'/:pk',
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const service = new WebhooksService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -1,15 +1,15 @@
import { RequestHandler } from 'express';
import { NextFunction, Request, RequestHandler, Response } from 'express';
import jwt, { TokenExpiredError, JsonWebTokenError } from 'jsonwebtoken';
import isJWT from '../utils/is-jwt';
import database from '../database';
import asyncHandler from 'express-async-handler';
import asyncHandler from '../utils/async-handler';
import { InvalidCredentialsException } from '../exceptions';
import env from '../env';
/**
* Verify the passed JWT and assign the user ID and role to `req`
*/
const authenticate: RequestHandler = asyncHandler(async (req, res, next) => {
const authenticate: RequestHandler = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
req.accountability = {
user: null,
role: null,

View File

@@ -1,11 +1,11 @@
import { RequestHandler } from 'express';
import asyncHandler from 'express-async-handler';
import { NextFunction, Request, RequestHandler, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import env from '../env';
import { getCacheKey } from '../utils/get-cache-key';
import cache from '../cache';
const checkCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
const checkCacheMiddleware: RequestHandler = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (req.method.toLowerCase() !== 'get') return next();
if (env.CACHE_ENABLED !== true) return next();
if (!cache) return next();

View File

@@ -1,9 +1,9 @@
import { RequestHandler } from 'express';
import asyncHandler from 'express-async-handler';
import { NextFunction, Request, RequestHandler, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import database from '../database';
import { InvalidIPException } from '../exceptions';
export const checkIP: RequestHandler = asyncHandler(async (req, res, next) => {
export const checkIP: RequestHandler = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const role = await database
.select('ip_access')
.from('directus_roles')

View File

@@ -2,13 +2,13 @@
* Check if requested collection exists, and save it to req.collection
*/
import { RequestHandler } from 'express';
import asyncHandler from 'express-async-handler';
import { NextFunction, Request, RequestHandler, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import database from '../database';
import { ForbiddenException } from '../exceptions';
import { systemCollectionRows } from '../database/system-data/collections';
const collectionExists: RequestHandler = asyncHandler(async (req, res, next) => {
const collectionExists: RequestHandler = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
if (!req.params.collection) return next();
if (req.params.collection in req.schema === false) {

View File

@@ -1,5 +1,5 @@
import { RequestHandler } from 'express';
import asyncHandler from 'express-async-handler';
import { NextFunction, Request, RequestHandler, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import {
RateLimiterMemory,
RateLimiterRedis,
@@ -21,7 +21,7 @@ if (env.RATE_LIMITER_ENABLED === true) {
rateLimiter = getRateLimiter();
checkRateLimit = asyncHandler(async (req, res, next) => {
checkRateLimit = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
try {
await rateLimiter.consume(req.ip, 1);
} catch (rateLimiterRes) {

View File

@@ -1,5 +1,5 @@
import { RequestHandler } from 'express';
import asyncHandler from 'express-async-handler';
import { Request, RequestHandler, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import env from '../env';
import { getCacheKey } from '../utils/get-cache-key';
import cache from '../cache';
@@ -7,7 +7,7 @@ import { Transform, transforms } from 'json2csv';
import { PassThrough } from 'stream';
import ms from 'ms';
export const respond: RequestHandler = asyncHandler(async (req, res) => {
export const respond: RequestHandler = asyncHandler(async (req: Request, res: Response) => {
if (
req.method.toLowerCase() === 'get' &&
env.CACHE_ENABLED === true &&

View File

@@ -1,9 +1,9 @@
import { RequestHandler } from 'express';
import asyncHandler from 'express-async-handler';
import { NextFunction, Request, RequestHandler, Response } from 'express';
import asyncHandler from '../utils/async-handler';
import { schemaInspector } from '../database';
import logger from '../logger';
const getSchema: RequestHandler = asyncHandler(async (req, res, next) => {
const getSchema: RequestHandler = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const schemaOverview = await schemaInspector.overview();
for (const [collection, info] of Object.entries(schemaOverview)) {

View File

@@ -2,10 +2,11 @@
* Set req.collection for use in other middleware. Used as an alternative on validate-collection for
* system collections
*/
import asyncHandler from 'express-async-handler';
import { NextFunction, Request, Response } from 'express';
import asyncHandler from '../utils/async-handler';
const useCollection = (collection: string) =>
asyncHandler(async (req, res, next) => {
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
req.collection = collection;
next();
});

View File

@@ -0,0 +1,35 @@
import { NextFunction, Request, Response } from 'express';
export type AsyncHandler<T> = (req: Request, res: Response, ...others: any) => Promise<T>;
export type AsyncHandlerNext<T> = (req: Request, res: Response, next: NextFunction, ...others: any) => Promise<T>;
export type AsyncHandlerError<T> = (
err: Error,
req: Request,
res: Response,
next: NextFunction,
...others: any
) => Promise<T>;
export type AsyncMiddleware<T> = AsyncHandler<T> | AsyncHandlerNext<T> | AsyncHandlerError<T>;
/**
* Handles promises in routes.
*/
function asyncHandler<T>(handler: AsyncMiddleware<T>): any {
if (handler.length == 2) {
return function (req: Request, res: Response, next: NextFunction, ...others: any) {
return Promise.resolve((handler as AsyncHandler<T>)(req, res, ...others)).catch(next);
};
} else if (handler.length == 3) {
return function (req: Request, res: Response, next: NextFunction, ...others: any) {
return Promise.resolve((handler as AsyncHandlerNext<T>)(req, res, next, ...others)).catch(next);
};
} else if (handler.length == 4) {
return function (err: Error, req: Request, res: Response, next: NextFunction, ...others: any) {
return Promise.resolve((handler as AsyncHandlerError<T>)(err, req, res, next, ...others)).catch(next);
};
} else {
throw new Error(`Failed to asyncHandle() function "${handler.name}"`);
}
}
export default asyncHandler;