vovk 3.0.0-draft.8 → 3.0.0-draft.80

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 (78) hide show
  1. package/README.md +8 -95
  2. package/{HttpException.d.ts → dist/HttpException.d.ts} +2 -2
  3. package/{HttpException.js → dist/HttpException.js} +3 -3
  4. package/{StreamResponse.d.ts → dist/StreamJSONResponse.d.ts} +3 -3
  5. package/{StreamResponse.js → dist/StreamJSONResponse.js} +5 -5
  6. package/{Segment.d.ts → dist/VovkApp.d.ts} +11 -10
  7. package/{Segment.js → dist/VovkApp.js} +28 -24
  8. package/dist/client/createRPC.d.ts +4 -0
  9. package/{client/clientizeController.js → dist/client/createRPC.js} +22 -40
  10. package/dist/client/defaultFetcher.d.ts +4 -0
  11. package/{client → dist/client}/defaultFetcher.js +19 -8
  12. package/{client → dist/client}/defaultHandler.d.ts +1 -1
  13. package/dist/client/defaultHandler.js +22 -0
  14. package/dist/client/defaultStreamHandler.d.ts +4 -0
  15. package/{client → dist/client}/defaultStreamHandler.js +5 -5
  16. package/dist/client/index.d.ts +2 -0
  17. package/dist/client/index.js +8 -0
  18. package/dist/client/types.d.ts +100 -0
  19. package/dist/createDecorator.d.ts +4 -0
  20. package/{createDecorator.js → dist/createDecorator.js} +16 -15
  21. package/{createSegment.d.ts → dist/createVovkApp.d.ts} +10 -10
  22. package/{createSegment.js → dist/createVovkApp.js} +30 -30
  23. package/dist/index.d.ts +60 -0
  24. package/dist/index.js +24 -0
  25. package/dist/openapi/fromSchema.d.ts +3 -0
  26. package/dist/openapi/fromSchema.js +26 -0
  27. package/dist/openapi/index.d.ts +1 -0
  28. package/dist/openapi/index.js +5 -0
  29. package/dist/openapi/openapi.d.ts +11 -0
  30. package/dist/openapi/openapi.js +70 -0
  31. package/dist/types.d.ts +148 -0
  32. package/dist/types.js +65 -0
  33. package/dist/utils/generateStaticAPI.d.ts +4 -0
  34. package/{generateStaticAPI.js → dist/utils/generateStaticAPI.js} +3 -3
  35. package/{utils → dist/utils}/getSchema.d.ts +1 -2
  36. package/dist/utils/getSchema.js +29 -0
  37. package/dist/utils/parseQuery.d.ts +25 -0
  38. package/dist/utils/parseQuery.js +156 -0
  39. package/dist/utils/reqForm.d.ts +2 -0
  40. package/dist/utils/reqForm.js +13 -0
  41. package/{utils → dist/utils}/reqMeta.d.ts +1 -2
  42. package/{utils → dist/utils}/reqQuery.d.ts +1 -2
  43. package/dist/utils/reqQuery.js +10 -0
  44. package/dist/utils/serializeQuery.d.ts +13 -0
  45. package/dist/utils/serializeQuery.js +65 -0
  46. package/dist/utils/setHandlerValidation.d.ts +4 -0
  47. package/dist/utils/setHandlerValidation.js +15 -0
  48. package/package.json +8 -2
  49. package/.npmignore +0 -2
  50. package/client/clientizeController.d.ts +0 -4
  51. package/client/defaultFetcher.d.ts +0 -4
  52. package/client/defaultHandler.js +0 -21
  53. package/client/defaultStreamHandler.d.ts +0 -4
  54. package/client/index.d.ts +0 -4
  55. package/client/index.js +0 -5
  56. package/client/types.d.ts +0 -100
  57. package/createDecorator.d.ts +0 -4
  58. package/generateStaticAPI.d.ts +0 -4
  59. package/index.d.ts +0 -60
  60. package/index.js +0 -20
  61. package/types.d.ts +0 -186
  62. package/types.js +0 -65
  63. package/utils/getSchema.js +0 -38
  64. package/utils/reqQuery.js +0 -25
  65. package/utils/setClientValidatorsForHandler.d.ts +0 -5
  66. package/utils/setClientValidatorsForHandler.js +0 -28
  67. package/worker/index.d.ts +0 -3
  68. package/worker/index.js +0 -7
  69. package/worker/promisifyWorker.d.ts +0 -2
  70. package/worker/promisifyWorker.js +0 -143
  71. package/worker/types.d.ts +0 -31
  72. package/worker/types.js +0 -2
  73. package/worker/worker.d.ts +0 -1
  74. package/worker/worker.js +0 -44
  75. /package/{client → dist/client}/types.js +0 -0
  76. /package/{utils → dist/utils}/reqMeta.js +0 -0
  77. /package/{utils → dist/utils}/shim.d.ts +0 -0
  78. /package/{utils → dist/utils}/shim.js +0 -0
