express-zod-api 28.7.5 → 29.0.0-beta.3
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 +68 -39
- package/README.md +36 -33
- package/SECURITY.md +2 -1
- package/dist/documentation-helpers-BLgXFiWr.d.ts +30 -0
- package/dist/documentation.d.ts +75 -0
- package/dist/documentation.js +1 -0
- package/dist/index.d.ts +48 -870
- package/dist/index.js +6 -20
- package/dist/integration.d.ts +85 -0
- package/dist/integration.js +4 -0
- package/dist/json-schema-helpers-Bhz0qPT7.js +1 -0
- package/dist/peer-helpers-eiBZCqrL.js +1 -0
- package/dist/routing-C0jFxoT6.d.ts +742 -0
- package/dist/routing-walker--qYUjhGi.js +1 -0
- package/package.json +18 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,690 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
A as ensureHttpError,
|
|
3
|
+
C as CommonConfig,
|
|
4
|
+
D as ResultHandler,
|
|
5
|
+
E as AbstractResultHandler,
|
|
6
|
+
F as Method,
|
|
7
|
+
I as BuiltinLogger,
|
|
8
|
+
L as AbstractLogger,
|
|
9
|
+
M as FinalInputSchema,
|
|
10
|
+
N as IOSchema,
|
|
11
|
+
O as arrayResultHandler,
|
|
12
|
+
P as ClientMethod,
|
|
13
|
+
R as LoggerOverrides,
|
|
14
|
+
S as AppConfig,
|
|
15
|
+
T as createConfig,
|
|
16
|
+
_ as EmptySchema,
|
|
17
|
+
a as Handler,
|
|
18
|
+
b as TagOverrides,
|
|
19
|
+
c as Middleware,
|
|
20
|
+
d as CookieSecurity,
|
|
21
|
+
f as HeaderSecurity,
|
|
22
|
+
g as EmptyObject,
|
|
23
|
+
h as OpenIdSecurity,
|
|
24
|
+
i as Endpoint,
|
|
25
|
+
j as Extension,
|
|
26
|
+
k as defaultResultHandler,
|
|
27
|
+
l as BasicSecurity,
|
|
28
|
+
m as OAuth2Security,
|
|
29
|
+
n as ServeStatic,
|
|
30
|
+
o as AbstractMiddleware,
|
|
31
|
+
p as InputSecurity,
|
|
32
|
+
r as AbstractEndpoint,
|
|
33
|
+
s as ExpressMiddleware,
|
|
34
|
+
t as Routing,
|
|
35
|
+
u as BearerSecurity,
|
|
36
|
+
v as FlatObject,
|
|
37
|
+
w as ServerConfig,
|
|
38
|
+
x as getMessageFromError,
|
|
39
|
+
y as Tag,
|
|
40
|
+
z as ApiResponse,
|
|
41
|
+
} from "./routing-C0jFxoT6.js";
|
|
42
|
+
import { i as OpenAPIContext } from "./documentation-helpers-BLgXFiWr.js";
|
|
1
43
|
import { z } from "zod";
|
|
2
|
-
import {
|
|
3
|
-
import express, { CookieOptions, IRouter, NextFunction, Request, RequestHandler, Response } from "express";
|
|
44
|
+
import express, { CookieOptions, Request, Response } from "express";
|
|
4
45
|
import http from "node:http";
|
|
5
|
-
import { ServerOptions } from "node:https";
|
|
6
|
-
import { OpenApiBuilder, ReferenceObject, SchemaObject, TagObject } from "openapi3-ts/oas31";
|
|
7
46
|
import { RequestOptions, ResponseOptions } from "node-mocks-http";
|
|
8
|
-
import
|
|
9
|
-
import fileUpload from "express-fileupload";
|
|
10
|
-
import cookieParser from "cookie-parser";
|
|
11
|
-
import { ListenOptions } from "node:net";
|
|
47
|
+
import "express-fileupload";
|
|
12
48
|
import { AugmentedRequest, Options, RateLimitInfo, RateLimitRequestHandler } from "express-rate-limit";
|
|
13
|
-
import ts from "typescript";
|
|
14
|
-
declare const methods: ("get" | "post" | "put" | "delete" | "patch")[];
|
|
15
|
-
declare const clientMethods: ("get" | "post" | "put" | "delete" | "patch" | "head")[];
|
|
16
|
-
/**
|
|
17
|
-
* @desc Methods supported by the framework API to produce Endpoints on EndpointsFactory.
|
|
18
|
-
* @see BuildProps
|
|
19
|
-
* @example "get" | "post" | "put" | "delete" | "patch"
|
|
20
|
-
* */
|
|
21
|
-
type Method = (typeof methods)[number];
|
|
22
|
-
/**
|
|
23
|
-
* @desc Methods usable on the client side, available via generated Integration and Documentation
|
|
24
|
-
* @see withHead
|
|
25
|
-
* @example Method | "head"
|
|
26
|
-
* */
|
|
27
|
-
type ClientMethod = (typeof clientMethods)[number];
|
|
28
|
-
declare const defaultStatusCodes: {
|
|
29
|
-
positive: number;
|
|
30
|
-
negative: number;
|
|
31
|
-
};
|
|
32
|
-
type ResponseVariant = keyof typeof defaultStatusCodes;
|
|
33
|
-
/**
|
|
34
|
-
* @desc A container for describing an API response: its schema, status code(s) and MIME type(s).
|
|
35
|
-
* @see ResultHandler
|
|
36
|
-
* */
|
|
37
|
-
interface ApiResponse<S extends z.ZodType> {
|
|
38
|
-
/** @desc The Zod schema describing the response body. */
|
|
39
|
-
schema: S;
|
|
40
|
-
/**
|
|
41
|
-
* @desc The status code(s) for this response.
|
|
42
|
-
* @default 200 for a positive response, 400 for a negative one
|
|
43
|
-
* */
|
|
44
|
-
statusCode?: number | [number, ...number[]];
|
|
45
|
-
/**
|
|
46
|
-
* @desc The MIME type(s) of the response.
|
|
47
|
-
* @default "application/json"
|
|
48
|
-
* @example null — no content, typical for 204 and 302
|
|
49
|
-
* */
|
|
50
|
-
mimeType?: string | [string, ...string[]] | null;
|
|
51
|
-
}
|
|
52
|
-
/** @since zod 3.25.61 output type fixed */
|
|
53
|
-
declare const emptySchema: z.ZodObject<{}, z.core.$strip>;
|
|
54
|
-
type EmptySchema = typeof emptySchema;
|
|
55
|
-
/** @desc this type does not allow props assignment, but it works for reading them when merged with another interface */
|
|
56
|
-
type EmptyObject = z.output<EmptySchema>;
|
|
57
|
-
type FlatObject = Record<string, unknown>;
|
|
58
|
-
/** @link https://stackoverflow.com/a/65492934 */
|
|
59
|
-
type NoNever<T, F> = [T] extends [never] ? F : T;
|
|
60
|
-
/**
|
|
61
|
-
* @desc Using module augmentation approach you can specify tags as the keys of this interface
|
|
62
|
-
* @example declare module "express-zod-api" { interface TagOverrides { users: unknown } }
|
|
63
|
-
* @link https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
|
|
64
|
-
* */
|
|
65
|
-
interface TagOverrides {}
|
|
66
|
-
type Tag = NoNever<keyof TagOverrides, string>;
|
|
67
|
-
declare const getMessageFromError: (error: Error) => string;
|
|
68
|
-
declare const severity: {
|
|
69
|
-
debug: number;
|
|
70
|
-
info: number;
|
|
71
|
-
warn: number;
|
|
72
|
-
error: number;
|
|
73
|
-
};
|
|
74
|
-
type Severity = keyof typeof severity;
|
|
75
|
-
/** @desc You can use any logger compatible with this type. */
|
|
76
|
-
type AbstractLogger = Record<Severity, (message: string, meta?: any) => any>;
|
|
77
|
-
/**
|
|
78
|
-
* @desc Using module augmentation approach you can set the type of the actual logger used
|
|
79
|
-
* @example declare module "express-zod-api" { interface LoggerOverrides extends winston.Logger {} }
|
|
80
|
-
* @link https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
|
|
81
|
-
* */
|
|
82
|
-
interface LoggerOverrides {}
|
|
83
|
-
type ActualLogger = AbstractLogger & LoggerOverrides;
|
|
84
|
-
interface Context extends FlatObject {
|
|
85
|
-
requestId?: string;
|
|
86
|
-
}
|
|
87
|
-
interface BuiltinLoggerConfig {
|
|
88
|
-
/**
|
|
89
|
-
* @desc The minimal severity to log or "silent" to disable logging
|
|
90
|
-
* @example "debug" also enables pretty output for inspected entities
|
|
91
|
-
* */
|
|
92
|
-
level: "silent" | "warn" | "info" | "debug";
|
|
93
|
-
/** @desc Enables colors on printed severity and inspected entities */
|
|
94
|
-
color: boolean;
|
|
95
|
-
/**
|
|
96
|
-
* @desc Control how deeply entities should be inspected
|
|
97
|
-
* @example null
|
|
98
|
-
* @example Infinity
|
|
99
|
-
* */
|
|
100
|
-
depth: number | null;
|
|
101
|
-
/**
|
|
102
|
-
* @desc Context: the metadata applicable for each logged entry, used by .child() method
|
|
103
|
-
* @see childLoggerProvider
|
|
104
|
-
* */
|
|
105
|
-
ctx: Context;
|
|
106
|
-
}
|
|
107
|
-
interface ProfilerOptions {
|
|
108
|
-
message: string;
|
|
109
|
-
/** @default "debug" */
|
|
110
|
-
severity?: Severity | ((ms: number) => Severity);
|
|
111
|
-
/** @default formatDuration - adaptive units and limited fraction */
|
|
112
|
-
formatter?: (ms: number) => string | number;
|
|
113
|
-
}
|
|
114
|
-
/** @desc Built-in console logger with optional colorful inspections */
|
|
115
|
-
declare class BuiltinLogger implements AbstractLogger {
|
|
116
|
-
protected readonly config: BuiltinLoggerConfig;
|
|
117
|
-
/** @example new BuiltinLogger({ level: "debug", color: true, depth: 4 }) */
|
|
118
|
-
constructor({ color, level, depth, ctx }?: Partial<BuiltinLoggerConfig>);
|
|
119
|
-
protected format(subject: unknown): string;
|
|
120
|
-
protected print(method: Severity, message: string, meta?: unknown): void;
|
|
121
|
-
debug(message: string, meta?: unknown): void;
|
|
122
|
-
info(message: string, meta?: unknown): void;
|
|
123
|
-
warn(message: string, meta?: unknown): void;
|
|
124
|
-
error(message: string, meta?: unknown): void;
|
|
125
|
-
child(ctx: Context): BuiltinLogger;
|
|
126
|
-
/**
|
|
127
|
-
* @desc The argument used for instance created by .child() method
|
|
128
|
-
* @see ChildLoggerProvider
|
|
129
|
-
* */
|
|
130
|
-
get ctx(): Context;
|
|
131
|
-
/** @desc Measures the duration until you invoke the returned callback */
|
|
132
|
-
profile(message: string): () => void;
|
|
133
|
-
profile(options: ProfilerOptions): () => void;
|
|
134
|
-
}
|
|
135
|
-
type Base$1 = object & {
|
|
136
|
-
[Symbol.iterator]?: never;
|
|
137
|
-
};
|
|
138
|
-
/** @desc The type allowed on the top level of Middlewares and Endpoints */
|
|
139
|
-
type IOSchema = z.ZodType<Base$1>;
|
|
140
|
-
/** EndpointsFactory schema extended type when adding a Middleware */
|
|
141
|
-
type Extension<Current extends IOSchema | undefined, Inc extends IOSchema | undefined> = Current extends IOSchema
|
|
142
|
-
? Inc extends IOSchema
|
|
143
|
-
? z.ZodIntersection<Current, Inc>
|
|
144
|
-
: Current
|
|
145
|
-
: Inc;
|
|
146
|
-
/** The Endpoint input schema type, condition wrapped into schema to make it z.output-compatible */
|
|
147
|
-
type FinalInputSchema<FIN extends IOSchema | undefined, BIN extends IOSchema> = z.ZodIntersection<
|
|
148
|
-
FIN extends IOSchema ? FIN : BIN,
|
|
149
|
-
BIN
|
|
150
|
-
>;
|
|
151
|
-
type LogicalOr<T> = {
|
|
152
|
-
or: T[];
|
|
153
|
-
};
|
|
154
|
-
type LogicalAnd<T> = {
|
|
155
|
-
and: T[];
|
|
156
|
-
};
|
|
157
|
-
type LogicalContainer<T> = LogicalOr<T | LogicalAnd<T>> | LogicalAnd<T | LogicalOr<T>> | T;
|
|
158
|
-
interface BasicSecurity {
|
|
159
|
-
type: "basic";
|
|
160
|
-
}
|
|
161
|
-
interface BearerSecurity {
|
|
162
|
-
type: "bearer";
|
|
163
|
-
format?: "JWT" | string;
|
|
164
|
-
}
|
|
165
|
-
interface InputSecurity<K extends string> {
|
|
166
|
-
type: "input";
|
|
167
|
-
name: K;
|
|
168
|
-
}
|
|
169
|
-
interface HeaderSecurity {
|
|
170
|
-
type: "header";
|
|
171
|
-
name: string;
|
|
172
|
-
}
|
|
173
|
-
interface CookieSecurity {
|
|
174
|
-
type: "cookie";
|
|
175
|
-
name: string;
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* @see https://swagger.io/docs/specification/authentication/openid-connect-discovery/
|
|
179
|
-
* @desc available scopes has to be provided via the specified URL
|
|
180
|
-
*/
|
|
181
|
-
interface OpenIdSecurity {
|
|
182
|
-
type: "openid";
|
|
183
|
-
url: string;
|
|
184
|
-
}
|
|
185
|
-
interface AuthUrl {
|
|
186
|
-
/**
|
|
187
|
-
* @desc The authorization URL to use for this flow. Can be relative to the API server URL.
|
|
188
|
-
* @see https://swagger.io/docs/specification/api-host-and-base-path/
|
|
189
|
-
*/
|
|
190
|
-
authorizationUrl: string;
|
|
191
|
-
}
|
|
192
|
-
interface TokenUrl {
|
|
193
|
-
/** @desc The token URL to use for this flow. Can be relative to the API server URL. */
|
|
194
|
-
tokenUrl: string;
|
|
195
|
-
}
|
|
196
|
-
interface RefreshUrl {
|
|
197
|
-
/** @desc The URL to be used for obtaining refresh tokens. Can be relative to the API server URL. */
|
|
198
|
-
refreshUrl?: string;
|
|
199
|
-
}
|
|
200
|
-
interface Scopes<K extends string> {
|
|
201
|
-
/** @desc The available scopes for the OAuth2 security and their short descriptions. Optional. */
|
|
202
|
-
scopes?: Record<K, string>;
|
|
203
|
-
}
|
|
204
|
-
type AuthCodeFlow<S extends string> = AuthUrl & TokenUrl & RefreshUrl & Scopes<S>;
|
|
205
|
-
type ImplicitFlow<S extends string> = AuthUrl & RefreshUrl & Scopes<S>;
|
|
206
|
-
type PasswordFlow<S extends string> = TokenUrl & RefreshUrl & Scopes<S>;
|
|
207
|
-
type ClientCredFlow<S extends string> = TokenUrl & RefreshUrl & Scopes<S>;
|
|
208
|
-
/**
|
|
209
|
-
* @see https://swagger.io/docs/specification/authentication/oauth2/
|
|
210
|
-
*/
|
|
211
|
-
interface OAuth2Security<S extends string> {
|
|
212
|
-
type: "oauth2";
|
|
213
|
-
flows?: {
|
|
214
|
-
/** @desc Authorization Code flow (previously called accessCode in OpenAPI 2.0) */
|
|
215
|
-
authorizationCode?: AuthCodeFlow<S>;
|
|
216
|
-
/** @desc Implicit flow */
|
|
217
|
-
implicit?: ImplicitFlow<S>;
|
|
218
|
-
/** @desc Resource Owner Password flow */
|
|
219
|
-
password?: PasswordFlow<S>;
|
|
220
|
-
/** @desc Client Credentials flow (previously called application in OpenAPI 2.0) */
|
|
221
|
-
clientCredentials?: ClientCredFlow<S>;
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* @desc Middleware security schema descriptor
|
|
226
|
-
* @param K is an optional input field used by InputSecurity
|
|
227
|
-
* @param S is an optional union of scopes used by OAuth2Security
|
|
228
|
-
* */
|
|
229
|
-
type Security<K extends string = string, S extends string = string> =
|
|
230
|
-
| BasicSecurity
|
|
231
|
-
| BearerSecurity
|
|
232
|
-
| InputSecurity<K>
|
|
233
|
-
| HeaderSecurity
|
|
234
|
-
| CookieSecurity
|
|
235
|
-
| OpenIdSecurity
|
|
236
|
-
| OAuth2Security<S>;
|
|
237
|
-
type Handler$2<IN, CTX, RET> = (params: {
|
|
238
|
-
/** @desc The inputs from the enabled input sources validated against the input schema of the Middleware. */
|
|
239
|
-
input: IN;
|
|
240
|
-
/**
|
|
241
|
-
* @desc The returns of the previously executed Middlewares (typed when chaining Middlewares).
|
|
242
|
-
* @link https://github.com/RobinTail/express-zod-api/discussions/1250
|
|
243
|
-
* */
|
|
244
|
-
ctx: CTX;
|
|
245
|
-
/** @link https://expressjs.com/en/5x/api.html#req */
|
|
246
|
-
request: Request;
|
|
247
|
-
/** @link https://expressjs.com/en/5x/api.html#res */
|
|
248
|
-
response: Response;
|
|
249
|
-
/** @desc The instance of the configured logger. */
|
|
250
|
-
logger: ActualLogger;
|
|
251
|
-
}) => Promise<RET>;
|
|
252
|
-
declare abstract class AbstractMiddleware {
|
|
253
|
-
abstract execute(params: {
|
|
254
|
-
input: unknown;
|
|
255
|
-
ctx: FlatObject;
|
|
256
|
-
request: Request;
|
|
257
|
-
response: Response;
|
|
258
|
-
logger: ActualLogger;
|
|
259
|
-
}): Promise<FlatObject>;
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* @desc A Middleware that validates its input schema, executes its handler, and returns context properties available to
|
|
263
|
-
* next middlewares and the Endpoint handler. Can also declare security schemas for Documentation.
|
|
264
|
-
* @see EndpointsFactory#addMiddleware
|
|
265
|
-
* */
|
|
266
|
-
declare class Middleware<
|
|
267
|
-
CTX extends FlatObject,
|
|
268
|
-
RET extends FlatObject,
|
|
269
|
-
SCO extends string,
|
|
270
|
-
IN extends IOSchema | undefined = undefined,
|
|
271
|
-
> extends AbstractMiddleware {
|
|
272
|
-
constructor({
|
|
273
|
-
input,
|
|
274
|
-
security,
|
|
275
|
-
handler,
|
|
276
|
-
}: {
|
|
277
|
-
/**
|
|
278
|
-
* @desc Input schema of the Middleware, combining properties from all the enabled input sources
|
|
279
|
-
* @default undefined
|
|
280
|
-
* @see defaultInputSources
|
|
281
|
-
* */
|
|
282
|
-
input?: IN;
|
|
283
|
-
/**
|
|
284
|
-
* @desc Declaration of the security schemas implemented within the handler (used by Documentation).
|
|
285
|
-
* @see Documentation
|
|
286
|
-
* */
|
|
287
|
-
security?: LogicalContainer<Security<Extract<keyof z.input<IN>, string>, SCO>>;
|
|
288
|
-
/** @desc The handler returning a context available to Endpoints. */
|
|
289
|
-
handler: Handler$2<z.output<IN>, CTX, RET>;
|
|
290
|
-
});
|
|
291
|
-
/** @throws InputValidationError */
|
|
292
|
-
execute({
|
|
293
|
-
input,
|
|
294
|
-
...rest
|
|
295
|
-
}: {
|
|
296
|
-
input: unknown;
|
|
297
|
-
ctx: CTX;
|
|
298
|
-
request: Request;
|
|
299
|
-
response: Response;
|
|
300
|
-
logger: ActualLogger;
|
|
301
|
-
}): Promise<RET>;
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* @desc A wrapper around native Express middlewares that converts them into the framework's Middleware instances.
|
|
305
|
-
* Optionally, a `provider` can extend the context, and a `transformer` can convert caught errors.
|
|
306
|
-
* @see EndpointsFactory#addExpressMiddleware
|
|
307
|
-
* */
|
|
308
|
-
declare class ExpressMiddleware<R extends Request, S extends Response, RET extends FlatObject> extends Middleware<
|
|
309
|
-
FlatObject,
|
|
310
|
-
RET,
|
|
311
|
-
string
|
|
312
|
-
> {
|
|
313
|
-
constructor(
|
|
314
|
-
nativeMw: (request: R, response: S, next: NextFunction) => any,
|
|
315
|
-
{
|
|
316
|
-
provider,
|
|
317
|
-
transformer,
|
|
318
|
-
}?: {
|
|
319
|
-
/** @desc Extracts context properties from request and response after the native middleware execution. */
|
|
320
|
-
provider?: (request: R, response: S) => RET | Promise<RET>;
|
|
321
|
-
/** @desc Transforms errors caught from the native middleware before they propagate further. */
|
|
322
|
-
transformer?: (err: Error) => Error;
|
|
323
|
-
},
|
|
324
|
-
);
|
|
325
|
-
}
|
|
326
|
-
type ResultSchema<R extends Result> = R extends Result<infer S> ? S : never;
|
|
327
|
-
type DiscriminatedResult =
|
|
328
|
-
| {
|
|
329
|
-
output: FlatObject;
|
|
330
|
-
error: null;
|
|
331
|
-
}
|
|
332
|
-
| {
|
|
333
|
-
output: null;
|
|
334
|
-
error: Error;
|
|
335
|
-
};
|
|
336
|
-
/**
|
|
337
|
-
* @example InputValidationError —> BadRequest(400)
|
|
338
|
-
* @example Error —> InternalServerError(500)
|
|
339
|
-
* */
|
|
340
|
-
declare const ensureHttpError: (error: Error) => HttpError;
|
|
341
|
-
type Handler$1<RES = unknown> = (
|
|
342
|
-
params: DiscriminatedResult & {
|
|
343
|
-
/** null in case of failure to parse or to find the matching endpoint (error: not found) */
|
|
344
|
-
input: FlatObject | null;
|
|
345
|
-
/** can be empty: check the presence of the required property using the "in" operator */
|
|
346
|
-
ctx: FlatObject;
|
|
347
|
-
request: Request;
|
|
348
|
-
response: Response<RES>;
|
|
349
|
-
logger: ActualLogger;
|
|
350
|
-
},
|
|
351
|
-
) => void | Promise<void>;
|
|
352
|
-
/**
|
|
353
|
-
* @desc Result definition for ResultHandler: a plain schema (for JSON and default status codes) or custom ApiResponse.
|
|
354
|
-
* @see ApiResponse
|
|
355
|
-
* */
|
|
356
|
-
type Result<S extends z.ZodType = z.ZodType> = S | ApiResponse<S> | ApiResponse<S>[];
|
|
357
|
-
/** @desc A function that lazily produces a Result definition. */
|
|
358
|
-
type LazyResult<R extends Result, A extends unknown[] = []> = (...args: A) => R;
|
|
359
|
-
declare abstract class AbstractResultHandler {
|
|
360
|
-
protected constructor(handler: Handler$1);
|
|
361
|
-
execute(...params: Parameters<Handler$1>): Promise<void>;
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* @desc The entity responsible to respond consistently. Accepts positive and negative Result definitions.
|
|
365
|
-
* The positive definition can be a lazy function receiving the output schema of an Endpoint.
|
|
366
|
-
* @see Result
|
|
367
|
-
* */
|
|
368
|
-
declare class ResultHandler<POS extends Result, NEG extends Result> extends AbstractResultHandler {
|
|
369
|
-
constructor(params: {
|
|
370
|
-
/** @desc A description of the API response in case of success (schema, status code, MIME type) */
|
|
371
|
-
positive: POS | LazyResult<POS, [IOSchema]>;
|
|
372
|
-
/** @desc A description of the API response in case of error (schema, status code, MIME type) */
|
|
373
|
-
negative: NEG | LazyResult<NEG>;
|
|
374
|
-
/** @desc The actual implementation to transmit the response in any case */
|
|
375
|
-
handler: Handler$1<z.output<ResultSchema<POS> | ResultSchema<NEG>>>;
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* @desc The default ResultHandler wrapping Endpoint output in `{ status: "success", data: output }`
|
|
380
|
-
* and errors in `{ status: "error", error: { message } }`. Responds with JSON Content-Type.
|
|
381
|
-
* Respects the status of errors from createHttpError(), others become InternalServerError (500).
|
|
382
|
-
* @see ensureHttpError
|
|
383
|
-
* */
|
|
384
|
-
declare const defaultResultHandler: ResultHandler<
|
|
385
|
-
z.ZodObject<
|
|
386
|
-
{
|
|
387
|
-
status: z.ZodLiteral<"success">;
|
|
388
|
-
data: IOSchema;
|
|
389
|
-
},
|
|
390
|
-
z.core.$strip
|
|
391
|
-
>,
|
|
392
|
-
z.ZodObject<
|
|
393
|
-
{
|
|
394
|
-
status: z.ZodLiteral<"error">;
|
|
395
|
-
error: z.ZodObject<
|
|
396
|
-
{
|
|
397
|
-
message: z.ZodString;
|
|
398
|
-
},
|
|
399
|
-
z.core.$strip
|
|
400
|
-
>;
|
|
401
|
-
},
|
|
402
|
-
z.core.$strip
|
|
403
|
-
>
|
|
404
|
-
>;
|
|
405
|
-
/**
|
|
406
|
-
* @deprecated Resist the urge of using it: this handler is designed only to simplify the migration of legacy APIs.
|
|
407
|
-
* @desc Responding with an array is a bad practice keeping your endpoints from evolving without breaking changes.
|
|
408
|
-
* @desc This handler expects your endpoint to have the property 'items' in the output object schema
|
|
409
|
-
* */
|
|
410
|
-
declare const arrayResultHandler: ResultHandler<
|
|
411
|
-
z.ZodArray<z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>> | z.ZodArray<z.ZodAny>,
|
|
412
|
-
{
|
|
413
|
-
schema: z.ZodString;
|
|
414
|
-
mimeType: string;
|
|
415
|
-
}
|
|
416
|
-
>;
|
|
417
|
-
type OriginalStatic = typeof express.static;
|
|
418
|
-
declare class ServeStatic {
|
|
419
|
-
constructor(...params: Parameters<OriginalStatic>);
|
|
420
|
-
}
|
|
421
|
-
/** @desc Returns child logger for the given request (if configured) or the configured logger otherwise */
|
|
422
|
-
type GetLogger = (request?: Request) => ActualLogger;
|
|
423
|
-
/**
|
|
424
|
-
* @example { v1: { books: { ":bookId": getBookEndpoint } } }
|
|
425
|
-
* @example { "v1/books/:bookId": getBookEndpoint }
|
|
426
|
-
* @example { "get /v1/books/:bookId": getBookEndpoint }
|
|
427
|
-
* @example { v1: { "patch /books/:bookId": changeBookEndpoint } }
|
|
428
|
-
* @example { dependsOnMethod: { get: retrieveEndpoint, post: createEndpoint } }
|
|
429
|
-
* @see CommonConfig.recognizeMethodDependentRoutes
|
|
430
|
-
* */
|
|
431
|
-
interface Routing {
|
|
432
|
-
[K: string]: Routing | AbstractEndpoint | ServeStatic;
|
|
433
|
-
}
|
|
434
|
-
type Handler<IN, OUT, CTX> = (params: {
|
|
435
|
-
/** @desc The inputs from the enabled input sources validated against the final input schema (incl. Middlewares) */
|
|
436
|
-
input: IN;
|
|
437
|
-
/** @desc The returns of the assigned Middlewares */
|
|
438
|
-
ctx: CTX;
|
|
439
|
-
/** @desc The instance of the configured logger */
|
|
440
|
-
logger: ActualLogger;
|
|
441
|
-
}) => Promise<OUT>;
|
|
442
|
-
declare abstract class AbstractEndpoint {
|
|
443
|
-
/** @desc Enables nested routes within the path assigned to the subject */
|
|
444
|
-
nest(routing: Routing): Routing;
|
|
445
|
-
/** @desc Marks the route as deprecated (makes a copy of the endpoint) */
|
|
446
|
-
abstract deprecated(): this;
|
|
447
|
-
abstract execute(params: {
|
|
448
|
-
request: Request;
|
|
449
|
-
response: Response;
|
|
450
|
-
logger: ActualLogger;
|
|
451
|
-
config: CommonConfig;
|
|
452
|
-
}): Promise<void>;
|
|
453
|
-
}
|
|
454
|
-
declare class Endpoint<IN extends IOSchema, OUT extends IOSchema, CTX extends FlatObject> extends AbstractEndpoint {
|
|
455
|
-
constructor(def: {
|
|
456
|
-
deprecated?: boolean;
|
|
457
|
-
middlewares?: AbstractMiddleware[];
|
|
458
|
-
inputSchema: IN;
|
|
459
|
-
outputSchema: OUT;
|
|
460
|
-
handler: Handler<z.output<IN>, z.input<OUT>, CTX>;
|
|
461
|
-
resultHandler: AbstractResultHandler;
|
|
462
|
-
description?: string;
|
|
463
|
-
summary?: string;
|
|
464
|
-
getOperationId?: (method: ClientMethod) => string | undefined;
|
|
465
|
-
methods?: Method[];
|
|
466
|
-
scopes?: string[];
|
|
467
|
-
tags?: string[];
|
|
468
|
-
});
|
|
469
|
-
deprecated(): this;
|
|
470
|
-
execute({
|
|
471
|
-
request,
|
|
472
|
-
response,
|
|
473
|
-
logger,
|
|
474
|
-
config,
|
|
475
|
-
}: {
|
|
476
|
-
request: Request;
|
|
477
|
-
response: Response;
|
|
478
|
-
logger: ActualLogger;
|
|
479
|
-
config: CommonConfig;
|
|
480
|
-
}): Promise<undefined>;
|
|
481
|
-
}
|
|
482
|
-
type InputSource = keyof Pick<Request, "query" | "body" | "files" | "params" | "headers" | "cookies" | "signedCookies">;
|
|
483
|
-
type InputSources = Record<Method, InputSource[]>;
|
|
484
|
-
type Headers = Record<string, string>;
|
|
485
|
-
type HeadersProvider = (params: {
|
|
486
|
-
/** @desc The default headers to be overridden. */
|
|
487
|
-
defaultHeaders: Headers;
|
|
488
|
-
request: Request;
|
|
489
|
-
endpoint: AbstractEndpoint;
|
|
490
|
-
logger: ActualLogger;
|
|
491
|
-
}) => Headers | Promise<Headers>;
|
|
492
|
-
type ChildLoggerProvider = (params: { request: Request; parent: ActualLogger }) => ActualLogger | Promise<ActualLogger>;
|
|
493
|
-
type LogAccess = (request: Request, logger: ActualLogger) => void;
|
|
494
|
-
interface CommonConfig {
|
|
495
|
-
/**
|
|
496
|
-
* @desc Enables cross-origin resource sharing.
|
|
497
|
-
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
|
498
|
-
* @desc You can override the default CORS headers by setting up a provider function here.
|
|
499
|
-
*/
|
|
500
|
-
cors: boolean | HeadersProvider;
|
|
501
|
-
/**
|
|
502
|
-
* @desc Controls how to respond to a request to an existing endpoint with an invalid HTTP method.
|
|
503
|
-
* @example true — respond with status code 405 and "Allow" header containing a list of valid methods
|
|
504
|
-
* @example false — respond with status code 404 (Not found)
|
|
505
|
-
* @default true
|
|
506
|
-
*/
|
|
507
|
-
hintAllowedMethods?: boolean;
|
|
508
|
-
/**
|
|
509
|
-
* @desc Controls how to treat Routing keys matching HTTP methods ("get", "post") and having Endpoint assigned.
|
|
510
|
-
* @example true — treat such keys as HTTP methods complementing their parent paths
|
|
511
|
-
* { users: { get: ... }} becomes GET /users
|
|
512
|
-
* @example false — treat such keys as nested path segments regardless of the name
|
|
513
|
-
* { users: { get: ... }} remains /users/get
|
|
514
|
-
* @default true
|
|
515
|
-
*/
|
|
516
|
-
recognizeMethodDependentRoutes?: boolean;
|
|
517
|
-
/**
|
|
518
|
-
* @desc The ResultHandler to use for handling routing, parsing and upload errors
|
|
519
|
-
* @default defaultResultHandler
|
|
520
|
-
* @see defaultResultHandler
|
|
521
|
-
*/
|
|
522
|
-
errorHandler?: AbstractResultHandler;
|
|
523
|
-
/**
|
|
524
|
-
* @desc Built-in logger configuration or an instance of any compatible logger.
|
|
525
|
-
* @example { level: "debug", color: true }
|
|
526
|
-
* @default { level: NODE_ENV === "production" ? "warn" : "debug", color: isSupported(), depth: 2 }
|
|
527
|
-
* */
|
|
528
|
-
logger?: Partial<BuiltinLoggerConfig> | AbstractLogger;
|
|
529
|
-
/**
|
|
530
|
-
* @desc A child logger returned by this function can override the logger in all handlers for each request
|
|
531
|
-
* @example ({ parent }) => parent.child({ requestId: uuid() })
|
|
532
|
-
* */
|
|
533
|
-
childLoggerProvider?: ChildLoggerProvider;
|
|
534
|
-
/**
|
|
535
|
-
* @desc The function for producing access logs
|
|
536
|
-
* @default ({ method, path }, logger) => logger.debug(`${method}: ${path}`)
|
|
537
|
-
* @example null — disables the feature
|
|
538
|
-
* */
|
|
539
|
-
accessLogger?: null | LogAccess;
|
|
540
|
-
/**
|
|
541
|
-
* @desc You can disable the startup logo.
|
|
542
|
-
* @default true
|
|
543
|
-
*/
|
|
544
|
-
startupLogo?: boolean;
|
|
545
|
-
/**
|
|
546
|
-
* @desc Which properties of request are combined into the input for endpoints and middlewares.
|
|
547
|
-
* @desc The order matters: priority from lowest to highest
|
|
548
|
-
* @default defaultInputSources
|
|
549
|
-
* @see defaultInputSources
|
|
550
|
-
*/
|
|
551
|
-
inputSources?: Partial<InputSources>;
|
|
552
|
-
}
|
|
553
|
-
type BeforeUpload = (params: { request: Request; logger: ActualLogger }) => void | Promise<void>;
|
|
554
|
-
type UploadOptions = Pick<
|
|
555
|
-
fileUpload.Options,
|
|
556
|
-
| "createParentPath"
|
|
557
|
-
| "uriDecodeFileNames"
|
|
558
|
-
| "safeFileNames"
|
|
559
|
-
| "preserveExtension"
|
|
560
|
-
| "useTempFiles"
|
|
561
|
-
| "tempFileDir"
|
|
562
|
-
| "debug"
|
|
563
|
-
| "uploadTimeout"
|
|
564
|
-
| "limits"
|
|
565
|
-
> & {
|
|
566
|
-
/**
|
|
567
|
-
* @desc The error to throw when the file exceeds the configured fileSize limit (handled by errorHandler).
|
|
568
|
-
* @see limits
|
|
569
|
-
* @override limitHandler
|
|
570
|
-
* @example createHttpError(413, "The file is too large")
|
|
571
|
-
* */
|
|
572
|
-
limitError?: Error;
|
|
573
|
-
/**
|
|
574
|
-
* @desc A handler to execute before uploading — it can be used for restrictions by throwing an error.
|
|
575
|
-
* @example ({ request }) => { throw createHttpError(403, "Not authorized"); }
|
|
576
|
-
* */
|
|
577
|
-
beforeUpload?: BeforeUpload;
|
|
578
|
-
};
|
|
579
|
-
interface CookieParserOptions extends cookieParser.CookieParseOptions {
|
|
580
|
-
/** @desc The secret string or array used by cookie-parser for signed cookies */
|
|
581
|
-
secret?: Parameters<typeof cookieParser>[0];
|
|
582
|
-
}
|
|
583
|
-
type CompressionOptions = Pick<
|
|
584
|
-
compression.CompressionOptions,
|
|
585
|
-
"threshold" | "level" | "strategy" | "chunkSize" | "memLevel"
|
|
586
|
-
>;
|
|
587
|
-
interface GracefulOptions {
|
|
588
|
-
/**
|
|
589
|
-
* @desc Time given to drain ongoing requests before closing the server.
|
|
590
|
-
* @default 1000
|
|
591
|
-
* */
|
|
592
|
-
timeout?: number;
|
|
593
|
-
/**
|
|
594
|
-
* @desc Process event (Signal) that triggers the graceful shutdown.
|
|
595
|
-
* @see Signals
|
|
596
|
-
* @default [SIGINT, SIGTERM]
|
|
597
|
-
* */
|
|
598
|
-
events?: string[];
|
|
599
|
-
/** @desc The hook to call after the server was closed, but before terminating the process. */
|
|
600
|
-
beforeExit?: () => void | Promise<void>;
|
|
601
|
-
}
|
|
602
|
-
/** @todo consider removing Promise to make createServer sync in next major */
|
|
603
|
-
type ServerHook = (params: {
|
|
604
|
-
app: IRouter;
|
|
605
|
-
/** @desc Returns child logger for the given request (if configured) or the configured logger otherwise */
|
|
606
|
-
getLogger: GetLogger;
|
|
607
|
-
}) => void | Promise<void>;
|
|
608
|
-
interface HttpConfig {
|
|
609
|
-
/** @desc Port, UNIX socket or custom options. */
|
|
610
|
-
listen: number | string | ListenOptions;
|
|
611
|
-
}
|
|
612
|
-
interface HttpsConfig extends HttpConfig {
|
|
613
|
-
/** @desc At least "cert" and "key" options required. */
|
|
614
|
-
options: ServerOptions;
|
|
615
|
-
}
|
|
616
|
-
interface ServerConfig extends CommonConfig {
|
|
617
|
-
/** @desc HTTP server configuration. */
|
|
618
|
-
http?: HttpConfig;
|
|
619
|
-
/** @desc HTTPS server configuration. */
|
|
620
|
-
https?: HttpsConfig;
|
|
621
|
-
/**
|
|
622
|
-
* @desc Custom JSON parser.
|
|
623
|
-
* @default express.json()
|
|
624
|
-
* @link https://expressjs.com/en/5x/api.html#express.json
|
|
625
|
-
* */
|
|
626
|
-
jsonParser?: RequestHandler;
|
|
627
|
-
/**
|
|
628
|
-
* @desc Enable or configure uploads handling.
|
|
629
|
-
* @requires express-fileupload
|
|
630
|
-
* */
|
|
631
|
-
upload?: boolean | UploadOptions;
|
|
632
|
-
/**
|
|
633
|
-
* @desc Enable or configure response compression.
|
|
634
|
-
* @requires compression
|
|
635
|
-
*/
|
|
636
|
-
compression?: boolean | CompressionOptions;
|
|
637
|
-
/**
|
|
638
|
-
* @desc Enable cookie parsing via cookie-parser.
|
|
639
|
-
* @requires cookie-parser
|
|
640
|
-
* @example true
|
|
641
|
-
* @example { secret: "my-secret" }
|
|
642
|
-
*/
|
|
643
|
-
cookies?: boolean | CookieParserOptions;
|
|
644
|
-
/**
|
|
645
|
-
* @desc Configure or customize the parser for request query string
|
|
646
|
-
* @example "simple" // for "node:querystring" module, array elements must be repeated: ?a=1&a=2
|
|
647
|
-
* @example "extended" // for "qs" module, supports nested objects and arrays with brackets: ?a[]=1&a[]=2
|
|
648
|
-
* @example (query) => qs.parse(query, {comma: true}) // for comma-separated arrays: ?a=1,2,3
|
|
649
|
-
* @default "simple"
|
|
650
|
-
* @link https://expressjs.com/en/5x/api.html#req.query
|
|
651
|
-
*/
|
|
652
|
-
queryParser?: "simple" | "extended" | ((query: string) => object);
|
|
653
|
-
/**
|
|
654
|
-
* @desc Custom raw parser (assigns Buffer to request body)
|
|
655
|
-
* @default express.raw()
|
|
656
|
-
* @link https://expressjs.com/en/5x/api.html#express.raw
|
|
657
|
-
* */
|
|
658
|
-
rawParser?: RequestHandler;
|
|
659
|
-
/**
|
|
660
|
-
* @desc Custom parser for URL Encoded requests used for submitting HTML forms
|
|
661
|
-
* @default express.urlencoded()
|
|
662
|
-
* @link https://expressjs.com/en/5x/api.html#express.urlencoded
|
|
663
|
-
* */
|
|
664
|
-
formParser?: RequestHandler;
|
|
665
|
-
/**
|
|
666
|
-
* @desc A code to execute before processing the Routing of your API (and before parsing).
|
|
667
|
-
* @desc This can be a good place for express middlewares establishing their own routes.
|
|
668
|
-
* @desc It can help to avoid making a DIY solution based on the attachRouting() approach.
|
|
669
|
-
* @example ({ app }) => { app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); }
|
|
670
|
-
* */
|
|
671
|
-
beforeRouting?: ServerHook;
|
|
672
|
-
/**
|
|
673
|
-
* @desc A code to execute after processing the Routing of your API, but before error handling.
|
|
674
|
-
* @see beforeRouting
|
|
675
|
-
* */
|
|
676
|
-
afterRouting?: ServerHook;
|
|
677
|
-
/**
|
|
678
|
-
* @desc Rejects new connections and attempts to finish ongoing ones in the specified time before exit.
|
|
679
|
-
* */
|
|
680
|
-
gracefulShutdown?: boolean | GracefulOptions;
|
|
681
|
-
}
|
|
682
|
-
interface AppConfig extends CommonConfig {
|
|
683
|
-
/** @desc Your custom express app or express router instead. */
|
|
684
|
-
app: IRouter;
|
|
685
|
-
}
|
|
686
|
-
declare function createConfig(config: ServerConfig): ServerConfig;
|
|
687
|
-
declare function createConfig(config: AppConfig): AppConfig;
|
|
688
49
|
/**
|
|
689
50
|
* @desc Directives shared by both request and response Cache-Control headers.
|
|
690
51
|
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control#cache_directives
|
|
@@ -1108,101 +469,11 @@ declare const attachRouting: (
|
|
|
1108
469
|
declare const createServer: (
|
|
1109
470
|
config: ServerConfig,
|
|
1110
471
|
routing: Routing,
|
|
1111
|
-
) =>
|
|
472
|
+
) => {
|
|
1112
473
|
app: import("express-serve-static-core").Express;
|
|
1113
474
|
logger: AbstractLogger | BuiltinLogger;
|
|
1114
475
|
servers: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>[];
|
|
1115
|
-
}
|
|
1116
|
-
interface ReqResCommons {
|
|
1117
|
-
makeRef: (key: object | string, value: SchemaObject | ReferenceObject, proposedName?: string) => ReferenceObject;
|
|
1118
|
-
path: string;
|
|
1119
|
-
method: ClientMethod;
|
|
1120
|
-
}
|
|
1121
|
-
interface OpenAPIContext extends ReqResCommons {
|
|
1122
|
-
isResponse: boolean;
|
|
1123
|
-
}
|
|
1124
|
-
type Depicter = (
|
|
1125
|
-
zodCtx: {
|
|
1126
|
-
zodSchema: z.core.$ZodType;
|
|
1127
|
-
jsonSchema: z.core.JSONSchema.BaseSchema;
|
|
1128
|
-
},
|
|
1129
|
-
oasCtx: OpenAPIContext,
|
|
1130
|
-
) => z.core.JSONSchema.BaseSchema | SchemaObject;
|
|
1131
|
-
/** @desc Using defaultIsHeader when returns null or undefined */
|
|
1132
|
-
type IsHeader = (name: string, method: ClientMethod, path: string) => boolean | null | undefined;
|
|
1133
|
-
type BrandHandling = Record<string | symbol, Depicter>;
|
|
1134
|
-
declare const depictTags: (
|
|
1135
|
-
tags: Partial<
|
|
1136
|
-
Record<
|
|
1137
|
-
Tag,
|
|
1138
|
-
| string
|
|
1139
|
-
| {
|
|
1140
|
-
description: string;
|
|
1141
|
-
url?: string;
|
|
1142
|
-
}
|
|
1143
|
-
>
|
|
1144
|
-
>,
|
|
1145
|
-
) => TagObject[];
|
|
1146
|
-
/** @desc Ensures the summary string does not exceed the limit */
|
|
1147
|
-
declare const trimSummary: (summary?: string, limit?: number) => string | undefined;
|
|
1148
|
-
type Component = `${ResponseVariant}Response` | "requestParameter" | "requestBody";
|
|
1149
|
-
/** @desc user defined function that creates a component description from its properties */
|
|
1150
|
-
type Descriptor = (
|
|
1151
|
-
props: Record<"method" | "path" | "operationId", string> & {
|
|
1152
|
-
statusCode?: number;
|
|
1153
|
-
},
|
|
1154
|
-
) => string;
|
|
1155
|
-
type Summarizer = (params: { summary?: string; description?: string; trim: typeof trimSummary }) => string | undefined;
|
|
1156
|
-
interface DocumentationParams {
|
|
1157
|
-
title: string;
|
|
1158
|
-
version: string;
|
|
1159
|
-
serverUrl: string | [string, ...string[]];
|
|
1160
|
-
routing: Routing;
|
|
1161
|
-
config: CommonConfig;
|
|
1162
|
-
/**
|
|
1163
|
-
* @desc Descriptions of various components based on their properties (method, path, operationId).
|
|
1164
|
-
* @desc When composition set to "components", component name is generated from this description
|
|
1165
|
-
* @default () => `${method} ${path} ${component}`
|
|
1166
|
-
* */
|
|
1167
|
-
descriptions?: Partial<Record<Component, Descriptor>>;
|
|
1168
|
-
/**
|
|
1169
|
-
* @desc The function that ensures the maximum length for summary fields. Can optionally make them from descriptions.
|
|
1170
|
-
* @see defaultSummarizer
|
|
1171
|
-
* @see trimSummary
|
|
1172
|
-
* */
|
|
1173
|
-
summarizer?: Summarizer;
|
|
1174
|
-
/**
|
|
1175
|
-
* @desc Depict the HEAD method for each Endpoint supporting the GET method (feature of Express)
|
|
1176
|
-
* @default true
|
|
1177
|
-
* */
|
|
1178
|
-
hasHeadMethod?: boolean;
|
|
1179
|
-
/** @default inline */
|
|
1180
|
-
composition?: "inline" | "components";
|
|
1181
|
-
/**
|
|
1182
|
-
* @desc Handling rules for your own schemas branded with `x-brand` metadata.
|
|
1183
|
-
* @desc Keys: brands (recommended to use unique symbols).
|
|
1184
|
-
* @desc Values: functions having Zod context as first argument, second one is the framework context.
|
|
1185
|
-
* @example { MyBrand: ({ zodSchema, jsonSchema }) => ({ type: "object" })
|
|
1186
|
-
* @link https://www.npmjs.com/package/@express-zod-api/zod-plugin
|
|
1187
|
-
*/
|
|
1188
|
-
brandHandling?: BrandHandling;
|
|
1189
|
-
/**
|
|
1190
|
-
* @desc Ability to configure recognition of headers among other input data
|
|
1191
|
-
* @desc Only applicable when "headers" is present within inputSources config option
|
|
1192
|
-
* @see defaultIsHeader
|
|
1193
|
-
* @link https://www.iana.org/assignments/http-fields/http-fields.xhtml
|
|
1194
|
-
* */
|
|
1195
|
-
isHeader?: IsHeader;
|
|
1196
|
-
/**
|
|
1197
|
-
* @desc Extended description of tags used in endpoints. For enforcing constraints:
|
|
1198
|
-
* @see TagOverrides
|
|
1199
|
-
* @example { users: "About users", files: { description: "About files", url: "https://example.com" } }
|
|
1200
|
-
* */
|
|
1201
|
-
tags?: Parameters<typeof depictTags>[0];
|
|
1202
|
-
}
|
|
1203
|
-
declare class Documentation extends OpenApiBuilder {
|
|
1204
|
-
constructor({ hasHeadMethod, ...rest }: DocumentationParams);
|
|
1205
|
-
}
|
|
476
|
+
};
|
|
1206
477
|
/** @desc An error related to the wrong Routing declaration */
|
|
1207
478
|
declare class RoutingError extends Error {
|
|
1208
479
|
name: string;
|
|
@@ -1282,7 +553,7 @@ declare const testEndpoint: <LOG extends FlatObject, REQ extends RequestOptions>
|
|
|
1282
553
|
responseMock: import("node-mocks-http").MockResponse<Response<any, Record<string, any>>>;
|
|
1283
554
|
loggerMock: AbstractLogger &
|
|
1284
555
|
LOG & {
|
|
1285
|
-
_getLogs: () => Record<"
|
|
556
|
+
_getLogs: () => Record<"error" | "debug" | "info" | "warn", unknown[]>;
|
|
1286
557
|
};
|
|
1287
558
|
}>;
|
|
1288
559
|
interface MiddlewareLike<RET extends FlatObject> {
|
|
@@ -1312,98 +583,9 @@ declare const testMiddleware: <LOG extends FlatObject, REQ extends RequestOption
|
|
|
1312
583
|
responseMock: import("node-mocks-http").MockResponse<Response<any, Record<string, any>>>;
|
|
1313
584
|
loggerMock: AbstractLogger &
|
|
1314
585
|
LOG & {
|
|
1315
|
-
_getLogs: () => Record<"
|
|
586
|
+
_getLogs: () => Record<"error" | "debug" | "info" | "warn", unknown[]>;
|
|
1316
587
|
};
|
|
1317
588
|
}>;
|
|
1318
|
-
declare abstract class IntegrationBase {
|
|
1319
|
-
protected readonly serverUrl: string;
|
|
1320
|
-
protected constructor(typescript: typeof ts, serverUrl: string);
|
|
1321
|
-
}
|
|
1322
|
-
interface NextHandlerInc<U> {
|
|
1323
|
-
next: (schema: z.core.$ZodType) => U;
|
|
1324
|
-
}
|
|
1325
|
-
type SchemaHandler<U, Context extends FlatObject = EmptyObject, Variant extends "regular" | "last" = "regular"> = (
|
|
1326
|
-
schema: any, // eslint-disable-line @typescript-eslint/no-explicit-any -- for assignment compatibility
|
|
1327
|
-
ctx: Context & (Variant extends "regular" ? NextHandlerInc<U> : Context),
|
|
1328
|
-
) => U;
|
|
1329
|
-
type HandlingRules<U, Context extends FlatObject = EmptyObject, K extends string | symbol = string | symbol> = Partial<
|
|
1330
|
-
Record<K, SchemaHandler<U, Context>>
|
|
1331
|
-
>;
|
|
1332
|
-
interface ZTSContext extends FlatObject {
|
|
1333
|
-
isResponse: boolean;
|
|
1334
|
-
makeAlias: (key: object, produce: () => ts.TypeNode) => ts.TypeNode;
|
|
1335
|
-
}
|
|
1336
|
-
type Producer = SchemaHandler<ts.TypeNode, ZTSContext>;
|
|
1337
|
-
interface IntegrationParams {
|
|
1338
|
-
/** @default loadPeer("typescript") */
|
|
1339
|
-
typescript?: typeof ts;
|
|
1340
|
-
routing: Routing;
|
|
1341
|
-
config: CommonConfig;
|
|
1342
|
-
/**
|
|
1343
|
-
* @desc What should be generated
|
|
1344
|
-
* @example "types" — types of your endpoint requests and responses (for a DIY solution)
|
|
1345
|
-
* @example "client" — an entity for performing typed requests and receiving typed responses
|
|
1346
|
-
* @default "client"
|
|
1347
|
-
* */
|
|
1348
|
-
variant?: "types" | "client";
|
|
1349
|
-
/** @default Client */
|
|
1350
|
-
clientClassName?: string;
|
|
1351
|
-
/** @default Subscription */
|
|
1352
|
-
subscriptionClassName?: string;
|
|
1353
|
-
/**
|
|
1354
|
-
* @desc The API URL to use in the generated code
|
|
1355
|
-
* @default https://example.com
|
|
1356
|
-
* */
|
|
1357
|
-
serverUrl?: string;
|
|
1358
|
-
/**
|
|
1359
|
-
* @desc The schema to use for responses without body such as 204
|
|
1360
|
-
* @default z.undefined()
|
|
1361
|
-
* */
|
|
1362
|
-
noBodySchema?: z.ZodType;
|
|
1363
|
-
/**
|
|
1364
|
-
* @desc Depict the HEAD method for each Endpoint supporting the GET method (feature of Express)
|
|
1365
|
-
* @default true
|
|
1366
|
-
* */
|
|
1367
|
-
hasHeadMethod?: boolean;
|
|
1368
|
-
/**
|
|
1369
|
-
* @desc Handling rules for your own schemas branded with `x-brand` metadata.
|
|
1370
|
-
* @desc Keys: brands (recommended to use unique symbols).
|
|
1371
|
-
* @desc Values: functions having schema as first argument that you should assign type to, second one is a context.
|
|
1372
|
-
* @example { MyBrand: (schema: typeof myBrandSchema, { next }) => createKeywordTypeNode(SyntaxKind.AnyKeyword)
|
|
1373
|
-
* @link https://www.npmjs.com/package/@express-zod-api/zod-plugin
|
|
1374
|
-
*/
|
|
1375
|
-
brandHandling?: HandlingRules<ts.TypeNode, ZTSContext>;
|
|
1376
|
-
}
|
|
1377
|
-
interface FormattedPrintingOptions {
|
|
1378
|
-
/** @desc Typescript printer options */
|
|
1379
|
-
printerOptions?: ts.PrinterOptions;
|
|
1380
|
-
/**
|
|
1381
|
-
* @desc Typescript code formatter
|
|
1382
|
-
* @default prettier.format
|
|
1383
|
-
* */
|
|
1384
|
-
format?: (program: string) => Promise<string>;
|
|
1385
|
-
}
|
|
1386
|
-
declare class Integration extends IntegrationBase {
|
|
1387
|
-
constructor({
|
|
1388
|
-
typescript,
|
|
1389
|
-
routing,
|
|
1390
|
-
config,
|
|
1391
|
-
brandHandling,
|
|
1392
|
-
variant,
|
|
1393
|
-
clientClassName,
|
|
1394
|
-
subscriptionClassName,
|
|
1395
|
-
serverUrl,
|
|
1396
|
-
noBodySchema,
|
|
1397
|
-
hasHeadMethod,
|
|
1398
|
-
}: IntegrationParams);
|
|
1399
|
-
/**
|
|
1400
|
-
* @deprecated use `new Integration()` without `typescript` option on its argument
|
|
1401
|
-
* @todo remove in the next major — no longer needed
|
|
1402
|
-
* */
|
|
1403
|
-
static create(params: Omit<IntegrationParams, "typescript">): Promise<Integration>;
|
|
1404
|
-
print(printerOptions?: ts.PrinterOptions): string;
|
|
1405
|
-
printFormatted({ printerOptions, format: userDefined }?: FormattedPrintingOptions): Promise<string>;
|
|
1406
|
-
}
|
|
1407
589
|
type EventsMap = Record<string, z.ZodType>;
|
|
1408
590
|
interface Emitter<E extends EventsMap> extends FlatObject {
|
|
1409
591
|
/** @desc Returns true when the connection was closed or terminated */
|
|
@@ -1597,8 +779,6 @@ export {
|
|
|
1597
779
|
type CachePolicy,
|
|
1598
780
|
type CommonConfig,
|
|
1599
781
|
type CookieSecurity,
|
|
1600
|
-
type Depicter,
|
|
1601
|
-
Documentation,
|
|
1602
782
|
DocumentationError,
|
|
1603
783
|
EndpointsFactory,
|
|
1604
784
|
EventStreamFactory,
|
|
@@ -1607,7 +787,6 @@ export {
|
|
|
1607
787
|
type IOSchema,
|
|
1608
788
|
type InputSecurity,
|
|
1609
789
|
InputValidationError,
|
|
1610
|
-
Integration,
|
|
1611
790
|
type LoggerOverrides,
|
|
1612
791
|
type Method,
|
|
1613
792
|
Middleware,
|
|
@@ -1615,7 +794,6 @@ export {
|
|
|
1615
794
|
type OAuth2Security,
|
|
1616
795
|
type OpenIdSecurity,
|
|
1617
796
|
OutputValidationError,
|
|
1618
|
-
type Producer,
|
|
1619
797
|
ResultHandler,
|
|
1620
798
|
type Routing,
|
|
1621
799
|
RoutingError,
|