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.
Files changed (168) hide show
  1. package/README.md +23 -12
  2. package/bin/index.mjs +10 -0
  3. package/dist/client/createRPC.d.ts +13 -3
  4. package/dist/client/createRPC.js +112 -50
  5. package/dist/client/defaultHandler.d.ts +5 -1
  6. package/dist/client/defaultHandler.js +12 -9
  7. package/dist/client/defaultStreamHandler.d.ts +16 -4
  8. package/dist/client/defaultStreamHandler.js +262 -62
  9. package/dist/client/fetcher.d.ts +41 -3
  10. package/dist/client/fetcher.js +125 -60
  11. package/dist/client/progressive.d.ts +15 -0
  12. package/dist/client/progressive.js +56 -0
  13. package/dist/{utils → client}/serializeQuery.d.ts +2 -2
  14. package/dist/{utils → client}/serializeQuery.js +2 -5
  15. package/dist/core/HttpException.d.ts +16 -0
  16. package/dist/core/HttpException.js +26 -0
  17. package/dist/core/JSONLinesResponder.d.ts +42 -0
  18. package/dist/core/JSONLinesResponder.js +94 -0
  19. package/dist/core/controllersToStaticParams.d.ts +13 -0
  20. package/dist/core/controllersToStaticParams.js +32 -0
  21. package/dist/core/createDecorator.d.ts +18 -0
  22. package/dist/core/createDecorator.js +60 -0
  23. package/dist/core/decorators.d.ts +66 -0
  24. package/dist/core/decorators.js +155 -0
  25. package/dist/core/getSchema.d.ts +21 -0
  26. package/dist/core/getSchema.js +31 -0
  27. package/dist/core/initSegment.d.ts +33 -0
  28. package/dist/core/initSegment.js +35 -0
  29. package/dist/core/multitenant.d.ts +33 -0
  30. package/dist/core/multitenant.js +132 -0
  31. package/dist/core/resolveGeneratorConfigValues.d.ts +19 -0
  32. package/dist/core/resolveGeneratorConfigValues.js +59 -0
  33. package/dist/{utils → core}/setHandlerSchema.d.ts +2 -2
  34. package/dist/{utils → core}/setHandlerSchema.js +1 -4
  35. package/dist/core/toDownloadResponse.d.ts +11 -0
  36. package/dist/core/toDownloadResponse.js +25 -0
  37. package/dist/core/vovkApp.d.ts +36 -0
  38. package/dist/core/vovkApp.js +318 -0
  39. package/dist/index.d.ts +25 -59
  40. package/dist/index.js +23 -23
  41. package/dist/internal.d.ts +17 -0
  42. package/dist/internal.js +10 -0
  43. package/dist/openapi/error.d.ts +6 -0
  44. package/dist/openapi/error.js +97 -0
  45. package/dist/openapi/openAPIToVovkSchema/applyComponentsSchemas.d.ts +3 -0
  46. package/dist/openapi/openAPIToVovkSchema/applyComponentsSchemas.js +65 -0
  47. package/dist/openapi/openAPIToVovkSchema/index.d.ts +5 -0
  48. package/dist/openapi/openAPIToVovkSchema/index.js +153 -0
  49. package/dist/openapi/openAPIToVovkSchema/inlineRefs.d.ts +9 -0
  50. package/dist/openapi/openAPIToVovkSchema/inlineRefs.js +99 -0
  51. package/dist/openapi/operation.d.ts +26 -0
  52. package/dist/openapi/operation.js +19 -0
  53. package/dist/openapi/tool.d.ts +6 -0
  54. package/dist/openapi/tool.js +12 -0
  55. package/dist/openapi/vovkSchemaToOpenAPI.d.ts +21 -0
  56. package/dist/openapi/vovkSchemaToOpenAPI.js +250 -0
  57. package/dist/req/bufferBody.d.ts +1 -0
  58. package/dist/req/bufferBody.js +30 -0
  59. package/dist/req/parseBody.d.ts +4 -0
  60. package/dist/req/parseBody.js +49 -0
  61. package/dist/req/parseForm.d.ts +1 -0
  62. package/dist/req/parseForm.js +24 -0
  63. package/dist/{utils → req}/parseQuery.d.ts +1 -2
  64. package/dist/{utils → req}/parseQuery.js +12 -12
  65. package/dist/req/reqMeta.d.ts +2 -0
  66. package/dist/{utils → req}/reqMeta.js +1 -4
  67. package/dist/req/reqQuery.d.ts +2 -0
  68. package/dist/req/reqQuery.js +4 -0
  69. package/dist/req/validateContentType.d.ts +1 -0
  70. package/dist/req/validateContentType.js +32 -0
  71. package/dist/samples/createCodeSamples.d.ts +20 -0
  72. package/dist/samples/createCodeSamples.js +293 -0
  73. package/dist/samples/objectToCode.d.ts +8 -0
  74. package/dist/samples/objectToCode.js +38 -0
  75. package/dist/samples/schemaToCode.d.ts +11 -0
  76. package/dist/samples/schemaToCode.js +264 -0
  77. package/dist/samples/schemaToObject.d.ts +2 -0
  78. package/dist/samples/schemaToObject.js +164 -0
  79. package/dist/samples/schemaToTsType.d.ts +2 -0
  80. package/dist/samples/schemaToTsType.js +114 -0
  81. package/dist/tools/ToModelOutput.d.ts +8 -0
  82. package/dist/tools/ToModelOutput.js +10 -0
  83. package/dist/tools/createTool.d.ts +126 -0
  84. package/dist/tools/createTool.js +6 -0
  85. package/dist/tools/createToolFactory.d.ts +135 -0
  86. package/dist/tools/createToolFactory.js +62 -0
  87. package/dist/tools/deriveTools.d.ts +46 -0
  88. package/dist/tools/deriveTools.js +132 -0
  89. package/dist/tools/toModelOutputDefault.d.ts +7 -0
  90. package/dist/tools/toModelOutputDefault.js +7 -0
  91. package/dist/tools/toModelOutputMCP.d.ts +30 -0
  92. package/dist/tools/toModelOutputMCP.js +54 -0
  93. package/dist/tsconfig.tsbuildinfo +1 -0
  94. package/dist/types/client.d.ts +139 -0
  95. package/dist/types/client.js +1 -0
  96. package/dist/types/config.d.ts +151 -0
  97. package/dist/types/config.js +1 -0
  98. package/dist/types/core.d.ts +115 -0
  99. package/dist/types/core.js +1 -0
  100. package/dist/types/enums.d.ts +75 -0
  101. package/dist/{types.js → types/enums.js} +21 -9
  102. package/dist/types/inference.d.ts +117 -0
  103. package/dist/types/inference.js +1 -0
  104. package/dist/types/json-schema.d.ts +51 -0
  105. package/dist/types/json-schema.js +1 -0
  106. package/dist/types/operation.d.ts +5 -0
  107. package/dist/types/operation.js +1 -0
  108. package/dist/types/package.d.ts +544 -0
  109. package/dist/types/package.js +5 -0
  110. package/dist/types/request.d.ts +48 -0
  111. package/dist/types/request.js +1 -0
  112. package/dist/types/standard-schema.d.ts +117 -0
  113. package/dist/types/standard-schema.js +6 -0
  114. package/dist/types/tools.d.ts +43 -0
  115. package/dist/types/tools.js +1 -0
  116. package/dist/types/utils.d.ts +9 -0
  117. package/dist/types/utils.js +1 -0
  118. package/dist/types/validation.d.ts +48 -0
  119. package/dist/types/validation.js +1 -0
  120. package/dist/utils/camelCase.d.ts +6 -0
  121. package/dist/utils/camelCase.js +34 -0
  122. package/dist/utils/deepExtend.d.ts +54 -0
  123. package/dist/utils/deepExtend.js +127 -0
  124. package/dist/utils/fileNameToDisposition.d.ts +1 -0
  125. package/dist/utils/fileNameToDisposition.js +3 -0
  126. package/dist/utils/shim.d.ts +1 -0
  127. package/dist/utils/shim.js +1 -1
  128. package/dist/utils/toKebabCase.d.ts +1 -0
  129. package/dist/utils/toKebabCase.js +5 -0
  130. package/dist/utils/trimPath.d.ts +1 -0
  131. package/dist/utils/trimPath.js +1 -0
  132. package/dist/utils/upperFirst.d.ts +1 -0
  133. package/dist/utils/upperFirst.js +3 -0
  134. package/dist/validation/createStandardValidation.d.ts +268 -0
  135. package/dist/validation/createStandardValidation.js +45 -0
  136. package/dist/validation/createValidateOnClient.d.ts +14 -0
  137. package/dist/validation/createValidateOnClient.js +23 -0
  138. package/dist/validation/procedure.d.ts +261 -0
  139. package/dist/validation/procedure.js +8 -0
  140. package/dist/validation/withValidationLibrary.d.ts +119 -0
  141. package/dist/validation/withValidationLibrary.js +174 -0
  142. package/package.json +45 -11
  143. package/dist/HttpException.d.ts +0 -7
  144. package/dist/HttpException.js +0 -15
  145. package/dist/StreamJSONResponse.d.ts +0 -14
  146. package/dist/StreamJSONResponse.js +0 -57
  147. package/dist/VovkApp.d.ts +0 -29
  148. package/dist/VovkApp.js +0 -188
  149. package/dist/client/index.d.ts +0 -3
  150. package/dist/client/index.js +0 -7
  151. package/dist/client/types.d.ts +0 -104
  152. package/dist/client/types.js +0 -2
  153. package/dist/createDecorator.d.ts +0 -6
  154. package/dist/createDecorator.js +0 -43
  155. package/dist/createVovkApp.d.ts +0 -62
  156. package/dist/createVovkApp.js +0 -118
  157. package/dist/types.d.ts +0 -220
  158. package/dist/utils/generateStaticAPI.d.ts +0 -4
  159. package/dist/utils/generateStaticAPI.js +0 -18
  160. package/dist/utils/getSchema.d.ts +0 -20
  161. package/dist/utils/getSchema.js +0 -33
  162. package/dist/utils/reqForm.d.ts +0 -2
  163. package/dist/utils/reqForm.js +0 -13
  164. package/dist/utils/reqMeta.d.ts +0 -2
  165. package/dist/utils/reqQuery.d.ts +0 -2
  166. package/dist/utils/reqQuery.js +0 -10
  167. package/dist/utils/withValidation.d.ts +0 -20
  168. package/dist/utils/withValidation.js +0 -72
