UNPKG

@mark01/express-utils

Version:

npm package that contains utilities for express.js

22 lines (21 loc) 1.12 kB
import { RequestHandler } from 'express'; import { Request, Response } from 'express'; import { ResultAsync } from 'neverthrow'; import { z, ZodFormattedError } from 'zod'; import { AppError } from '../error'; export type ResultAsyncRequestSchemas<TParams, TQuery, TBody, TResponse> = { params?: z.ZodType<TParams>; query?: z.ZodType<TQuery>; body?: z.ZodType<TBody>; response?: z.ZodType<TResponse>; }; export interface ResultAsyncErrorListItem<TParams, TQuery, TBody> { type: 'Params' | 'Body' | 'Query' | 'Response'; errors: ZodFormattedError<TParams | TQuery | TBody, string>; } export type ResultAsyncRequestCallback<TParams, TQuery, TBody, TResponse> = (data: { params: TParams; query: TQuery; body: TBody; }, req: Request, res: Response) => ResultAsync<TResponse, AppError>; export declare const resultAsyncController: <TParams = unknown, TQuery = unknown, TBody = unknown, TResponse = unknown>(schemas: ResultAsyncRequestSchemas<TParams, TQuery, TBody, TResponse>, cb: ResultAsyncRequestCallback<TParams, TQuery, TBody, TResponse>, successStatusCode?: number) => RequestHandler;