mirror of
https://github.com/directus/directus.git
synced 2026-01-23 11:47:59 -05:00
use express types instead of custom versions
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import { Request, Router, Response, NextFunction } from 'express';
|
||||
import { Router, Response } from 'express';
|
||||
import { graphqlHTTP } from 'express-graphql';
|
||||
import { GraphQLService } from '../services';
|
||||
import { respond } from '../middleware/respond';
|
||||
import asyncHandler from '../utils/async-handler';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.use(
|
||||
asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
|
||||
asyncHandler(async (req, res, next) => {
|
||||
const service = new GraphQLService({
|
||||
accountability: req.accountability,
|
||||
schema: req.schema,
|
||||
|
||||
@@ -1,32 +1,19 @@
|
||||
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>;
|
||||
import { RequestHandler, ErrorRequestHandler } from 'express';
|
||||
|
||||
/**
|
||||
* 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);
|
||||
};
|
||||
function asyncHandler(handler: RequestHandler): RequestHandler;
|
||||
function asyncHandler(handler: ErrorRequestHandler): ErrorRequestHandler;
|
||||
function asyncHandler(handler: RequestHandler | ErrorRequestHandler): RequestHandler | ErrorRequestHandler {
|
||||
if (handler.length === 2 || handler.length === 3) {
|
||||
const scoped: RequestHandler = (req, res, next) =>
|
||||
Promise.resolve((handler as RequestHandler)(req, res, next)).catch(next);
|
||||
return scoped;
|
||||
} else if (handler.length === 4) {
|
||||
const scoped: ErrorRequestHandler = (err, req, res, next) =>
|
||||
Promise.resolve((handler as ErrorRequestHandler)(err, req, res, next)).catch(next);
|
||||
return scoped;
|
||||
} else {
|
||||
throw new Error(`Failed to asyncHandle() function "${handler.name}"`);
|
||||
}
|
||||
|
||||
116516
package-lock.json
generated
116516
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user