vovk 3.0.0-draft.99 → 3.0.2
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/README.md +23 -12
- package/bin/index.mjs +10 -0
- package/dist/client/createRPC.d.ts +13 -3
- package/dist/client/createRPC.js +112 -50
- package/dist/client/defaultHandler.d.ts +5 -1
- package/dist/client/defaultHandler.js +12 -9
- package/dist/client/defaultStreamHandler.d.ts +16 -4
- package/dist/client/defaultStreamHandler.js +262 -62
- package/dist/client/fetcher.d.ts +41 -3
- package/dist/client/fetcher.js +125 -60
- package/dist/client/progressive.d.ts +15 -0
- package/dist/client/progressive.js +56 -0
- package/dist/{utils → client}/serializeQuery.d.ts +2 -2
- package/dist/{utils → client}/serializeQuery.js +2 -5
- package/dist/core/HttpException.d.ts +16 -0
- package/dist/core/HttpException.js +26 -0
- package/dist/core/JSONLinesResponder.d.ts +42 -0
- package/dist/core/JSONLinesResponder.js +94 -0
- package/dist/core/controllersToStaticParams.d.ts +13 -0
- package/dist/core/controllersToStaticParams.js +32 -0
- package/dist/core/createDecorator.d.ts +18 -0
- package/dist/core/createDecorator.js +60 -0
- package/dist/core/decorators.d.ts +66 -0
- package/dist/core/decorators.js +155 -0
- package/dist/core/getSchema.d.ts +21 -0
- package/dist/core/getSchema.js +31 -0
- package/dist/core/initSegment.d.ts +33 -0
- package/dist/core/initSegment.js +35 -0
- package/dist/core/multitenant.d.ts +33 -0
- package/dist/core/multitenant.js +132 -0
- package/dist/core/resolveGeneratorConfigValues.d.ts +19 -0
- package/dist/core/resolveGeneratorConfigValues.js +59 -0
- package/dist/{utils → core}/setHandlerSchema.d.ts +2 -2
- package/dist/{utils → core}/setHandlerSchema.js +1 -4
- package/dist/core/toDownloadResponse.d.ts +11 -0
- package/dist/core/toDownloadResponse.js +25 -0
- package/dist/core/vovkApp.d.ts +36 -0
- package/dist/core/vovkApp.js +318 -0
- package/dist/index.d.ts +25 -59
- package/dist/index.js +23 -23
- package/dist/internal.d.ts +17 -0
- package/dist/internal.js +10 -0
- package/dist/openapi/error.d.ts +6 -0
- package/dist/openapi/error.js +97 -0
- package/dist/openapi/openAPIToVovkSchema/applyComponentsSchemas.d.ts +3 -0
- package/dist/openapi/openAPIToVovkSchema/applyComponentsSchemas.js +65 -0
- package/dist/openapi/openAPIToVovkSchema/index.d.ts +5 -0
- package/dist/openapi/openAPIToVovkSchema/index.js +153 -0
- package/dist/openapi/openAPIToVovkSchema/inlineRefs.d.ts +9 -0
- package/dist/openapi/openAPIToVovkSchema/inlineRefs.js +99 -0
- package/dist/openapi/operation.d.ts +26 -0
- package/dist/openapi/operation.js +19 -0
- package/dist/openapi/tool.d.ts +6 -0
- package/dist/openapi/tool.js +12 -0
- package/dist/openapi/vovkSchemaToOpenAPI.d.ts +21 -0
- package/dist/openapi/vovkSchemaToOpenAPI.js +250 -0
- package/dist/req/bufferBody.d.ts +1 -0
- package/dist/req/bufferBody.js +30 -0
- package/dist/req/parseBody.d.ts +4 -0
- package/dist/req/parseBody.js +49 -0
- package/dist/req/parseForm.d.ts +1 -0
- package/dist/req/parseForm.js +24 -0
- package/dist/{utils → req}/parseQuery.d.ts +1 -2
- package/dist/{utils → req}/parseQuery.js +12 -12
- package/dist/req/reqMeta.d.ts +2 -0
- package/dist/{utils → req}/reqMeta.js +1 -4
- package/dist/req/reqQuery.d.ts +2 -0
- package/dist/req/reqQuery.js +4 -0
- package/dist/req/validateContentType.d.ts +1 -0
- package/dist/req/validateContentType.js +32 -0
- package/dist/samples/createCodeSamples.d.ts +20 -0
- package/dist/samples/createCodeSamples.js +293 -0
- package/dist/samples/objectToCode.d.ts +8 -0
- package/dist/samples/objectToCode.js +38 -0
- package/dist/samples/schemaToCode.d.ts +11 -0
- package/dist/samples/schemaToCode.js +264 -0
- package/dist/samples/schemaToObject.d.ts +2 -0
- package/dist/samples/schemaToObject.js +164 -0
- package/dist/samples/schemaToTsType.d.ts +2 -0
- package/dist/samples/schemaToTsType.js +114 -0
- package/dist/tools/ToModelOutput.d.ts +8 -0
- package/dist/tools/ToModelOutput.js +10 -0
- package/dist/tools/createTool.d.ts +126 -0
- package/dist/tools/createTool.js +6 -0
- package/dist/tools/createToolFactory.d.ts +135 -0
- package/dist/tools/createToolFactory.js +62 -0
- package/dist/tools/deriveTools.d.ts +46 -0
- package/dist/tools/deriveTools.js +132 -0
- package/dist/tools/toModelOutputDefault.d.ts +7 -0
- package/dist/tools/toModelOutputDefault.js +7 -0
- package/dist/tools/toModelOutputMCP.d.ts +30 -0
- package/dist/tools/toModelOutputMCP.js +54 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/client.d.ts +139 -0
- package/dist/types/client.js +1 -0
- package/dist/types/config.d.ts +151 -0
- package/dist/types/config.js +1 -0
- package/dist/types/core.d.ts +115 -0
- package/dist/types/core.js +1 -0
- package/dist/types/enums.d.ts +75 -0
- package/dist/{types.js → types/enums.js} +21 -9
- package/dist/types/inference.d.ts +117 -0
- package/dist/types/inference.js +1 -0
- package/dist/types/json-schema.d.ts +51 -0
- package/dist/types/json-schema.js +1 -0
- package/dist/types/operation.d.ts +5 -0
- package/dist/types/operation.js +1 -0
- package/dist/types/package.d.ts +544 -0
- package/dist/types/package.js +5 -0
- package/dist/types/request.d.ts +48 -0
- package/dist/types/request.js +1 -0
- package/dist/types/standard-schema.d.ts +117 -0
- package/dist/types/standard-schema.js +6 -0
- package/dist/types/tools.d.ts +43 -0
- package/dist/types/tools.js +1 -0
- package/dist/types/utils.d.ts +9 -0
- package/dist/types/utils.js +1 -0
- package/dist/types/validation.d.ts +48 -0
- package/dist/types/validation.js +1 -0
- package/dist/utils/camelCase.d.ts +6 -0
- package/dist/utils/camelCase.js +34 -0
- package/dist/utils/deepExtend.d.ts +54 -0
- package/dist/utils/deepExtend.js +127 -0
- package/dist/utils/fileNameToDisposition.d.ts +1 -0
- package/dist/utils/fileNameToDisposition.js +3 -0
- package/dist/utils/shim.d.ts +1 -0
- package/dist/utils/shim.js +1 -1
- package/dist/utils/toKebabCase.d.ts +1 -0
- package/dist/utils/toKebabCase.js +5 -0
- package/dist/utils/trimPath.d.ts +1 -0
- package/dist/utils/trimPath.js +1 -0
- package/dist/utils/upperFirst.d.ts +1 -0
- package/dist/utils/upperFirst.js +3 -0
- package/dist/validation/createStandardValidation.d.ts +268 -0
- package/dist/validation/createStandardValidation.js +45 -0
- package/dist/validation/createValidateOnClient.d.ts +14 -0
- package/dist/validation/createValidateOnClient.js +23 -0
- package/dist/validation/procedure.d.ts +261 -0
- package/dist/validation/procedure.js +8 -0
- package/dist/validation/withValidationLibrary.d.ts +119 -0
- package/dist/validation/withValidationLibrary.js +174 -0
- package/package.json +45 -11
- package/dist/HttpException.d.ts +0 -7
- package/dist/HttpException.js +0 -15
- package/dist/StreamJSONResponse.d.ts +0 -14
- package/dist/StreamJSONResponse.js +0 -57
- package/dist/VovkApp.d.ts +0 -29
- package/dist/VovkApp.js +0 -188
- package/dist/client/index.d.ts +0 -3
- package/dist/client/index.js +0 -7
- package/dist/client/types.d.ts +0 -104
- package/dist/client/types.js +0 -2
- package/dist/createDecorator.d.ts +0 -6
- package/dist/createDecorator.js +0 -43
- package/dist/createVovkApp.d.ts +0 -62
- package/dist/createVovkApp.js +0 -118
- package/dist/types.d.ts +0 -220
- package/dist/utils/generateStaticAPI.d.ts +0 -4
- package/dist/utils/generateStaticAPI.js +0 -18
- package/dist/utils/getSchema.d.ts +0 -20
- package/dist/utils/getSchema.js +0 -33
- package/dist/utils/reqForm.d.ts +0 -2
- package/dist/utils/reqForm.js +0 -13
- package/dist/utils/reqMeta.d.ts +0 -2
- package/dist/utils/reqQuery.d.ts +0 -2
- package/dist/utils/reqQuery.js +0 -10
- package/dist/utils/withValidation.d.ts +0 -20
- package/dist/utils/withValidation.js +0 -72
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { HttpException } from './HttpException.js';
|
|
3
|
+
import { JSONLinesResponder, Responder } from './JSONLinesResponder.js';
|
|
4
|
+
import { reqQuery } from '../req/reqQuery.js';
|
|
5
|
+
import { reqMeta } from '../req/reqMeta.js';
|
|
6
|
+
import { HttpMethod, HttpStatus } from '../types/enums.js';
|
|
7
|
+
import { parseBody } from '../req/parseBody.js';
|
|
8
|
+
class VovkApp {
|
|
9
|
+
static getHeadersFromDecoratorOptions(options) {
|
|
10
|
+
if (!options)
|
|
11
|
+
return {};
|
|
12
|
+
const corsHeaders = {
|
|
13
|
+
'access-control-allow-origin': '*',
|
|
14
|
+
'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS, HEAD',
|
|
15
|
+
'access-control-allow-headers': 'content-type, authorization',
|
|
16
|
+
};
|
|
17
|
+
const headers = {
|
|
18
|
+
...(options.cors ? corsHeaders : {}),
|
|
19
|
+
...(options.headers ?? {}),
|
|
20
|
+
};
|
|
21
|
+
return headers;
|
|
22
|
+
}
|
|
23
|
+
routes = {
|
|
24
|
+
GET: new Map(),
|
|
25
|
+
POST: new Map(),
|
|
26
|
+
PUT: new Map(),
|
|
27
|
+
PATCH: new Map(),
|
|
28
|
+
DELETE: new Map(),
|
|
29
|
+
HEAD: new Map(),
|
|
30
|
+
OPTIONS: new Map(),
|
|
31
|
+
};
|
|
32
|
+
GET = async (req, data, segmentName) => this.#callMethod({ httpMethod: HttpMethod.GET, req, params: await data.params, segmentName });
|
|
33
|
+
POST = async (req, data, segmentName) => this.#callMethod({ httpMethod: HttpMethod.POST, req, params: await data.params, segmentName });
|
|
34
|
+
PUT = async (req, data, segmentName) => this.#callMethod({ httpMethod: HttpMethod.PUT, req, params: await data.params, segmentName });
|
|
35
|
+
PATCH = async (req, data, segmentName) => this.#callMethod({ httpMethod: HttpMethod.PATCH, req, params: await data.params, segmentName });
|
|
36
|
+
DELETE = async (req, data, segmentName) => this.#callMethod({ httpMethod: HttpMethod.DELETE, req, params: await data.params, segmentName });
|
|
37
|
+
HEAD = async (req, data, segmentName) => this.#callMethod({ httpMethod: HttpMethod.HEAD, req, params: await data.params, segmentName });
|
|
38
|
+
OPTIONS = async (req, data, segmentName) => this.#callMethod({ httpMethod: HttpMethod.OPTIONS, req, params: await data.params, segmentName });
|
|
39
|
+
respond = async ({ statusCode, responseBody, options, }) => {
|
|
40
|
+
const response = new Response(JSON.stringify(responseBody), {
|
|
41
|
+
status: statusCode,
|
|
42
|
+
headers: {
|
|
43
|
+
'content-type': 'application/json',
|
|
44
|
+
..._a.getHeadersFromDecoratorOptions(options),
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
return response;
|
|
48
|
+
};
|
|
49
|
+
#respondWithError = ({ req, statusCode, message, options, cause, }) => {
|
|
50
|
+
return this.respond({
|
|
51
|
+
req,
|
|
52
|
+
statusCode,
|
|
53
|
+
responseBody: {
|
|
54
|
+
cause,
|
|
55
|
+
statusCode,
|
|
56
|
+
message,
|
|
57
|
+
isError: true,
|
|
58
|
+
},
|
|
59
|
+
options,
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
#routeRegexCache = new Map();
|
|
63
|
+
#routeSegmentsCache = new Map();
|
|
64
|
+
#routeParamPositionsCache = new Map();
|
|
65
|
+
#routeMatchCache = new Map();
|
|
66
|
+
#getHandler = ({ handlers, path, params, }) => {
|
|
67
|
+
let methodParams = {};
|
|
68
|
+
if (Object.keys(params).length === 0) {
|
|
69
|
+
return { handler: handlers[''], methodParams };
|
|
70
|
+
}
|
|
71
|
+
const pathStr = path.join('/');
|
|
72
|
+
// Fast path: Check if this exact path has been matched before
|
|
73
|
+
const cachedMatch = this.#routeMatchCache.get(pathStr);
|
|
74
|
+
if (cachedMatch) {
|
|
75
|
+
return {
|
|
76
|
+
handler: handlers[cachedMatch.route],
|
|
77
|
+
methodParams: cachedMatch.params,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
// Check for direct static route match
|
|
81
|
+
let methodKey = handlers[pathStr] ? pathStr : null;
|
|
82
|
+
if (!methodKey) {
|
|
83
|
+
const methodKeys = [];
|
|
84
|
+
const pathLength = path.length;
|
|
85
|
+
// First pass: group routes by length for quick filtering
|
|
86
|
+
const routesByLength = new Map();
|
|
87
|
+
for (const p of Object.keys(handlers)) {
|
|
88
|
+
let routeSegments = this.#routeSegmentsCache.get(p);
|
|
89
|
+
if (!routeSegments) {
|
|
90
|
+
routeSegments = p.split('/');
|
|
91
|
+
this.#routeSegmentsCache.set(p, routeSegments);
|
|
92
|
+
// Pre-compute parameter positions for routes with parameters
|
|
93
|
+
if (p.includes('{')) {
|
|
94
|
+
const paramPositions = [];
|
|
95
|
+
for (let i = 0; i < routeSegments.length; i++) {
|
|
96
|
+
const segment = routeSegments[i];
|
|
97
|
+
if (segment.includes('{')) {
|
|
98
|
+
const paramMatch = segment.match(/\{(\w+)\}/);
|
|
99
|
+
if (paramMatch) {
|
|
100
|
+
paramPositions.push({ index: i, paramName: paramMatch[1] });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
this.#routeParamPositionsCache.set(p, paramPositions);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const segmentLength = routeSegments.length;
|
|
108
|
+
if (segmentLength !== pathLength)
|
|
109
|
+
continue;
|
|
110
|
+
const lengthRoutes = routesByLength.get(segmentLength) || [];
|
|
111
|
+
lengthRoutes.push(p);
|
|
112
|
+
routesByLength.set(segmentLength, lengthRoutes);
|
|
113
|
+
}
|
|
114
|
+
// Only process routes with matching segment count
|
|
115
|
+
const candidateRoutes = routesByLength.get(pathLength) || [];
|
|
116
|
+
for (const p of candidateRoutes) {
|
|
117
|
+
const routeSegments = this.#routeSegmentsCache.get(p);
|
|
118
|
+
if (!routeSegments)
|
|
119
|
+
continue; // This should never happen, fix TS error
|
|
120
|
+
const params = {};
|
|
121
|
+
// Fast path for routes with parameters
|
|
122
|
+
const paramPositions = this.#routeParamPositionsCache.get(p);
|
|
123
|
+
if (paramPositions) {
|
|
124
|
+
let isMatch = true;
|
|
125
|
+
// First check all non-parameter segments for a quick fail
|
|
126
|
+
for (let i = 0; i < routeSegments.length; i++) {
|
|
127
|
+
const routeSegment = routeSegments[i];
|
|
128
|
+
if (!routeSegment.includes('{') && routeSegment !== path[i]) {
|
|
129
|
+
isMatch = false;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!isMatch)
|
|
134
|
+
continue;
|
|
135
|
+
// Now process parameter segments
|
|
136
|
+
for (const { index, paramName } of paramPositions) {
|
|
137
|
+
const routeSegment = routeSegments[index];
|
|
138
|
+
const pathSegment = path[index];
|
|
139
|
+
let regex = this.#routeRegexCache.get(routeSegment);
|
|
140
|
+
if (!regex) {
|
|
141
|
+
const regexPattern = routeSegment
|
|
142
|
+
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
143
|
+
.replace(/\\{(\w+)\\}/g, '(?<$1>[^/]+)');
|
|
144
|
+
regex = new RegExp(`^${regexPattern}$`);
|
|
145
|
+
this.#routeRegexCache.set(routeSegment, regex);
|
|
146
|
+
}
|
|
147
|
+
const values = pathSegment.match(regex)?.groups;
|
|
148
|
+
if (!values) {
|
|
149
|
+
isMatch = false;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
if (paramName in params) {
|
|
153
|
+
throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, `Duplicate parameter "${paramName}" at ${p}`);
|
|
154
|
+
}
|
|
155
|
+
params[paramName] = values[paramName];
|
|
156
|
+
}
|
|
157
|
+
if (isMatch) {
|
|
158
|
+
methodParams = params;
|
|
159
|
+
methodKeys.push(p);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
// Static route - simple equality comparison for all segments
|
|
164
|
+
let isMatch = true;
|
|
165
|
+
for (let i = 0; i < routeSegments.length; i++) {
|
|
166
|
+
if (routeSegments[i] !== path[i]) {
|
|
167
|
+
isMatch = false;
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (isMatch) {
|
|
172
|
+
methodKeys.push(p);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (methodKeys.length > 1) {
|
|
177
|
+
throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, `Conflicting routes found: ${methodKeys.join(', ')}`);
|
|
178
|
+
}
|
|
179
|
+
[methodKey] = methodKeys;
|
|
180
|
+
// Cache successful matches
|
|
181
|
+
if (methodKey) {
|
|
182
|
+
this.#routeMatchCache.set(pathStr, { route: methodKey, params: methodParams });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (methodKey) {
|
|
186
|
+
return { handler: handlers[methodKey], methodParams };
|
|
187
|
+
}
|
|
188
|
+
return { handler: null, methodParams };
|
|
189
|
+
};
|
|
190
|
+
#allHandlers = {};
|
|
191
|
+
#collectHandlers = (httpMethod, segmentName) => {
|
|
192
|
+
const controllers = this.routes[httpMethod];
|
|
193
|
+
const handlers = {};
|
|
194
|
+
controllers.forEach((staticMethods, controller) => {
|
|
195
|
+
if (segmentName !== controller._segmentName)
|
|
196
|
+
return;
|
|
197
|
+
const prefix = controller._prefix ?? '';
|
|
198
|
+
Object.entries(staticMethods ?? {}).forEach(([path, staticMethod]) => {
|
|
199
|
+
const fullPath = [prefix, path].filter(Boolean).join('/');
|
|
200
|
+
handlers[fullPath] = { staticMethod, controller };
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
return handlers;
|
|
204
|
+
};
|
|
205
|
+
#callMethod = async ({ httpMethod, req: request, params, segmentName, }) => {
|
|
206
|
+
const req = request;
|
|
207
|
+
const path = params[Object.keys(params)[0]] ?? [];
|
|
208
|
+
const handlers = this.#allHandlers[segmentName]?.[httpMethod] ?? this.#collectHandlers(httpMethod, segmentName);
|
|
209
|
+
this.#allHandlers[segmentName] ??= {};
|
|
210
|
+
this.#allHandlers[segmentName][httpMethod] = handlers;
|
|
211
|
+
let headerList;
|
|
212
|
+
try {
|
|
213
|
+
headerList = request.headers;
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
// this is static rendering environment, headers are not available
|
|
217
|
+
headerList = null;
|
|
218
|
+
}
|
|
219
|
+
const xMeta = headerList?.get('x-meta');
|
|
220
|
+
const xMetaHeader = xMeta && JSON.parse(xMeta);
|
|
221
|
+
if (xMetaHeader)
|
|
222
|
+
reqMeta(req, { xMetaHeader });
|
|
223
|
+
const { handler, methodParams } = this.#getHandler({ handlers, path, params });
|
|
224
|
+
if (!handler) {
|
|
225
|
+
return this.#respondWithError({
|
|
226
|
+
req,
|
|
227
|
+
statusCode: HttpStatus.NOT_FOUND,
|
|
228
|
+
message: `Route '${path.join('/')}' is not found for ${httpMethod} method at ${segmentName === '' ? 'the root segment' : `segment '${segmentName}'`}`,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
const { staticMethod, controller } = handler;
|
|
232
|
+
const headersFromDecoratorOptions = _a.getHeadersFromDecoratorOptions(staticMethod._options);
|
|
233
|
+
const { _onSuccess: onSuccess, _onBefore: onBefore } = controller;
|
|
234
|
+
req.vovk = {
|
|
235
|
+
body: () => parseBody(req),
|
|
236
|
+
query: () => reqQuery(req),
|
|
237
|
+
meta: (meta) => reqMeta(req, meta),
|
|
238
|
+
params: () => methodParams,
|
|
239
|
+
};
|
|
240
|
+
try {
|
|
241
|
+
await staticMethod._options?.before?.call(controller, req);
|
|
242
|
+
await onBefore?.(req);
|
|
243
|
+
const result = await staticMethod.call(controller, req, methodParams);
|
|
244
|
+
if (result instanceof Response) {
|
|
245
|
+
await onSuccess?.(result, req);
|
|
246
|
+
// set headers from decorator options
|
|
247
|
+
for (const [key, value] of Object.entries(headersFromDecoratorOptions)) {
|
|
248
|
+
if (!result.headers.has(key)) {
|
|
249
|
+
result.headers.set(key, value);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return result;
|
|
253
|
+
}
|
|
254
|
+
if (result instanceof Responder) {
|
|
255
|
+
await onSuccess?.(result, req);
|
|
256
|
+
// set headers from decorator options
|
|
257
|
+
for (const [key, value] of Object.entries(headersFromDecoratorOptions)) {
|
|
258
|
+
if (!result.response.headers.has(key)) {
|
|
259
|
+
result.response.headers.set(key, value);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return result.response;
|
|
263
|
+
}
|
|
264
|
+
const isIterator = typeof result === 'object' &&
|
|
265
|
+
!!result &&
|
|
266
|
+
!Array.isArray(result) &&
|
|
267
|
+
((Reflect.has(result, Symbol.iterator) &&
|
|
268
|
+
typeof result[Symbol.iterator] === 'function') ||
|
|
269
|
+
(Reflect.has(result, Symbol.asyncIterator) &&
|
|
270
|
+
typeof result[Symbol.asyncIterator] === 'function'));
|
|
271
|
+
if (isIterator) {
|
|
272
|
+
const responder = new JSONLinesResponder(req, ({ headers, readableStream }) => new Response(readableStream, {
|
|
273
|
+
headers: { ...headersFromDecoratorOptions, ...headers },
|
|
274
|
+
}));
|
|
275
|
+
void (async () => {
|
|
276
|
+
try {
|
|
277
|
+
for await (const chunk of result) {
|
|
278
|
+
await responder.send(chunk);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
catch (e) {
|
|
282
|
+
return responder.throw(e);
|
|
283
|
+
}
|
|
284
|
+
return responder.close();
|
|
285
|
+
})();
|
|
286
|
+
await onSuccess?.(responder, req);
|
|
287
|
+
return responder.response;
|
|
288
|
+
}
|
|
289
|
+
const responseBody = result ?? null;
|
|
290
|
+
await onSuccess?.(responseBody, req);
|
|
291
|
+
return this.respond({ req, statusCode: 200, responseBody, options: staticMethod._options });
|
|
292
|
+
}
|
|
293
|
+
catch (e) {
|
|
294
|
+
const err = e;
|
|
295
|
+
try {
|
|
296
|
+
await controller._onError?.(err, req);
|
|
297
|
+
}
|
|
298
|
+
catch (onErrorError) {
|
|
299
|
+
// eslint-disable-next-line no-console
|
|
300
|
+
console.error('An error caught in onError handler:', onErrorError);
|
|
301
|
+
}
|
|
302
|
+
if (err.message !== 'NEXT_REDIRECT' && err.message !== 'NEXT_NOT_FOUND') {
|
|
303
|
+
const statusCode = err.statusCode || HttpStatus.INTERNAL_SERVER_ERROR;
|
|
304
|
+
return this.#respondWithError({
|
|
305
|
+
req,
|
|
306
|
+
statusCode,
|
|
307
|
+
message: err.message,
|
|
308
|
+
options: staticMethod._options,
|
|
309
|
+
cause: err.cause,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
throw e; // if NEXT_REDIRECT or NEXT_NOT_FOUND, rethrow it
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
_a = VovkApp;
|
|
317
|
+
const vovkApp = new VovkApp();
|
|
318
|
+
export { vovkApp };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,59 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export {
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
|
|
27
|
-
auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
|
|
28
|
-
}, options: {
|
|
29
|
-
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
|
|
30
|
-
auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
|
|
31
|
-
}, prefix: (givenPath?: string) => (givenTarget: KnownAny) => any, initVovk: (options: {
|
|
32
|
-
segmentName?: string;
|
|
33
|
-
controllers: Record<string, import("./types").StaticClass>;
|
|
34
|
-
exposeValidation?: boolean;
|
|
35
|
-
emitSchema?: boolean;
|
|
36
|
-
onError?: (err: Error, req: VovkRequest) => void | Promise<void>;
|
|
37
|
-
}) => {
|
|
38
|
-
GET: (req: import("next/server").NextRequest, data: {
|
|
39
|
-
params: Promise<Record<string, string[]>>;
|
|
40
|
-
}) => Promise<Response>;
|
|
41
|
-
POST: (req: import("next/server").NextRequest, data: {
|
|
42
|
-
params: Promise<Record<string, string[]>>;
|
|
43
|
-
}) => Promise<Response>;
|
|
44
|
-
PUT: (req: import("next/server").NextRequest, data: {
|
|
45
|
-
params: Promise<Record<string, string[]>>;
|
|
46
|
-
}) => Promise<Response>;
|
|
47
|
-
PATCH: (req: import("next/server").NextRequest, data: {
|
|
48
|
-
params: Promise<Record<string, string[]>>;
|
|
49
|
-
}) => Promise<Response>;
|
|
50
|
-
DELETE: (req: import("next/server").NextRequest, data: {
|
|
51
|
-
params: Promise<Record<string, string[]>>;
|
|
52
|
-
}) => Promise<Response>;
|
|
53
|
-
HEAD: (req: import("next/server").NextRequest, data: {
|
|
54
|
-
params: Promise<Record<string, string[]>>;
|
|
55
|
-
}) => Promise<Response>;
|
|
56
|
-
OPTIONS: (req: import("next/server").NextRequest, data: {
|
|
57
|
-
params: Promise<Record<string, string[]>>;
|
|
58
|
-
}) => Promise<Response>;
|
|
59
|
-
};
|
|
1
|
+
export { HttpException } from './core/HttpException.js';
|
|
2
|
+
export { createDecorator } from './core/createDecorator.js';
|
|
3
|
+
export { controllersToStaticParams } from './core/controllersToStaticParams.js';
|
|
4
|
+
export { multitenant } from './core/multitenant.js';
|
|
5
|
+
export { JSONLinesResponder } from './core/JSONLinesResponder.js';
|
|
6
|
+
export { toDownloadResponse } from './core/toDownloadResponse.js';
|
|
7
|
+
export { get, post, put, patch, del, head, options, prefix, cloneControllerMetadata } from './core/decorators.js';
|
|
8
|
+
export { progressive } from './client/progressive.js';
|
|
9
|
+
export { fetcher, createFetcher } from './client/fetcher.js';
|
|
10
|
+
export { initSegment } from './core/initSegment.js';
|
|
11
|
+
export { operation } from './openapi/operation.js';
|
|
12
|
+
export { createValidateOnClient } from './validation/createValidateOnClient.js';
|
|
13
|
+
export { procedure } from './validation/procedure.js';
|
|
14
|
+
export { ToModelOutput } from './tools/ToModelOutput.js';
|
|
15
|
+
export { createTool } from './tools/createTool.js';
|
|
16
|
+
export { deriveTools } from './tools/deriveTools.js';
|
|
17
|
+
export { HttpStatus, HttpMethod } from './types/enums.js';
|
|
18
|
+
export type { VovkBody, VovkQuery, VovkParams, VovkReturnType, VovkYieldType, VovkOutput, VovkIteration, } from './types/inference.js';
|
|
19
|
+
export type { VovkRequest } from './types/request.js';
|
|
20
|
+
export type { VovkJSONSchemaBase } from './types/json-schema.js';
|
|
21
|
+
export type { VovkConfig } from './types/config.js';
|
|
22
|
+
export type { VovkSchema } from './types/core.js';
|
|
23
|
+
export type { VovkFetcher } from './types/client.js';
|
|
24
|
+
export type { VovkValidateOnClient } from './types/validation.js';
|
|
25
|
+
export type { VovkTool } from './types/tools.js';
|
package/dist/index.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
// core
|
|
2
|
+
export { HttpException } from './core/HttpException.js';
|
|
3
|
+
export { createDecorator } from './core/createDecorator.js';
|
|
4
|
+
export { controllersToStaticParams } from './core/controllersToStaticParams.js';
|
|
5
|
+
export { multitenant } from './core/multitenant.js';
|
|
6
|
+
export { JSONLinesResponder } from './core/JSONLinesResponder.js';
|
|
7
|
+
export { toDownloadResponse } from './core/toDownloadResponse.js';
|
|
8
|
+
export { get, post, put, patch, del, head, options, prefix, cloneControllerMetadata } from './core/decorators.js';
|
|
9
|
+
// client
|
|
10
|
+
export { progressive } from './client/progressive.js';
|
|
11
|
+
export { fetcher, createFetcher } from './client/fetcher.js';
|
|
12
|
+
export { initSegment } from './core/initSegment.js';
|
|
13
|
+
// openapi
|
|
14
|
+
export { operation } from './openapi/operation.js';
|
|
15
|
+
// validation
|
|
16
|
+
export { createValidateOnClient } from './validation/createValidateOnClient.js';
|
|
17
|
+
export { procedure } from './validation/procedure.js';
|
|
18
|
+
// tools
|
|
19
|
+
export { ToModelOutput } from './tools/ToModelOutput.js';
|
|
20
|
+
export { createTool } from './tools/createTool.js';
|
|
21
|
+
export { deriveTools } from './tools/deriveTools.js';
|
|
22
|
+
// types
|
|
23
|
+
export { HttpStatus, HttpMethod } from './types/enums.js';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { deepExtend } from './utils/deepExtend.js';
|
|
2
|
+
export { resolveGeneratorConfigValues } from './core/resolveGeneratorConfigValues.js';
|
|
3
|
+
export { createCodeSamples } from './samples/createCodeSamples.js';
|
|
4
|
+
export { withValidationLibrary } from './validation/withValidationLibrary.js';
|
|
5
|
+
export { operation } from './openapi/operation.js';
|
|
6
|
+
export { openAPIToVovkSchema } from './openapi/openAPIToVovkSchema/index.js';
|
|
7
|
+
export { vovkSchemaToOpenAPI } from './openapi/vovkSchemaToOpenAPI.js';
|
|
8
|
+
export { readableStreamToAsyncIterable } from './client/defaultStreamHandler.js';
|
|
9
|
+
export { VovkSchemaIdEnum } from './types/enums.js';
|
|
10
|
+
export type { VovkToolDerived, VovkToolNonDerived } from './types/tools.js';
|
|
11
|
+
export type { MCPModelOutput } from './tools/toModelOutputMCP.js';
|
|
12
|
+
export type { VovkErrorResponse, VovkMetaSchema, VovkSegmentSchema, VovkControllerSchema, VovkHandlerSchema, VovkValidationType, } from './types/core.js';
|
|
13
|
+
export type { VovkTypedProcedure } from './types/validation.js';
|
|
14
|
+
export type { VovkOutputConfig, VovkReadmeConfig, VovkSamplesConfig, VovkPackageJson, VovkOpenAPIMixin, VovkOpenAPIMixinNormalized, VovkStrictConfig, VovkBundleConfig, VovkSegmentConfig, } from './types/config.js';
|
|
15
|
+
export type { VovkOperationObject } from './types/operation.js';
|
|
16
|
+
export type { VovkRPCModule, VovkFetcherOptions, VovkStreamAsyncIterable } from './types/client.js';
|
|
17
|
+
export type { IsAny, IsNotAny } from './types/utils.js';
|
package/dist/internal.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// internal exports for other packages and tests
|
|
2
|
+
export { deepExtend } from './utils/deepExtend.js';
|
|
3
|
+
export { resolveGeneratorConfigValues } from './core/resolveGeneratorConfigValues.js';
|
|
4
|
+
export { createCodeSamples } from './samples/createCodeSamples.js';
|
|
5
|
+
export { withValidationLibrary } from './validation/withValidationLibrary.js';
|
|
6
|
+
export { operation } from './openapi/operation.js';
|
|
7
|
+
export { openAPIToVovkSchema } from './openapi/openAPIToVovkSchema/index.js';
|
|
8
|
+
export { vovkSchemaToOpenAPI } from './openapi/vovkSchemaToOpenAPI.js';
|
|
9
|
+
export { readableStreamToAsyncIterable } from './client/defaultStreamHandler.js';
|
|
10
|
+
export { VovkSchemaIdEnum } from './types/enums.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { HttpStatus } from '../types/enums.js';
|
|
2
|
+
export declare const error: (status: HttpStatus, message: string) => (target: import("../types/utils.js").KnownAny, propertyKeyOrContext: string | {
|
|
3
|
+
kind: string;
|
|
4
|
+
name: string | symbol;
|
|
5
|
+
addInitializer: (fn: () => void) => void;
|
|
6
|
+
}) => void;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { HttpStatus } from '../types/enums.js';
|
|
2
|
+
import { createDecorator } from '../core/createDecorator.js';
|
|
3
|
+
const statusDisplayText = {
|
|
4
|
+
[HttpStatus.NULL]: 'Error',
|
|
5
|
+
[HttpStatus.CONTINUE]: 'Continue',
|
|
6
|
+
[HttpStatus.SWITCHING_PROTOCOLS]: 'Switching Protocols',
|
|
7
|
+
[HttpStatus.PROCESSING]: 'Processing',
|
|
8
|
+
[HttpStatus.EARLYHINTS]: 'Early Hints',
|
|
9
|
+
[HttpStatus.OK]: 'OK',
|
|
10
|
+
[HttpStatus.CREATED]: 'Created',
|
|
11
|
+
[HttpStatus.ACCEPTED]: 'Accepted',
|
|
12
|
+
[HttpStatus.NON_AUTHORITATIVE_INFORMATION]: 'Non Authoritative Information',
|
|
13
|
+
[HttpStatus.NO_CONTENT]: 'No Content',
|
|
14
|
+
[HttpStatus.RESET_CONTENT]: 'Reset Content',
|
|
15
|
+
[HttpStatus.PARTIAL_CONTENT]: 'Partial Content',
|
|
16
|
+
[HttpStatus.AMBIGUOUS]: 'Ambiguous',
|
|
17
|
+
[HttpStatus.MOVED_PERMANENTLY]: 'Moved Permanently',
|
|
18
|
+
[HttpStatus.FOUND]: 'Found',
|
|
19
|
+
[HttpStatus.SEE_OTHER]: 'See Other',
|
|
20
|
+
[HttpStatus.NOT_MODIFIED]: 'Not Modified',
|
|
21
|
+
[HttpStatus.TEMPORARY_REDIRECT]: 'Temporary Redirect',
|
|
22
|
+
[HttpStatus.PERMANENT_REDIRECT]: 'Permanent Redirect',
|
|
23
|
+
[HttpStatus.BAD_REQUEST]: 'Bad Request',
|
|
24
|
+
[HttpStatus.UNAUTHORIZED]: 'Unauthorized',
|
|
25
|
+
[HttpStatus.PAYMENT_REQUIRED]: 'Payment Required',
|
|
26
|
+
[HttpStatus.FORBIDDEN]: 'Forbidden',
|
|
27
|
+
[HttpStatus.NOT_FOUND]: 'Not Found',
|
|
28
|
+
[HttpStatus.METHOD_NOT_ALLOWED]: 'Method Not Allowed',
|
|
29
|
+
[HttpStatus.NOT_ACCEPTABLE]: 'Not Acceptable',
|
|
30
|
+
[HttpStatus.PROXY_AUTHENTICATION_REQUIRED]: 'Proxy Authentication Required',
|
|
31
|
+
[HttpStatus.REQUEST_TIMEOUT]: 'Request Timeout',
|
|
32
|
+
[HttpStatus.CONFLICT]: 'Conflict',
|
|
33
|
+
[HttpStatus.GONE]: 'Gone',
|
|
34
|
+
[HttpStatus.LENGTH_REQUIRED]: 'Length Required',
|
|
35
|
+
[HttpStatus.PRECONDITION_FAILED]: 'Precondition Failed',
|
|
36
|
+
[HttpStatus.PAYLOAD_TOO_LARGE]: 'Payload Too Large',
|
|
37
|
+
[HttpStatus.URI_TOO_LONG]: 'URI Too Long',
|
|
38
|
+
[HttpStatus.UNSUPPORTED_MEDIA_TYPE]: 'Unsupported Media Type',
|
|
39
|
+
[HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE]: 'Requested Range Not Satisfiable',
|
|
40
|
+
[HttpStatus.EXPECTATION_FAILED]: 'Expectation Failed',
|
|
41
|
+
[HttpStatus.I_AM_A_TEAPOT]: 'I am a teapot',
|
|
42
|
+
[HttpStatus.MISDIRECTED]: 'Misdirected',
|
|
43
|
+
[HttpStatus.UNPROCESSABLE_ENTITY]: 'Unprocessable Entity',
|
|
44
|
+
[HttpStatus.FAILED_DEPENDENCY]: 'Failed Dependency',
|
|
45
|
+
[HttpStatus.PRECONDITION_REQUIRED]: 'Precondition Required',
|
|
46
|
+
[HttpStatus.TOO_MANY_TRequestS]: 'Too Many Requests',
|
|
47
|
+
[HttpStatus.INTERNAL_SERVER_ERROR]: 'Internal Server Error',
|
|
48
|
+
[HttpStatus.NOT_IMPLEMENTED]: 'Not Implemented',
|
|
49
|
+
[HttpStatus.BAD_GATEWAY]: 'Bad Gateway',
|
|
50
|
+
[HttpStatus.SERVICE_UNAVAILABLE]: 'Service Unavailable',
|
|
51
|
+
[HttpStatus.GATEWAY_TIMEOUT]: 'Gateway Timeout',
|
|
52
|
+
[HttpStatus.HTTP_VERSION_NOT_SUPPORTED]: 'HTTP Version Not Supported',
|
|
53
|
+
};
|
|
54
|
+
export const error = createDecorator(null, (status, message) => {
|
|
55
|
+
return (handlerSchema) => {
|
|
56
|
+
return {
|
|
57
|
+
...handlerSchema,
|
|
58
|
+
operationObject: {
|
|
59
|
+
...handlerSchema?.operationObject,
|
|
60
|
+
responses: {
|
|
61
|
+
...handlerSchema?.operationObject?.responses,
|
|
62
|
+
[status]: {
|
|
63
|
+
description: `${status} ${statusDisplayText[status]}`,
|
|
64
|
+
content: {
|
|
65
|
+
'application/json': {
|
|
66
|
+
schema: {
|
|
67
|
+
allOf: [
|
|
68
|
+
{
|
|
69
|
+
$ref: '#/components/schemas/VovkErrorResponse',
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
type: 'object',
|
|
73
|
+
properties: {
|
|
74
|
+
message: {
|
|
75
|
+
type: 'string',
|
|
76
|
+
enum: [
|
|
77
|
+
message,
|
|
78
|
+
...(handlerSchema?.operationObject?.responses?.[status]?.content?.['application/json']
|
|
79
|
+
?.schema?.allOf?.[1]?.properties?.message?.enum ?? []),
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
statusCode: {
|
|
83
|
+
type: 'integer',
|
|
84
|
+
enum: [status],
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
});
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { ComponentsObject } from 'openapi3-ts/oas31';
|
|
2
|
+
import type { VovkJSONSchemaBase } from '../../types/json-schema.js';
|
|
3
|
+
export declare function applyComponentsSchemas(schema: VovkJSONSchemaBase, components: ComponentsObject['schemas'], mixinName: string): VovkJSONSchemaBase | VovkJSONSchemaBase[];
|