expressentials 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +116 -0
- package/dist/index.cjs +530 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +268 -0
- package/dist/index.d.ts +268 -0
- package/dist/index.js +467 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/status/index.ts","../src/message/index.ts","../src/errors/api-error.ts","../src/errors/http-error.ts","../src/logger/index.ts","../src/context/index.ts","../src/middleware/error-handler.ts","../src/middleware/request-id.ts","../src/middleware/request-context.ts","../src/middleware/http-logger.ts","../src/middleware/validate.ts","../src/middleware/async-handler.ts","../src/middleware/timeout.ts","../src/middleware/health-check.ts"],"sourcesContent":["export { status } from \"./status/index.js\";\nexport { message } from \"./message/index.js\";\nexport { ApiError } from \"./errors/api-error.js\";\nexport {\n NotFound,\n BadRequest,\n Unauthorized,\n Forbidden,\n Conflict,\n InternalServerError,\n ServiceUnavailable,\n TooManyRequests,\n ValidationError,\n GatewayTimeout,\n} from \"./errors/http-error.js\";\nexport { Logger, createLogger } from \"./logger/index.js\";\nexport { getRequestContext, getLogger, getRequestId, runWithContext } from \"./context/index.js\";\nexport {\n errorHandler,\n requestId,\n requestContext,\n httpLogger,\n validate,\n asyncHandler,\n timeout,\n healthCheck,\n} from \"./middleware/index.js\";\n","export const status = {\n // 1xx Informational\n continue: 100,\n switchingProtocols: 101,\n processing: 102,\n earlyHints: 103,\n\n // 2xx Success\n ok: 200,\n created: 201,\n accepted: 202,\n nonAuthoritativeInformation: 203,\n noContent: 204,\n resetContent: 205,\n partialContent: 206,\n\n // 3xx Redirection\n multipleChoices: 300,\n movedPermanently: 301,\n found: 302,\n seeOther: 303,\n notModified: 304,\n useProxy: 305,\n temporaryRedirect: 307,\n permanentRedirect: 308,\n\n // 4xx Client Error\n badRequest: 400,\n unauthorized: 401,\n paymentRequired: 402,\n forbidden: 403,\n notFound: 404,\n methodNotAllowed: 405,\n notAcceptable: 406,\n proxyAuthenticationRequired: 407,\n requestTimeout: 408,\n conflict: 409,\n gone: 410,\n lengthRequired: 411,\n preconditionFailed: 412,\n payloadTooLarge: 413,\n uriTooLong: 414,\n unsupportedMediaType: 415,\n rangeNotSatisfiable: 416,\n expectationFailed: 417,\n imATeapot: 418,\n unprocessableEntity: 422,\n tooEarly: 425,\n upgradeRequired: 426,\n preconditionRequired: 428,\n tooManyRequests: 429,\n requestHeaderFieldsTooLarge: 431,\n unavailableForLegalReasons: 451,\n\n // 5xx Server Error\n internalServerError: 500,\n notImplemented: 501,\n badGateway: 502,\n serviceUnavailable: 503,\n gatewayTimeout: 504,\n httpVersionNotSupported: 505,\n insufficientStorage: 507,\n loopDetected: 508,\n notExtended: 510,\n networkAuthenticationRequired: 511,\n} as const;\n","export const message = {\n // 1xx Informational\n continue: \"Continue\",\n switchingProtocols: \"Switching Protocols\",\n processing: \"Processing request\",\n earlyHints: \"Early hints available\",\n\n // 2xx Success\n ok: \"Success\",\n created: \"Created successfully\",\n accepted: \"Request accepted\",\n nonAuthoritativeInformation: \"Non-authoritative information\",\n noContent: \"No content\",\n resetContent: \"Reset content\",\n partialContent: \"Partial content\",\n\n // 3xx Redirection\n multipleChoices: \"Multiple redirect options available\",\n movedPermanently: \"Resource moved permanently\",\n found: \"Resource found elsewhere\",\n seeOther: \"See other resource\",\n notModified: \"Resource not modified\",\n useProxy: \"Use proxy to access resource\",\n temporaryRedirect: \"Resource temporarily moved\",\n permanentRedirect: \"Resource permanently moved\",\n\n // 4xx Client Error\n badRequest: \"Invalid request\",\n unauthorized: \"Authentication required\",\n paymentRequired: \"Payment required\",\n forbidden: \"You don't have permission to access this resource\",\n notFound: \"Resource not found\",\n methodNotAllowed: \"Method not allowed for this resource\",\n notAcceptable: \"Requested format not available\",\n proxyAuthenticationRequired: \"Proxy authentication required\",\n requestTimeout: \"Request timed out\",\n conflict: \"Resource conflict detected\",\n gone: \"Resource no longer available\",\n lengthRequired: \"Content length header is required\",\n preconditionFailed: \"Request precondition failed\",\n payloadTooLarge: \"Request payload exceeds size limit\",\n uriTooLong: \"Request URI exceeds length limit\",\n unsupportedMediaType: \"Unsupported media type\",\n rangeNotSatisfiable: \"Requested range is not available\",\n expectationFailed: \"Request expectation could not be met\",\n imATeapot: \"I'm a teapot\",\n unprocessableEntity: \"Unable to process request entity\",\n tooEarly: \"Request sent too early, retry later\",\n upgradeRequired: \"Protocol upgrade required\",\n preconditionRequired: \"Precondition header is required\",\n tooManyRequests: \"Too many requests, please slow down\",\n requestHeaderFieldsTooLarge: \"Request headers exceed size limit\",\n unavailableForLegalReasons: \"Resource unavailable for legal reasons\",\n\n // 5xx Server Error\n internalServerError: \"Something went wrong on our end\",\n notImplemented: \"Feature not yet implemented\",\n badGateway: \"Bad gateway response from upstream\",\n serviceUnavailable: \"Service temporarily unavailable, try again later\",\n gatewayTimeout: \"Upstream server timed out\",\n httpVersionNotSupported: \"HTTP version not supported by server\",\n insufficientStorage: \"Server has insufficient storage\",\n loopDetected: \"Infinite request loop detected\",\n notExtended: \"Server cannot fulfill extended request\",\n networkAuthenticationRequired: \"Network authentication required\",\n} as const;\n","import { status } from \"../status/index.js\";\nimport { message } from \"../message/index.js\";\n\nexport class ApiError extends Error {\n constructor(\n public readonly statusCode: number,\n message?: string,\n public readonly details?: unknown,\n ) {\n super(message ??= getDefaultMessage(statusCode));\n this.name = \"ApiError\";\n }\n\n static notFound(message?: string) {\n return new ApiError(status.notFound, message);\n }\n\n static badRequest(message?: string) {\n return new ApiError(status.badRequest, message);\n }\n\n static unauthorized(message?: string) {\n return new ApiError(status.unauthorized, message);\n }\n\n static forbidden(message?: string) {\n return new ApiError(status.forbidden, message);\n }\n\n static conflict(message?: string) {\n return new ApiError(status.conflict, message);\n }\n\n static internalServerError(message?: string) {\n return new ApiError(status.internalServerError, message);\n }\n\n static serviceUnavailable(message?: string) {\n return new ApiError(status.serviceUnavailable, message);\n }\n\n static tooManyRequests(message?: string) {\n return new ApiError(status.tooManyRequests, message);\n }\n}\n\nfunction getDefaultMessage(code: number): string {\n const entry = Object.entries(status).find(([, v]) => v === code);\n if (!entry) return \"Error\";\n return message[entry[0] as keyof typeof message];\n}\n","import { ApiError } from \"./api-error.js\";\n\nexport class NotFound extends ApiError {\n constructor(message?: string) {\n super(404, message);\n }\n}\n\nexport class BadRequest extends ApiError {\n constructor(message?: string) {\n super(400, message);\n }\n}\n\nexport class Unauthorized extends ApiError {\n constructor(message?: string) {\n super(401, message);\n }\n}\n\nexport class Forbidden extends ApiError {\n constructor(message?: string) {\n super(403, message);\n }\n}\n\nexport class Conflict extends ApiError {\n constructor(message?: string) {\n super(409, message);\n }\n}\n\nexport class InternalServerError extends ApiError {\n constructor(message?: string) {\n super(500, message);\n }\n}\n\nexport class ServiceUnavailable extends ApiError {\n constructor(message?: string) {\n super(503, message);\n }\n}\n\nexport class TooManyRequests extends ApiError {\n constructor(message?: string) {\n super(429, message);\n }\n}\n\nexport class ValidationError extends ApiError {\n constructor(message?: string, details?: unknown) {\n super(422, message ?? \"Validation failed\", details);\n }\n}\n\nexport class GatewayTimeout extends ApiError {\n constructor(message?: string) {\n super(504, message);\n }\n}\n","export type LogLevel = \"debug\" | \"info\" | \"warn\" | \"error\";\n\nconst levelPriority: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n};\n\nexport interface LoggerEntry {\n level: LogLevel;\n message?: string;\n [key: string]: unknown;\n}\n\nexport type LogDestination = (entry: LoggerEntry) => void;\n\nexport interface LoggerOptions {\n level?: LogLevel;\n destination?: LogDestination;\n}\n\nconst defaultDestination: LogDestination = (entry) => {\n console.log(JSON.stringify(entry));\n};\n\nexport class Logger {\n private level: LogLevel;\n private destination: LogDestination;\n private bindings: Record<string, unknown>;\n\n constructor(options: LoggerOptions = {}, bindings: Record<string, unknown> = {}) {\n this.level = options.level ?? \"info\";\n this.destination = options.destination ?? defaultDestination;\n this.bindings = bindings;\n }\n\n private shouldLog(level: LogLevel): boolean {\n return levelPriority[level] >= levelPriority[this.level];\n }\n\n private log(level: LogLevel, ...args: [unknown, ...unknown[]]) {\n if (!this.shouldLog(level)) return;\n\n let message: string | undefined;\n let meta: Record<string, unknown> = {};\n\n if (args.length === 1) {\n message = String(args[0]);\n } else if (typeof args[0] === \"object\" && args[0] !== null) {\n meta = { ...args[0] as Record<string, unknown> };\n message = String(args[1]);\n } else {\n message = String(args[0]);\n if (args[1] !== undefined) {\n meta = args[1] as Record<string, unknown>;\n }\n }\n\n this.destination({\n level,\n ...this.bindings,\n ...meta,\n ...(message ? { message } : {}),\n });\n }\n\n debug(...args: [unknown, ...unknown[]]) {\n this.log(\"debug\", ...args);\n }\n\n info(...args: [unknown, ...unknown[]]) {\n this.log(\"info\", ...args);\n }\n\n warn(...args: [unknown, ...unknown[]]) {\n this.log(\"warn\", ...args);\n }\n\n error(...args: [unknown, ...unknown[]]) {\n this.log(\"error\", ...args);\n }\n\n child(bindings: Record<string, unknown>): Logger {\n return new Logger(\n { level: this.level, destination: this.destination },\n { ...this.bindings, ...bindings },\n );\n }\n}\n\nexport function createLogger(options?: LoggerOptions): Logger {\n return new Logger(options);\n}\n","import { AsyncLocalStorage } from \"node:async_hooks\";\nimport type { Logger } from \"../logger/index.js\";\n\nexport interface RequestContext {\n requestId: string;\n log: Logger;\n}\n\nconst storage = new AsyncLocalStorage<RequestContext>();\n\nexport function getRequestContext(): RequestContext | undefined {\n return storage.getStore();\n}\n\nexport function getLogger(): Logger | undefined {\n return storage.getStore()?.log;\n}\n\nexport function getRequestId(): string | undefined {\n return storage.getStore()?.requestId;\n}\n\nexport function runWithContext(context: RequestContext, fn: () => void): void {\n storage.run(context, fn);\n}\n\nexport { storage };\n","import type { Request, Response, NextFunction } from \"express\";\nimport { ApiError } from \"../errors/api-error.js\";\nimport { message } from \"../message/index.js\";\n\nexport interface ErrorHandlerOptions {\n log?: boolean;\n}\n\nexport function errorHandler(options: ErrorHandlerOptions = {}) {\n const log = options.log ?? true;\n\n return (err: unknown, _req: Request, res: Response, _next: NextFunction): void => {\n if (err instanceof ApiError) {\n res.status(err.statusCode).json({\n error: {\n message: err.message,\n statusCode: err.statusCode,\n ...(err.details !== undefined && { details: err.details }),\n },\n });\n return;\n }\n\n if (log) {\n console.error(err);\n }\n\n res.status(500).json({\n error: {\n message: message.internalServerError,\n statusCode: 500,\n },\n });\n };\n}\n","import type { Request, Response, NextFunction } from \"express\";\nimport crypto from \"node:crypto\";\n\ndeclare global {\n namespace Express {\n interface Request {\n requestId: string;\n }\n }\n}\n\nexport interface RequestIdOptions {\n header?: string;\n generator?: () => string;\n respectExisting?: boolean;\n}\n\nexport function requestId(options: RequestIdOptions = {}) {\n const {\n header = \"x-request-id\",\n generator = crypto.randomUUID,\n respectExisting = true,\n } = options;\n\n return (req: Request, res: Response, next: NextFunction): void => {\n const existing = req.get(header);\n const id = existing && respectExisting ? existing : generator();\n\n req.requestId = id;\n res.setHeader(header, id);\n next();\n };\n}\n","import type { Request, Response, NextFunction } from \"express\";\nimport { Logger } from \"../logger/index.js\";\nimport { runWithContext } from \"../context/index.js\";\n\ndeclare global {\n namespace Express {\n interface Request {\n log: Logger;\n }\n }\n}\n\nexport interface RequestContextOptions {\n logger?: Logger;\n}\n\nexport function requestContext(options: RequestContextOptions = {}) {\n const logger = options.logger ?? new Logger();\n\n return (req: Request, _res: Response, next: NextFunction): void => {\n const scoped = logger.child({ requestId: req.requestId });\n req.log = scoped;\n\n runWithContext({ requestId: req.requestId, log: scoped }, () => {\n next();\n });\n };\n}\n","import type { Request, Response, NextFunction } from \"express\";\n\nexport interface HttpLoggerOptions {\n skip?: (req: Request, res: Response) => boolean;\n}\n\nexport function httpLogger(options: HttpLoggerOptions = {}) {\n const { skip } = options;\n\n return (req: Request, res: Response, next: NextFunction): void => {\n if (skip?.(req, res)) {\n next();\n return;\n }\n\n const start = Date.now();\n\n res.on(\"finish\", () => {\n req.log.info({\n method: req.method,\n path: req.originalUrl ?? req.url,\n status: res.statusCode,\n durationMs: Date.now() - start,\n });\n });\n\n next();\n };\n}\n","import type { Request, Response, NextFunction } from \"express\";\nimport { ValidationError } from \"../errors/http-error.js\";\n\nexport interface Schema {\n parse: (data: unknown) => unknown;\n}\n\nexport interface ValidationSchemas {\n body?: Schema;\n params?: Schema;\n query?: Schema;\n}\n\nexport interface ValidationIssue {\n path: (string | number)[];\n message: string;\n code?: string;\n}\n\nexport type IssueFormatter = (error: unknown) => ValidationIssue[];\n\nfunction defaultFormatter(error: unknown): ValidationIssue[] {\n if (\n error &&\n typeof error === \"object\" &&\n \"issues\" in error &&\n Array.isArray((error as { issues: unknown }).issues)\n ) {\n return (error as { issues: ValidationIssue[] }).issues;\n }\n\n if (error instanceof Error) {\n return [{ path: [], message: error.message }];\n }\n\n return [{ path: [], message: String(error) }];\n}\n\nexport interface ValidateOptions {\n formatError?: IssueFormatter;\n}\n\nexport function validate(\n schemas: ValidationSchemas,\n options: ValidateOptions = {},\n) {\n const formatError = options.formatError ?? defaultFormatter;\n\n return (req: Request, _res: Response, next: NextFunction): void => {\n try {\n if (schemas.body) req.body = schemas.body.parse(req.body);\n if (schemas.params) req.params = schemas.params.parse(req.params) as typeof req.params;\n if (schemas.query) req.query = schemas.query.parse(req.query) as typeof req.query;\n next();\n } catch (err) {\n next(new ValidationError(\"Validation failed\", formatError(err)));\n }\n };\n}\n","import type { Request, Response, NextFunction } from \"express\";\n\nexport function asyncHandler(\n fn: (req: Request, res: Response, next: NextFunction) => Promise<void>,\n) {\n return (req: Request, res: Response, next: NextFunction): void => {\n Promise.resolve(fn(req, res, next)).catch(next);\n };\n}\n","import type { Request, Response, NextFunction } from \"express\";\nimport { GatewayTimeout } from \"../errors/http-error.js\";\n\nexport interface TimeoutOptions {\n message?: string;\n}\n\nexport function timeout(ms: number, options: TimeoutOptions = {}) {\n return (req: Request, _res: Response, next: NextFunction): void => {\n const timer = setTimeout(() => {\n next(new GatewayTimeout(options.message ?? \"Request timed out\"));\n }, ms);\n\n const done = () => clearTimeout(timer);\n\n _res.on(\"finish\", done);\n _res.on(\"close\", done);\n\n next();\n };\n}\n","import type { Request, Response, NextFunction } from \"express\";\n\nexport interface HealthCheckOptions {\n uptime?: () => number;\n timestamp?: () => string;\n checks?: () => Record<string, unknown>;\n}\n\nexport function healthCheck(options: HealthCheckOptions = {}) {\n const getUptime = options.uptime ?? (() => process.uptime());\n const getTimestamp = options.timestamp ?? (() => new Date().toISOString());\n\n return (_req: Request, res: Response, _next: NextFunction): void => {\n res.json({\n status: \"ok\",\n uptime: getUptime(),\n timestamp: getTimestamp(),\n ...(options.checks?.() ?? {}),\n });\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,SAAS;AAAA;AAAA,EAEpB,UAAU;AAAA,EACV,oBAAoB;AAAA,EACpB,YAAY;AAAA,EACZ,YAAY;AAAA;AAAA,EAGZ,IAAI;AAAA,EACJ,SAAS;AAAA,EACT,UAAU;AAAA,EACV,6BAA6B;AAAA,EAC7B,WAAW;AAAA,EACX,cAAc;AAAA,EACd,gBAAgB;AAAA;AAAA,EAGhB,iBAAiB;AAAA,EACjB,kBAAkB;AAAA,EAClB,OAAO;AAAA,EACP,UAAU;AAAA,EACV,aAAa;AAAA,EACb,UAAU;AAAA,EACV,mBAAmB;AAAA,EACnB,mBAAmB;AAAA;AAAA,EAGnB,YAAY;AAAA,EACZ,cAAc;AAAA,EACd,iBAAiB;AAAA,EACjB,WAAW;AAAA,EACX,UAAU;AAAA,EACV,kBAAkB;AAAA,EAClB,eAAe;AAAA,EACf,6BAA6B;A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GAAG,UAAU,IAAI;AACtB,SAAK,GAAG,SAAS,IAAI;AAErB,SAAK;AAAA,EACP;AACF;;;ACZO,SAAS,YAAY,UAA8B,CAAC,GAAG;AAC5D,QAAM,YAAY,QAAQ,WAAW,MAAM,QAAQ,OAAO;AAC1D,QAAM,eAAe,QAAQ,cAAc,OAAM,oBAAI,KAAK,GAAE,YAAY;AAExE,SAAO,CAAC,MAAe,KAAe,UAA8B;AAClE,QAAI,KAAK;AAAA,MACP,QAAQ;AAAA,MACR,QAAQ,UAAU;AAAA,MAClB,WAAW,aAAa;AAAA,MACxB,GAAI,QAAQ,SAAS,KAAK,CAAC;AAAA,IAC7B,CAAC;AAAA,EACH;AACF;","names":["message","message","message","crypto"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
|
|
3
|
+
declare const status: {
|
|
4
|
+
readonly continue: 100;
|
|
5
|
+
readonly switchingProtocols: 101;
|
|
6
|
+
readonly processing: 102;
|
|
7
|
+
readonly earlyHints: 103;
|
|
8
|
+
readonly ok: 200;
|
|
9
|
+
readonly created: 201;
|
|
10
|
+
readonly accepted: 202;
|
|
11
|
+
readonly nonAuthoritativeInformation: 203;
|
|
12
|
+
readonly noContent: 204;
|
|
13
|
+
readonly resetContent: 205;
|
|
14
|
+
readonly partialContent: 206;
|
|
15
|
+
readonly multipleChoices: 300;
|
|
16
|
+
readonly movedPermanently: 301;
|
|
17
|
+
readonly found: 302;
|
|
18
|
+
readonly seeOther: 303;
|
|
19
|
+
readonly notModified: 304;
|
|
20
|
+
readonly useProxy: 305;
|
|
21
|
+
readonly temporaryRedirect: 307;
|
|
22
|
+
readonly permanentRedirect: 308;
|
|
23
|
+
readonly badRequest: 400;
|
|
24
|
+
readonly unauthorized: 401;
|
|
25
|
+
readonly paymentRequired: 402;
|
|
26
|
+
readonly forbidden: 403;
|
|
27
|
+
readonly notFound: 404;
|
|
28
|
+
readonly methodNotAllowed: 405;
|
|
29
|
+
readonly notAcceptable: 406;
|
|
30
|
+
readonly proxyAuthenticationRequired: 407;
|
|
31
|
+
readonly requestTimeout: 408;
|
|
32
|
+
readonly conflict: 409;
|
|
33
|
+
readonly gone: 410;
|
|
34
|
+
readonly lengthRequired: 411;
|
|
35
|
+
readonly preconditionFailed: 412;
|
|
36
|
+
readonly payloadTooLarge: 413;
|
|
37
|
+
readonly uriTooLong: 414;
|
|
38
|
+
readonly unsupportedMediaType: 415;
|
|
39
|
+
readonly rangeNotSatisfiable: 416;
|
|
40
|
+
readonly expectationFailed: 417;
|
|
41
|
+
readonly imATeapot: 418;
|
|
42
|
+
readonly unprocessableEntity: 422;
|
|
43
|
+
readonly tooEarly: 425;
|
|
44
|
+
readonly upgradeRequired: 426;
|
|
45
|
+
readonly preconditionRequired: 428;
|
|
46
|
+
readonly tooManyRequests: 429;
|
|
47
|
+
readonly requestHeaderFieldsTooLarge: 431;
|
|
48
|
+
readonly unavailableForLegalReasons: 451;
|
|
49
|
+
readonly internalServerError: 500;
|
|
50
|
+
readonly notImplemented: 501;
|
|
51
|
+
readonly badGateway: 502;
|
|
52
|
+
readonly serviceUnavailable: 503;
|
|
53
|
+
readonly gatewayTimeout: 504;
|
|
54
|
+
readonly httpVersionNotSupported: 505;
|
|
55
|
+
readonly insufficientStorage: 507;
|
|
56
|
+
readonly loopDetected: 508;
|
|
57
|
+
readonly notExtended: 510;
|
|
58
|
+
readonly networkAuthenticationRequired: 511;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
declare const message: {
|
|
62
|
+
readonly continue: "Continue";
|
|
63
|
+
readonly switchingProtocols: "Switching Protocols";
|
|
64
|
+
readonly processing: "Processing request";
|
|
65
|
+
readonly earlyHints: "Early hints available";
|
|
66
|
+
readonly ok: "Success";
|
|
67
|
+
readonly created: "Created successfully";
|
|
68
|
+
readonly accepted: "Request accepted";
|
|
69
|
+
readonly nonAuthoritativeInformation: "Non-authoritative information";
|
|
70
|
+
readonly noContent: "No content";
|
|
71
|
+
readonly resetContent: "Reset content";
|
|
72
|
+
readonly partialContent: "Partial content";
|
|
73
|
+
readonly multipleChoices: "Multiple redirect options available";
|
|
74
|
+
readonly movedPermanently: "Resource moved permanently";
|
|
75
|
+
readonly found: "Resource found elsewhere";
|
|
76
|
+
readonly seeOther: "See other resource";
|
|
77
|
+
readonly notModified: "Resource not modified";
|
|
78
|
+
readonly useProxy: "Use proxy to access resource";
|
|
79
|
+
readonly temporaryRedirect: "Resource temporarily moved";
|
|
80
|
+
readonly permanentRedirect: "Resource permanently moved";
|
|
81
|
+
readonly badRequest: "Invalid request";
|
|
82
|
+
readonly unauthorized: "Authentication required";
|
|
83
|
+
readonly paymentRequired: "Payment required";
|
|
84
|
+
readonly forbidden: "You don't have permission to access this resource";
|
|
85
|
+
readonly notFound: "Resource not found";
|
|
86
|
+
readonly methodNotAllowed: "Method not allowed for this resource";
|
|
87
|
+
readonly notAcceptable: "Requested format not available";
|
|
88
|
+
readonly proxyAuthenticationRequired: "Proxy authentication required";
|
|
89
|
+
readonly requestTimeout: "Request timed out";
|
|
90
|
+
readonly conflict: "Resource conflict detected";
|
|
91
|
+
readonly gone: "Resource no longer available";
|
|
92
|
+
readonly lengthRequired: "Content length header is required";
|
|
93
|
+
readonly preconditionFailed: "Request precondition failed";
|
|
94
|
+
readonly payloadTooLarge: "Request payload exceeds size limit";
|
|
95
|
+
readonly uriTooLong: "Request URI exceeds length limit";
|
|
96
|
+
readonly unsupportedMediaType: "Unsupported media type";
|
|
97
|
+
readonly rangeNotSatisfiable: "Requested range is not available";
|
|
98
|
+
readonly expectationFailed: "Request expectation could not be met";
|
|
99
|
+
readonly imATeapot: "I'm a teapot";
|
|
100
|
+
readonly unprocessableEntity: "Unable to process request entity";
|
|
101
|
+
readonly tooEarly: "Request sent too early, retry later";
|
|
102
|
+
readonly upgradeRequired: "Protocol upgrade required";
|
|
103
|
+
readonly preconditionRequired: "Precondition header is required";
|
|
104
|
+
readonly tooManyRequests: "Too many requests, please slow down";
|
|
105
|
+
readonly requestHeaderFieldsTooLarge: "Request headers exceed size limit";
|
|
106
|
+
readonly unavailableForLegalReasons: "Resource unavailable for legal reasons";
|
|
107
|
+
readonly internalServerError: "Something went wrong on our end";
|
|
108
|
+
readonly notImplemented: "Feature not yet implemented";
|
|
109
|
+
readonly badGateway: "Bad gateway response from upstream";
|
|
110
|
+
readonly serviceUnavailable: "Service temporarily unavailable, try again later";
|
|
111
|
+
readonly gatewayTimeout: "Upstream server timed out";
|
|
112
|
+
readonly httpVersionNotSupported: "HTTP version not supported by server";
|
|
113
|
+
readonly insufficientStorage: "Server has insufficient storage";
|
|
114
|
+
readonly loopDetected: "Infinite request loop detected";
|
|
115
|
+
readonly notExtended: "Server cannot fulfill extended request";
|
|
116
|
+
readonly networkAuthenticationRequired: "Network authentication required";
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
declare class ApiError extends Error {
|
|
120
|
+
readonly statusCode: number;
|
|
121
|
+
readonly details?: unknown | undefined;
|
|
122
|
+
constructor(statusCode: number, message?: string, details?: unknown | undefined);
|
|
123
|
+
static notFound(message?: string): ApiError;
|
|
124
|
+
static badRequest(message?: string): ApiError;
|
|
125
|
+
static unauthorized(message?: string): ApiError;
|
|
126
|
+
static forbidden(message?: string): ApiError;
|
|
127
|
+
static conflict(message?: string): ApiError;
|
|
128
|
+
static internalServerError(message?: string): ApiError;
|
|
129
|
+
static serviceUnavailable(message?: string): ApiError;
|
|
130
|
+
static tooManyRequests(message?: string): ApiError;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
declare class NotFound extends ApiError {
|
|
134
|
+
constructor(message?: string);
|
|
135
|
+
}
|
|
136
|
+
declare class BadRequest extends ApiError {
|
|
137
|
+
constructor(message?: string);
|
|
138
|
+
}
|
|
139
|
+
declare class Unauthorized extends ApiError {
|
|
140
|
+
constructor(message?: string);
|
|
141
|
+
}
|
|
142
|
+
declare class Forbidden extends ApiError {
|
|
143
|
+
constructor(message?: string);
|
|
144
|
+
}
|
|
145
|
+
declare class Conflict extends ApiError {
|
|
146
|
+
constructor(message?: string);
|
|
147
|
+
}
|
|
148
|
+
declare class InternalServerError extends ApiError {
|
|
149
|
+
constructor(message?: string);
|
|
150
|
+
}
|
|
151
|
+
declare class ServiceUnavailable extends ApiError {
|
|
152
|
+
constructor(message?: string);
|
|
153
|
+
}
|
|
154
|
+
declare class TooManyRequests extends ApiError {
|
|
155
|
+
constructor(message?: string);
|
|
156
|
+
}
|
|
157
|
+
declare class ValidationError extends ApiError {
|
|
158
|
+
constructor(message?: string, details?: unknown);
|
|
159
|
+
}
|
|
160
|
+
declare class GatewayTimeout extends ApiError {
|
|
161
|
+
constructor(message?: string);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
165
|
+
interface LoggerEntry {
|
|
166
|
+
level: LogLevel;
|
|
167
|
+
message?: string;
|
|
168
|
+
[key: string]: unknown;
|
|
169
|
+
}
|
|
170
|
+
type LogDestination = (entry: LoggerEntry) => void;
|
|
171
|
+
interface LoggerOptions {
|
|
172
|
+
level?: LogLevel;
|
|
173
|
+
destination?: LogDestination;
|
|
174
|
+
}
|
|
175
|
+
declare class Logger {
|
|
176
|
+
private level;
|
|
177
|
+
private destination;
|
|
178
|
+
private bindings;
|
|
179
|
+
constructor(options?: LoggerOptions, bindings?: Record<string, unknown>);
|
|
180
|
+
private shouldLog;
|
|
181
|
+
private log;
|
|
182
|
+
debug(...args: [unknown, ...unknown[]]): void;
|
|
183
|
+
info(...args: [unknown, ...unknown[]]): void;
|
|
184
|
+
warn(...args: [unknown, ...unknown[]]): void;
|
|
185
|
+
error(...args: [unknown, ...unknown[]]): void;
|
|
186
|
+
child(bindings: Record<string, unknown>): Logger;
|
|
187
|
+
}
|
|
188
|
+
declare function createLogger(options?: LoggerOptions): Logger;
|
|
189
|
+
|
|
190
|
+
interface RequestContext {
|
|
191
|
+
requestId: string;
|
|
192
|
+
log: Logger;
|
|
193
|
+
}
|
|
194
|
+
declare function getRequestContext(): RequestContext | undefined;
|
|
195
|
+
declare function getLogger(): Logger | undefined;
|
|
196
|
+
declare function getRequestId(): string | undefined;
|
|
197
|
+
declare function runWithContext(context: RequestContext, fn: () => void): void;
|
|
198
|
+
|
|
199
|
+
interface ErrorHandlerOptions {
|
|
200
|
+
log?: boolean;
|
|
201
|
+
}
|
|
202
|
+
declare function errorHandler(options?: ErrorHandlerOptions): (err: unknown, _req: Request, res: Response, _next: NextFunction) => void;
|
|
203
|
+
|
|
204
|
+
declare global {
|
|
205
|
+
namespace Express {
|
|
206
|
+
interface Request {
|
|
207
|
+
requestId: string;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
interface RequestIdOptions {
|
|
212
|
+
header?: string;
|
|
213
|
+
generator?: () => string;
|
|
214
|
+
respectExisting?: boolean;
|
|
215
|
+
}
|
|
216
|
+
declare function requestId(options?: RequestIdOptions): (req: Request, res: Response, next: NextFunction) => void;
|
|
217
|
+
|
|
218
|
+
declare global {
|
|
219
|
+
namespace Express {
|
|
220
|
+
interface Request {
|
|
221
|
+
log: Logger;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
interface RequestContextOptions {
|
|
226
|
+
logger?: Logger;
|
|
227
|
+
}
|
|
228
|
+
declare function requestContext(options?: RequestContextOptions): (req: Request, _res: Response, next: NextFunction) => void;
|
|
229
|
+
|
|
230
|
+
interface HttpLoggerOptions {
|
|
231
|
+
skip?: (req: Request, res: Response) => boolean;
|
|
232
|
+
}
|
|
233
|
+
declare function httpLogger(options?: HttpLoggerOptions): (req: Request, res: Response, next: NextFunction) => void;
|
|
234
|
+
|
|
235
|
+
interface Schema {
|
|
236
|
+
parse: (data: unknown) => unknown;
|
|
237
|
+
}
|
|
238
|
+
interface ValidationSchemas {
|
|
239
|
+
body?: Schema;
|
|
240
|
+
params?: Schema;
|
|
241
|
+
query?: Schema;
|
|
242
|
+
}
|
|
243
|
+
interface ValidationIssue {
|
|
244
|
+
path: (string | number)[];
|
|
245
|
+
message: string;
|
|
246
|
+
code?: string;
|
|
247
|
+
}
|
|
248
|
+
type IssueFormatter = (error: unknown) => ValidationIssue[];
|
|
249
|
+
interface ValidateOptions {
|
|
250
|
+
formatError?: IssueFormatter;
|
|
251
|
+
}
|
|
252
|
+
declare function validate(schemas: ValidationSchemas, options?: ValidateOptions): (req: Request, _res: Response, next: NextFunction) => void;
|
|
253
|
+
|
|
254
|
+
declare function asyncHandler(fn: (req: Request, res: Response, next: NextFunction) => Promise<void>): (req: Request, res: Response, next: NextFunction) => void;
|
|
255
|
+
|
|
256
|
+
interface TimeoutOptions {
|
|
257
|
+
message?: string;
|
|
258
|
+
}
|
|
259
|
+
declare function timeout(ms: number, options?: TimeoutOptions): (req: Request, _res: Response, next: NextFunction) => void;
|
|
260
|
+
|
|
261
|
+
interface HealthCheckOptions {
|
|
262
|
+
uptime?: () => number;
|
|
263
|
+
timestamp?: () => string;
|
|
264
|
+
checks?: () => Record<string, unknown>;
|
|
265
|
+
}
|
|
266
|
+
declare function healthCheck(options?: HealthCheckOptions): (_req: Request, res: Response, _next: NextFunction) => void;
|
|
267
|
+
|
|
268
|
+
export { ApiError, BadRequest, Conflict, Forbidden, GatewayTimeout, InternalServerError, Logger, NotFound, ServiceUnavailable, TooManyRequests, Unauthorized, ValidationError, asyncHandler, createLogger, errorHandler, getLogger, getRequestContext, getRequestId, healthCheck, httpLogger, message, requestContext, requestId, runWithContext, status, timeout, validate };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
|
|
3
|
+
declare const status: {
|
|
4
|
+
readonly continue: 100;
|
|
5
|
+
readonly switchingProtocols: 101;
|
|
6
|
+
readonly processing: 102;
|
|
7
|
+
readonly earlyHints: 103;
|
|
8
|
+
readonly ok: 200;
|
|
9
|
+
readonly created: 201;
|
|
10
|
+
readonly accepted: 202;
|
|
11
|
+
readonly nonAuthoritativeInformation: 203;
|
|
12
|
+
readonly noContent: 204;
|
|
13
|
+
readonly resetContent: 205;
|
|
14
|
+
readonly partialContent: 206;
|
|
15
|
+
readonly multipleChoices: 300;
|
|
16
|
+
readonly movedPermanently: 301;
|
|
17
|
+
readonly found: 302;
|
|
18
|
+
readonly seeOther: 303;
|
|
19
|
+
readonly notModified: 304;
|
|
20
|
+
readonly useProxy: 305;
|
|
21
|
+
readonly temporaryRedirect: 307;
|
|
22
|
+
readonly permanentRedirect: 308;
|
|
23
|
+
readonly badRequest: 400;
|
|
24
|
+
readonly unauthorized: 401;
|
|
25
|
+
readonly paymentRequired: 402;
|
|
26
|
+
readonly forbidden: 403;
|
|
27
|
+
readonly notFound: 404;
|
|
28
|
+
readonly methodNotAllowed: 405;
|
|
29
|
+
readonly notAcceptable: 406;
|
|
30
|
+
readonly proxyAuthenticationRequired: 407;
|
|
31
|
+
readonly requestTimeout: 408;
|
|
32
|
+
readonly conflict: 409;
|
|
33
|
+
readonly gone: 410;
|
|
34
|
+
readonly lengthRequired: 411;
|
|
35
|
+
readonly preconditionFailed: 412;
|
|
36
|
+
readonly payloadTooLarge: 413;
|
|
37
|
+
readonly uriTooLong: 414;
|
|
38
|
+
readonly unsupportedMediaType: 415;
|
|
39
|
+
readonly rangeNotSatisfiable: 416;
|
|
40
|
+
readonly expectationFailed: 417;
|
|
41
|
+
readonly imATeapot: 418;
|
|
42
|
+
readonly unprocessableEntity: 422;
|
|
43
|
+
readonly tooEarly: 425;
|
|
44
|
+
readonly upgradeRequired: 426;
|
|
45
|
+
readonly preconditionRequired: 428;
|
|
46
|
+
readonly tooManyRequests: 429;
|
|
47
|
+
readonly requestHeaderFieldsTooLarge: 431;
|
|
48
|
+
readonly unavailableForLegalReasons: 451;
|
|
49
|
+
readonly internalServerError: 500;
|
|
50
|
+
readonly notImplemented: 501;
|
|
51
|
+
readonly badGateway: 502;
|
|
52
|
+
readonly serviceUnavailable: 503;
|
|
53
|
+
readonly gatewayTimeout: 504;
|
|
54
|
+
readonly httpVersionNotSupported: 505;
|
|
55
|
+
readonly insufficientStorage: 507;
|
|
56
|
+
readonly loopDetected: 508;
|
|
57
|
+
readonly notExtended: 510;
|
|
58
|
+
readonly networkAuthenticationRequired: 511;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
declare const message: {
|
|
62
|
+
readonly continue: "Continue";
|
|
63
|
+
readonly switchingProtocols: "Switching Protocols";
|
|
64
|
+
readonly processing: "Processing request";
|
|
65
|
+
readonly earlyHints: "Early hints available";
|
|
66
|
+
readonly ok: "Success";
|
|
67
|
+
readonly created: "Created successfully";
|
|
68
|
+
readonly accepted: "Request accepted";
|
|
69
|
+
readonly nonAuthoritativeInformation: "Non-authoritative information";
|
|
70
|
+
readonly noContent: "No content";
|
|
71
|
+
readonly resetContent: "Reset content";
|
|
72
|
+
readonly partialContent: "Partial content";
|
|
73
|
+
readonly multipleChoices: "Multiple redirect options available";
|
|
74
|
+
readonly movedPermanently: "Resource moved permanently";
|
|
75
|
+
readonly found: "Resource found elsewhere";
|
|
76
|
+
readonly seeOther: "See other resource";
|
|
77
|
+
readonly notModified: "Resource not modified";
|
|
78
|
+
readonly useProxy: "Use proxy to access resource";
|
|
79
|
+
readonly temporaryRedirect: "Resource temporarily moved";
|
|
80
|
+
readonly permanentRedirect: "Resource permanently moved";
|
|
81
|
+
readonly badRequest: "Invalid request";
|
|
82
|
+
readonly unauthorized: "Authentication required";
|
|
83
|
+
readonly paymentRequired: "Payment required";
|
|
84
|
+
readonly forbidden: "You don't have permission to access this resource";
|
|
85
|
+
readonly notFound: "Resource not found";
|
|
86
|
+
readonly methodNotAllowed: "Method not allowed for this resource";
|
|
87
|
+
readonly notAcceptable: "Requested format not available";
|
|
88
|
+
readonly proxyAuthenticationRequired: "Proxy authentication required";
|
|
89
|
+
readonly requestTimeout: "Request timed out";
|
|
90
|
+
readonly conflict: "Resource conflict detected";
|
|
91
|
+
readonly gone: "Resource no longer available";
|
|
92
|
+
readonly lengthRequired: "Content length header is required";
|
|
93
|
+
readonly preconditionFailed: "Request precondition failed";
|
|
94
|
+
readonly payloadTooLarge: "Request payload exceeds size limit";
|
|
95
|
+
readonly uriTooLong: "Request URI exceeds length limit";
|
|
96
|
+
readonly unsupportedMediaType: "Unsupported media type";
|
|
97
|
+
readonly rangeNotSatisfiable: "Requested range is not available";
|
|
98
|
+
readonly expectationFailed: "Request expectation could not be met";
|
|
99
|
+
readonly imATeapot: "I'm a teapot";
|
|
100
|
+
readonly unprocessableEntity: "Unable to process request entity";
|
|
101
|
+
readonly tooEarly: "Request sent too early, retry later";
|
|
102
|
+
readonly upgradeRequired: "Protocol upgrade required";
|
|
103
|
+
readonly preconditionRequired: "Precondition header is required";
|
|
104
|
+
readonly tooManyRequests: "Too many requests, please slow down";
|
|
105
|
+
readonly requestHeaderFieldsTooLarge: "Request headers exceed size limit";
|
|
106
|
+
readonly unavailableForLegalReasons: "Resource unavailable for legal reasons";
|
|
107
|
+
readonly internalServerError: "Something went wrong on our end";
|
|
108
|
+
readonly notImplemented: "Feature not yet implemented";
|
|
109
|
+
readonly badGateway: "Bad gateway response from upstream";
|
|
110
|
+
readonly serviceUnavailable: "Service temporarily unavailable, try again later";
|
|
111
|
+
readonly gatewayTimeout: "Upstream server timed out";
|
|
112
|
+
readonly httpVersionNotSupported: "HTTP version not supported by server";
|
|
113
|
+
readonly insufficientStorage: "Server has insufficient storage";
|
|
114
|
+
readonly loopDetected: "Infinite request loop detected";
|
|
115
|
+
readonly notExtended: "Server cannot fulfill extended request";
|
|
116
|
+
readonly networkAuthenticationRequired: "Network authentication required";
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
declare class ApiError extends Error {
|
|
120
|
+
readonly statusCode: number;
|
|
121
|
+
readonly details?: unknown | undefined;
|
|
122
|
+
constructor(statusCode: number, message?: string, details?: unknown | undefined);
|
|
123
|
+
static notFound(message?: string): ApiError;
|
|
124
|
+
static badRequest(message?: string): ApiError;
|
|
125
|
+
static unauthorized(message?: string): ApiError;
|
|
126
|
+
static forbidden(message?: string): ApiError;
|
|
127
|
+
static conflict(message?: string): ApiError;
|
|
128
|
+
static internalServerError(message?: string): ApiError;
|
|
129
|
+
static serviceUnavailable(message?: string): ApiError;
|
|
130
|
+
static tooManyRequests(message?: string): ApiError;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
declare class NotFound extends ApiError {
|
|
134
|
+
constructor(message?: string);
|
|
135
|
+
}
|
|
136
|
+
declare class BadRequest extends ApiError {
|
|
137
|
+
constructor(message?: string);
|
|
138
|
+
}
|
|
139
|
+
declare class Unauthorized extends ApiError {
|
|
140
|
+
constructor(message?: string);
|
|
141
|
+
}
|
|
142
|
+
declare class Forbidden extends ApiError {
|
|
143
|
+
constructor(message?: string);
|
|
144
|
+
}
|
|
145
|
+
declare class Conflict extends ApiError {
|
|
146
|
+
constructor(message?: string);
|
|
147
|
+
}
|
|
148
|
+
declare class InternalServerError extends ApiError {
|
|
149
|
+
constructor(message?: string);
|
|
150
|
+
}
|
|
151
|
+
declare class ServiceUnavailable extends ApiError {
|
|
152
|
+
constructor(message?: string);
|
|
153
|
+
}
|
|
154
|
+
declare class TooManyRequests extends ApiError {
|
|
155
|
+
constructor(message?: string);
|
|
156
|
+
}
|
|
157
|
+
declare class ValidationError extends ApiError {
|
|
158
|
+
constructor(message?: string, details?: unknown);
|
|
159
|
+
}
|
|
160
|
+
declare class GatewayTimeout extends ApiError {
|
|
161
|
+
constructor(message?: string);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
165
|
+
interface LoggerEntry {
|
|
166
|
+
level: LogLevel;
|
|
167
|
+
message?: string;
|
|
168
|
+
[key: string]: unknown;
|
|
169
|
+
}
|
|
170
|
+
type LogDestination = (entry: LoggerEntry) => void;
|
|
171
|
+
interface LoggerOptions {
|
|
172
|
+
level?: LogLevel;
|
|
173
|
+
destination?: LogDestination;
|
|
174
|
+
}
|
|
175
|
+
declare class Logger {
|
|
176
|
+
private level;
|
|
177
|
+
private destination;
|
|
178
|
+
private bindings;
|
|
179
|
+
constructor(options?: LoggerOptions, bindings?: Record<string, unknown>);
|
|
180
|
+
private shouldLog;
|
|
181
|
+
private log;
|
|
182
|
+
debug(...args: [unknown, ...unknown[]]): void;
|
|
183
|
+
info(...args: [unknown, ...unknown[]]): void;
|
|
184
|
+
warn(...args: [unknown, ...unknown[]]): void;
|
|
185
|
+
error(...args: [unknown, ...unknown[]]): void;
|
|
186
|
+
child(bindings: Record<string, unknown>): Logger;
|
|
187
|
+
}
|
|
188
|
+
declare function createLogger(options?: LoggerOptions): Logger;
|
|
189
|
+
|
|
190
|
+
interface RequestContext {
|
|
191
|
+
requestId: string;
|
|
192
|
+
log: Logger;
|
|
193
|
+
}
|
|
194
|
+
declare function getRequestContext(): RequestContext | undefined;
|
|
195
|
+
declare function getLogger(): Logger | undefined;
|
|
196
|
+
declare function getRequestId(): string | undefined;
|
|
197
|
+
declare function runWithContext(context: RequestContext, fn: () => void): void;
|
|
198
|
+
|
|
199
|
+
interface ErrorHandlerOptions {
|
|
200
|
+
log?: boolean;
|
|
201
|
+
}
|
|
202
|
+
declare function errorHandler(options?: ErrorHandlerOptions): (err: unknown, _req: Request, res: Response, _next: NextFunction) => void;
|
|
203
|
+
|
|
204
|
+
declare global {
|
|
205
|
+
namespace Express {
|
|
206
|
+
interface Request {
|
|
207
|
+
requestId: string;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
interface RequestIdOptions {
|
|
212
|
+
header?: string;
|
|
213
|
+
generator?: () => string;
|
|
214
|
+
respectExisting?: boolean;
|
|
215
|
+
}
|
|
216
|
+
declare function requestId(options?: RequestIdOptions): (req: Request, res: Response, next: NextFunction) => void;
|
|
217
|
+
|
|
218
|
+
declare global {
|
|
219
|
+
namespace Express {
|
|
220
|
+
interface Request {
|
|
221
|
+
log: Logger;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
interface RequestContextOptions {
|
|
226
|
+
logger?: Logger;
|
|
227
|
+
}
|
|
228
|
+
declare function requestContext(options?: RequestContextOptions): (req: Request, _res: Response, next: NextFunction) => void;
|
|
229
|
+
|
|
230
|
+
interface HttpLoggerOptions {
|
|
231
|
+
skip?: (req: Request, res: Response) => boolean;
|
|
232
|
+
}
|
|
233
|
+
declare function httpLogger(options?: HttpLoggerOptions): (req: Request, res: Response, next: NextFunction) => void;
|
|
234
|
+
|
|
235
|
+
interface Schema {
|
|
236
|
+
parse: (data: unknown) => unknown;
|
|
237
|
+
}
|
|
238
|
+
interface ValidationSchemas {
|
|
239
|
+
body?: Schema;
|
|
240
|
+
params?: Schema;
|
|
241
|
+
query?: Schema;
|
|
242
|
+
}
|
|
243
|
+
interface ValidationIssue {
|
|
244
|
+
path: (string | number)[];
|
|
245
|
+
message: string;
|
|
246
|
+
code?: string;
|
|
247
|
+
}
|
|
248
|
+
type IssueFormatter = (error: unknown) => ValidationIssue[];
|
|
249
|
+
interface ValidateOptions {
|
|
250
|
+
formatError?: IssueFormatter;
|
|
251
|
+
}
|
|
252
|
+
declare function validate(schemas: ValidationSchemas, options?: ValidateOptions): (req: Request, _res: Response, next: NextFunction) => void;
|
|
253
|
+
|
|
254
|
+
declare function asyncHandler(fn: (req: Request, res: Response, next: NextFunction) => Promise<void>): (req: Request, res: Response, next: NextFunction) => void;
|
|
255
|
+
|
|
256
|
+
interface TimeoutOptions {
|
|
257
|
+
message?: string;
|
|
258
|
+
}
|
|
259
|
+
declare function timeout(ms: number, options?: TimeoutOptions): (req: Request, _res: Response, next: NextFunction) => void;
|
|
260
|
+
|
|
261
|
+
interface HealthCheckOptions {
|
|
262
|
+
uptime?: () => number;
|
|
263
|
+
timestamp?: () => string;
|
|
264
|
+
checks?: () => Record<string, unknown>;
|
|
265
|
+
}
|
|
266
|
+
declare function healthCheck(options?: HealthCheckOptions): (_req: Request, res: Response, _next: NextFunction) => void;
|
|
267
|
+
|
|
268
|
+
export { ApiError, BadRequest, Conflict, Forbidden, GatewayTimeout, InternalServerError, Logger, NotFound, ServiceUnavailable, TooManyRequests, Unauthorized, ValidationError, asyncHandler, createLogger, errorHandler, getLogger, getRequestContext, getRequestId, healthCheck, httpLogger, message, requestContext, requestId, runWithContext, status, timeout, validate };
|