use express types instead of custom versions

This commit is contained in:
rijkvanzanten
2021-01-12 16:06:35 -05:00
parent a41559b32f
commit c1f87113bf
3 changed files with 80998 additions and 35560 deletions

View File

@@ -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,

View File

@@ -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

File diff suppressed because it is too large Load Diff