express-zod-api 3.1.2 → 4.2.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 +47 -0
- package/README.md +33 -21
- package/SECURITY.md +1 -0
- package/dist/common-helpers.d.ts +3 -1
- package/dist/common-helpers.js +38 -5
- package/dist/common-helpers.js.map +1 -1
- package/dist/endpoints-factory.d.ts +2 -2
- package/dist/endpoints-factory.js +11 -2
- package/dist/endpoints-factory.js.map +1 -1
- package/dist/open-api-helpers.d.ts +36 -3
- package/dist/open-api-helpers.js +64 -41
- package/dist/open-api-helpers.js.map +1 -1
- package/dist/open-api.d.ts +3 -1
- package/dist/open-api.js +10 -6
- package/dist/open-api.js.map +1 -1
- package/dist/startup-logo.js +3 -3
- package/dist-esm/common-helpers.js +33 -1
- package/dist-esm/common-helpers.js.map +1 -1
- package/dist-esm/endpoints-factory.js +11 -2
- package/dist-esm/endpoints-factory.js.map +1 -1
- package/dist-esm/open-api-helpers.js +31 -32
- package/dist-esm/open-api-helpers.js.map +1 -1
- package/dist-esm/open-api.js +10 -6
- package/dist-esm/open-api.js.map +1 -1
- package/dist-esm/package.json +1 -1
- package/dist-esm/startup-logo.js +3 -3
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,54 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## Version 4
|
|
4
|
+
|
|
5
|
+
### v4.2.0
|
|
6
|
+
|
|
7
|
+
- `express` version is 4.17.2.
|
|
8
|
+
- `http-errors` version is 2.0.0.
|
|
9
|
+
|
|
10
|
+
### v4.1.0
|
|
11
|
+
|
|
12
|
+
- Feature #230. The shorthand method `EndpointsFactory::addOptions()`.
|
|
13
|
+
- You may find it useful in case you'd like to provide your Endpoint's handler with some entities that do not depend
|
|
14
|
+
on Request, maybe a database connection instance of similar.
|
|
15
|
+
- Under the hood the method creates a middleware with an empty input and attaches it to the factory.
|
|
16
|
+
- The argument supplied to the method is available within `options` parameter of the Endpoint's `handler`.
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { defaultEndpointsFactory } from "express-zod-api";
|
|
20
|
+
|
|
21
|
+
const newFactory = defaultEndpointsFactory.addOptions({
|
|
22
|
+
db: mongoose.connect("mongodb://connection.string"),
|
|
23
|
+
privateKey: fs.readFileSync("private-key.pem", "utf-8"),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const endpoint = newFactory.build({
|
|
27
|
+
method: "get",
|
|
28
|
+
input: z.object({}),
|
|
29
|
+
output: z.object({}),
|
|
30
|
+
handler: async ({ options }) => {
|
|
31
|
+
return {}; // options: { db, privateKey }
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### v4.0.0
|
|
37
|
+
|
|
38
|
+
- The deprecated parameter `type` of `EndpointsFactory::build({...})` has been removed.
|
|
39
|
+
- The OpenAPI generator now requires `config` parameter to be supplied in `new OpenAPI({...})`.
|
|
40
|
+
- The OpenAPI generator takes into account possibly customized `inputSources` from `config`.
|
|
41
|
+
|
|
3
42
|
## Version 3
|
|
4
43
|
|
|
44
|
+
### v3.2.0
|
|
45
|
+
|
|
46
|
+
- Feature #204. Detecting usage of `z.upload()` within Endpoint's input schema automatically.
|
|
47
|
+
- There is no longer need to specify `type: "upload"` for `EndpointsFactory::build({...})`.
|
|
48
|
+
- In case you are using `z.upload()` in endpoint's input schema, inputs will be parsed by the
|
|
49
|
+
`multipart/form-data` parser.
|
|
50
|
+
- The optional parameter `type?: "json" | "upload"` of `build({...})` is deprecated.
|
|
51
|
+
|
|
5
52
|
### v3.1.2
|
|
6
53
|
|
|
7
54
|
- Fixed issue #202, originally reported in PR #201.
|
package/README.md
CHANGED
|
@@ -27,24 +27,25 @@ Start your API server with I/O schema validation and custom middlewares in minut
|
|
|
27
27
|
7. [Try it](#try-it)
|
|
28
28
|
4. [Fascinating features](#fascinating-features)
|
|
29
29
|
1. [Middlewares](#middlewares)
|
|
30
|
-
2. [
|
|
31
|
-
3. [
|
|
32
|
-
4. [
|
|
33
|
-
5. [
|
|
34
|
-
6. [
|
|
35
|
-
7. [
|
|
36
|
-
8. [
|
|
37
|
-
9. [
|
|
38
|
-
10. [
|
|
39
|
-
11. [
|
|
40
|
-
12. [
|
|
41
|
-
13. [
|
|
30
|
+
2. [Options](#options)
|
|
31
|
+
3. [Refinements](#refinements)
|
|
32
|
+
4. [Transformations](#transformations)
|
|
33
|
+
5. [Route path params](#route-path-params)
|
|
34
|
+
6. [Response customization](#response-customization)
|
|
35
|
+
7. [Non-object response](#non-object-response) including file downloads
|
|
36
|
+
8. [File uploads](#file-uploads)
|
|
37
|
+
9. [Customizing logger](#customizing-logger)
|
|
38
|
+
10. [Usage with your own express app](#usage-with-your-own-express-app)
|
|
39
|
+
11. [Multiple schemas for one route](#multiple-schemas-for-one-route)
|
|
40
|
+
12. [Customizing input sources](#customizing-input-sources)
|
|
41
|
+
13. [Exporting endpoint types to frontend](#exporting-endpoint-types-to-frontend)
|
|
42
|
+
14. [Creating a documentation](#creating-a-documentation)
|
|
42
43
|
5. [Known issues](#known-issues)
|
|
43
44
|
1. [Excessive properties in endpoint output](#excessive-properties-in-endpoint-output)
|
|
44
45
|
6. [Your input to my output](#your-input-to-my-output)
|
|
45
46
|
|
|
46
47
|
You can find the release notes in [Changelog](CHANGELOG.md). Along with recommendations for migrating from
|
|
47
|
-
[
|
|
48
|
+
[v3](CHANGELOG.md#v400), [v2](CHANGELOG.md#v300-beta1) and [v1](CHANGELOG.md#v200-beta1).
|
|
48
49
|
|
|
49
50
|
# Why and what is it for
|
|
50
51
|
|
|
@@ -265,6 +266,20 @@ const endpointsFactory = defaultEndpointsFactory.addMiddleware(authMiddleware);
|
|
|
265
266
|
|
|
266
267
|
You can connect as many middlewares as you want, they will be executed in order.
|
|
267
268
|
|
|
269
|
+
## Options
|
|
270
|
+
|
|
271
|
+
In case you'd like to provide your endpoints with options that do not depend on Request, like database connection
|
|
272
|
+
instance, consider shorthand method `addOptions`.
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
import { defaultEndpointsFactory } from "express-zod-api";
|
|
276
|
+
|
|
277
|
+
const endpointsFactory = defaultEndpointsFactory.addOptions({
|
|
278
|
+
db: mongoose.connect("mongodb://connection.string"),
|
|
279
|
+
privateKey: fs.readFileSync("private-key.pem", "utf-8"),
|
|
280
|
+
});
|
|
281
|
+
```
|
|
282
|
+
|
|
268
283
|
## Refinements
|
|
269
284
|
|
|
270
285
|
By the way, you can implement additional validation within schema.
|
|
@@ -443,8 +458,8 @@ const fileStreamingEndpointsFactory = new EndpointsFactory(
|
|
|
443
458
|
|
|
444
459
|
## File uploads
|
|
445
460
|
|
|
446
|
-
You can switch the `Endpoint` to handle requests with the `multipart/
|
|
447
|
-
Together with a corresponding configuration option, this makes it possible to handle file uploads.
|
|
461
|
+
You can switch the `Endpoint` to handle requests with the `multipart/form-data` content type instead of JSON by using
|
|
462
|
+
`z.upload()` schema. Together with a corresponding configuration option, this makes it possible to handle file uploads.
|
|
448
463
|
Here is a simplified example:
|
|
449
464
|
|
|
450
465
|
```typescript
|
|
@@ -459,9 +474,8 @@ const config = createConfig({
|
|
|
459
474
|
|
|
460
475
|
const fileUploadEndpoint = defaultEndpointsFactory.build({
|
|
461
476
|
method: "post",
|
|
462
|
-
type: "upload", // <- required
|
|
463
477
|
input: z.object({
|
|
464
|
-
avatar: z.upload(),
|
|
478
|
+
avatar: z.upload(), // <--
|
|
465
479
|
}),
|
|
466
480
|
output: z.object({
|
|
467
481
|
/* ... */
|
|
@@ -469,9 +483,6 @@ const fileUploadEndpoint = defaultEndpointsFactory.build({
|
|
|
469
483
|
handler: async ({ input: { avatar } }) => {
|
|
470
484
|
// avatar: {name, mv(), mimetype, data, size, etc}
|
|
471
485
|
// avatar.truncated is true on failure
|
|
472
|
-
return {
|
|
473
|
-
/* ... */
|
|
474
|
-
};
|
|
475
486
|
},
|
|
476
487
|
});
|
|
477
488
|
```
|
|
@@ -589,7 +600,8 @@ You can generate the specification of your API and write it to a `.yaml` file, t
|
|
|
589
600
|
import { OpenAPI } from "express-zod-api";
|
|
590
601
|
|
|
591
602
|
const yamlString = new OpenAPI({
|
|
592
|
-
routing,
|
|
603
|
+
routing, // the same routing and config that you use to start the server
|
|
604
|
+
config,
|
|
593
605
|
version: "1.2.3",
|
|
594
606
|
title: "Example API",
|
|
595
607
|
serverUrl: "https://example.com",
|
package/SECURITY.md
CHANGED
package/dist/common-helpers.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Request } from "express";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
import { CommonConfig, LoggerConfig } from "./config-type";
|
|
3
|
+
import { CommonConfig, InputSources, LoggerConfig } from "./config-type";
|
|
4
4
|
import { MiddlewareDefinition } from "./middleware";
|
|
5
5
|
export declare type FlatObject = Record<string, any>;
|
|
6
6
|
declare type ObjectSchema = z.AnyZodObject;
|
|
@@ -24,6 +24,7 @@ export declare type ReplaceMarkerInShape<S extends z.ZodRawShape, OUT extends IO
|
|
|
24
24
|
};
|
|
25
25
|
export declare function extractObjectSchema(subject: IOSchema): ObjectSchema;
|
|
26
26
|
export declare function combineEndpointAndMiddlewareInputSchemas<IN extends IOSchema, MwIN>(input: IN, middlewares: MiddlewareDefinition<IOSchema, any, any>[]): Merge<IN, MwIN>;
|
|
27
|
+
export declare const defaultInputSources: InputSources;
|
|
27
28
|
export declare function getInitialInput(request: Request, inputAssignment: CommonConfig["inputSources"]): any;
|
|
28
29
|
export declare function isLoggerConfig(logger: any): logger is LoggerConfig;
|
|
29
30
|
export declare function getMessageFromError(error: Error): string;
|
|
@@ -38,6 +39,7 @@ export declare const combinations: <T extends unknown>(a: T[], b: T[]) => {
|
|
|
38
39
|
value: [T, T][];
|
|
39
40
|
};
|
|
40
41
|
export declare function getRoutePathParams(path: string): string[];
|
|
42
|
+
export declare function hasUpload(schema: z.ZodTypeAny): boolean;
|
|
41
43
|
export declare type ErrMessage = Exclude<Parameters<typeof z.ZodString.prototype.email>[0], undefined>;
|
|
42
44
|
export declare const errToObj: (message: ErrMessage | undefined) => {
|
|
43
45
|
message?: string | undefined;
|
package/dist/common-helpers.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.errToObj = exports.getRoutePathParams = exports.combinations = exports.getExamples = exports.getStatusCodeFromError = exports.getMessageFromError = exports.isLoggerConfig = exports.getInitialInput = exports.combineEndpointAndMiddlewareInputSchemas = exports.extractObjectSchema = exports.routePathParamsRegex = exports.markOutput = void 0;
|
|
3
|
+
exports.errToObj = exports.hasUpload = exports.getRoutePathParams = exports.combinations = exports.getExamples = exports.getStatusCodeFromError = exports.getMessageFromError = exports.isLoggerConfig = exports.getInitialInput = exports.defaultInputSources = exports.combineEndpointAndMiddlewareInputSchemas = exports.extractObjectSchema = exports.routePathParamsRegex = exports.markOutput = void 0;
|
|
4
4
|
const http_errors_1 = require("http-errors");
|
|
5
5
|
const zod_1 = require("zod");
|
|
6
6
|
const config_type_1 = require("./config-type");
|
|
7
7
|
const metadata_1 = require("./metadata");
|
|
8
8
|
const mime_1 = require("./mime");
|
|
9
|
+
const upload_schema_1 = require("./upload-schema");
|
|
9
10
|
const markOutput = (output) => output;
|
|
10
11
|
exports.markOutput = markOutput;
|
|
11
12
|
/** @see https://expressjs.com/en/guide/routing.html */
|
|
@@ -45,19 +46,19 @@ function areFilesAvailable(request) {
|
|
|
45
46
|
const isMultipart = contentType.substr(0, mime_1.mimeMultipart.length).toLowerCase() === mime_1.mimeMultipart;
|
|
46
47
|
return "files" in request && isMultipart;
|
|
47
48
|
}
|
|
48
|
-
|
|
49
|
+
exports.defaultInputSources = {
|
|
49
50
|
get: ["query", "params"],
|
|
50
51
|
post: ["body", "params", "files"],
|
|
51
52
|
put: ["body", "params"],
|
|
52
53
|
patch: ["body", "params"],
|
|
53
54
|
delete: ["body", "query", "params"],
|
|
54
55
|
};
|
|
55
|
-
const fallbackInputSource = defaultInputSources.delete;
|
|
56
|
+
const fallbackInputSource = exports.defaultInputSources.delete;
|
|
56
57
|
function getInitialInput(request, inputAssignment) {
|
|
57
58
|
const method = request.method.toLowerCase();
|
|
58
59
|
let props = fallbackInputSource;
|
|
59
|
-
if (method in defaultInputSources) {
|
|
60
|
-
props = defaultInputSources[method];
|
|
60
|
+
if (method in exports.defaultInputSources) {
|
|
61
|
+
props = exports.defaultInputSources[method];
|
|
61
62
|
}
|
|
62
63
|
if (inputAssignment && method in inputAssignment) {
|
|
63
64
|
props = inputAssignment[method] || props;
|
|
@@ -136,6 +137,38 @@ function getRoutePathParams(path) {
|
|
|
136
137
|
return match.map((param) => param.slice(1));
|
|
137
138
|
}
|
|
138
139
|
exports.getRoutePathParams = getRoutePathParams;
|
|
140
|
+
function hasUpload(schema) {
|
|
141
|
+
if (schema instanceof upload_schema_1.ZodUpload) {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
const reduceBool = (arr) => arr.reduce((carry, check) => carry || check, false);
|
|
145
|
+
if (schema instanceof zod_1.z.ZodObject) {
|
|
146
|
+
return reduceBool(Object.values(schema.shape).map(hasUpload));
|
|
147
|
+
}
|
|
148
|
+
if (schema instanceof zod_1.z.ZodUnion) {
|
|
149
|
+
return reduceBool(schema._def.options.map(hasUpload));
|
|
150
|
+
}
|
|
151
|
+
if (schema instanceof zod_1.z.ZodIntersection) {
|
|
152
|
+
return reduceBool([schema._def.left, schema._def.right].map(hasUpload));
|
|
153
|
+
}
|
|
154
|
+
if (schema instanceof zod_1.z.ZodOptional || schema instanceof zod_1.z.ZodNullable) {
|
|
155
|
+
return hasUpload(schema.unwrap());
|
|
156
|
+
}
|
|
157
|
+
if (schema instanceof zod_1.z.ZodEffects || schema instanceof zod_1.z.ZodTransformer) {
|
|
158
|
+
return hasUpload(schema._def.schema);
|
|
159
|
+
}
|
|
160
|
+
if (schema instanceof zod_1.z.ZodRecord) {
|
|
161
|
+
return hasUpload(schema._def.valueType);
|
|
162
|
+
}
|
|
163
|
+
if (schema instanceof zod_1.z.ZodArray) {
|
|
164
|
+
return hasUpload(schema._def.type);
|
|
165
|
+
}
|
|
166
|
+
if (schema instanceof zod_1.z.ZodDefault) {
|
|
167
|
+
return hasUpload(schema._def.innerType);
|
|
168
|
+
}
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
exports.hasUpload = hasUpload;
|
|
139
172
|
// the copy of the private Zod errorUtil.errToObj
|
|
140
173
|
const errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
141
174
|
exports.errToObj = errToObj;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common-helpers.js","sourceRoot":"","sources":["../src/common-helpers.ts"],"names":[],"mappings":";;;AACA,6CAAwC;AACxC,6BAAwB;AACxB,+CAKuB;AACvB,yCAA+C;AAG/C,iCAAuC;
|
|
1
|
+
{"version":3,"file":"common-helpers.js","sourceRoot":"","sources":["../src/common-helpers.ts"],"names":[],"mappings":";;;AACA,6CAAwC;AACxC,6BAAwB;AACxB,+CAKuB;AACvB,yCAA+C;AAG/C,iCAAuC;AACvC,mDAA4C;AAgDrC,MAAM,UAAU,GAAG,CAAC,MAAgB,EAAE,EAAE,CAAC,MAAsB,CAAC;AAA1D,QAAA,UAAU,cAAgD;AAEvE,uDAAuD;AAC1C,QAAA,oBAAoB,GAAG,mBAAmB,CAAC;AASxD,SAAgB,mBAAmB,CAAC,OAAiB;IACnD,IAAI,OAAO,YAAY,OAAC,CAAC,SAAS,EAAE;QAClC,OAAO,OAAO,CAAC;KAChB;IACD,IAAI,YAA0B,CAAC;IAC/B,IAAI,OAAO,YAAY,OAAC,CAAC,QAAQ,EAAE;QACjC,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,CACpD,GAAG,CAAC,OAAO,EAAE,CAAC,KAAK,CAA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O,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;KACzC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AA/BD,8BA+BC;AAQD,iDAAiD;AAC1C,MAAM,QAAQ,GAAG,CAAC,OAA+B,EAAE,EAAE,CAC1D,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,OAAO,IAAI,EAAE,CAAC;AAD/C,QAAA,QAAQ,YACuC"}
|
|
@@ -10,7 +10,6 @@ declare type BuildProps<IN extends IOSchema, OUT extends IOSchema, MwIN, MwOUT,
|
|
|
10
10
|
output: OUT;
|
|
11
11
|
handler: Handler<z.output<Merge<IN, MwIN>>, z.input<OUT>, MwOUT>;
|
|
12
12
|
description?: string;
|
|
13
|
-
type?: "json" | "upload";
|
|
14
13
|
} & MethodsDefinition<M>;
|
|
15
14
|
export declare class EndpointsFactory<MwIN, MwOUT, POS extends ApiResponse, NEG extends ApiResponse> {
|
|
16
15
|
#private;
|
|
@@ -18,7 +17,8 @@ export declare class EndpointsFactory<MwIN, MwOUT, POS extends ApiResponse, NEG
|
|
|
18
17
|
protected middlewares: MiddlewareDefinition<any, any, any>[];
|
|
19
18
|
constructor(resultHandler: ResultHandlerDefinition<POS, NEG>);
|
|
20
19
|
addMiddleware<IN extends IOSchema, OUT extends FlatObject>(definition: MiddlewareDefinition<IN, MwOUT, OUT>): EndpointsFactory<Merge<IN, MwIN>, MwOUT & OUT, POS, NEG>;
|
|
21
|
-
|
|
20
|
+
addOptions<OUT extends FlatObject>(options: OUT): EndpointsFactory<MwIN, MwOUT & OUT, POS, NEG>;
|
|
21
|
+
build<IN extends IOSchema, OUT extends IOSchema, M extends Method>({ input, output, handler, description, ...rest }: BuildProps<IN, OUT, MwIN, MwOUT, M>): Endpoint<IN, OUT, MwIN, MwOUT, M, POS, NEG>;
|
|
22
22
|
}
|
|
23
23
|
export declare const defaultEndpointsFactory: EndpointsFactory<unknown, unknown, ApiResponse<z.ZodObject<{
|
|
24
24
|
status: z.ZodLiteral<"success">;
|
|
@@ -7,7 +7,10 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
7
7
|
var _a, _EndpointsFactory_create;
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.defaultEndpointsFactory = exports.EndpointsFactory = void 0;
|
|
10
|
+
const zod_1 = require("zod");
|
|
10
11
|
const endpoint_1 = require("./endpoint");
|
|
12
|
+
const common_helpers_1 = require("./common-helpers");
|
|
13
|
+
const middleware_1 = require("./middleware");
|
|
11
14
|
const mime_1 = require("./mime");
|
|
12
15
|
const result_handler_1 = require("./result-handler");
|
|
13
16
|
class EndpointsFactory {
|
|
@@ -19,7 +22,13 @@ class EndpointsFactory {
|
|
|
19
22
|
addMiddleware(definition) {
|
|
20
23
|
return __classPrivateFieldGet(EndpointsFactory, _a, "m", _EndpointsFactory_create).call(EndpointsFactory, this.middlewares.concat(definition), this.resultHandler);
|
|
21
24
|
}
|
|
22
|
-
|
|
25
|
+
addOptions(options) {
|
|
26
|
+
return __classPrivateFieldGet(EndpointsFactory, _a, "m", _EndpointsFactory_create).call(EndpointsFactory, this.middlewares.concat((0, middleware_1.createMiddleware)({
|
|
27
|
+
input: zod_1.z.object({}),
|
|
28
|
+
middleware: async () => options,
|
|
29
|
+
})), this.resultHandler);
|
|
30
|
+
}
|
|
31
|
+
build({ input, output, handler, description, ...rest }) {
|
|
23
32
|
return new endpoint_1.Endpoint({
|
|
24
33
|
handler,
|
|
25
34
|
description,
|
|
@@ -27,7 +36,7 @@ class EndpointsFactory {
|
|
|
27
36
|
inputSchema: input,
|
|
28
37
|
outputSchema: output,
|
|
29
38
|
resultHandler: this.resultHandler,
|
|
30
|
-
mimeTypes:
|
|
39
|
+
mimeTypes: (0, common_helpers_1.hasUpload)(input) ? [mime_1.mimeMultipart] : [mime_1.mimeJson],
|
|
31
40
|
...rest,
|
|
32
41
|
});
|
|
33
42
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoints-factory.js","sourceRoot":"","sources":["../src/endpoints-factory.ts"],"names":[],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"endpoints-factory.js","sourceRoot":"","sources":["../src/endpoints-factory.ts"],"names":[],"mappings":";;;;;;;;;AAAA,6BAAwB;AAExB,yCAA+C;AAC/C,qDAA0E;AAE1E,6CAAsE;AACtE,iCAAiD;AACjD,qDAG0B;AAe1B,MAAa,gBAAgB;IAQ3B,YAAsB,aAAgD;QAAhD,kBAAa,GAAb,aAAa,CAAmC;QAF5D,gBAAW,GAA0C,EAAE,CAAC;QAGhE,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACrC,CAAC;IAkBM,aAAa,CAClB,UAAgD;QAEhD,OAAO,uBAAA,gBAAgB,oCAAQ,MAAxB,gBAAgB,EACrB,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,UAAU,CAAC,EACnC,IAAI,CAAC,aAAa,CACnB,CAAC;IACJ,CAAC;IAEM,UAAU,CAAyB,OAAY;QACpD,OAAO,uBAAA,gBAAgB,oCAAQ,MAAxB,gBAAgB,EACrB,IAAI,CAAC,WAAW,CAAC,MAAM,CACrB,IAAA,6BAAgB,EAAC;YACf,KAAK,EAAE,OAAC,CAAC,MAAM,CAAC,EAAE,CAAC;YACnB,UAAU,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO;SAChC,CAAC,CACH,EACD,IAAI,CAAC,aAAa,CACnB,CAAC;IACJ,CAAC;IAEM,KAAK,CAA8D,EACxE,KAAK,EACL,MAAM,EACN,OAAO,EACP,WAAW,EACX,GAAG,IAAI,EAC6B;QACpC,OAAO,IAAI,mBAAQ,CAAoC;YACrD,OAAO;YACP,WAAW;YACX,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,WAAW,EAAE,KAAK;YAClB,YAAY,EAAE,MAAM;YACpB,aAAa,EAAE,IAAI,CAAC,aAAa;YACjC,SAAS,EAAE,IAAA,0BAAS,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,oBAAa,CAAC,CAAC,CAAC,CAAC,CAAC,eAAQ,CAAC;YAC1D,GAAG,IAAI;SACR,CAAC,CAAC;IACL,CAAC;CACF;AAnED,4CAmEC;oFAjDG,WAAkD,EAClD,aAAoD;IAEpD,MAAM,OAAO,GAAG,IAAI,gBAAgB,CAClC,aAAa,CACd,CAAC;IACF,OAAO,CAAC,WAAW,GAAG,WAAW,CAAC;IAClC,OAAO,OAAO,CAAC;AACjB,CAAC;AA2CU,QAAA,uBAAuB,GAAG,IAAI,gBAAgB,CACzD,qCAAoB,CACrB,CAAC"}
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
import { ParameterObject, SchemaObject } from "openapi3-ts";
|
|
1
|
+
import { MediaTypeObject, ParameterObject, SchemaObject } from "openapi3-ts";
|
|
2
2
|
import { RequestBodyObject, ResponseObject } from "openapi3-ts/src/model/OpenApi";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
+
import { IOSchema } from "./common-helpers";
|
|
5
|
+
import { InputSources } from "./config-type";
|
|
4
6
|
import { AbstractEndpoint } from "./endpoint";
|
|
7
|
+
import { ZodFile } from "./file-schema";
|
|
5
8
|
import { Method } from "./method";
|
|
9
|
+
import { ZodUpload } from "./upload-schema";
|
|
10
|
+
declare type MediaExamples = Pick<MediaTypeObject, "examples">;
|
|
6
11
|
declare type DepictHelper<T extends z.ZodType<any>> = (params: {
|
|
7
12
|
schema: T;
|
|
8
13
|
initial?: SchemaObject;
|
|
@@ -13,10 +18,38 @@ interface ReqResDepictHelperCommonProps {
|
|
|
13
18
|
path: string;
|
|
14
19
|
endpoint: AbstractEndpoint;
|
|
15
20
|
}
|
|
16
|
-
export declare
|
|
17
|
-
export declare const
|
|
21
|
+
export declare const reformatParamsInPath: (path: string) => string;
|
|
22
|
+
export declare const depictDefault: DepictHelper<z.ZodDefault<z.ZodTypeAny>>;
|
|
23
|
+
export declare const depictAny: DepictHelper<z.ZodAny>;
|
|
24
|
+
export declare const depictUpload: DepictHelper<ZodUpload>;
|
|
25
|
+
export declare const depictFile: DepictHelper<ZodFile>;
|
|
26
|
+
export declare const depictUnion: DepictHelper<z.ZodUnion<[z.ZodTypeAny, ...z.ZodTypeAny[]]>>;
|
|
27
|
+
export declare const depictIntersection: DepictHelper<z.ZodIntersection<z.ZodTypeAny, z.ZodTypeAny>>;
|
|
28
|
+
export declare const depictOptionalOrNullable: DepictHelper<z.ZodOptional<any> | z.ZodNullable<any>>;
|
|
29
|
+
export declare const depictEnum: DepictHelper<z.ZodEnum<any> | z.ZodNativeEnum<any>>;
|
|
30
|
+
export declare const depictLiteral: DepictHelper<z.ZodLiteral<any>>;
|
|
31
|
+
export declare const depictObject: DepictHelper<z.AnyZodObject>;
|
|
32
|
+
/** @see https://swagger.io/docs/specification/data-models/data-types/ */
|
|
33
|
+
export declare const depictNull: DepictHelper<z.ZodNull>;
|
|
34
|
+
export declare const depictDate: DepictHelper<z.ZodDate>;
|
|
35
|
+
export declare const depictBoolean: DepictHelper<z.ZodBoolean>;
|
|
36
|
+
export declare const depictBigInt: DepictHelper<z.ZodBigInt>;
|
|
37
|
+
export declare const depictRecord: DepictHelper<z.ZodRecord<z.ZodTypeAny>>;
|
|
38
|
+
export declare const depictArray: DepictHelper<z.ZodArray<z.ZodTypeAny>>;
|
|
39
|
+
/** @todo improve it when OpenAPI 3.1.0 will be released */
|
|
40
|
+
export declare const depictTuple: DepictHelper<z.ZodTuple>;
|
|
41
|
+
export declare const depictString: DepictHelper<z.ZodString>;
|
|
42
|
+
export declare const depictNumber: DepictHelper<z.ZodNumber>;
|
|
43
|
+
export declare const depictObjectProperties: ({ schema: { shape }, isResponse, }: Parameters<DepictHelper<z.AnyZodObject>>[0]) => Record<string, SchemaObject>;
|
|
44
|
+
export declare const depictEffect: DepictHelper<z.ZodEffects<z.ZodTypeAny>>;
|
|
45
|
+
export declare const depictIOExamples: <T extends IOSchema>(schema: T, isResponse: boolean, omitProps?: string[]) => MediaExamples;
|
|
46
|
+
export declare const depictIOParamExamples: <T extends IOSchema>(schema: T, isResponse: boolean, param: string) => MediaExamples;
|
|
47
|
+
export declare const depictRequestParams: ({ path, method, endpoint, inputSources, }: ReqResDepictHelperCommonProps & {
|
|
48
|
+
inputSources: InputSources[Method];
|
|
49
|
+
}) => ParameterObject[];
|
|
18
50
|
export declare const depictSchema: DepictHelper<z.ZodTypeAny>;
|
|
19
51
|
export declare const excludeParamsFromDepiction: (depicted: SchemaObject, pathParams: string[]) => SchemaObject;
|
|
52
|
+
export declare const excludeExampleFromDepiction: (depicted: SchemaObject) => SchemaObject;
|
|
20
53
|
export declare const depictResponse: ({ method, path, description, endpoint, isPositive, }: ReqResDepictHelperCommonProps & {
|
|
21
54
|
description: string;
|
|
22
55
|
isPositive: boolean;
|