vovk 3.0.0-beta.5 → 3.0.0-draft.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/LICENSE +28 -0
  2. package/README.md +27 -1
  3. package/bin/index.mjs +8 -0
  4. package/cjs/HttpException.d.ts +7 -0
  5. package/cjs/HttpException.js +15 -0
  6. package/cjs/JSONLinesResponse.d.ts +19 -0
  7. package/cjs/JSONLinesResponse.js +95 -0
  8. package/cjs/VovkApp.d.ts +34 -0
  9. package/cjs/VovkApp.js +304 -0
  10. package/cjs/client/createRPC.d.ts +5 -0
  11. package/cjs/client/createRPC.js +116 -0
  12. package/cjs/client/defaultHandler.d.ts +6 -0
  13. package/cjs/client/defaultHandler.js +29 -0
  14. package/cjs/client/defaultStreamHandler.d.ts +9 -0
  15. package/cjs/client/defaultStreamHandler.js +105 -0
  16. package/cjs/client/fetcher.d.ts +32 -0
  17. package/cjs/client/fetcher.js +99 -0
  18. package/cjs/client/index.d.ts +4 -0
  19. package/cjs/client/index.js +10 -0
  20. package/cjs/client/progressive.d.ts +9 -0
  21. package/cjs/client/progressive.js +54 -0
  22. package/cjs/client/types.d.ts +120 -0
  23. package/cjs/client/types.js +2 -0
  24. package/cjs/createVovkApp.d.ts +65 -0
  25. package/cjs/createVovkApp.js +146 -0
  26. package/cjs/index.d.ts +69 -0
  27. package/cjs/index.js +42 -0
  28. package/cjs/openapi/error.d.ts +2 -0
  29. package/cjs/openapi/error.js +100 -0
  30. package/cjs/openapi/index.d.ts +8 -0
  31. package/cjs/openapi/index.js +21 -0
  32. package/cjs/openapi/openAPIToVovkSchema/applyComponentsSchemas.d.ts +3 -0
  33. package/cjs/openapi/openAPIToVovkSchema/applyComponentsSchemas.js +67 -0
  34. package/cjs/openapi/openAPIToVovkSchema/index.d.ts +4 -0
  35. package/cjs/openapi/openAPIToVovkSchema/index.js +141 -0
  36. package/cjs/openapi/openAPIToVovkSchema/inlineRefs.d.ts +10 -0
  37. package/cjs/openapi/openAPIToVovkSchema/inlineRefs.js +102 -0
  38. package/cjs/openapi/vovkSchemaToOpenAPI.d.ts +20 -0
  39. package/cjs/openapi/vovkSchemaToOpenAPI.js +251 -0
  40. package/cjs/types.d.ts +465 -0
  41. package/cjs/types.js +77 -0
  42. package/cjs/utils/camelCase.d.ts +6 -0
  43. package/cjs/utils/camelCase.js +37 -0
  44. package/cjs/utils/createCodeSamples.d.ts +20 -0
  45. package/cjs/utils/createCodeSamples.js +279 -0
  46. package/cjs/utils/createDecorator.d.ts +6 -0
  47. package/cjs/utils/createDecorator.js +48 -0
  48. package/cjs/utils/createLLMTools.d.ts +50 -0
  49. package/cjs/utils/createLLMTools.js +168 -0
  50. package/cjs/utils/createStandardValidation.d.ts +83 -0
  51. package/cjs/utils/createStandardValidation.js +35 -0
  52. package/cjs/utils/createValidateOnClient.d.ts +7 -0
  53. package/cjs/utils/createValidateOnClient.js +19 -0
  54. package/cjs/utils/deepExtend.d.ts +54 -0
  55. package/cjs/utils/deepExtend.js +134 -0
  56. package/cjs/utils/generateStaticAPI.d.ts +4 -0
  57. package/cjs/utils/generateStaticAPI.js +30 -0
  58. package/cjs/utils/getJSONSchemaExample.d.ts +11 -0
  59. package/cjs/utils/getJSONSchemaExample.js +264 -0
  60. package/cjs/utils/getJSONSchemaSample.d.ts +2 -0
  61. package/cjs/utils/getJSONSchemaSample.js +167 -0
  62. package/cjs/utils/getSampleFromObject.d.ts +9 -0
  63. package/cjs/utils/getSampleFromObject.js +41 -0
  64. package/cjs/utils/getSchema.d.ts +21 -0
  65. package/cjs/utils/getSchema.js +38 -0
  66. package/cjs/utils/multitenant.d.ts +24 -0
  67. package/cjs/utils/multitenant.js +131 -0
  68. package/cjs/utils/parseQuery.d.ts +25 -0
  69. package/cjs/utils/parseQuery.js +156 -0
  70. package/cjs/utils/reqForm.d.ts +2 -0
  71. package/cjs/utils/reqForm.js +33 -0
  72. package/cjs/utils/reqMeta.d.ts +2 -0
  73. package/cjs/utils/reqMeta.js +13 -0
  74. package/cjs/utils/reqQuery.d.ts +2 -0
  75. package/cjs/utils/reqQuery.js +10 -0
  76. package/cjs/utils/resolveGeneratorConfigValues.d.ts +19 -0
  77. package/cjs/utils/resolveGeneratorConfigValues.js +60 -0
  78. package/cjs/utils/serializeQuery.d.ts +13 -0
  79. package/cjs/utils/serializeQuery.js +65 -0
  80. package/cjs/utils/setHandlerSchema.d.ts +4 -0
  81. package/cjs/utils/setHandlerSchema.js +15 -0
  82. package/cjs/utils/shim.d.ts +0 -0
  83. package/cjs/utils/shim.js +17 -0
  84. package/cjs/utils/upperFirst.d.ts +1 -0
  85. package/cjs/utils/upperFirst.js +6 -0
  86. package/cjs/utils/withValidationLibrary.d.ts +78 -0
  87. package/cjs/utils/withValidationLibrary.js +133 -0
  88. package/mjs/HttpException.d.ts +7 -0
  89. package/mjs/HttpException.js +15 -0
  90. package/mjs/JSONLinesResponse.d.ts +19 -0
  91. package/mjs/JSONLinesResponse.js +95 -0
  92. package/mjs/VovkApp.d.ts +34 -0
  93. package/mjs/VovkApp.js +304 -0
  94. package/mjs/client/createRPC.d.ts +5 -0
  95. package/mjs/client/createRPC.js +116 -0
  96. package/mjs/client/defaultHandler.d.ts +6 -0
  97. package/mjs/client/defaultHandler.js +29 -0
  98. package/mjs/client/defaultStreamHandler.d.ts +9 -0
  99. package/mjs/client/defaultStreamHandler.js +105 -0
  100. package/mjs/client/fetcher.d.ts +32 -0
  101. package/mjs/client/fetcher.js +99 -0
  102. package/mjs/client/index.d.ts +4 -0
  103. package/mjs/client/index.js +10 -0
  104. package/mjs/client/progressive.d.ts +9 -0
  105. package/mjs/client/progressive.js +54 -0
  106. package/mjs/client/types.d.ts +120 -0
  107. package/mjs/client/types.js +2 -0
  108. package/mjs/createVovkApp.d.ts +65 -0
  109. package/mjs/createVovkApp.js +146 -0
  110. package/mjs/index.d.ts +69 -0
  111. package/mjs/index.js +42 -0
  112. package/mjs/openapi/error.d.ts +2 -0
  113. package/mjs/openapi/error.js +100 -0
  114. package/mjs/openapi/index.d.ts +8 -0
  115. package/mjs/openapi/index.js +21 -0
  116. package/mjs/openapi/openAPIToVovkSchema/applyComponentsSchemas.d.ts +3 -0
  117. package/mjs/openapi/openAPIToVovkSchema/applyComponentsSchemas.js +67 -0
  118. package/mjs/openapi/openAPIToVovkSchema/index.d.ts +4 -0
  119. package/mjs/openapi/openAPIToVovkSchema/index.js +141 -0
  120. package/mjs/openapi/openAPIToVovkSchema/inlineRefs.d.ts +10 -0
  121. package/mjs/openapi/openAPIToVovkSchema/inlineRefs.js +102 -0
  122. package/mjs/openapi/vovkSchemaToOpenAPI.d.ts +20 -0
  123. package/mjs/openapi/vovkSchemaToOpenAPI.js +251 -0
  124. package/mjs/types.d.ts +465 -0
  125. package/mjs/types.js +77 -0
  126. package/mjs/utils/camelCase.d.ts +6 -0
  127. package/mjs/utils/camelCase.js +37 -0
  128. package/mjs/utils/createCodeSamples.d.ts +20 -0
  129. package/mjs/utils/createCodeSamples.js +279 -0
  130. package/mjs/utils/createDecorator.d.ts +6 -0
  131. package/mjs/utils/createDecorator.js +48 -0
  132. package/mjs/utils/createLLMTools.d.ts +50 -0
  133. package/mjs/utils/createLLMTools.js +168 -0
  134. package/mjs/utils/createStandardValidation.d.ts +83 -0
  135. package/mjs/utils/createStandardValidation.js +35 -0
  136. package/mjs/utils/createValidateOnClient.d.ts +7 -0
  137. package/mjs/utils/createValidateOnClient.js +19 -0
  138. package/mjs/utils/deepExtend.d.ts +54 -0
  139. package/mjs/utils/deepExtend.js +134 -0
  140. package/mjs/utils/generateStaticAPI.d.ts +4 -0
  141. package/mjs/utils/generateStaticAPI.js +30 -0
  142. package/mjs/utils/getJSONSchemaExample.d.ts +11 -0
  143. package/mjs/utils/getJSONSchemaExample.js +264 -0
  144. package/mjs/utils/getJSONSchemaSample.d.ts +2 -0
  145. package/mjs/utils/getJSONSchemaSample.js +167 -0
  146. package/mjs/utils/getSampleFromObject.d.ts +9 -0
  147. package/mjs/utils/getSampleFromObject.js +41 -0
  148. package/mjs/utils/getSchema.d.ts +21 -0
  149. package/mjs/utils/getSchema.js +38 -0
  150. package/mjs/utils/multitenant.d.ts +24 -0
  151. package/mjs/utils/multitenant.js +131 -0
  152. package/mjs/utils/parseQuery.d.ts +25 -0
  153. package/mjs/utils/parseQuery.js +156 -0
  154. package/mjs/utils/reqForm.d.ts +2 -0
  155. package/mjs/utils/reqForm.js +33 -0
  156. package/mjs/utils/reqMeta.d.ts +2 -0
  157. package/mjs/utils/reqMeta.js +13 -0
  158. package/mjs/utils/reqQuery.d.ts +2 -0
  159. package/mjs/utils/reqQuery.js +10 -0
  160. package/mjs/utils/resolveGeneratorConfigValues.d.ts +19 -0
  161. package/mjs/utils/resolveGeneratorConfigValues.js +60 -0
  162. package/mjs/utils/serializeQuery.d.ts +13 -0
  163. package/mjs/utils/serializeQuery.js +65 -0
  164. package/mjs/utils/setHandlerSchema.d.ts +4 -0
  165. package/mjs/utils/setHandlerSchema.js +15 -0
  166. package/mjs/utils/shim.d.ts +1 -0
  167. package/mjs/utils/shim.js +18 -0
  168. package/mjs/utils/upperFirst.d.ts +1 -0
  169. package/mjs/utils/upperFirst.js +6 -0
  170. package/mjs/utils/withValidationLibrary.d.ts +78 -0
  171. package/mjs/utils/withValidationLibrary.js +133 -0
  172. package/package.json +29 -6
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "vovk",
3
+ "version": "3.0.0-beta.9",
4
+ "description": "RESTful RPC for Next.js - Transforms Next.js into a powerful REST API platform with RPC capabilities.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/finom/vovk.git"
8
+ },
9
+ "scripts": {
10
+ "build": "rm -rf dist && tsc && cp {package.json,LICENSE} && cp ../../README.md dist ",
11
+ "lint": "eslint . --fix",
12
+ "npm-publish": "npm publish ./dist --dry-run",
13
+ "ncu": "npm-check-updates -u"
14
+ },
15
+ "keywords": [
16
+ "nextjs",
17
+ "router"
18
+ ],
19
+ "author": "Andrey Gubanov",
20
+ "license": "MIT",
21
+ "bugs": {
22
+ "url": "https://github.com/finom/vovk/issues"
23
+ },
24
+ "homepage": "https://vovk.dev",
25
+ "peerDependencies": {
26
+ "next": "*"
27
+ }
28
+ }
package/README.md CHANGED
@@ -1 +1,27 @@
1
- Description is coming soon.
1
+ <p align="center">
2
+ <a href="https://vovk.dev">
3
+ <picture>
4
+ <source width="300" media="(prefers-color-scheme: dark)" srcset="https://vovk.dev/vovk-logo-white.svg">
5
+ <source width="300" media="(prefers-color-scheme: light)" srcset="https://vovk.dev/vovk-logo.svg">
6
+ <img width="300" alt="vovk" src="https://vovk.dev/vovk-logo.svg">
7
+ </picture>
8
+ </a>
9
+ <br>
10
+ <strong>Back-end for Next.js (beta)</strong>
11
+ <br />
12
+ <a href="https://vovk.dev/about">About Vovk.ts</a>
13
+ &nbsp;
14
+ <a href="https://vovk.dev/quick-install">Quick Start</a>
15
+ &nbsp;
16
+ <a href="https://github.com/finom/vovk">Github Repo</a>
17
+ </p>
18
+
19
+ ---
20
+
21
+ ## vovk [![npm version](https://badge.fury.io/js/vovk.svg)](https://www.npmjs.com/package/vovk)
22
+
23
+ The main library with [100% self-composition](https://bundlephobia.com/result?p=vovk) that's going to be used in production. It provides a wrapper for Next.js API routes, client-side RPC tooling, utilities and types.
24
+
25
+ ```sh
26
+ npm install vovk
27
+ ```
package/bin/index.mjs ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ import { spawn } from 'child_process';
3
+
4
+ console.warn(`Vovk CLI requires vovk-cli package. Running "npx vovk-cli ${process.argv.slice(2).join(' ')}" instead.`);
5
+
6
+ spawn('npx', ['vovk-cli', ...process.argv.slice(2)], { stdio: 'inherit' }).on('exit', (code) => {
7
+ process.exit(code);
8
+ });
@@ -0,0 +1,7 @@
1
+ import type { HttpStatus } from './types';
2
+ export declare class HttpException extends Error {
3
+ statusCode: HttpStatus;
4
+ message: string;
5
+ cause?: unknown;
6
+ constructor(statusCode: HttpStatus, message: string, cause?: unknown);
7
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpException = void 0;
4
+ class HttpException extends Error {
5
+ statusCode;
6
+ message;
7
+ cause;
8
+ constructor(statusCode, message, cause) {
9
+ super(message);
10
+ this.statusCode = statusCode;
11
+ this.message = message;
12
+ this.cause = cause;
13
+ }
14
+ }
15
+ exports.HttpException = HttpException;
@@ -0,0 +1,19 @@
1
+ import type { KnownAny, StreamAbortMessage } from './types';
2
+ import './utils/shim';
3
+ export declare class JSONLinesResponse<T> extends Response {
4
+ isClosed: boolean;
5
+ controller?: ReadableStreamDefaultController | null;
6
+ readonly encoder: TextEncoder | null;
7
+ readonly readableStream: ReadableStream | null;
8
+ private iteratorQueue;
9
+ private iteratorResolvers;
10
+ constructor(request?: Request, init?: ResponseInit);
11
+ send: (data: T | StreamAbortMessage) => void;
12
+ close: () => void;
13
+ throw: (e: KnownAny) => void;
14
+ [Symbol.dispose]: () => void;
15
+ [Symbol.asyncDispose]: () => void;
16
+ [Symbol.asyncIterator]: () => {
17
+ next: () => Promise<IteratorResult<T | StreamAbortMessage>>;
18
+ };
19
+ }
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JSONLinesResponse = void 0;
4
+ require("./utils/shim");
5
+ class JSONLinesResponse extends Response {
6
+ isClosed = false;
7
+ controller;
8
+ encoder;
9
+ readableStream;
10
+ iteratorQueue = [];
11
+ iteratorResolvers = [];
12
+ constructor(request, init) {
13
+ const encoder = new TextEncoder();
14
+ let readableController;
15
+ const readableStream = new ReadableStream({
16
+ cancel: () => {
17
+ this.isClosed = true;
18
+ },
19
+ start: (controller) => {
20
+ readableController = controller;
21
+ },
22
+ });
23
+ const accept = request?.headers?.get('accept');
24
+ super(readableStream, {
25
+ ...init,
26
+ headers: {
27
+ 'content-type': !request || accept?.includes('application/jsonl')
28
+ ? 'application/jsonl; charset=utf-8'
29
+ : 'text/plain; charset=utf-8',
30
+ ...init?.headers,
31
+ },
32
+ });
33
+ this.readableStream = request ? readableStream : null;
34
+ this.encoder = request ? encoder : null;
35
+ this.controller = request ? readableController : null;
36
+ request?.signal?.addEventListener('abort', this.close, { once: true });
37
+ // this will make promise on the client-side to resolve immediately
38
+ this.controller?.enqueue(encoder?.encode(''));
39
+ }
40
+ send = (data) => {
41
+ const { controller, encoder } = this;
42
+ if (this.isClosed)
43
+ return;
44
+ // Enqueue to the ReadableStream
45
+ controller?.enqueue(encoder?.encode(JSON.stringify(data) + '\n'));
46
+ // Handle async iterator consumers
47
+ if (this.iteratorResolvers.length > 0) {
48
+ // If there's a pending next() call, resolve it immediately
49
+ const resolve = this.iteratorResolvers.shift();
50
+ resolve({ value: data, done: false });
51
+ }
52
+ else {
53
+ // Otherwise, queue the value for later consumption
54
+ this.iteratorQueue.push(data);
55
+ }
56
+ };
57
+ close = () => {
58
+ const { controller } = this;
59
+ if (this.isClosed)
60
+ return;
61
+ this.isClosed = true;
62
+ controller?.close();
63
+ // Resolve all pending iterator next() calls with done: true
64
+ while (this.iteratorResolvers.length > 0) {
65
+ const resolve = this.iteratorResolvers.shift();
66
+ resolve({ done: true, value: undefined });
67
+ }
68
+ };
69
+ throw = (e) => {
70
+ this.send({ isError: true, reason: e instanceof Error ? e.message : e });
71
+ return this.close();
72
+ };
73
+ [Symbol.dispose] = () => this.close();
74
+ [Symbol.asyncDispose] = () => this.close();
75
+ [Symbol.asyncIterator] = () => {
76
+ return {
77
+ next: async () => {
78
+ // If we have queued values, return them immediately
79
+ if (this.iteratorQueue.length > 0) {
80
+ const value = this.iteratorQueue.shift();
81
+ return { value, done: false };
82
+ }
83
+ // If the stream is closed and no more values, we're done
84
+ if (this.isClosed) {
85
+ return { done: true, value: undefined };
86
+ }
87
+ // Otherwise, wait for the next value or close
88
+ return new Promise((resolve) => {
89
+ this.iteratorResolvers.push(resolve);
90
+ });
91
+ },
92
+ };
93
+ };
94
+ }
95
+ exports.JSONLinesResponse = JSONLinesResponse;
@@ -0,0 +1,34 @@
1
+ import type { NextRequest } from 'next/server';
2
+ import { HttpMethod, HttpStatus, type RouteHandler, type VovkController, type DecoratorOptions, type VovkRequest } from './types';
3
+ export declare class VovkApp {
4
+ #private;
5
+ private static getHeadersFromOptions;
6
+ routes: Record<HttpMethod, Map<VovkController, Record<string, RouteHandler>>>;
7
+ GET: (req: NextRequest, data: {
8
+ params: Promise<Record<string, string[]>>;
9
+ }) => Promise<Response>;
10
+ POST: (req: NextRequest, data: {
11
+ params: Promise<Record<string, string[]>>;
12
+ }) => Promise<Response>;
13
+ PUT: (req: NextRequest, data: {
14
+ params: Promise<Record<string, string[]>>;
15
+ }) => Promise<Response>;
16
+ PATCH: (req: NextRequest, data: {
17
+ params: Promise<Record<string, string[]>>;
18
+ }) => Promise<Response>;
19
+ DELETE: (req: NextRequest, data: {
20
+ params: Promise<Record<string, string[]>>;
21
+ }) => Promise<Response>;
22
+ HEAD: (req: NextRequest, data: {
23
+ params: Promise<Record<string, string[]>>;
24
+ }) => Promise<Response>;
25
+ OPTIONS: (req: NextRequest, data: {
26
+ params: Promise<Record<string, string[]>>;
27
+ }) => Promise<Response>;
28
+ respond: ({ statusCode, responseBody, options, }: {
29
+ req: VovkRequest;
30
+ statusCode: HttpStatus;
31
+ responseBody: unknown;
32
+ options?: DecoratorOptions;
33
+ }) => Promise<Response>;
34
+ }
package/cjs/VovkApp.js ADDED
@@ -0,0 +1,304 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ var _a;
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.VovkApp = void 0;
8
+ const types_1 = require("./types");
9
+ const HttpException_1 = require("./HttpException");
10
+ const JSONLinesResponse_1 = require("./JSONLinesResponse");
11
+ const reqQuery_1 = __importDefault(require("./utils/reqQuery"));
12
+ const reqMeta_1 = __importDefault(require("./utils/reqMeta"));
13
+ const reqForm_1 = __importDefault(require("./utils/reqForm"));
14
+ class VovkApp {
15
+ static getHeadersFromOptions(options) {
16
+ if (!options)
17
+ return {};
18
+ const corsHeaders = {
19
+ 'access-control-allow-origin': '*',
20
+ 'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS, HEAD',
21
+ 'access-control-allow-headers': 'content-type, authorization',
22
+ };
23
+ const headers = {
24
+ ...(options.cors ? corsHeaders : {}),
25
+ ...(options.headers ?? {}),
26
+ };
27
+ return headers;
28
+ }
29
+ routes = {
30
+ GET: new Map(),
31
+ POST: new Map(),
32
+ PUT: new Map(),
33
+ PATCH: new Map(),
34
+ DELETE: new Map(),
35
+ HEAD: new Map(),
36
+ OPTIONS: new Map(),
37
+ };
38
+ GET = async (req, data) => this.#callMethod({ httpMethod: types_1.HttpMethod.GET, req, params: await data.params });
39
+ POST = async (req, data) => this.#callMethod({ httpMethod: types_1.HttpMethod.POST, req, params: await data.params });
40
+ PUT = async (req, data) => this.#callMethod({ httpMethod: types_1.HttpMethod.PUT, req, params: await data.params });
41
+ PATCH = async (req, data) => this.#callMethod({ httpMethod: types_1.HttpMethod.PATCH, req, params: await data.params });
42
+ DELETE = async (req, data) => this.#callMethod({ httpMethod: types_1.HttpMethod.DELETE, req, params: await data.params });
43
+ HEAD = async (req, data) => this.#callMethod({ httpMethod: types_1.HttpMethod.HEAD, req, params: await data.params });
44
+ OPTIONS = async (req, data) => this.#callMethod({ httpMethod: types_1.HttpMethod.OPTIONS, req, params: await data.params });
45
+ respond = async ({ statusCode, responseBody, options, }) => {
46
+ const response = new Response(JSON.stringify(responseBody), {
47
+ status: statusCode,
48
+ headers: {
49
+ 'content-type': 'application/json',
50
+ ..._a.getHeadersFromOptions(options),
51
+ },
52
+ });
53
+ return response;
54
+ };
55
+ #respondWithError = ({ req, statusCode, message, options, cause, }) => {
56
+ return this.respond({
57
+ req,
58
+ statusCode,
59
+ responseBody: {
60
+ cause,
61
+ statusCode,
62
+ message,
63
+ isError: true,
64
+ },
65
+ options,
66
+ });
67
+ };
68
+ #routeRegexCache = new Map();
69
+ #routeSegmentsCache = new Map();
70
+ #routeParamPositionsCache = new Map();
71
+ #routeMatchCache = new Map();
72
+ #getHandler = ({ handlers, path, params, }) => {
73
+ let methodParams = {};
74
+ if (Object.keys(params).length === 0) {
75
+ return { handler: handlers[''], methodParams };
76
+ }
77
+ const pathStr = path.join('/');
78
+ // Fast path: Check if this exact path has been matched before
79
+ const cachedMatch = this.#routeMatchCache.get(pathStr);
80
+ if (cachedMatch) {
81
+ return {
82
+ handler: handlers[cachedMatch.route],
83
+ methodParams: cachedMatch.params,
84
+ };
85
+ }
86
+ // Check for direct static route match
87
+ let methodKey = handlers[pathStr] ? pathStr : null;
88
+ if (!methodKey) {
89
+ const methodKeys = [];
90
+ const pathLength = path.length;
91
+ // First pass: group routes by length for quick filtering
92
+ const routesByLength = new Map();
93
+ for (const p of Object.keys(handlers)) {
94
+ let routeSegments = this.#routeSegmentsCache.get(p);
95
+ if (!routeSegments) {
96
+ routeSegments = p.split('/');
97
+ this.#routeSegmentsCache.set(p, routeSegments);
98
+ // Pre-compute parameter positions for routes with parameters
99
+ if (p.includes('{')) {
100
+ const paramPositions = [];
101
+ for (let i = 0; i < routeSegments.length; i++) {
102
+ const segment = routeSegments[i];
103
+ if (segment.includes('{')) {
104
+ const paramMatch = segment.match(/\{(\w+)\}/);
105
+ if (paramMatch) {
106
+ paramPositions.push({ index: i, paramName: paramMatch[1] });
107
+ }
108
+ }
109
+ }
110
+ this.#routeParamPositionsCache.set(p, paramPositions);
111
+ }
112
+ }
113
+ const segmentLength = routeSegments.length;
114
+ if (segmentLength !== pathLength)
115
+ continue;
116
+ const lengthRoutes = routesByLength.get(segmentLength) || [];
117
+ lengthRoutes.push(p);
118
+ routesByLength.set(segmentLength, lengthRoutes);
119
+ }
120
+ // Only process routes with matching segment count
121
+ const candidateRoutes = routesByLength.get(pathLength) || [];
122
+ for (const p of candidateRoutes) {
123
+ const routeSegments = this.#routeSegmentsCache.get(p);
124
+ const params = {};
125
+ // Fast path for routes with parameters
126
+ const paramPositions = this.#routeParamPositionsCache.get(p);
127
+ if (paramPositions) {
128
+ let isMatch = true;
129
+ // First check all non-parameter segments for a quick fail
130
+ for (let i = 0; i < routeSegments.length; i++) {
131
+ const routeSegment = routeSegments[i];
132
+ if (!routeSegment.includes('{') && routeSegment !== path[i]) {
133
+ isMatch = false;
134
+ break;
135
+ }
136
+ }
137
+ if (!isMatch)
138
+ continue;
139
+ // Now process parameter segments
140
+ for (const { index, paramName } of paramPositions) {
141
+ const routeSegment = routeSegments[index];
142
+ const pathSegment = path[index];
143
+ let regex = this.#routeRegexCache.get(routeSegment);
144
+ if (!regex) {
145
+ const regexPattern = routeSegment
146
+ .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
147
+ .replace(/\\{(\w+)\\}/g, '(?<$1>[^/]+)');
148
+ regex = new RegExp(`^${regexPattern}$`);
149
+ this.#routeRegexCache.set(routeSegment, regex);
150
+ }
151
+ const values = pathSegment.match(regex)?.groups;
152
+ if (!values) {
153
+ isMatch = false;
154
+ break;
155
+ }
156
+ if (paramName in params) {
157
+ throw new HttpException_1.HttpException(types_1.HttpStatus.INTERNAL_SERVER_ERROR, `Duplicate parameter "${paramName}" at ${p}`);
158
+ }
159
+ params[paramName] = values[paramName];
160
+ }
161
+ if (isMatch) {
162
+ methodParams = params;
163
+ methodKeys.push(p);
164
+ }
165
+ }
166
+ else {
167
+ // Static route - simple equality comparison for all segments
168
+ let isMatch = true;
169
+ for (let i = 0; i < routeSegments.length; i++) {
170
+ if (routeSegments[i] !== path[i]) {
171
+ isMatch = false;
172
+ break;
173
+ }
174
+ }
175
+ if (isMatch) {
176
+ methodKeys.push(p);
177
+ }
178
+ }
179
+ }
180
+ if (methodKeys.length > 1) {
181
+ throw new HttpException_1.HttpException(types_1.HttpStatus.INTERNAL_SERVER_ERROR, `Conflicting routes found: ${methodKeys.join(', ')}`);
182
+ }
183
+ [methodKey] = methodKeys;
184
+ // Cache successful matches
185
+ if (methodKey) {
186
+ this.#routeMatchCache.set(pathStr, { route: methodKey, params: methodParams });
187
+ }
188
+ }
189
+ if (methodKey) {
190
+ return { handler: handlers[methodKey], methodParams };
191
+ }
192
+ return { handler: null, methodParams };
193
+ };
194
+ #allHandlers = {};
195
+ #collectHandlers = (httpMethod) => {
196
+ const controllers = this.routes[httpMethod];
197
+ const handlers = {};
198
+ controllers.forEach((staticMethods, controller) => {
199
+ const prefix = controller._prefix ?? '';
200
+ Object.entries(staticMethods ?? {}).forEach(([path, staticMethod]) => {
201
+ const fullPath = [prefix, path].filter(Boolean).join('/');
202
+ handlers[fullPath] = { staticMethod, controller };
203
+ });
204
+ });
205
+ return handlers;
206
+ };
207
+ #callMethod = async ({ httpMethod, req: nextReq, params, }) => {
208
+ const req = nextReq;
209
+ const path = params[Object.keys(params)[0]];
210
+ const handlers = this.#allHandlers[httpMethod] ?? this.#collectHandlers(httpMethod);
211
+ this.#allHandlers[httpMethod] = handlers;
212
+ let headerList;
213
+ try {
214
+ headerList = nextReq.headers;
215
+ }
216
+ catch {
217
+ // this is static rendering environment, headers are not available
218
+ headerList = null;
219
+ }
220
+ const xMeta = headerList?.get('x-meta');
221
+ const xMetaHeader = xMeta && JSON.parse(xMeta);
222
+ if (xMetaHeader)
223
+ (0, reqMeta_1.default)(req, { xMetaHeader });
224
+ const { handler, methodParams } = this.#getHandler({ handlers, path, params });
225
+ if (!handler) {
226
+ return this.#respondWithError({
227
+ req,
228
+ statusCode: types_1.HttpStatus.NOT_FOUND,
229
+ message: `Route ${path.join('/')} is not found`,
230
+ });
231
+ }
232
+ const { staticMethod, controller } = handler;
233
+ const { _onSuccess: onSuccess, _onBefore: onBefore } = controller;
234
+ req.vovk = {
235
+ body: () => req.json(),
236
+ query: () => (0, reqQuery_1.default)(req),
237
+ meta: (meta) => (0, reqMeta_1.default)(req, meta),
238
+ form: () => (0, reqForm_1.default)(req),
239
+ params: () => methodParams,
240
+ };
241
+ try {
242
+ await staticMethod._options?.before?.call(controller, req);
243
+ await onBefore?.(req);
244
+ const result = await staticMethod.call(controller, req, methodParams);
245
+ if (result instanceof Response) {
246
+ await onSuccess?.(result, req);
247
+ return result;
248
+ }
249
+ const isIterator = typeof result === 'object' &&
250
+ !!result &&
251
+ !(result instanceof Array) &&
252
+ ((Reflect.has(result, Symbol.iterator) &&
253
+ typeof result[Symbol.iterator] === 'function') ||
254
+ (Reflect.has(result, Symbol.asyncIterator) &&
255
+ typeof result[Symbol.asyncIterator] === 'function'));
256
+ if (isIterator) {
257
+ const streamResponse = new JSONLinesResponse_1.JSONLinesResponse(req, {
258
+ headers: {
259
+ ..._a.getHeadersFromOptions(staticMethod._options),
260
+ },
261
+ });
262
+ void (async () => {
263
+ try {
264
+ for await (const chunk of result) {
265
+ streamResponse.send(chunk);
266
+ }
267
+ }
268
+ catch (e) {
269
+ return streamResponse.throw(e);
270
+ }
271
+ return streamResponse.close();
272
+ })();
273
+ await onSuccess?.(streamResponse, req);
274
+ return streamResponse;
275
+ }
276
+ const responseBody = result ?? null;
277
+ await onSuccess?.(responseBody, req);
278
+ return this.respond({ req, statusCode: 200, responseBody, options: staticMethod._options });
279
+ }
280
+ catch (e) {
281
+ const err = e;
282
+ try {
283
+ await controller._onError?.(err, req);
284
+ }
285
+ catch (onErrorError) {
286
+ // eslint-disable-next-line no-console
287
+ console.error(onErrorError);
288
+ }
289
+ if (err.message !== 'NEXT_REDIRECT' && err.message !== 'NEXT_NOT_FOUND') {
290
+ const statusCode = err.statusCode || types_1.HttpStatus.INTERNAL_SERVER_ERROR;
291
+ return this.#respondWithError({
292
+ req,
293
+ statusCode,
294
+ message: err.message,
295
+ options: staticMethod._options,
296
+ cause: err.cause,
297
+ });
298
+ }
299
+ throw e; // if NEXT_REDIRECT or NEXT_NOT_FOUND, rethrow it
300
+ }
301
+ };
302
+ }
303
+ exports.VovkApp = VovkApp;
304
+ _a = VovkApp;
@@ -0,0 +1,5 @@
1
+ import type { KnownAny } from '../types';
2
+ import type { VovkRPCModule, VovkFetcher, VovkFetcherOptions } from './types';
3
+ export declare const createRPC: <T, OPTS extends Record<string, KnownAny> = Record<string, never>>(givenSchema: unknown, segmentName: string, rpcModuleName: string, givenFetcher?: VovkFetcher<OPTS> | Promise<{
4
+ fetcher: VovkFetcher<OPTS>;
5
+ }>, options?: VovkFetcherOptions<OPTS>) => VovkRPCModule<T, OPTS>;
@@ -0,0 +1,116 @@
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.createRPC = void 0;
7
+ const fetcher_1 = require("./fetcher");
8
+ const defaultHandler_1 = require("./defaultHandler");
9
+ const defaultStreamHandler_1 = require("./defaultStreamHandler");
10
+ const serializeQuery_1 = __importDefault(require("../utils/serializeQuery"));
11
+ const trimPath = (path) => path.trim().replace(/^\/|\/$/g, '');
12
+ const getHandlerPath = (endpoint, params, query) => {
13
+ let result = endpoint;
14
+ const queryStr = query ? (0, serializeQuery_1.default)(query) : null;
15
+ for (const [key, value] of Object.entries(params ?? {})) {
16
+ result = result.replace(`{${key}}`, value);
17
+ }
18
+ return `${result}${queryStr ? '?' : ''}${queryStr}`;
19
+ };
20
+ const createRPC = (givenSchema, segmentName, rpcModuleName, givenFetcher, options) => {
21
+ const schema = givenSchema; // fixes incompatibilities with JSON module
22
+ // fetcher ??= defaultFetcher as NonNullable<typeof fetcher>;
23
+ const segmentNamePath = options?.segmentNameOverride ?? segmentName;
24
+ const segmentSchema = schema.segments[segmentName];
25
+ if (!segmentSchema)
26
+ throw new Error(`Unable to create RPC module. Segment schema is missing for segment "${segmentName}".`);
27
+ let controllerSchema = schema.segments[segmentName]?.controllers[rpcModuleName];
28
+ const client = {};
29
+ if (!controllerSchema) {
30
+ // eslint-disable-next-line no-console
31
+ console.warn(`🐺 Unable to create RPC module. Controller schema is missing for module "${rpcModuleName}" from segment "${segmentName}". Assuming that schema is not ready yet and a segment is importing an uncompiled RPC module.`);
32
+ controllerSchema = {
33
+ rpcModuleName,
34
+ prefix: '',
35
+ handlers: {},
36
+ };
37
+ }
38
+ const controllerPrefix = trimPath(controllerSchema.prefix ?? '');
39
+ const forceApiRoot = controllerSchema.forceApiRoot ?? segmentSchema.forceApiRoot;
40
+ const originalApiRoot = forceApiRoot ?? options?.apiRoot ?? '/api';
41
+ for (const [staticMethodName, handlerSchema] of Object.entries(controllerSchema.handlers ?? {})) {
42
+ const { path, httpMethod, validation } = handlerSchema;
43
+ const getEndpoint = ({ apiRoot, params, query }) => {
44
+ apiRoot = apiRoot ?? originalApiRoot;
45
+ const endpoint = [
46
+ apiRoot.startsWith('http://') || apiRoot.startsWith('https://') || apiRoot.startsWith('/') ? '' : '/',
47
+ apiRoot,
48
+ forceApiRoot ? '' : segmentNamePath,
49
+ getHandlerPath([controllerPrefix, path].filter(Boolean).join('/'), params, query),
50
+ ]
51
+ .filter(Boolean)
52
+ .join('/')
53
+ .replace(/([^:])\/+/g, '$1/'); // replace // by / but not for protocols (http://, https://)
54
+ return endpoint;
55
+ };
56
+ const handler = (async (input = {}) => {
57
+ const fetcher = givenFetcher instanceof Promise
58
+ ? (await givenFetcher).fetcher
59
+ : (givenFetcher ?? fetcher_1.fetcher);
60
+ const validate = async (validationInput, { endpoint, }) => {
61
+ const validateOnClient = input.validateOnClient ??
62
+ (options?.validateOnClient instanceof Promise
63
+ ? (await options?.validateOnClient)?.validateOnClient
64
+ : options?.validateOnClient);
65
+ if (validateOnClient && validation) {
66
+ if (typeof validateOnClient !== 'function') {
67
+ throw new Error('validateOnClient must be a function');
68
+ }
69
+ return ((await validateOnClient({ ...validationInput }, validation, { fullSchema: schema, endpoint })) ??
70
+ validationInput);
71
+ }
72
+ return validationInput;
73
+ };
74
+ const internalOptions = {
75
+ name: staticMethodName,
76
+ httpMethod: httpMethod,
77
+ getEndpoint,
78
+ validate,
79
+ defaultHandler: defaultHandler_1.defaultHandler,
80
+ defaultStreamHandler: defaultStreamHandler_1.defaultStreamHandler,
81
+ schema: handlerSchema,
82
+ };
83
+ const internalInput = {
84
+ ...options,
85
+ ...input,
86
+ body: input.body ?? null,
87
+ query: input.query ?? {},
88
+ params: input.params ?? {},
89
+ };
90
+ if (!fetcher)
91
+ throw new Error('Fetcher is not provided');
92
+ const [respData, resp] = await fetcher(internalOptions, internalInput);
93
+ return input.transform ? input.transform(respData, resp) : respData;
94
+ });
95
+ // TODO use Object.freeze, Object.seal or Object.defineProperty to avoid mutation
96
+ handler.schema = handlerSchema;
97
+ handler.controllerSchema = controllerSchema;
98
+ handler.segmentSchema = segmentSchema;
99
+ handler.fullSchema = schema;
100
+ handler.isRPC = true;
101
+ handler.apiRoot = originalApiRoot;
102
+ handler.path = [segmentNamePath, controllerPrefix, path].filter(Boolean).join('/');
103
+ handler.queryKey = (key) => [
104
+ handler.segmentSchema.segmentName,
105
+ handler.controllerSchema.prefix ?? '',
106
+ handler.controllerSchema.rpcModuleName,
107
+ handler.schema.path,
108
+ handler.schema.httpMethod,
109
+ ...(key ?? []),
110
+ ];
111
+ // @ts-expect-error TODO
112
+ client[staticMethodName] = handler;
113
+ }
114
+ return client;
115
+ };
116
+ exports.createRPC = createRPC;
@@ -0,0 +1,6 @@
1
+ import { VovkHandlerSchema } from '../types';
2
+ export declare const DEFAULT_ERROR_MESSAGE = "Unknown error at defaultHandler";
3
+ export declare const defaultHandler: ({ response, schema }: {
4
+ response: Response;
5
+ schema: VovkHandlerSchema;
6
+ }) => Promise<unknown>;