express-endpoints-collection 1.0.23 → 1.0.24
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/dist/EndpointsCollection.d.ts +30 -0
- package/dist/EndpointsCollection.d.ts.map +1 -0
- package/dist/EndpointsCollection.js +159 -0
- package/dist/EndpointsCollection.js.map +1 -0
- package/dist/generator.d.ts +3 -2
- package/dist/generator.d.ts.map +1 -1
- package/dist/generator.js +16 -1
- package/dist/generator.js.map +1 -1
- package/dist/index.d.ts +2 -29
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -156
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { EndpointInfo, EndpointInputSchema, EndpointOutputSchema } from "./types/EndpointInfo";
|
|
2
|
+
import { CustomErrorHandler } from "./types/CustomErrorHandler";
|
|
3
|
+
import { HttpMethod } from "./types/HttpMethod";
|
|
4
|
+
import { EndpointArgs } from "./types/EndpointArgs";
|
|
5
|
+
import { TypedRequestHandler } from "./types/TypedRequestHandler";
|
|
6
|
+
export declare class EndpointsCollection {
|
|
7
|
+
private endpoints;
|
|
8
|
+
private router;
|
|
9
|
+
private readonly collectionPrefix?;
|
|
10
|
+
private readonly customErrorHandler?;
|
|
11
|
+
constructor({ collectionPrefix, customErrorHandler, }?: {
|
|
12
|
+
collectionPrefix?: string;
|
|
13
|
+
customErrorHandler?: CustomErrorHandler;
|
|
14
|
+
});
|
|
15
|
+
private validateInput;
|
|
16
|
+
private coerceAll;
|
|
17
|
+
private coerceOnly;
|
|
18
|
+
callOriginal<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(method: HttpMethod, path: string, { inputSchema, outputSchema, summary, beforeInputValidation, afterInputValidation, beforeResponse, }: EndpointArgs<TInput, TOutput>, handler: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
19
|
+
get<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
20
|
+
post<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handler: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
21
|
+
put<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
22
|
+
delete<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
23
|
+
patch<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
24
|
+
options<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
25
|
+
head<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
26
|
+
trace<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
27
|
+
getEndpoints(): EndpointInfo[];
|
|
28
|
+
getRouter(): import("express-serve-static-core").Router;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=EndpointsCollection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EndpointsCollection.d.ts","sourceRoot":"","sources":["../src/EndpointsCollection.ts"],"names":[],"mappings":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S,oBAAoB,EAEpC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,QAAQ,EAAE,mBAAmB,CAAC,MAAM,EAAE,OAAO,CAAC;IAKzC,YAAY;IAIZ,SAAS;CAGjB"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EndpointsCollection = void 0;
|
|
4
|
+
const express_1 = require("express");
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
class EndpointsCollection {
|
|
7
|
+
constructor({ collectionPrefix, customErrorHandler, } = {}) {
|
|
8
|
+
this.endpoints = [];
|
|
9
|
+
this.router = (0, express_1.Router)();
|
|
10
|
+
this.collectionPrefix = collectionPrefix;
|
|
11
|
+
this.customErrorHandler = customErrorHandler;
|
|
12
|
+
}
|
|
13
|
+
validateInput(schema) {
|
|
14
|
+
return (req, res, next) => {
|
|
15
|
+
try {
|
|
16
|
+
if (schema.query) {
|
|
17
|
+
req.query = this.coerceAll(schema.query, req.query);
|
|
18
|
+
req.query = schema.query.parse(req.query);
|
|
19
|
+
}
|
|
20
|
+
if (schema.body) {
|
|
21
|
+
req.body = this.coerceAll(schema.body, req.body);
|
|
22
|
+
req.body = schema.body.parse(req.body);
|
|
23
|
+
}
|
|
24
|
+
if (schema.params) {
|
|
25
|
+
req.params = this.coerceAll(schema.params, req.params);
|
|
26
|
+
req.params = schema.params.parse(req.params);
|
|
27
|
+
}
|
|
28
|
+
if (schema.headers) {
|
|
29
|
+
req.headers = this.coerceAll(schema.headers, req.headers);
|
|
30
|
+
req.headers = schema.headers.parse(req.headers);
|
|
31
|
+
}
|
|
32
|
+
next();
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (error instanceof zod_1.z.ZodError) {
|
|
36
|
+
if (this.customErrorHandler) {
|
|
37
|
+
return res
|
|
38
|
+
.status(400)
|
|
39
|
+
.json(this.customErrorHandler(error, error.errors));
|
|
40
|
+
}
|
|
41
|
+
return res.status(400).json({
|
|
42
|
+
error: error.errors,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
next(error);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
coerceAll(schema, data) {
|
|
50
|
+
const deepClone = {};
|
|
51
|
+
for (const key in schema.shape) {
|
|
52
|
+
const coerced = this.coerceOnly(schema.shape[key], data[key]);
|
|
53
|
+
deepClone[key] = coerced.success ? coerced.data : data[key];
|
|
54
|
+
}
|
|
55
|
+
return deepClone;
|
|
56
|
+
}
|
|
57
|
+
coerceOnly(schema, value) {
|
|
58
|
+
if (schema instanceof zod_1.z.ZodString) {
|
|
59
|
+
return zod_1.z.coerce.string().safeParse(value);
|
|
60
|
+
}
|
|
61
|
+
else if (schema instanceof zod_1.z.ZodNumber) {
|
|
62
|
+
return zod_1.z.coerce.number().safeParse(value);
|
|
63
|
+
}
|
|
64
|
+
else if (schema instanceof zod_1.z.ZodBoolean) {
|
|
65
|
+
return zod_1.z.coerce.boolean().safeParse(value);
|
|
66
|
+
}
|
|
67
|
+
else if (schema instanceof zod_1.z.ZodDate) {
|
|
68
|
+
return zod_1.z.coerce.date().safeParse(value);
|
|
69
|
+
}
|
|
70
|
+
else if (schema instanceof zod_1.z.ZodBigInt) {
|
|
71
|
+
return zod_1.z.coerce.bigint().safeParse(value);
|
|
72
|
+
}
|
|
73
|
+
else if (schema instanceof zod_1.z.ZodArray) {
|
|
74
|
+
if (Array.isArray(value)) {
|
|
75
|
+
const coercedArray = value.map((item) => this.coerceOnly(schema.element, item));
|
|
76
|
+
return zod_1.z
|
|
77
|
+
.array(zod_1.z.any())
|
|
78
|
+
.safeParse(coercedArray.map((result) => result.success ? result.data : result.error));
|
|
79
|
+
}
|
|
80
|
+
return zod_1.z.array(zod_1.z.any()).safeParse(value);
|
|
81
|
+
}
|
|
82
|
+
else if (schema instanceof zod_1.z.ZodObject) {
|
|
83
|
+
if (typeof value === "object" && value !== null) {
|
|
84
|
+
const coercedObject = {};
|
|
85
|
+
for (const [key, propertySchema] of Object.entries(schema.shape)) {
|
|
86
|
+
const propertyValue = value[key];
|
|
87
|
+
const coercedProperty = this.coerceOnly(propertySchema, propertyValue);
|
|
88
|
+
coercedObject[key] = coercedProperty.success
|
|
89
|
+
? coercedProperty.data
|
|
90
|
+
: propertyValue;
|
|
91
|
+
}
|
|
92
|
+
return zod_1.z.object(schema.shape).safeParse(coercedObject);
|
|
93
|
+
}
|
|
94
|
+
return zod_1.z.object(schema.shape).safeParse(value);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
return schema.safeParse(value);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
callOriginal(method, path, { inputSchema, outputSchema, summary, beforeInputValidation = [], afterInputValidation = [], beforeResponse = [], }, handler) {
|
|
101
|
+
const pathToUse = this.collectionPrefix
|
|
102
|
+
? `${this.collectionPrefix}${path}`
|
|
103
|
+
: path;
|
|
104
|
+
this.endpoints.push({
|
|
105
|
+
path: pathToUse,
|
|
106
|
+
method: method,
|
|
107
|
+
inputSchema,
|
|
108
|
+
outputSchema,
|
|
109
|
+
summary,
|
|
110
|
+
});
|
|
111
|
+
const combinedHandlers = [];
|
|
112
|
+
if (beforeInputValidation) {
|
|
113
|
+
combinedHandlers.push(...beforeInputValidation);
|
|
114
|
+
}
|
|
115
|
+
if (inputSchema) {
|
|
116
|
+
combinedHandlers.push(this.validateInput(inputSchema));
|
|
117
|
+
}
|
|
118
|
+
if (afterInputValidation) {
|
|
119
|
+
combinedHandlers.push(...afterInputValidation);
|
|
120
|
+
}
|
|
121
|
+
combinedHandlers.push(handler);
|
|
122
|
+
if (beforeResponse) {
|
|
123
|
+
combinedHandlers.push(...beforeResponse);
|
|
124
|
+
}
|
|
125
|
+
return this.router[method](pathToUse, ...combinedHandlers);
|
|
126
|
+
}
|
|
127
|
+
get(path, args, handlers) {
|
|
128
|
+
return this.callOriginal("get", path, args, handlers);
|
|
129
|
+
}
|
|
130
|
+
post(path, args, handler) {
|
|
131
|
+
return this.callOriginal("post", path, args, handler);
|
|
132
|
+
}
|
|
133
|
+
put(path, args, handlers) {
|
|
134
|
+
return this.callOriginal("put", path, args, handlers);
|
|
135
|
+
}
|
|
136
|
+
delete(path, args, handlers) {
|
|
137
|
+
return this.callOriginal("delete", path, args, handlers);
|
|
138
|
+
}
|
|
139
|
+
patch(path, args, handlers) {
|
|
140
|
+
return this.callOriginal("patch", path, args, handlers);
|
|
141
|
+
}
|
|
142
|
+
options(path, args, handlers) {
|
|
143
|
+
return this.callOriginal("options", path, args, handlers);
|
|
144
|
+
}
|
|
145
|
+
head(path, args, handlers) {
|
|
146
|
+
return this.callOriginal("head", path, args, handlers);
|
|
147
|
+
}
|
|
148
|
+
trace(path, args, handlers) {
|
|
149
|
+
return this.callOriginal("trace", path, args, handlers);
|
|
150
|
+
}
|
|
151
|
+
getEndpoints() {
|
|
152
|
+
return this.endpoints;
|
|
153
|
+
}
|
|
154
|
+
getRouter() {
|
|
155
|
+
return this.router;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
exports.EndpointsCollection = EndpointsCollection;
|
|
159
|
+
//# sourceMappingURL=EndpointsCollection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EndpointsCollection.js","sourceRoot":"","sources":["../src/EndpointsCollection.ts"],"names":[],"mappings":";;;AAKA,qCAMiB;AAIjB,6BAAsC;AAKtC,MAAa,mBAAmB;IAM9B,YAAmB,EACjB,gBAAgB,EAChB,kBAAkB,MAIhB,EAAE;QAXE,cAAS,GAAmB,EAAE,CAAC;QAC/B,WAAM,GAAG,IAAA,gBAAM,GAAE,CAAC;QAWxB,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;IAC/C,CAAC;IAEO,aAAa,CAAC,MAA2B;QAC/C,OAAO,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AI,CAAC,YAAY,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC1D,CAAC;IAEM,OAAO,CAIZ,IAAY,EACZ,IAAmC,EACnC,QAA8C;QAE9C,OAAO,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC5D,CAAC;IAEM,IAAI,CAIT,IAAY,EACZ,IAAmC,EACnC,QAA8C;QAE9C,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IACzD,CAAC;IAEM,KAAK,CAIV,IAAY,EACZ,IAAmC,EACnC,QAA8C;QAE9C,OAAO,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC1D,CAAC;IAEM,YAAY;QACjB,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAEM,SAAS;QACd,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF;AAzQD,kDAyQC"}
|
package/dist/generator.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { EndpointInfo } from "./types/EndpointInfo";
|
|
2
|
-
export declare function generateOpenAPI({ title, version, endpoints, servers, asJson, }: {
|
|
1
|
+
import { EndpointInfo, EndpointOutputSchema } from "./types/EndpointInfo";
|
|
2
|
+
export declare function generateOpenAPI({ title, version, endpoints, servers, asJson, commonResponses, }: {
|
|
3
3
|
title: string;
|
|
4
4
|
version: string;
|
|
5
5
|
endpoints: EndpointInfo[];
|
|
6
6
|
servers: string[];
|
|
7
7
|
asJson?: boolean;
|
|
8
|
+
commonResponses?: EndpointOutputSchema;
|
|
8
9
|
}): string | import("openapi3-ts/oas30").OpenAPIObject;
|
|
9
10
|
//# sourceMappingURL=generator.d.ts.map
|
package/dist/generator.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAY1E,wBAAgB,eAAe,CAAC,EAC9B,KAAK,EACL,OAAO,EACP,SAAS,EACT,OAAO,EACP,MAAM,EACN,eAAe,GAChB,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,YAAY,EAAE,CAAC;IAC1B,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,eAAe,CAAC,EAAE,oBAAoB,CAAC;CACxC,sDAuEA"}
|
package/dist/generator.js
CHANGED
|
@@ -8,7 +8,7 @@ const zod_to_openapi_1 = require("@asteasolutions/zod-to-openapi");
|
|
|
8
8
|
const zod_1 = require("zod");
|
|
9
9
|
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
10
10
|
(0, zod_to_openapi_1.extendZodWithOpenApi)(zod_1.z);
|
|
11
|
-
function generateOpenAPI({ title, version, endpoints, servers, asJson, }) {
|
|
11
|
+
function generateOpenAPI({ title, version, endpoints, servers, asJson, commonResponses, }) {
|
|
12
12
|
const registry = new zod_to_openapi_1.OpenAPIRegistry();
|
|
13
13
|
endpoints.forEach((endpoint) => {
|
|
14
14
|
const openapiPath = endpoint.path.replace(/:(\w+)/g, "{$1}");
|
|
@@ -26,6 +26,21 @@ function generateOpenAPI({ title, version, endpoints, servers, asJson, }) {
|
|
|
26
26
|
: undefined,
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
|
+
if (commonResponses) {
|
|
30
|
+
for (const outputSchema of commonResponses) {
|
|
31
|
+
responses[outputSchema.status.toString()] = {
|
|
32
|
+
description: outputSchema.description ||
|
|
33
|
+
`Response for status code ${outputSchema.status}`,
|
|
34
|
+
content: outputSchema.body
|
|
35
|
+
? {
|
|
36
|
+
"application/json": {
|
|
37
|
+
schema: outputSchema.body,
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
: undefined,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
29
44
|
registry.registerPath({
|
|
30
45
|
method: endpoint.method,
|
|
31
46
|
path: openapiPath,
|
package/dist/generator.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":";;;;;AAYA,
|
|
1
|
+
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":";;;;;AAYA,0CAqFC;AAhGD,mEAKwC;AACxC,6BAAwB;AACxB,sDAA2B;AAE3B,IAAA,qCAAoB,EAAC,OAAC,CAAC,CAAC;AAExB,SAAgB,eAAe,CAAC,EAC9B,KAAK,EACL,OAAO,EACP,SAAS,EACT,OAAO,EACP,MAAM,EACN,eAAe,GAQhB;IACC,MAAM,QAAQ,GAAG,IAAI,gCAAe,EAAE,CAAC;IAEvC,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;QAC7B,MAAM,WAAW,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QAC7D,MAAM,SAAS,GAAmC,EAAE,CAAC;QAErD,KAAK,MAAM,YAAY,IAAI,QAAQ,CAAC,YAAY,EAAE,CAAC;YACjD,SAAS,CAAC,YAAY,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,GAAG;gBAC1C,WAAW,EACT,YAAY,CAAC,WAAW;oBACxB,4BAA4B,YAAY,CAAC,MAAM,EAAE;gBACnD,OAAO,EAAE,YAAY,CAAC,IAAI;oBACxB,CAAC,CAAC;w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,OAAO,EAAE,OAAO;QAChB,IAAI,EAAE;YACJ,KAAK;YACL,OAAO;SACR;QACD,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;KACzC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,iBAAI,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;AACzD,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,35 +1,8 @@
|
|
|
1
|
-
import { EndpointInfo, EndpointInputSchema, EndpointOutputSchema } from "./types/EndpointInfo";
|
|
2
|
-
import { HttpMethod } from "./types/HttpMethod";
|
|
3
|
-
import { CustomErrorHandler } from "./types/CustomErrorHandler";
|
|
4
|
-
import { TypedRequestHandler } from "./types/TypedRequestHandler";
|
|
5
|
-
import { EndpointArgs } from "./types/EndpointArgs";
|
|
6
1
|
export * from "./types/EndpointInfo";
|
|
7
2
|
export * from "./types/HttpMethod";
|
|
8
3
|
export * from "./types/CustomErrorHandler";
|
|
9
4
|
export * from "./types/EndpointArgs";
|
|
10
5
|
export * from "./types/TypedRequestHandler";
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
private router;
|
|
14
|
-
private readonly collectionPrefix?;
|
|
15
|
-
private readonly customErrorHandler?;
|
|
16
|
-
constructor({ collectionPrefix, customErrorHandler, }?: {
|
|
17
|
-
collectionPrefix?: string;
|
|
18
|
-
customErrorHandler?: CustomErrorHandler;
|
|
19
|
-
});
|
|
20
|
-
private validateInput;
|
|
21
|
-
private coerceAll;
|
|
22
|
-
private coerceOnly;
|
|
23
|
-
callOriginal<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(method: HttpMethod, path: string, { inputSchema, outputSchema, summary, beforeInputValidation, afterInputValidation, beforeResponse, }: EndpointArgs<TInput, TOutput>, handler: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
24
|
-
get<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
25
|
-
post<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handler: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
26
|
-
put<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
27
|
-
delete<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
28
|
-
patch<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
29
|
-
options<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
30
|
-
head<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
31
|
-
trace<TInput extends EndpointInputSchema, TOutput extends EndpointOutputSchema>(path: string, args: EndpointArgs<TInput, TOutput>, handlers: TypedRequestHandler<TInput, TOutput>): import("express-serve-static-core").Router;
|
|
32
|
-
getEndpoints(): EndpointInfo[];
|
|
33
|
-
getRouter(): import("express-serve-static-core").Router;
|
|
34
|
-
}
|
|
6
|
+
export * from "./generator";
|
|
7
|
+
export * from "./EndpointsCollection";
|
|
35
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,aAAa,CAAC;AAC5B,cAAc,uBAAuB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -14,165 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.EndpointsCollection = void 0;
|
|
18
|
-
const express_1 = require("express");
|
|
19
|
-
const zod_1 = require("zod");
|
|
20
17
|
__exportStar(require("./types/EndpointInfo"), exports);
|
|
21
18
|
__exportStar(require("./types/HttpMethod"), exports);
|
|
22
19
|
__exportStar(require("./types/CustomErrorHandler"), exports);
|
|
23
20
|
__exportStar(require("./types/EndpointArgs"), exports);
|
|
24
21
|
__exportStar(require("./types/TypedRequestHandler"), exports);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.endpoints = [];
|
|
28
|
-
this.router = (0, express_1.Router)();
|
|
29
|
-
this.collectionPrefix = collectionPrefix;
|
|
30
|
-
this.customErrorHandler = customErrorHandler;
|
|
31
|
-
}
|
|
32
|
-
validateInput(schema) {
|
|
33
|
-
return (req, res, next) => {
|
|
34
|
-
try {
|
|
35
|
-
if (schema.query) {
|
|
36
|
-
req.query = this.coerceAll(schema.query, req.query);
|
|
37
|
-
req.query = schema.query.parse(req.query);
|
|
38
|
-
}
|
|
39
|
-
if (schema.body) {
|
|
40
|
-
req.body = this.coerceAll(schema.body, req.body);
|
|
41
|
-
req.body = schema.body.parse(req.body);
|
|
42
|
-
}
|
|
43
|
-
if (schema.params) {
|
|
44
|
-
req.params = this.coerceAll(schema.params, req.params);
|
|
45
|
-
req.params = schema.params.parse(req.params);
|
|
46
|
-
}
|
|
47
|
-
if (schema.headers) {
|
|
48
|
-
req.headers = this.coerceAll(schema.headers, req.headers);
|
|
49
|
-
req.headers = schema.headers.parse(req.headers);
|
|
50
|
-
}
|
|
51
|
-
next();
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
if (error instanceof zod_1.z.ZodError) {
|
|
55
|
-
if (this.customErrorHandler) {
|
|
56
|
-
return res
|
|
57
|
-
.status(400)
|
|
58
|
-
.json(this.customErrorHandler(error, error.errors));
|
|
59
|
-
}
|
|
60
|
-
return res.status(400).json({
|
|
61
|
-
error: error.errors,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
next(error);
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
coerceAll(schema, data) {
|
|
69
|
-
const deepClone = {};
|
|
70
|
-
for (const key in schema.shape) {
|
|
71
|
-
const coerced = this.coerceOnly(schema.shape[key], data[key]);
|
|
72
|
-
deepClone[key] = coerced.success ? coerced.data : data[key];
|
|
73
|
-
}
|
|
74
|
-
return deepClone;
|
|
75
|
-
}
|
|
76
|
-
coerceOnly(schema, value) {
|
|
77
|
-
if (schema instanceof zod_1.z.ZodString) {
|
|
78
|
-
return zod_1.z.coerce.string().safeParse(value);
|
|
79
|
-
}
|
|
80
|
-
else if (schema instanceof zod_1.z.ZodNumber) {
|
|
81
|
-
return zod_1.z.coerce.number().safeParse(value);
|
|
82
|
-
}
|
|
83
|
-
else if (schema instanceof zod_1.z.ZodBoolean) {
|
|
84
|
-
return zod_1.z.coerce.boolean().safeParse(value);
|
|
85
|
-
}
|
|
86
|
-
else if (schema instanceof zod_1.z.ZodDate) {
|
|
87
|
-
return zod_1.z.coerce.date().safeParse(value);
|
|
88
|
-
}
|
|
89
|
-
else if (schema instanceof zod_1.z.ZodBigInt) {
|
|
90
|
-
return zod_1.z.coerce.bigint().safeParse(value);
|
|
91
|
-
}
|
|
92
|
-
else if (schema instanceof zod_1.z.ZodArray) {
|
|
93
|
-
if (Array.isArray(value)) {
|
|
94
|
-
const coercedArray = value.map((item) => this.coerceOnly(schema.element, item));
|
|
95
|
-
return zod_1.z
|
|
96
|
-
.array(zod_1.z.any())
|
|
97
|
-
.safeParse(coercedArray.map((result) => result.success ? result.data : result.error));
|
|
98
|
-
}
|
|
99
|
-
return zod_1.z.array(zod_1.z.any()).safeParse(value);
|
|
100
|
-
}
|
|
101
|
-
else if (schema instanceof zod_1.z.ZodObject) {
|
|
102
|
-
if (typeof value === "object" && value !== null) {
|
|
103
|
-
const coercedObject = {};
|
|
104
|
-
for (const [key, propertySchema] of Object.entries(schema.shape)) {
|
|
105
|
-
const propertyValue = value[key];
|
|
106
|
-
const coercedProperty = this.coerceOnly(propertySchema, propertyValue);
|
|
107
|
-
coercedObject[key] = coercedProperty.success
|
|
108
|
-
? coercedProperty.data
|
|
109
|
-
: propertyValue;
|
|
110
|
-
}
|
|
111
|
-
return zod_1.z.object(schema.shape).safeParse(coercedObject);
|
|
112
|
-
}
|
|
113
|
-
return zod_1.z.object(schema.shape).safeParse(value);
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
return schema.safeParse(value);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
callOriginal(method, path, { inputSchema, outputSchema, summary, beforeInputValidation = [], afterInputValidation = [], beforeResponse = [], }, handler) {
|
|
120
|
-
const pathToUse = this.collectionPrefix
|
|
121
|
-
? `${this.collectionPrefix}${path}`
|
|
122
|
-
: path;
|
|
123
|
-
this.endpoints.push({
|
|
124
|
-
path: pathToUse,
|
|
125
|
-
method: method,
|
|
126
|
-
inputSchema,
|
|
127
|
-
outputSchema,
|
|
128
|
-
summary,
|
|
129
|
-
});
|
|
130
|
-
const combinedHandlers = [];
|
|
131
|
-
if (beforeInputValidation) {
|
|
132
|
-
combinedHandlers.push(...beforeInputValidation);
|
|
133
|
-
}
|
|
134
|
-
if (inputSchema) {
|
|
135
|
-
combinedHandlers.push(this.validateInput(inputSchema));
|
|
136
|
-
}
|
|
137
|
-
if (afterInputValidation) {
|
|
138
|
-
combinedHandlers.push(...afterInputValidation);
|
|
139
|
-
}
|
|
140
|
-
combinedHandlers.push(handler);
|
|
141
|
-
if (beforeResponse) {
|
|
142
|
-
combinedHandlers.push(...beforeResponse);
|
|
143
|
-
}
|
|
144
|
-
return this.router[method](pathToUse, ...combinedHandlers);
|
|
145
|
-
}
|
|
146
|
-
get(path, args, handlers) {
|
|
147
|
-
return this.callOriginal("get", path, args, handlers);
|
|
148
|
-
}
|
|
149
|
-
post(path, args, handler) {
|
|
150
|
-
return this.callOriginal("post", path, args, handler);
|
|
151
|
-
}
|
|
152
|
-
put(path, args, handlers) {
|
|
153
|
-
return this.callOriginal("put", path, args, handlers);
|
|
154
|
-
}
|
|
155
|
-
delete(path, args, handlers) {
|
|
156
|
-
return this.callOriginal("delete", path, args, handlers);
|
|
157
|
-
}
|
|
158
|
-
patch(path, args, handlers) {
|
|
159
|
-
return this.callOriginal("patch", path, args, handlers);
|
|
160
|
-
}
|
|
161
|
-
options(path, args, handlers) {
|
|
162
|
-
return this.callOriginal("options", path, args, handlers);
|
|
163
|
-
}
|
|
164
|
-
head(path, args, handlers) {
|
|
165
|
-
return this.callOriginal("head", path, args, handlers);
|
|
166
|
-
}
|
|
167
|
-
trace(path, args, handlers) {
|
|
168
|
-
return this.callOriginal("trace", path, args, handlers);
|
|
169
|
-
}
|
|
170
|
-
getEndpoints() {
|
|
171
|
-
return this.endpoints;
|
|
172
|
-
}
|
|
173
|
-
getRouter() {
|
|
174
|
-
return this.router;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
exports.EndpointsCollection = EndpointsCollection;
|
|
22
|
+
__exportStar(require("./generator"), exports);
|
|
23
|
+
__exportStar(require("./EndpointsCollection"), exports);
|
|
178
24
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAqC;AACrC,qDAAmC;AACnC,6DAA2C;AAC3C,uDAAqC;AACrC,8DAA4C;AAC5C,8CAA4B;AAC5B,wDAAsC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-endpoints-collection",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.24",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"repository": "https://github.com/pilotpirxie/express-endpoints-collection.git",
|
|
6
6
|
"author": "pilotpirxie <10637666+pilotpirxie@users.noreply.github.com>",
|