package/dist/types.d.ts DELETED
@@ -1,220 +0,0 @@
1
- import type { NextRequest } from 'next/server';
2
- import type { OperationObject } from 'openapi3-ts/oas31';
3
- import type { StreamJSONResponse } from './StreamJSONResponse';
4
- import { VovkStreamAsyncIterable } from './client/types';
5
- export type KnownAny = any;
6
- export type StaticClass = Function;
7
- export type VovkHandlerSchema = {
8
- path: string;
9
- httpMethod: string;
10
- validation?: {
11
- query?: KnownAny;
12
- body?: KnownAny;
13
- params?: KnownAny;
14
- output?: KnownAny;
15
- iteration?: KnownAny;
16
- };
17
- openapi?: OperationObject;
18
- custom?: Record<string, KnownAny>;
19
- };
20
- export type VovkControllerSchema = {
21
- controllerName: string;
22
- originalControllerName: string;
23
- prefix?: string;
24
- handlers: Record<string, VovkHandlerSchema>;
25
- };
26
- export type VovkSegmentSchema = {
27
- emitSchema: boolean;
28
- segmentName: string;
29
- controllers: Record<string, VovkControllerSchema>;
30
- };
31
- export type VovkErrorResponse = {
32
- cause?: unknown;
33
- statusCode: HttpStatus;
34
- message: string;
35
- isError: true;
36
- };
37
- export type VovkControllerInternal = {
38
- _controllerName?: VovkControllerSchema['controllerName'];
39
- _prefix?: VovkControllerSchema['prefix'];
40
- _handlers: VovkControllerSchema['handlers'];
41
- _activated?: true;
42
- _onError?: (err: Error, req: VovkRequest) => void | Promise<void>;
43
- };
44
- export type VovkController = StaticClass & VovkControllerInternal & {
45
- [key: string]: unknown;
46
- };
47
- export type DecoratorOptions = {
48
- cors?: boolean;
49
- headers?: Record<string, string>;
50
- };
51
- export type RouteHandler = ((req: VovkRequest, params: Record<string, string>) => Response | Promise<Response> | Iterable<unknown> | AsyncIterable<unknown>) & {
52
- _options?: DecoratorOptions;
53
- };
54
- export interface VovkRequest<BODY extends KnownAny = null, QUERY extends KnownAny = undefined, PARAMS extends KnownAny = undefined> extends Omit<NextRequest, 'json' | 'nextUrl'> {
55
- json: () => Promise<BODY>;
56
- nextUrl: Omit<NextRequest['nextUrl'], 'searchParams'> & {
57
- searchParams: Omit<NextRequest['nextUrl']['searchParams'], 'get' | 'getAll' | 'entries' | 'forEach' | 'keys' | 'values'> & {
58
- get: <KEY extends keyof QUERY>(key: KEY) => QUERY[KEY] extends readonly (infer ITEM)[] ? ITEM : QUERY[KEY];
59
- getAll: <KEY extends keyof QUERY>(key: KEY) => QUERY[KEY] extends KnownAny[] ? QUERY[KEY] : QUERY[KEY][];
60
- entries: () => IterableIterator<[keyof QUERY, QUERY[keyof QUERY]]>;
61
- forEach: (callbackfn: (value: QUERY[keyof QUERY], key: keyof QUERY, searchParams: NextRequest['nextUrl']['searchParams']) => void) => void;
62
- keys: () => IterableIterator<keyof QUERY>;
63
- values: () => IterableIterator<QUERY[keyof QUERY]>;
64
- };
65
- };
66
- vovk: {
67
- body: () => Promise<BODY>;
68
- query: () => QUERY;
69
- meta: <T = Record<KnownAny, KnownAny>>(meta?: T | null) => T;
70
- form: <T = KnownAny>() => Promise<T>;
71
- params: () => PARAMS;
72
- };
73
- }
74
- export type ControllerStaticMethod<REQ extends VovkRequest<KnownAny, KnownAny> = VovkRequest<undefined, Record<string, KnownAny>>, PARAMS extends {
75
- [key: string]: string;
76
- } = KnownAny> = ((req: REQ, params: PARAMS) => unknown) & {
77
- _controller?: VovkController;
78
- };
79
- export type VovkControllerBody<T extends (...args: KnownAny) => KnownAny> = Awaited<ReturnType<Parameters<T>[0]['vovk']['body']>>;
80
- export type VovkControllerQuery<T extends (...args: KnownAny) => KnownAny> = ReturnType<Parameters<T>[0]['vovk']['query']>;
81
- export type VovkControllerParams<T extends (...args: KnownAny) => KnownAny> = Parameters<T>[1] extends object ? Parameters<T>[1] : ReturnType<Parameters<T>[0]['vovk']['params']>;
82
- export type VovkControllerYieldType<T extends (req: VovkRequest<KnownAny, KnownAny>) => KnownAny> = T extends (...args: KnownAny[]) => AsyncGenerator<infer Y, KnownAny, KnownAny> ? Y : T extends (...args: KnownAny[]) => Generator<infer Y, KnownAny, KnownAny> ? Y : T extends (...args: KnownAny[]) => Promise<StreamJSONResponse<infer Y>> | StreamJSONResponse<infer Y> ? Y : never;
83
- export type VovkControllerOutput<T extends ((...args: KnownAny) => KnownAny) & {
84
- __output?: KnownAny;
85
- }> = T['__output'];
86
- export type VovkControllerIteration<T extends ((...args: KnownAny) => KnownAny) & {
87
- __iteration?: KnownAny;
88
- }> = T['__iteration'];
89
- export type VovkBody<T extends (...args: KnownAny[]) => unknown> = Parameters<T>[0]['body'];
90
- export type VovkQuery<T extends (...args: KnownAny[]) => unknown> = Parameters<T>[0]['query'];
91
- export type VovkParams<T extends (...args: KnownAny[]) => unknown> = Parameters<T>[0]['params'];
92
- export type VovkYieldType<T extends (...args: KnownAny[]) => unknown> = T extends (...args: KnownAny[]) => Promise<VovkStreamAsyncIterable<infer Y>> ? Y : never;
93
- export type VovkReturnType<T extends (...args: KnownAny) => unknown> = Awaited<ReturnType<T>>;
94
- export type StreamAbortMessage = {
95
- isError: true;
96
- reason: KnownAny;
97
- };
98
- type LogLevelNames = 'trace' | 'debug' | 'info' | 'warn' | 'error';
99
- export type VovkEnv = {
100
- PORT?: string;
101
- VOVK_CLIENT_OUT_DIR?: string;
102
- VOVK_SCHEMA_OUT_DIR?: string;
103
- VOVK_FETCHER_PATH?: string;
104
- VOVK_VALIDATE_ON_CLIENT_PATH?: string;
105
- VOVK_CREATE_RPC_PATH?: string;
106
- VOVK_MODULES_DIR?: string;
107
- VOVK_ORIGIN?: string;
108
- VOVK_ROOT_ENTRY?: string;
109
- VOVK_API_ENTRY_POINT?: string;
110
- VOVK_ROOT_SEGMENT_MODULES_DIR_NAME?: string;
111
- VOVK_LOG_LEVEL?: LogLevelNames;
112
- VOVK_PRETTIFY_CLIENT?: string;
113
- VOVK_DEV_HTTPS?: string;
114
- __VOVK_START_WATCHER_IN_STANDALONE_MODE__?: 'true';
115
- __VOVK_EXIT__?: 'true' | 'false';
116
- };
117
- export type GenerateFromTemplate = {
118
- templatePath: string;
119
- outDir?: string;
120
- templateName?: string;
121
- fullSchema?: string | boolean;
122
- origin?: string | null;
123
- };
124
- type GenerateFrom = (string | GenerateFromTemplate)[];
125
- export type VovkConfig = {
126
- emitConfig?: boolean | (keyof VovkStrictConfig)[];
127
- clientOutDir?: string;
128
- schemaOutDir?: string;
129
- fetcherImport?: string | string[];
130
- validateOnClientImport?: string | string[] | null;
131
- createRPCImport?: string | string[];
132
- modulesDir?: string;
133
- rootEntry?: string;
134
- origin?: string;
135
- rootSegmentModulesDirName?: string;
136
- logLevel?: LogLevelNames;
137
- prettifyClient?: boolean;
138
- devHttps?: boolean;
139
- generateFrom?: GenerateFrom | ((value: GenerateFrom) => GenerateFrom);
140
- templates?: {
141
- service?: string;
142
- controller?: string;
143
- [key: string]: string | undefined;
144
- };
145
- libs?: Record<string, KnownAny>;
146
- };
147
- export type VovkStrictConfig = Required<Omit<VovkConfig, 'emitConfig' | 'validateOnClientImport' | 'fetcherImport' | 'createRPCImport' | 'generateFrom' | 'libs'>> & {
148
- emitConfig: (keyof VovkStrictConfig)[];
149
- validateOnClientImport: string[] | null;
150
- fetcherImport: string[];
151
- createRPCImport: string[];
152
- generateFrom: GenerateFrom;
153
- libs: Record<string, KnownAny>;
154
- };
155
- export type VovkFullSchema = {
156
- config: Partial<VovkStrictConfig>;
157
- segments: Record<string, VovkSegmentSchema>;
158
- };
159
- export type VovkValidationType = 'body' | 'query' | 'params' | 'output' | 'iteration';
160
- export declare enum HttpMethod {
161
- GET = "GET",
162
- POST = "POST",
163
- PUT = "PUT",
164
- PATCH = "PATCH",
165
- DELETE = "DELETE",
166
- HEAD = "HEAD",
167
- OPTIONS = "OPTIONS"
168
- }
169
- export declare enum HttpStatus {
170
- NULL = 0,
171
- CONTINUE = 100,
172
- SWITCHING_PROTOCOLS = 101,
173
- PROCESSING = 102,
174
- EARLYHINTS = 103,
175
- OK = 200,
176
- CREATED = 201,
177
- ACCEPTED = 202,
178
- NON_AUTHORITATIVE_INFORMATION = 203,
179
- NO_CONTENT = 204,
180
- RESET_CONTENT = 205,
181
- PARTIAL_CONTENT = 206,
182
- AMBIGUOUS = 300,
183
- MOVED_PERMANENTLY = 301,
184
- FOUND = 302,
185
- SEE_OTHER = 303,
186
- NOT_MODIFIED = 304,
187
- TEMPORARY_REDIRECT = 307,
188
- PERMANENT_REDIRECT = 308,
189
- BAD_REQUEST = 400,
190
- UNAUTHORIZED = 401,
191
- PAYMENT_REQUIRED = 402,
192
- FORBIDDEN = 403,
193
- NOT_FOUND = 404,
194
- METHOD_NOT_ALLOWED = 405,
195
- NOT_ACCEPTABLE = 406,
196
- PROXY_AUTHENTICATION_REQUIRED = 407,
197
- REQUEST_TIMEOUT = 408,
198
- CONFLICT = 409,
199
- GONE = 410,
200
- LENGTH_REQUIRED = 411,
201
- PRECONDITION_FAILED = 412,
202
- PAYLOAD_TOO_LARGE = 413,
203
- URI_TOO_LONG = 414,
204
- UNSUPPORTED_MEDIA_TYPE = 415,
205
- REQUESTED_RANGE_NOT_SATISFIABLE = 416,
206
- EXPECTATION_FAILED = 417,
207
- I_AM_A_TEAPOT = 418,
208
- MISDIRECTED = 421,
209
- UNPROCESSABLE_ENTITY = 422,
210
- FAILED_DEPENDENCY = 424,
211
- PRECONDITION_REQUIRED = 428,
212
- TOO_MANY_REQUESTS = 429,
213
- INTERNAL_SERVER_ERROR = 500,
214
- NOT_IMPLEMENTED = 501,
215
- BAD_GATEWAY = 502,
216
- SERVICE_UNAVAILABLE = 503,
217
- GATEWAY_TIMEOUT = 504,
218
- HTTP_VERSION_NOT_SUPPORTED = 505
219
- }
220
- export {};
@@ -1,4 +0,0 @@
1
- import type { StaticClass } from '../types';
2
- export declare function generateStaticAPI(c: Record<string, StaticClass>, slug?: string): {
3
- [x: string]: string[];
4
- }[];
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateStaticAPI = generateStaticAPI;
4
- function generateStaticAPI(c, slug = 'vovk') {
5
- const controllers = c;
6
- return [
7
- { [slug]: ['_schema_'] },
8
- ...Object.values(controllers)
9
- .map((controller) => {
10
- const handlers = controller._handlers;
11
- const splitPrefix = controller._prefix?.split('/') ?? [];
12
- return Object.values(handlers).map((handler) => {
13
- return { [slug]: [...splitPrefix, ...handler.path.split('/')].filter(Boolean) };
14
- });
15
- })
16
- .flat(),
17
- ];
18
- }
@@ -1,20 +0,0 @@
1
- import type { VovkSegmentSchema, VovkController, StaticClass } from '../types';
2
- export declare function getControllerSchema(controller: VovkController, controllerName: string, exposeValidation: boolean): {
3
- controllerName: string;
4
- originalControllerName: string;
5
- prefix: string;
6
- handlers: {
7
- [x: string]: {
8
- path: string;
9
- httpMethod: string;
10
- openapi?: import("openapi3-ts/oas31").OperationObject;
11
- custom?: Record<string, import("../types").KnownAny>;
12
- };
13
- };
14
- };
15
- export default function getSchema(options: {
16
- emitSchema?: boolean;
17
- segmentName?: string;
18
- controllers: Record<string, StaticClass>;
19
- exposeValidation?: boolean;
20
- }): VovkSegmentSchema;
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getControllerSchema = getControllerSchema;
4
- exports.default = getSchema;
5
- function getControllerSchema(controller, controllerName, exposeValidation) {
6
- return {
7
- controllerName,
8
- originalControllerName: controller.name,
9
- prefix: controller._prefix ?? '',
10
- handlers: {
11
- ...(exposeValidation
12
- ? controller._handlers
13
- : Object.fromEntries(
14
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
- Object.entries(controller._handlers ?? {}).map(([key, { validation: _v, ...value }]) => [key, value]))),
16
- },
17
- };
18
- }
19
- function getSchema(options) {
20
- const exposeValidation = options?.exposeValidation ?? true;
21
- const emitSchema = options.emitSchema ?? true;
22
- const schema = {
23
- emitSchema,
24
- segmentName: options.segmentName ?? '',
25
- controllers: {},
26
- };
27
- if (!emitSchema)
28
- return schema;
29
- for (const [controllerName, controller] of Object.entries(options.controllers)) {
30
- schema.controllers[controllerName] = getControllerSchema(controller, controllerName, exposeValidation);
31
- }
32
- return schema;
33
- }
@@ -1,2 +0,0 @@
1
- import type { KnownAny, VovkRequest } from '../types';
2
- export default function reqForm<T = KnownAny>(req: VovkRequest<KnownAny, KnownAny>): Promise<T>;
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = reqForm;
4
- const formMap = new WeakMap();
5
- async function reqForm(req) {
6
- if (formMap.has(req)) {
7
- return formMap.get(req);
8
- }
9
- const body = await req.formData();
10
- const formData = Object.fromEntries(body.entries());
11
- formMap.set(req, formData);
12
- return formData;
13
- }
@@ -1,2 +0,0 @@
1
- import type { KnownAny, VovkRequest } from '../types';
2
- export default function reqMeta<T = Record<KnownAny, KnownAny>>(req: VovkRequest<KnownAny, KnownAny>, meta?: T | null): T;
@@ -1,2 +0,0 @@
1
- import type { KnownAny, VovkRequest } from '../types';
2
- export default function reqQuery<T extends object | undefined>(req: VovkRequest<KnownAny, T>): T;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.default = reqQuery;
7
- const parseQuery_1 = __importDefault(require("./parseQuery"));
8
- function reqQuery(req) {
9
- return (0, parseQuery_1.default)(req.nextUrl.search);
10
- }
@@ -1,20 +0,0 @@
1
- import { VovkHandlerSchema, VovkValidationType, type KnownAny, type VovkRequest } from '../types';
2
- export declare function withValidation<T extends (req: KnownAny, params: KnownAny) => KnownAny, BODY_MODEL, QUERY_MODEL, PARAMS_MODEL, OUTPUT_MODEL, ITERATION_MODEL>({ skipServerSideValidation, skipSchemaEmission, validateEveryIteration, body, query, params, output, iteration, handle, getHandlerSchema, validate, }: {
3
- skipServerSideValidation?: boolean | VovkValidationType[];
4
- skipSchemaEmission?: boolean | VovkValidationType[];
5
- validateEveryIteration?: boolean;
6
- body?: BODY_MODEL;
7
- query?: QUERY_MODEL;
8
- params?: PARAMS_MODEL;
9
- output?: OUTPUT_MODEL;
10
- iteration?: ITERATION_MODEL;
11
- handle: T;
12
- getHandlerSchema?: (options: {
13
- skipSchemaEmissionKeys: VovkValidationType[];
14
- }) => Omit<VovkHandlerSchema, 'httpMethod' | 'path'>;
15
- validate: (data: KnownAny, model: NonNullable<BODY_MODEL | QUERY_MODEL | PARAMS_MODEL | OUTPUT_MODEL | ITERATION_MODEL>, meta: {
16
- type: VovkValidationType;
17
- req: VovkRequest<KnownAny, KnownAny>;
18
- status?: number;
19
- }) => KnownAny;
20
- }): T;
@@ -1,72 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.withValidation = withValidation;
4
- const HttpException_1 = require("../HttpException");
5
- const types_1 = require("../types");
6
- const setHandlerSchema_1 = require("./setHandlerSchema");
7
- const validationTypes = ['body', 'query', 'params', 'output', 'iteration'];
8
- function withValidation({ skipServerSideValidation, skipSchemaEmission, validateEveryIteration, body, query, params, output, iteration, handle, getHandlerSchema, validate, }) {
9
- const skipServerSideValidationKeys = skipServerSideValidation === false
10
- ? []
11
- : skipServerSideValidation === true
12
- ? validationTypes
13
- : (skipServerSideValidation ?? []);
14
- const skipSchemaEmissionKeys = skipSchemaEmission === false ? [] : skipSchemaEmission === true ? validationTypes : (skipSchemaEmission ?? []);
15
- const outputHandler = async (req, handlerParams) => {
16
- const data = await handle(req, handlerParams);
17
- if (output && iteration) {
18
- throw new HttpException_1.HttpException(types_1.HttpStatus.INTERNAL_SERVER_ERROR, "Output and iteration are mutually exclusive. You can't use them together.");
19
- }
20
- if (output && !skipServerSideValidationKeys.includes('output')) {
21
- if (!data) {
22
- throw new HttpException_1.HttpException(types_1.HttpStatus.INTERNAL_SERVER_ERROR, 'Output is required. You probably forgot to return something from your handler.');
23
- }
24
- await validate(data, output, { type: 'output', req });
25
- }
26
- if (iteration && !skipServerSideValidationKeys.includes('iteration')) {
27
- // We assume `data` is an async iterable here; you might want to check that:
28
- if (!data || typeof data[Symbol.asyncIterator] !== 'function') {
29
- throw new HttpException_1.HttpException(types_1.HttpStatus.INTERNAL_SERVER_ERROR, 'Data is not an async iterable but iteration validation is defined.');
30
- }
31
- // Return a brand-new async generator that yields validated items
32
- return (async function* () {
33
- let i = 0;
34
- for await (const item of data) {
35
- if (validateEveryIteration || i === 0) {
36
- await validate(item, iteration, { type: 'iteration', req, status: 200 });
37
- }
38
- i++;
39
- yield item;
40
- }
41
- })();
42
- }
43
- else if (validateEveryIteration) {
44
- throw new HttpException_1.HttpException(types_1.HttpStatus.INTERNAL_SERVER_ERROR, 'validateEveryIteration is set but iteration is not defined.');
45
- }
46
- return data;
47
- };
48
- const resultHandler = async (req, handlerParams) => {
49
- if (body && !skipServerSideValidationKeys.includes('body')) {
50
- const data = await req.json();
51
- const instance = (await validate(data, body, { type: 'body', req })) ?? data;
52
- // redeclare to add ability to call req.json() again
53
- req.json = () => Promise.resolve(data);
54
- req.vovk.body = () => Promise.resolve(instance);
55
- }
56
- if (query && !skipServerSideValidationKeys.includes('query')) {
57
- const data = req.vovk.query();
58
- const instance = (await validate(data, query, { type: 'query', req })) ?? data;
59
- req.vovk.query = () => instance;
60
- }
61
- if (params && !skipServerSideValidationKeys.includes('params')) {
62
- const data = req.vovk.params();
63
- const instance = (await validate(data, params, { type: 'params', req })) ?? data;
64
- req.vovk.params = () => instance;
65
- }
66
- return outputHandler(req, handlerParams);
67
- };
68
- if (getHandlerSchema) {
69
- (0, setHandlerSchema_1.setHandlerSchema)(resultHandler, getHandlerSchema({ skipSchemaEmissionKeys }));
70
- }
71
- return resultHandler;
72
- }