express-zod-api 22.12.0-beta.1 → 22.13.0
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/CHANGELOG.md +54 -2
- package/CONTRIBUTING.md +4 -4
- package/README.md +39 -24
- package/SECURITY.md +2 -2
- package/dist/index.cjs +8 -8
- package/dist/index.d.cts +69 -10
- package/dist/index.d.ts +69 -10
- package/dist/index.js +8 -8
- package/migration/index.cjs +2 -2
- package/migration/index.js +3 -3
- package/package.json +11 -9
package/dist/index.d.cts
CHANGED
|
@@ -69,7 +69,7 @@ declare class BuiltinLogger implements AbstractLogger {
|
|
|
69
69
|
protected readonly config: BuiltinLoggerConfig;
|
|
70
70
|
/** @example new BuiltinLogger({ level: "debug", color: true, depth: 4 }) */
|
|
71
71
|
constructor({ color, level, depth, ctx, }?: Partial<BuiltinLoggerConfig>);
|
|
72
|
-
protected
|
|
72
|
+
protected format(subject: unknown): string;
|
|
73
73
|
protected print(method: Severity, message: string, meta?: unknown): void;
|
|
74
74
|
debug(message: string, meta?: unknown): void;
|
|
75
75
|
info(message: string, meta?: unknown): void;
|
|
@@ -116,6 +116,10 @@ interface NormalizedResponse {
|
|
|
116
116
|
mimeTypes: [string, ...string[]] | null;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
/** @desc Accepts an object shape or a custom object schema */
|
|
120
|
+
declare const form: <S extends z.ZodRawShape>(base: S | z.ZodObject<S>) => z.ZodBranded<z.ZodObject<S, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<S>, any> extends infer T ? { [k in keyof T]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<S>, any>[k]; } : never, z.baseObjectInputType<S> extends infer T_1 ? { [k_1 in keyof T_1]: z.baseObjectInputType<S>[k_1]; } : never>, symbol>;
|
|
121
|
+
type FormSchema = ReturnType<typeof form>;
|
|
122
|
+
|
|
119
123
|
type LogicalOr<T> = {
|
|
120
124
|
or: T[];
|
|
121
125
|
};
|
|
@@ -199,10 +203,18 @@ interface OAuth2Security<S extends string> {
|
|
|
199
203
|
type Security<K extends string = string, S extends string = string> = BasicSecurity | BearerSecurity | InputSecurity<K> | CustomHeaderSecurity | CookieSecurity | OpenIdSecurity | OAuth2Security<S>;
|
|
200
204
|
|
|
201
205
|
type Handler$2<IN, OPT, OUT> = (params: {
|
|
206
|
+
/** @desc The inputs from the enabled input sources validated against final input schema of the Middleware */
|
|
202
207
|
input: IN;
|
|
208
|
+
/**
|
|
209
|
+
* @desc The returns of the previously executed Middlewares (typed when chaining Middlewares)
|
|
210
|
+
* @link https://github.com/RobinTail/express-zod-api/discussions/1250
|
|
211
|
+
* */
|
|
203
212
|
options: OPT;
|
|
213
|
+
/** @link https://expressjs.com/en/5x/api.html#req */
|
|
204
214
|
request: Request;
|
|
215
|
+
/** @link https://expressjs.com/en/5x/api.html#res */
|
|
205
216
|
response: Response;
|
|
217
|
+
/** @desc The instance of the configured logger */
|
|
206
218
|
logger: ActualLogger;
|
|
207
219
|
}) => Promise<OUT>;
|
|
208
220
|
declare abstract class AbstractMiddleware {
|
|
@@ -219,8 +231,15 @@ declare abstract class AbstractMiddleware {
|
|
|
219
231
|
declare class Middleware<OPT extends FlatObject, OUT extends FlatObject, SCO extends string, IN extends IOSchema<"strip"> = EmptySchema> extends AbstractMiddleware {
|
|
220
232
|
#private;
|
|
221
233
|
constructor({ input, security, handler, }: {
|
|
234
|
+
/**
|
|
235
|
+
* @desc Input schema of the Middleware, combining properties from all the enabled input sources
|
|
236
|
+
* @default z.object({})
|
|
237
|
+
* @see defaultInputSources
|
|
238
|
+
* */
|
|
222
239
|
input?: IN;
|
|
240
|
+
/** @desc Declaration of the security schemas implemented within the handler */
|
|
223
241
|
security?: LogicalContainer<Security<Extract<keyof z.input<IN>, string>, SCO>>;
|
|
242
|
+
/** @desc The handler returning options available to Endpoints */
|
|
224
243
|
handler: Handler$2<z.output<IN>, OPT, OUT>;
|
|
225
244
|
});
|
|
226
245
|
getSecurity(): LogicalContainer<Security<Extract<keyof z.input<IN>, string>, SCO>> | undefined;
|
|
@@ -263,7 +282,7 @@ type EffectsChain<U extends z.UnknownKeysParam> = ObjectBasedEffect<BaseObject<U
|
|
|
263
282
|
* @desc The type allowed on the top level of Middlewares and Endpoints
|
|
264
283
|
* @param U — only "strip" is allowed for Middlewares due to intersection issue (Zod) #600
|
|
265
284
|
* */
|
|
266
|
-
type IOSchema<U extends z.UnknownKeysParam = z.UnknownKeysParam> = BaseObject<U> | EffectsChain<U> | RawSchema | z.ZodUnion<[IOSchema<U>, ...IOSchema<U>[]]> | z.ZodIntersection<IOSchema<U>, IOSchema<U>> | z.ZodDiscriminatedUnion<string, BaseObject<U>[]> | z.ZodPipeline<ObjectBasedEffect<BaseObject<U>>, BaseObject<U>>;
|
|
285
|
+
type IOSchema<U extends z.UnknownKeysParam = z.UnknownKeysParam> = BaseObject<U> | EffectsChain<U> | RawSchema | FormSchema | z.ZodUnion<[IOSchema<U>, ...IOSchema<U>[]]> | z.ZodIntersection<IOSchema<U>, IOSchema<U>> | z.ZodDiscriminatedUnion<string, BaseObject<U>[]> | z.ZodPipeline<ObjectBasedEffect<BaseObject<U>>, BaseObject<U>>;
|
|
267
286
|
|
|
268
287
|
declare const methods: ("get" | "post" | "put" | "delete" | "patch")[];
|
|
269
288
|
type Method = (typeof methods)[number];
|
|
@@ -273,6 +292,7 @@ declare const contentTypes: {
|
|
|
273
292
|
upload: string;
|
|
274
293
|
raw: string;
|
|
275
294
|
sse: string;
|
|
295
|
+
form: string;
|
|
276
296
|
};
|
|
277
297
|
type ContentType = keyof typeof contentTypes;
|
|
278
298
|
|
|
@@ -384,8 +404,11 @@ declare abstract class Routable {
|
|
|
384
404
|
}
|
|
385
405
|
|
|
386
406
|
type Handler<IN, OUT, OPT> = (params: {
|
|
407
|
+
/** @desc The inputs from the enabled input sources validated against the final input schema (incl. Middlewares) */
|
|
387
408
|
input: IN;
|
|
409
|
+
/** @desc The returns of the assigned Middlewares */
|
|
388
410
|
options: OPT;
|
|
411
|
+
/** @desc The instance of the configured logger */
|
|
389
412
|
logger: ActualLogger;
|
|
390
413
|
}) => Promise<OUT>;
|
|
391
414
|
type DescriptionVariant = "short" | "long";
|
|
@@ -430,7 +453,7 @@ declare class Endpoint<IN extends IOSchema, OUT extends IOSchema, OPT extends Fl
|
|
|
430
453
|
getMethods(): Readonly<("get" | "post" | "put" | "delete" | "patch")[] | undefined>;
|
|
431
454
|
getSchema(variant: "input"): IN;
|
|
432
455
|
getSchema(variant: "output"): OUT;
|
|
433
|
-
getRequestType(): "json" | "upload" | "raw";
|
|
456
|
+
getRequestType(): "form" | "json" | "upload" | "raw";
|
|
434
457
|
getResponses(variant: ResponseVariant): readonly NormalizedResponse[];
|
|
435
458
|
getSecurity(): LogicalContainer<Security>[];
|
|
436
459
|
getScopes(): readonly string[];
|
|
@@ -458,6 +481,7 @@ type ChildLoggerProvider = (params: {
|
|
|
458
481
|
request: Request;
|
|
459
482
|
parent: ActualLogger;
|
|
460
483
|
}) => ActualLogger | Promise<ActualLogger>;
|
|
484
|
+
type LogAccess = (request: Request, logger: ActualLogger) => void;
|
|
461
485
|
interface CommonConfig {
|
|
462
486
|
/**
|
|
463
487
|
* @desc Enables cross-origin resource sharing.
|
|
@@ -490,6 +514,12 @@ interface CommonConfig {
|
|
|
490
514
|
* @example ({ parent }) => parent.child({ requestId: uuid() })
|
|
491
515
|
* */
|
|
492
516
|
childLoggerProvider?: ChildLoggerProvider;
|
|
517
|
+
/**
|
|
518
|
+
* @desc The function for producing access logs
|
|
519
|
+
* @default ({ method, path }, logger) => logger.debug(`${method}: ${path}`)
|
|
520
|
+
* @example null — disables the feature
|
|
521
|
+
* */
|
|
522
|
+
accessLogger?: null | LogAccess;
|
|
493
523
|
/**
|
|
494
524
|
* @desc You can disable the startup logo.
|
|
495
525
|
* @default true
|
|
@@ -517,7 +547,6 @@ type UploadOptions = Pick<express_fileupload__default.Options, "createParentPath
|
|
|
517
547
|
limitError?: Error;
|
|
518
548
|
/**
|
|
519
549
|
* @desc A handler to execute before uploading — it can be used for restrictions by throwing an error.
|
|
520
|
-
* @default undefined
|
|
521
550
|
* @example ({ request }) => { throw createHttpError(403, "Not authorized"); }
|
|
522
551
|
* */
|
|
523
552
|
beforeUpload?: BeforeUpload;
|
|
@@ -562,13 +591,11 @@ interface ServerConfig extends CommonConfig {
|
|
|
562
591
|
jsonParser?: RequestHandler;
|
|
563
592
|
/**
|
|
564
593
|
* @desc Enable or configure uploads handling.
|
|
565
|
-
* @default undefined
|
|
566
594
|
* @requires express-fileupload
|
|
567
595
|
* */
|
|
568
596
|
upload?: boolean | UploadOptions;
|
|
569
597
|
/**
|
|
570
598
|
* @desc Enable or configure response compression.
|
|
571
|
-
* @default undefined
|
|
572
599
|
* @requires compression
|
|
573
600
|
*/
|
|
574
601
|
compression?: boolean | CompressionOptions;
|
|
@@ -578,17 +605,21 @@ interface ServerConfig extends CommonConfig {
|
|
|
578
605
|
* @link https://expressjs.com/en/4x/api.html#express.raw
|
|
579
606
|
* */
|
|
580
607
|
rawParser?: RequestHandler;
|
|
608
|
+
/**
|
|
609
|
+
* @desc Custom parser for URL Encoded requests used for submitting HTML forms
|
|
610
|
+
* @default express.urlencoded()
|
|
611
|
+
* @link https://expressjs.com/en/4x/api.html#express.urlencoded
|
|
612
|
+
* */
|
|
613
|
+
formParser?: RequestHandler;
|
|
581
614
|
/**
|
|
582
615
|
* @desc A code to execute before processing the Routing of your API (and before parsing).
|
|
583
616
|
* @desc This can be a good place for express middlewares establishing their own routes.
|
|
584
617
|
* @desc It can help to avoid making a DIY solution based on the attachRouting() approach.
|
|
585
|
-
* @default undefined
|
|
586
618
|
* @example ({ app }) => { app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); }
|
|
587
619
|
* */
|
|
588
620
|
beforeRouting?: BeforeRouting;
|
|
589
621
|
/**
|
|
590
622
|
* @desc Rejects new connections and attempts to finish ongoing ones in the specified time before exit.
|
|
591
|
-
* @default undefined
|
|
592
623
|
* */
|
|
593
624
|
gracefulShutdown?: boolean | GracefulOptions;
|
|
594
625
|
}
|
|
@@ -684,15 +715,39 @@ declare module "zod" {
|
|
|
684
715
|
}
|
|
685
716
|
|
|
686
717
|
interface BuildProps<IN extends IOSchema, OUT extends IOSchema | z.ZodVoid, MIN extends IOSchema<"strip">, OPT extends FlatObject, SCO extends string> {
|
|
718
|
+
/**
|
|
719
|
+
* @desc Input schema of the Endpoint, combining properties from all the enabled input sources (path params, headers)
|
|
720
|
+
* @default z.object({})
|
|
721
|
+
* @see defaultInputSources
|
|
722
|
+
* */
|
|
687
723
|
input?: IN;
|
|
724
|
+
/** @desc The schema by which the returns of the Endpoint handler is validated */
|
|
688
725
|
output: OUT;
|
|
726
|
+
/** @desc The Endpoint handler receiving the validated inputs, returns of added Middlewares (options) and a logger */
|
|
689
727
|
handler: Handler<z.output<z.ZodIntersection<MIN, IN>>, z.input<OUT>, OPT>;
|
|
728
|
+
/** @desc The operation description for the generated Documentation */
|
|
690
729
|
description?: string;
|
|
730
|
+
/** @desc The operation summary for the generated Documentation (50 symbols max) */
|
|
691
731
|
shortDescription?: string;
|
|
732
|
+
/** @desc The operation ID for the generated Documentation (must be unique) */
|
|
692
733
|
operationId?: string | ((method: Method) => string);
|
|
734
|
+
/**
|
|
735
|
+
* @desc HTTP method(s) this endpoint can handle
|
|
736
|
+
* @default "get" unless the Endpoint is assigned within DependsOnMethod
|
|
737
|
+
* @see DependsOnMethod
|
|
738
|
+
* */
|
|
693
739
|
method?: Method | [Method, ...Method[]];
|
|
740
|
+
/**
|
|
741
|
+
* @desc Scope(s) from the list of the ones defined by the added Middlewares having "oauth2" security type
|
|
742
|
+
* @see OAuth2Security
|
|
743
|
+
* */
|
|
694
744
|
scope?: SCO | SCO[];
|
|
745
|
+
/**
|
|
746
|
+
* @desc Tag(s) for generating Documentation. For establishing constraints:
|
|
747
|
+
* @see TagOverrides
|
|
748
|
+
* */
|
|
695
749
|
tag?: Tag | Tag[];
|
|
750
|
+
/** @desc Marks the operation deprecated in the generated Documentation */
|
|
696
751
|
deprecated?: boolean;
|
|
697
752
|
}
|
|
698
753
|
declare class EndpointsFactory<IN extends IOSchema<"strip"> = EmptySchema, OUT extends FlatObject = EmptyObject, SCO extends string = string> {
|
|
@@ -743,6 +798,7 @@ declare function file<K extends Variant>(variant: K): ReturnType<Variants[K]>;
|
|
|
743
798
|
declare const ez: {
|
|
744
799
|
dateIn: () => zod.ZodBranded<zod.ZodPipeline<zod.ZodEffects<zod.ZodUnion<[zod.ZodString, zod.ZodString, zod.ZodString]>, Date, string>, zod.ZodEffects<zod.ZodDate, Date, Date>>, symbol>;
|
|
745
800
|
dateOut: () => zod.ZodBranded<zod.ZodEffects<zod.ZodEffects<zod.ZodDate, Date, Date>, string, Date>, symbol>;
|
|
801
|
+
form: <S extends zod.ZodRawShape>(base: S | zod.ZodObject<S>) => zod.ZodBranded<zod.ZodObject<S, zod.UnknownKeysParam, zod.ZodTypeAny, zod.objectUtil.addQuestionMarks<zod.baseObjectOutputType<S>, any> extends infer T ? { [k in keyof T]: zod.objectUtil.addQuestionMarks<zod.baseObjectOutputType<S>, any>[k]; } : never, zod.baseObjectInputType<S> extends infer T_1 ? { [k_1 in keyof T_1]: zod.baseObjectInputType<S>[k_1]; } : never>, symbol>;
|
|
746
802
|
file: typeof file;
|
|
747
803
|
upload: () => zod.ZodBranded<zod.ZodType<express_fileupload.UploadedFile, zod.ZodTypeDef, express_fileupload.UploadedFile>, symbol>;
|
|
748
804
|
raw: <S extends zod.ZodRawShape>(extra?: S) => zod.ZodBranded<zod.ZodObject<zod.objectUtil.extendShape<{
|
|
@@ -912,7 +968,10 @@ declare const testMiddleware: <LOG extends FlatObject, REQ extends RequestOption
|
|
|
912
968
|
middleware: AbstractMiddleware;
|
|
913
969
|
/** @desc The aggregated output from previously executed middlewares */
|
|
914
970
|
options?: FlatObject;
|
|
915
|
-
/**
|
|
971
|
+
/**
|
|
972
|
+
* @desc Enables transforming possible middleware errors into response, so that test Middleware does not throw
|
|
973
|
+
* @todo consider utilizing errorHandler from config instead in v23
|
|
974
|
+
* */
|
|
916
975
|
errorHandler?: (error: Error, response: Response) => void;
|
|
917
976
|
}) => Promise<{
|
|
918
977
|
requestMock: node_mocks_http.MockRequest<Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>> & REQ>;
|
|
@@ -1070,7 +1129,7 @@ type EventsMap = Record<string, z.ZodTypeAny>;
|
|
|
1070
1129
|
interface Emitter<E extends EventsMap> extends FlatObject {
|
|
1071
1130
|
/** @desc Returns true when the connection was closed or terminated */
|
|
1072
1131
|
isClosed: () => boolean;
|
|
1073
|
-
/** @desc Sends an event to the stream
|
|
1132
|
+
/** @desc Sends an event to the stream according to the declared schema */
|
|
1074
1133
|
emit: <K extends keyof E>(event: K, data: z.input<E[K]>) => void;
|
|
1075
1134
|
}
|
|
1076
1135
|
declare class EventStreamFactory<E extends EventsMap> extends EndpointsFactory<EmptySchema, Emitter<E>> {
|
package/dist/index.d.ts
CHANGED
|
@@ -69,7 +69,7 @@ declare class BuiltinLogger implements AbstractLogger {
|
|
|
69
69
|
protected readonly config: BuiltinLoggerConfig;
|
|
70
70
|
/** @example new BuiltinLogger({ level: "debug", color: true, depth: 4 }) */
|
|
71
71
|
constructor({ color, level, depth, ctx, }?: Partial<BuiltinLoggerConfig>);
|
|
72
|
-
protected
|
|
72
|
+
protected format(subject: unknown): string;
|
|
73
73
|
protected print(method: Severity, message: string, meta?: unknown): void;
|
|
74
74
|
debug(message: string, meta?: unknown): void;
|
|
75
75
|
info(message: string, meta?: unknown): void;
|
|
@@ -116,6 +116,10 @@ interface NormalizedResponse {
|
|
|
116
116
|
mimeTypes: [string, ...string[]] | null;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
/** @desc Accepts an object shape or a custom object schema */
|
|
120
|
+
declare const form: <S extends z.ZodRawShape>(base: S | z.ZodObject<S>) => z.ZodBranded<z.ZodObject<S, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<S>, any> extends infer T ? { [k in keyof T]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<S>, any>[k]; } : never, z.baseObjectInputType<S> extends infer T_1 ? { [k_1 in keyof T_1]: z.baseObjectInputType<S>[k_1]; } : never>, symbol>;
|
|
121
|
+
type FormSchema = ReturnType<typeof form>;
|
|
122
|
+
|
|
119
123
|
type LogicalOr<T> = {
|
|
120
124
|
or: T[];
|
|
121
125
|
};
|
|
@@ -199,10 +203,18 @@ interface OAuth2Security<S extends string> {
|
|
|
199
203
|
type Security<K extends string = string, S extends string = string> = BasicSecurity | BearerSecurity | InputSecurity<K> | CustomHeaderSecurity | CookieSecurity | OpenIdSecurity | OAuth2Security<S>;
|
|
200
204
|
|
|
201
205
|
type Handler$2<IN, OPT, OUT> = (params: {
|
|
206
|
+
/** @desc The inputs from the enabled input sources validated against final input schema of the Middleware */
|
|
202
207
|
input: IN;
|
|
208
|
+
/**
|
|
209
|
+
* @desc The returns of the previously executed Middlewares (typed when chaining Middlewares)
|
|
210
|
+
* @link https://github.com/RobinTail/express-zod-api/discussions/1250
|
|
211
|
+
* */
|
|
203
212
|
options: OPT;
|
|
213
|
+
/** @link https://expressjs.com/en/5x/api.html#req */
|
|
204
214
|
request: Request;
|
|
215
|
+
/** @link https://expressjs.com/en/5x/api.html#res */
|
|
205
216
|
response: Response;
|
|
217
|
+
/** @desc The instance of the configured logger */
|
|
206
218
|
logger: ActualLogger;
|
|
207
219
|
}) => Promise<OUT>;
|
|
208
220
|
declare abstract class AbstractMiddleware {
|
|
@@ -219,8 +231,15 @@ declare abstract class AbstractMiddleware {
|
|
|
219
231
|
declare class Middleware<OPT extends FlatObject, OUT extends FlatObject, SCO extends string, IN extends IOSchema<"strip"> = EmptySchema> extends AbstractMiddleware {
|
|
220
232
|
#private;
|
|
221
233
|
constructor({ input, security, handler, }: {
|
|
234
|
+
/**
|
|
235
|
+
* @desc Input schema of the Middleware, combining properties from all the enabled input sources
|
|
236
|
+
* @default z.object({})
|
|
237
|
+
* @see defaultInputSources
|
|
238
|
+
* */
|
|
222
239
|
input?: IN;
|
|
240
|
+
/** @desc Declaration of the security schemas implemented within the handler */
|
|
223
241
|
security?: LogicalContainer<Security<Extract<keyof z.input<IN>, string>, SCO>>;
|
|
242
|
+
/** @desc The handler returning options available to Endpoints */
|
|
224
243
|
handler: Handler$2<z.output<IN>, OPT, OUT>;
|
|
225
244
|
});
|
|
226
245
|
getSecurity(): LogicalContainer<Security<Extract<keyof z.input<IN>, string>, SCO>> | undefined;
|
|
@@ -263,7 +282,7 @@ type EffectsChain<U extends z.UnknownKeysParam> = ObjectBasedEffect<BaseObject<U
|
|
|
263
282
|
* @desc The type allowed on the top level of Middlewares and Endpoints
|
|
264
283
|
* @param U — only "strip" is allowed for Middlewares due to intersection issue (Zod) #600
|
|
265
284
|
* */
|
|
266
|
-
type IOSchema<U extends z.UnknownKeysParam = z.UnknownKeysParam> = BaseObject<U> | EffectsChain<U> | RawSchema | z.ZodUnion<[IOSchema<U>, ...IOSchema<U>[]]> | z.ZodIntersection<IOSchema<U>, IOSchema<U>> | z.ZodDiscriminatedUnion<string, BaseObject<U>[]> | z.ZodPipeline<ObjectBasedEffect<BaseObject<U>>, BaseObject<U>>;
|
|
285
|
+
type IOSchema<U extends z.UnknownKeysParam = z.UnknownKeysParam> = BaseObject<U> | EffectsChain<U> | RawSchema | FormSchema | z.ZodUnion<[IOSchema<U>, ...IOSchema<U>[]]> | z.ZodIntersection<IOSchema<U>, IOSchema<U>> | z.ZodDiscriminatedUnion<string, BaseObject<U>[]> | z.ZodPipeline<ObjectBasedEffect<BaseObject<U>>, BaseObject<U>>;
|
|
267
286
|
|
|
268
287
|
declare const methods: ("get" | "post" | "put" | "delete" | "patch")[];
|
|
269
288
|
type Method = (typeof methods)[number];
|
|
@@ -273,6 +292,7 @@ declare const contentTypes: {
|
|
|
273
292
|
upload: string;
|
|
274
293
|
raw: string;
|
|
275
294
|
sse: string;
|
|
295
|
+
form: string;
|
|
276
296
|
};
|
|
277
297
|
type ContentType = keyof typeof contentTypes;
|
|
278
298
|
|
|
@@ -384,8 +404,11 @@ declare abstract class Routable {
|
|
|
384
404
|
}
|
|
385
405
|
|
|
386
406
|
type Handler<IN, OUT, OPT> = (params: {
|
|
407
|
+
/** @desc The inputs from the enabled input sources validated against the final input schema (incl. Middlewares) */
|
|
387
408
|
input: IN;
|
|
409
|
+
/** @desc The returns of the assigned Middlewares */
|
|
388
410
|
options: OPT;
|
|
411
|
+
/** @desc The instance of the configured logger */
|
|
389
412
|
logger: ActualLogger;
|
|
390
413
|
}) => Promise<OUT>;
|
|
391
414
|
type DescriptionVariant = "short" | "long";
|
|
@@ -430,7 +453,7 @@ declare class Endpoint<IN extends IOSchema, OUT extends IOSchema, OPT extends Fl
|
|
|
430
453
|
getMethods(): Readonly<("get" | "post" | "put" | "delete" | "patch")[] | undefined>;
|
|
431
454
|
getSchema(variant: "input"): IN;
|
|
432
455
|
getSchema(variant: "output"): OUT;
|
|
433
|
-
getRequestType(): "json" | "upload" | "raw";
|
|
456
|
+
getRequestType(): "form" | "json" | "upload" | "raw";
|
|
434
457
|
getResponses(variant: ResponseVariant): readonly NormalizedResponse[];
|
|
435
458
|
getSecurity(): LogicalContainer<Security>[];
|
|
436
459
|
getScopes(): readonly string[];
|
|
@@ -458,6 +481,7 @@ type ChildLoggerProvider = (params: {
|
|
|
458
481
|
request: Request;
|
|
459
482
|
parent: ActualLogger;
|
|
460
483
|
}) => ActualLogger | Promise<ActualLogger>;
|
|
484
|
+
type LogAccess = (request: Request, logger: ActualLogger) => void;
|
|
461
485
|
interface CommonConfig {
|
|
462
486
|
/**
|
|
463
487
|
* @desc Enables cross-origin resource sharing.
|
|
@@ -490,6 +514,12 @@ interface CommonConfig {
|
|
|
490
514
|
* @example ({ parent }) => parent.child({ requestId: uuid() })
|
|
491
515
|
* */
|
|
492
516
|
childLoggerProvider?: ChildLoggerProvider;
|
|
517
|
+
/**
|
|
518
|
+
* @desc The function for producing access logs
|
|
519
|
+
* @default ({ method, path }, logger) => logger.debug(`${method}: ${path}`)
|
|
520
|
+
* @example null — disables the feature
|
|
521
|
+
* */
|
|
522
|
+
accessLogger?: null | LogAccess;
|
|
493
523
|
/**
|
|
494
524
|
* @desc You can disable the startup logo.
|
|
495
525
|
* @default true
|
|
@@ -517,7 +547,6 @@ type UploadOptions = Pick<express_fileupload__default.Options, "createParentPath
|
|
|
517
547
|
limitError?: Error;
|
|
518
548
|
/**
|
|
519
549
|
* @desc A handler to execute before uploading — it can be used for restrictions by throwing an error.
|
|
520
|
-
* @default undefined
|
|
521
550
|
* @example ({ request }) => { throw createHttpError(403, "Not authorized"); }
|
|
522
551
|
* */
|
|
523
552
|
beforeUpload?: BeforeUpload;
|
|
@@ -562,13 +591,11 @@ interface ServerConfig extends CommonConfig {
|
|
|
562
591
|
jsonParser?: RequestHandler;
|
|
563
592
|
/**
|
|
564
593
|
* @desc Enable or configure uploads handling.
|
|
565
|
-
* @default undefined
|
|
566
594
|
* @requires express-fileupload
|
|
567
595
|
* */
|
|
568
596
|
upload?: boolean | UploadOptions;
|
|
569
597
|
/**
|
|
570
598
|
* @desc Enable or configure response compression.
|
|
571
|
-
* @default undefined
|
|
572
599
|
* @requires compression
|
|
573
600
|
*/
|
|
574
601
|
compression?: boolean | CompressionOptions;
|
|
@@ -578,17 +605,21 @@ interface ServerConfig extends CommonConfig {
|
|
|
578
605
|
* @link https://expressjs.com/en/4x/api.html#express.raw
|
|
579
606
|
* */
|
|
580
607
|
rawParser?: RequestHandler;
|
|
608
|
+
/**
|
|
609
|
+
* @desc Custom parser for URL Encoded requests used for submitting HTML forms
|
|
610
|
+
* @default express.urlencoded()
|
|
611
|
+
* @link https://expressjs.com/en/4x/api.html#express.urlencoded
|
|
612
|
+
* */
|
|
613
|
+
formParser?: RequestHandler;
|
|
581
614
|
/**
|
|
582
615
|
* @desc A code to execute before processing the Routing of your API (and before parsing).
|
|
583
616
|
* @desc This can be a good place for express middlewares establishing their own routes.
|
|
584
617
|
* @desc It can help to avoid making a DIY solution based on the attachRouting() approach.
|
|
585
|
-
* @default undefined
|
|
586
618
|
* @example ({ app }) => { app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); }
|
|
587
619
|
* */
|
|
588
620
|
beforeRouting?: BeforeRouting;
|
|
589
621
|
/**
|
|
590
622
|
* @desc Rejects new connections and attempts to finish ongoing ones in the specified time before exit.
|
|
591
|
-
* @default undefined
|
|
592
623
|
* */
|
|
593
624
|
gracefulShutdown?: boolean | GracefulOptions;
|
|
594
625
|
}
|
|
@@ -684,15 +715,39 @@ declare module "zod" {
|
|
|
684
715
|
}
|
|
685
716
|
|
|
686
717
|
interface BuildProps<IN extends IOSchema, OUT extends IOSchema | z.ZodVoid, MIN extends IOSchema<"strip">, OPT extends FlatObject, SCO extends string> {
|
|
718
|
+
/**
|
|
719
|
+
* @desc Input schema of the Endpoint, combining properties from all the enabled input sources (path params, headers)
|
|
720
|
+
* @default z.object({})
|
|
721
|
+
* @see defaultInputSources
|
|
722
|
+
* */
|
|
687
723
|
input?: IN;
|
|
724
|
+
/** @desc The schema by which the returns of the Endpoint handler is validated */
|
|
688
725
|
output: OUT;
|
|
726
|
+
/** @desc The Endpoint handler receiving the validated inputs, returns of added Middlewares (options) and a logger */
|
|
689
727
|
handler: Handler<z.output<z.ZodIntersection<MIN, IN>>, z.input<OUT>, OPT>;
|
|
728
|
+
/** @desc The operation description for the generated Documentation */
|
|
690
729
|
description?: string;
|
|
730
|
+
/** @desc The operation summary for the generated Documentation (50 symbols max) */
|
|
691
731
|
shortDescription?: string;
|
|
732
|
+
/** @desc The operation ID for the generated Documentation (must be unique) */
|
|
692
733
|
operationId?: string | ((method: Method) => string);
|
|
734
|
+
/**
|
|
735
|
+
* @desc HTTP method(s) this endpoint can handle
|
|
736
|
+
* @default "get" unless the Endpoint is assigned within DependsOnMethod
|
|
737
|
+
* @see DependsOnMethod
|
|
738
|
+
* */
|
|
693
739
|
method?: Method | [Method, ...Method[]];
|
|
740
|
+
/**
|
|
741
|
+
* @desc Scope(s) from the list of the ones defined by the added Middlewares having "oauth2" security type
|
|
742
|
+
* @see OAuth2Security
|
|
743
|
+
* */
|
|
694
744
|
scope?: SCO | SCO[];
|
|
745
|
+
/**
|
|
746
|
+
* @desc Tag(s) for generating Documentation. For establishing constraints:
|
|
747
|
+
* @see TagOverrides
|
|
748
|
+
* */
|
|
695
749
|
tag?: Tag | Tag[];
|
|
750
|
+
/** @desc Marks the operation deprecated in the generated Documentation */
|
|
696
751
|
deprecated?: boolean;
|
|
697
752
|
}
|
|
698
753
|
declare class EndpointsFactory<IN extends IOSchema<"strip"> = EmptySchema, OUT extends FlatObject = EmptyObject, SCO extends string = string> {
|
|
@@ -743,6 +798,7 @@ declare function file<K extends Variant>(variant: K): ReturnType<Variants[K]>;
|
|
|
743
798
|
declare const ez: {
|
|
744
799
|
dateIn: () => zod.ZodBranded<zod.ZodPipeline<zod.ZodEffects<zod.ZodUnion<[zod.ZodString, zod.ZodString, zod.ZodString]>, Date, string>, zod.ZodEffects<zod.ZodDate, Date, Date>>, symbol>;
|
|
745
800
|
dateOut: () => zod.ZodBranded<zod.ZodEffects<zod.ZodEffects<zod.ZodDate, Date, Date>, string, Date>, symbol>;
|
|
801
|
+
form: <S extends zod.ZodRawShape>(base: S | zod.ZodObject<S>) => zod.ZodBranded<zod.ZodObject<S, zod.UnknownKeysParam, zod.ZodTypeAny, zod.objectUtil.addQuestionMarks<zod.baseObjectOutputType<S>, any> extends infer T ? { [k in keyof T]: zod.objectUtil.addQuestionMarks<zod.baseObjectOutputType<S>, any>[k]; } : never, zod.baseObjectInputType<S> extends infer T_1 ? { [k_1 in keyof T_1]: zod.baseObjectInputType<S>[k_1]; } : never>, symbol>;
|
|
746
802
|
file: typeof file;
|
|
747
803
|
upload: () => zod.ZodBranded<zod.ZodType<express_fileupload.UploadedFile, zod.ZodTypeDef, express_fileupload.UploadedFile>, symbol>;
|
|
748
804
|
raw: <S extends zod.ZodRawShape>(extra?: S) => zod.ZodBranded<zod.ZodObject<zod.objectUtil.extendShape<{
|
|
@@ -912,7 +968,10 @@ declare const testMiddleware: <LOG extends FlatObject, REQ extends RequestOption
|
|
|
912
968
|
middleware: AbstractMiddleware;
|
|
913
969
|
/** @desc The aggregated output from previously executed middlewares */
|
|
914
970
|
options?: FlatObject;
|
|
915
|
-
/**
|
|
971
|
+
/**
|
|
972
|
+
* @desc Enables transforming possible middleware errors into response, so that test Middleware does not throw
|
|
973
|
+
* @todo consider utilizing errorHandler from config instead in v23
|
|
974
|
+
* */
|
|
916
975
|
errorHandler?: (error: Error, response: Response) => void;
|
|
917
976
|
}) => Promise<{
|
|
918
977
|
requestMock: node_mocks_http.MockRequest<Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>> & REQ>;
|
|
@@ -1070,7 +1129,7 @@ type EventsMap = Record<string, z.ZodTypeAny>;
|
|
|
1070
1129
|
interface Emitter<E extends EventsMap> extends FlatObject {
|
|
1071
1130
|
/** @desc Returns true when the connection was closed or terminated */
|
|
1072
1131
|
isClosed: () => boolean;
|
|
1073
|
-
/** @desc Sends an event to the stream
|
|
1132
|
+
/** @desc Sends an event to the stream according to the declared schema */
|
|
1074
1133
|
emit: <K extends keyof E>(event: K, data: z.input<E[K]>) => void;
|
|
1075
1134
|
}
|
|
1076
1135
|
declare class EventStreamFactory<E extends EventsMap> extends EndpointsFactory<EmptySchema, Emitter<E>> {
|