@@ -0,0 +1,100 @@
1
+ import type { KnownAny, HttpMethod, ControllerStaticMethod, VovkControllerBody, VovkControllerQuery, VovkControllerParams, VovkHandlerSchema, VovkControllerSchema } from '../types';
2
+ import type { StreamJSONResponse } from '../StreamJSONResponse';
3
+ import type { NextResponse } from 'next/server';
4
+ export type StaticMethodInput<T extends ControllerStaticMethod> = (VovkControllerBody<T> extends undefined | void ? {
5
+ body?: undefined;
6
+ } : VovkControllerBody<T> extends null ? {
7
+ body?: null;
8
+ } : {
9
+ body: VovkControllerBody<T>;
10
+ }) & (VovkControllerQuery<T> extends undefined | void ? {
11
+ query?: undefined;
12
+ } : {
13
+ query: VovkControllerQuery<T>;
14
+ }) & (VovkControllerParams<T> extends undefined | void ? {
15
+ params?: undefined;
16
+ } : {
17
+ params: VovkControllerParams<T>;
18
+ });
19
+ type ToPromise<T> = T extends PromiseLike<unknown> ? T : Promise<T>;
20
+ export type VovkStreamAsyncIterable<T> = {
21
+ status: number;
22
+ [Symbol.dispose](): Promise<void> | void;
23
+ [Symbol.asyncDispose](): Promise<void> | void;
24
+ [Symbol.asyncIterator](): AsyncIterator<T>;
25
+ cancel: () => Promise<void> | void;
26
+ };
27
+ type StaticMethodReturn<T extends ControllerStaticMethod> = ReturnType<T> extends NextResponse<infer U> | Promise<NextResponse<infer U>> ? U : ReturnType<T> extends Response | Promise<Response> ? Awaited<ReturnType<T>> : ReturnType<T>;
28
+ type StaticMethodReturnPromise<T extends ControllerStaticMethod> = ToPromise<StaticMethodReturn<T>>;
29
+ type ClientMethod<T extends (...args: KnownAny[]) => void | object | StreamJSONResponse<STREAM> | Promise<StreamJSONResponse<STREAM>>, OPTS extends Record<string, KnownAny>, STREAM extends KnownAny = unknown> = (<R>(options: (StaticMethodInput<T> extends {
30
+ body?: undefined | null;
31
+ query?: undefined;
32
+ params?: undefined;
33
+ } ? unknown : Parameters<T>[0] extends void ? StaticMethodInput<T>['params'] extends object ? {
34
+ params: StaticMethodInput<T>['params'];
35
+ } : unknown : StaticMethodInput<T>) & (Partial<OPTS & {
36
+ transform: (staticMethodReturn: Awaited<StaticMethodReturn<T>>) => R;
37
+ }> | void)) => ReturnType<T> extends Promise<StreamJSONResponse<infer U>> | StreamJSONResponse<infer U> | Iterator<infer U> | AsyncIterator<infer U> ? Promise<VovkStreamAsyncIterable<U>> : R extends object ? Promise<R> : StaticMethodReturnPromise<T>) & {
38
+ schema: VovkHandlerSchema;
39
+ controllerSchema: VovkControllerSchema;
40
+ };
41
+ type OmitNever<T> = {
42
+ [K in keyof T as T[K] extends never ? never : K]: T[K];
43
+ };
44
+ type VovkClientWithNever<T, OPTS extends {
45
+ [key: string]: KnownAny;
46
+ }> = {
47
+ [K in keyof T]: T[K] extends (...args: KnownAny) => KnownAny ? ClientMethod<T[K], OPTS> : never;
48
+ };
49
+ export type VovkClient<T, OPTS extends {
50
+ [key: string]: KnownAny;
51
+ }> = OmitNever<VovkClientWithNever<T, OPTS>>;
52
+ export type VovkClientFetcher<OPTS extends Record<string, KnownAny> = Record<string, never>, T = KnownAny> = (options: {
53
+ name: keyof T;
54
+ httpMethod: HttpMethod;
55
+ getEndpoint: (data: {
56
+ apiRoot: string;
57
+ params: {
58
+ [key: string]: string;
59
+ };
60
+ query: {
61
+ [key: string]: string;
62
+ };
63
+ }) => string;
64
+ validate: (input: {
65
+ body?: unknown;
66
+ query?: unknown;
67
+ endpoint: string;
68
+ }) => void | Promise<void>;
69
+ defaultStreamHandler: (response: Response) => Promise<VovkStreamAsyncIterable<unknown>>;
70
+ defaultHandler: (response: Response) => Promise<unknown>;
71
+ }, input: {
72
+ body: unknown;
73
+ query: {
74
+ [key: string]: string;
75
+ };
76
+ params: {
77
+ [key: string]: string;
78
+ };
79
+ } & OPTS) => KnownAny;
80
+ export interface VovkDefaultFetcherOptions extends Omit<RequestInit, 'body' | 'method'> {
81
+ reactNative?: {
82
+ textStreaming: boolean;
83
+ };
84
+ apiRoot?: string;
85
+ segmentName?: string;
86
+ disableClientValidation?: boolean;
87
+ validateOnClient?: VovkValidateOnClient;
88
+ fetcher?: VovkClientFetcher;
89
+ }
90
+ export type VovkValidateOnClient = (input: {
91
+ body?: unknown;
92
+ query?: unknown;
93
+ endpoint: string;
94
+ }, validation: Exclude<VovkHandlerSchema['validation'], undefined>) => void | Promise<void>;
95
+ export type VovkClientOptions<OPTS extends Record<string, KnownAny> = Record<string, never>> = {
96
+ fetcher?: VovkClientFetcher<OPTS>;
97
+ validateOnClient?: VovkValidateOnClient;
98
+ defaultOptions?: Partial<OPTS>;
99
+ };
100
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { VovkHandlerSchema, KnownAny, VovkController, VovkRequest } from './types';
2
+ type Next = () => Promise<unknown>;
3
+ export declare function createDecorator<ARGS extends unknown[], REQUEST = VovkRequest>(handler: null | ((this: VovkController, req: REQUEST, next: Next, ...args: ARGS) => unknown), initHandler?: (this: VovkController, ...args: ARGS) => Omit<VovkHandlerSchema, 'path' | 'httpMethod'> | ((handlerSchema: VovkHandlerSchema | null) => Omit<Partial<VovkHandlerSchema>, 'path' | 'httpMethod'>) | null | undefined): (...args: ARGS) => (target: KnownAny, propertyKey: string) => void;
4
+ export {};
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._createDecorator = _createDecorator;
4
- function _createDecorator(handler, initHandler) {
3
+ exports.createDecorator = createDecorator;
4
+ function createDecorator(handler, initHandler) {
5
5
  return function decoratorCreator(...args) {
6
6
  return function decorator(target, propertyKey) {
7
7
  const controller = target;
@@ -10,6 +10,17 @@ function _createDecorator(handler, initHandler) {
10
10
  throw new Error(`Unable to decorate: ${propertyKey} is not a function`);
11
11
  }
12
12
  const sourceMethod = originalMethod._sourceMethod ?? originalMethod;
13
+ const method = function method(req, params) {
14
+ const next = async () => {
15
+ return (await originalMethod.call(controller, req, params));
16
+ };
17
+ return handler ? handler.call(controller, req, next, ...args) : next();
18
+ };
19
+ controller[propertyKey] = method;
20
+ method._controller = controller;
21
+ method._sourceMethod = sourceMethod;
22
+ // TODO define internal method type
23
+ originalMethod._controller = controller;
13
24
  const handlerSchema = controller._handlers?.[propertyKey] ?? null;
14
25
  const initResultReturn = initHandler?.call(controller, ...args);
15
26
  const initResult = typeof initResultReturn === 'function' ? initResultReturn(handlerSchema) : initResultReturn;
@@ -18,21 +29,11 @@ function _createDecorator(handler, initHandler) {
18
29
  [propertyKey]: {
19
30
  ...handlerSchema,
20
31
  // avoid override of path and httpMethod
21
- ...(initResult?.clientValidators ? { clientValidators: initResult.clientValidators } : {}),
22
- ...(initResult?.customSchema ? { customSchema: initResult.customSchema } : {}),
32
+ ...(initResult?.validation ? { validation: initResult.validation } : {}),
33
+ ...(initResult?.openapi ? { openapi: initResult.openapi } : {}),
34
+ ...(initResult?.custom ? { custom: initResult.custom } : {}),
23
35
  },
24
36
  };
25
- const method = function method(req, params) {
26
- const next = async () => {
27
- return (await originalMethod.call(controller, req, params));
28
- };
29
- return handler ? handler.call(controller, req, next, ...args) : next();
30
- };
31
- method._controller = controller;
32
- // TODO define internal method type
33
- originalMethod._controller = controller;
34
- controller[propertyKey] = method;
35
- method._sourceMethod = sourceMethod;
36
37
  };
37
38
  };
38
39
  }
@@ -1,5 +1,6 @@
1
- import { type _KnownAny as KnownAny, type _DecoratorOptions as DecoratorOptions, type _VovkRequest as VovkRequest, type _StaticClass as StaticClass } from './types';
2
- export declare function _createSegment(): {
1
+ import type { NextRequest } from 'next/server';
2
+ import { type KnownAny, type DecoratorOptions, type VovkRequest, type StaticClass } from './types';
3
+ export declare function createVovkApp(): {
3
4
  get: {
4
5
  (givenPath?: string | undefined, options?: DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
5
6
  auto: (options?: DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
@@ -32,30 +33,29 @@ export declare function _createSegment(): {
32
33
  initVovk: (options: {
33
34
  segmentName?: string;
34
35
  controllers: Record<string, StaticClass>;
35
- workers?: Record<string, StaticClass>;
36
36
  exposeValidation?: boolean;
37
37
  emitSchema?: boolean;
38
38
  onError?: (err: Error, req: VovkRequest) => void | Promise<void>;
39
39
  }) => {
40
- GET: (req: VovkRequest, data: {
40
+ GET: (req: NextRequest, data: {
41
41
  params: Promise<Record<string, string[]>>;
42
42
  }) => Promise<Response>;
43
- POST: (req: VovkRequest, data: {
43
+ POST: (req: NextRequest, data: {
44
44
  params: Promise<Record<string, string[]>>;
45
45
  }) => Promise<Response>;
46
- PUT: (req: VovkRequest, data: {
46
+ PUT: (req: NextRequest, data: {
47
47
  params: Promise<Record<string, string[]>>;
48
48
  }) => Promise<Response>;
49
- PATCH: (req: VovkRequest, data: {
49
+ PATCH: (req: NextRequest, data: {
50
50
  params: Promise<Record<string, string[]>>;
51
51
  }) => Promise<Response>;
52
- DELETE: (req: VovkRequest, data: {
52
+ DELETE: (req: NextRequest, data: {
53
53
  params: Promise<Record<string, string[]>>;
54
54
  }) => Promise<Response>;
55
- HEAD: (req: VovkRequest, data: {
55
+ HEAD: (req: NextRequest, data: {
56
56
  params: Promise<Record<string, string[]>>;
57
57
  }) => Promise<Response>;
58
- OPTIONS: (req: VovkRequest, data: {
58
+ OPTIONS: (req: NextRequest, data: {
59
59
  params: Promise<Record<string, string[]>>;
60
60
  }) => Promise<Response>;
61
61
  };
@@ -3,8 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports._createSegment = _createSegment;
7
- const Segment_1 = require("./Segment");
6
+ exports.createVovkApp = createVovkApp;
7
+ const VovkApp_1 = require("./VovkApp");
8
8
  const types_1 = require("./types");
9
9
  const getSchema_1 = __importDefault(require("./utils/getSchema"));
10
10
  const trimPath = (path) => path.trim().replace(/^\/|\/$/g, '');
@@ -13,9 +13,9 @@ const toKebabCase = (str) => str
13
13
  .replace(/([A-Z])/g, '-$1')
14
14
  .toLowerCase()
15
15
  .replace(/^-/, '');
16
- function _createSegment() {
17
- const segment = new Segment_1._Segment();
18
- const getDecoratorCreator = (httpMethod) => {
16
+ function createVovkApp() {
17
+ const vovkApp = new VovkApp_1.VovkApp();
18
+ const createHTTPDecorator = (httpMethod) => {
19
19
  const assignSchema = (controller, propertyKey, path, options) => {
20
20
  if (typeof window !== 'undefined') {
21
21
  throw new Error('Decorators are intended for server-side use only. You have probably imported a controller on the client-side.');
@@ -26,19 +26,21 @@ function _createSegment() {
26
26
  decoratorName = 'del';
27
27
  throw new Error(`Decorator must be used on a static class method. Check the controller method named "${propertyKey}" used with @${decoratorName}.`);
28
28
  }
29
- const methods = segment._routes[httpMethod].get(controller) ?? {};
30
- segment._routes[httpMethod].set(controller, methods);
29
+ const methods = vovkApp.routes[httpMethod].get(controller) ?? {};
30
+ vovkApp.routes[httpMethod].set(controller, methods);
31
+ const originalMethod = controller[propertyKey];
32
+ originalMethod._controller = controller;
33
+ originalMethod._sourceMethod = originalMethod._sourceMethod ?? originalMethod;
34
+ const validation = originalMethod._sourceMethod._getValidation?.(controller);
31
35
  controller._handlers = {
32
36
  ...controller._handlers,
33
37
  [propertyKey]: {
38
+ ...(validation ? { validation } : {}),
34
39
  ...(controller._handlers ?? {})[propertyKey],
35
40
  path,
36
41
  httpMethod,
37
42
  },
38
43
  };
39
- const originalMethod = controller[propertyKey];
40
- originalMethod._controller = controller;
41
- originalMethod._sourceMethod = originalMethod._sourceMethod ?? originalMethod;
42
44
  methods[path] = controller[propertyKey];
43
45
  methods[path]._options = options;
44
46
  };
@@ -53,8 +55,8 @@ function _createSegment() {
53
55
  const auto = (options) => {
54
56
  function decorator(givenTarget, propertyKey) {
55
57
  const controller = givenTarget;
56
- const methods = segment._routes[httpMethod].get(controller) ?? {};
57
- segment._routes[httpMethod].set(controller, methods);
58
+ const methods = vovkApp.routes[httpMethod].get(controller) ?? {};
59
+ vovkApp.routes[httpMethod].set(controller, methods);
58
60
  controller._handlers = {
59
61
  ...controller._handlers,
60
62
  [propertyKey]: {
@@ -87,31 +89,29 @@ function _createSegment() {
87
89
  async function GET_DEV(req, data) {
88
90
  const params = await data.params;
89
91
  if (params[Object.keys(params)[0]]?.[0] === '_schema_') {
90
- // Wait for schema to be set (it can be set after decorators are called with another setTimeout)
91
- await new Promise((resolve) => setTimeout(resolve, 10));
92
92
  const schema = (0, getSchema_1.default)(options);
93
- return segment.respond(200, { schema });
93
+ return vovkApp.respond(200, { schema });
94
94
  }
95
- return segment.GET(req, data);
95
+ return vovkApp.GET(req, data);
96
96
  }
97
97
  return {
98
- GET: process.env.NODE_ENV === 'development' ? GET_DEV : segment.GET,
99
- POST: segment.POST,
100
- PUT: segment.PUT,
101
- PATCH: segment.PATCH,
102
- DELETE: segment.DELETE,
103
- HEAD: segment.HEAD,
104
- OPTIONS: segment.OPTIONS,
98
+ GET: process.env.NODE_ENV === 'development' ? GET_DEV : vovkApp.GET,
99
+ POST: vovkApp.POST,
100
+ PUT: vovkApp.PUT,
101
+ PATCH: vovkApp.PATCH,
102
+ DELETE: vovkApp.DELETE,
103
+ HEAD: vovkApp.HEAD,
104
+ OPTIONS: vovkApp.OPTIONS,
105
105
  };
106
106
  };
107
107
  return {
108
- get: getDecoratorCreator(types_1._HttpMethod.GET),
109
- post: getDecoratorCreator(types_1._HttpMethod.POST),
110
- put: getDecoratorCreator(types_1._HttpMethod.PUT),
111
- patch: getDecoratorCreator(types_1._HttpMethod.PATCH),
112
- del: getDecoratorCreator(types_1._HttpMethod.DELETE),
113
- head: getDecoratorCreator(types_1._HttpMethod.HEAD),
114
- options: getDecoratorCreator(types_1._HttpMethod.OPTIONS),
108
+ get: createHTTPDecorator(types_1.HttpMethod.GET),
109
+ post: createHTTPDecorator(types_1.HttpMethod.POST),
110
+ put: createHTTPDecorator(types_1.HttpMethod.PUT),
111
+ patch: createHTTPDecorator(types_1.HttpMethod.PATCH),
112
+ del: createHTTPDecorator(types_1.HttpMethod.DELETE),
113
+ head: createHTTPDecorator(types_1.HttpMethod.HEAD),
114
+ options: createHTTPDecorator(types_1.HttpMethod.OPTIONS),
115
115
  prefix,
116
116
  initVovk,
117
117
  };
@@ -0,0 +1,60 @@
1
+ import { createVovkApp } from './createVovkApp';
2
+ import { HttpStatus as HttpStatus, HttpMethod as HttpMethod, type KnownAny, type VovkErrorResponse, type VovkRequest, type VovkBody, type VovkQuery, type VovkParams, type VovkReturnType, type VovkYieldType, type VovkControllerBody, type VovkControllerQuery, type VovkControllerParams, type VovkControllerYieldType, type VovkSchema, type VovkControllerSchema, type VovkHandlerSchema } from './types';
3
+ import { type VovkClient, type VovkClientOptions, type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkValidateOnClient, type VovkStreamAsyncIterable, createRPC } from './client';
4
+ import { openapi } from './openapi';
5
+ import { HttpException } from './HttpException';
6
+ import { createDecorator } from './createDecorator';
7
+ import { StreamJSONResponse } from './StreamJSONResponse';
8
+ import { generateStaticAPI } from './utils/generateStaticAPI';
9
+ import { setHandlerValidation } from './utils/setHandlerValidation';
10
+ export { type KnownAny, type VovkClient, type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkStreamAsyncIterable, type VovkValidateOnClient, type VovkSchema, type VovkErrorResponse, type VovkRequest, type VovkControllerBody, type VovkControllerQuery, type VovkControllerParams, type VovkControllerYieldType, type VovkBody, type VovkQuery, type VovkParams, type VovkYieldType, type VovkReturnType, type VovkClientOptions, type VovkControllerSchema, type VovkHandlerSchema, StreamJSONResponse, HttpException, HttpStatus, HttpMethod, createVovkApp, createDecorator, createRPC, generateStaticAPI, openapi, setHandlerValidation, };
11
+ export declare const get: {
12
+ (givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
13
+ auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
14
+ }, post: {
15
+ (givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
16
+ auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
17
+ }, put: {
18
+ (givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
19
+ auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
20
+ }, patch: {
21
+ (givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
22
+ auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
23
+ }, del: {
24
+ (givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
25
+ auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
26
+ }, head: {
27
+ (givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
28
+ auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
29
+ }, options: {
30
+ (givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
31
+ auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
32
+ }, prefix: (givenPath?: string) => (givenTarget: KnownAny) => any, initVovk: (options: {
33
+ segmentName?: string;
34
+ controllers: Record<string, import("./types").StaticClass>;
35
+ exposeValidation?: boolean;
36
+ emitSchema?: boolean;
37
+ onError?: (err: Error, req: VovkRequest) => void | Promise<void>;
38
+ }) => {
39
+ GET: (req: import("next/server").NextRequest, data: {
40
+ params: Promise<Record<string, string[]>>;
41
+ }) => Promise<Response>;
42
+ POST: (req: import("next/server").NextRequest, data: {
43
+ params: Promise<Record<string, string[]>>;
44
+ }) => Promise<Response>;
45
+ PUT: (req: import("next/server").NextRequest, data: {
46
+ params: Promise<Record<string, string[]>>;
47
+ }) => Promise<Response>;
48
+ PATCH: (req: import("next/server").NextRequest, data: {
49
+ params: Promise<Record<string, string[]>>;
50
+ }) => Promise<Response>;
51
+ DELETE: (req: import("next/server").NextRequest, data: {
52
+ params: Promise<Record<string, string[]>>;
53
+ }) => Promise<Response>;
54
+ HEAD: (req: import("next/server").NextRequest, data: {
55
+ params: Promise<Record<string, string[]>>;
56
+ }) => Promise<Response>;
57
+ OPTIONS: (req: import("next/server").NextRequest, data: {
58
+ params: Promise<Record<string, string[]>>;
59
+ }) => Promise<Response>;
60
+ };
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.initVovk = exports.prefix = exports.options = exports.head = exports.del = exports.patch = exports.put = exports.post = exports.get = exports.setHandlerValidation = exports.openapi = exports.generateStaticAPI = exports.createRPC = exports.createDecorator = exports.createVovkApp = exports.HttpMethod = exports.HttpStatus = exports.HttpException = exports.StreamJSONResponse = void 0;
5
+ const createVovkApp_1 = require("./createVovkApp");
6
+ Object.defineProperty(exports, "createVovkApp", { enumerable: true, get: function () { return createVovkApp_1.createVovkApp; } });
7
+ const types_1 = require("./types");
8
+ Object.defineProperty(exports, "HttpStatus", { enumerable: true, get: function () { return types_1.HttpStatus; } });
9
+ Object.defineProperty(exports, "HttpMethod", { enumerable: true, get: function () { return types_1.HttpMethod; } });
10
+ const client_1 = require("./client");
11
+ Object.defineProperty(exports, "createRPC", { enumerable: true, get: function () { return client_1.createRPC; } });
12
+ const openapi_1 = require("./openapi");
13
+ Object.defineProperty(exports, "openapi", { enumerable: true, get: function () { return openapi_1.openapi; } });
14
+ const HttpException_1 = require("./HttpException");
15
+ Object.defineProperty(exports, "HttpException", { enumerable: true, get: function () { return HttpException_1.HttpException; } });
16
+ const createDecorator_1 = require("./createDecorator");
17
+ Object.defineProperty(exports, "createDecorator", { enumerable: true, get: function () { return createDecorator_1.createDecorator; } });
18
+ const StreamJSONResponse_1 = require("./StreamJSONResponse");
19
+ Object.defineProperty(exports, "StreamJSONResponse", { enumerable: true, get: function () { return StreamJSONResponse_1.StreamJSONResponse; } });
20
+ const generateStaticAPI_1 = require("./utils/generateStaticAPI");
21
+ Object.defineProperty(exports, "generateStaticAPI", { enumerable: true, get: function () { return generateStaticAPI_1.generateStaticAPI; } });
22
+ const setHandlerValidation_1 = require("./utils/setHandlerValidation");
23
+ Object.defineProperty(exports, "setHandlerValidation", { enumerable: true, get: function () { return setHandlerValidation_1.setHandlerValidation; } });
24
+ _a = (0, createVovkApp_1.createVovkApp)(), exports.get = _a.get, exports.post = _a.post, exports.put = _a.put, exports.patch = _a.patch, exports.del = _a.del, exports.head = _a.head, exports.options = _a.options, exports.prefix = _a.prefix, exports.initVovk = _a.initVovk;
@@ -0,0 +1,3 @@
1
+ import type { OpenAPIObject } from 'openapi3-ts/oas31';
2
+ import type { VovkSchema } from '../types';
3
+ export declare function fromSchema(apiRoot: string, vovkSchema: Record<string, VovkSchema>, extendWith?: Partial<OpenAPIObject>): OpenAPIObject;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fromSchema = fromSchema;
4
+ function fromSchema(apiRoot, vovkSchema, extendWith) {
5
+ const paths = {};
6
+ for (const [segmentName, schema] of Object.entries(vovkSchema)) {
7
+ for (const c of Object.values(schema.controllers)) {
8
+ for (const h of Object.values(c.handlers)) {
9
+ if (h.openapi) {
10
+ const path = '/' + [apiRoot.replace(/^\/+|\/+$/g, ''), segmentName, c.prefix, h.path].filter(Boolean).join('/');
11
+ paths[path] = paths[path] ?? {};
12
+ paths[path][h.httpMethod.toLowerCase()] = { ...h.openapi };
13
+ }
14
+ }
15
+ }
16
+ }
17
+ return {
18
+ ...extendWith,
19
+ openapi: '3.1.0',
20
+ info: extendWith?.info ?? {
21
+ title: 'API',
22
+ version: '1.0.0',
23
+ },
24
+ paths,
25
+ };
26
+ }
@@ -0,0 +1 @@
1
+ export { openapi } from './openapi';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.openapi = void 0;
4
+ var openapi_1 = require("./openapi");
5
+ Object.defineProperty(exports, "openapi", { enumerable: true, get: function () { return openapi_1.openapi; } });
@@ -0,0 +1,11 @@
1
+ import type { OperationObject } from 'openapi3-ts/oas31';
2
+ import { fromSchema } from './fromSchema';
3
+ import type { KnownAny } from '../types';
4
+ type OperationObjectWithCustomProperties = OperationObject & {
5
+ [key in `${'x' | 'X'}-${string}`]: KnownAny;
6
+ };
7
+ declare const openapiDecorator: (openAPIOperationObject?: OperationObjectWithCustomProperties | undefined) => (target: KnownAny, propertyKey: string) => void;
8
+ export declare const openapi: typeof openapiDecorator & {
9
+ fromSchema: typeof fromSchema;
10
+ };
11
+ export {};
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.openapi = void 0;
4
+ const createDecorator_1 = require("../createDecorator");
5
+ const fromSchema_1 = require("./fromSchema");
6
+ const openapiDecorator = (0, createDecorator_1.createDecorator)(null, (openAPIOperationObject = {}) => {
7
+ return (handlerSchema) => ({
8
+ ...handlerSchema,
9
+ openapi: {
10
+ ...handlerSchema?.openapi,
11
+ ...(handlerSchema?.validation?.output &&
12
+ 'type' in handlerSchema.validation.output &&
13
+ 'properties' in handlerSchema.validation.output
14
+ ? {
15
+ responses: {
16
+ 200: {
17
+ description: 'description' in handlerSchema.validation.output
18
+ ? handlerSchema.validation.output.description
19
+ : 'Success',
20
+ content: {
21
+ 'application/json': {
22
+ schema: handlerSchema.validation.output,
23
+ },
24
+ },
25
+ },
26
+ },
27
+ }
28
+ : {}),
29
+ ...(handlerSchema?.validation?.body &&
30
+ 'type' in handlerSchema.validation.body &&
31
+ 'properties' in handlerSchema.validation.body
32
+ ? {
33
+ requestBody: {
34
+ description: 'description' in handlerSchema.validation.body
35
+ ? handlerSchema.validation.body.description
36
+ : 'Request body',
37
+ required: true,
38
+ content: {
39
+ 'application/json': {
40
+ schema: handlerSchema.validation.body,
41
+ },
42
+ },
43
+ },
44
+ }
45
+ : {}),
46
+ ...(handlerSchema?.validation?.query &&
47
+ 'type' in handlerSchema.validation.query &&
48
+ 'properties' in handlerSchema.validation.query
49
+ ? {
50
+ parameters: Object.entries(handlerSchema.validation.query.properties)
51
+ // .filter(([, propSchema]) => propSchema.type !== 'object')
52
+ .map(([propName, propSchema]) => ({
53
+ name: propName,
54
+ in: 'query',
55
+ description: propSchema.description || '',
56
+ required: handlerSchema.validation?.query.required
57
+ ? handlerSchema.validation.query.required.includes(propName)
58
+ : false,
59
+ schema: {
60
+ type: 'string',
61
+ },
62
+ })),
63
+ }
64
+ : {}),
65
+ ...openAPIOperationObject,
66
+ },
67
+ });
68
+ });
69
+ exports.openapi = openapiDecorator;
70
+ exports.openapi.fromSchema = fromSchema_1.fromSchema;