vovk 3.0.0-draft.93 → 3.0.0-draft.96

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.
@@ -28,11 +28,11 @@ const assignSchema = ({ controller, propertyKey, path, options, httpMethod, vovk
28
28
  const originalMethod = controller[propertyKey];
29
29
  originalMethod._controller = controller;
30
30
  originalMethod._sourceMethod = originalMethod._sourceMethod ?? originalMethod;
31
- const validation = originalMethod._sourceMethod._getValidation?.(controller);
31
+ const schema = originalMethod._sourceMethod._getSchema?.(controller);
32
32
  controller._handlers = {
33
33
  ...controller._handlers,
34
34
  [propertyKey]: {
35
- ...(validation ? { validation } : {}),
35
+ ...schema,
36
36
  ...(controller._handlers ?? {})[propertyKey],
37
37
  path,
38
38
  httpMethod,
package/dist/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
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 VovkControllerOutput, type VovkSegmentSchema, type VovkControllerSchema, type VovkHandlerSchema, type VovkFullSchema, type VovkConfig, type VovkStrictConfig, type VovkEnv } from './types';
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 VovkControllerOutput, type VovkSegmentSchema, type VovkControllerSchema, type VovkHandlerSchema, type VovkFullSchema, type VovkConfig, type VovkStrictConfig, type VovkEnv, type VovkValidationType } from './types';
3
3
  import { type VovkClient, type VovkClientOptions, type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkValidateOnClient, type VovkStreamAsyncIterable, createRPC, fetcher } from './client';
4
4
  import { HttpException } from './HttpException';
5
5
  import { createDecorator } from './createDecorator';
6
6
  import { StreamJSONResponse } from './StreamJSONResponse';
7
7
  import { generateStaticAPI } from './utils/generateStaticAPI';
8
- import { setHandlerValidation } from './utils/setHandlerValidation';
9
- export { type KnownAny, type VovkClient, type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkStreamAsyncIterable, type VovkValidateOnClient, type VovkSegmentSchema, type VovkErrorResponse, type VovkRequest, type VovkControllerBody, type VovkControllerQuery, type VovkControllerParams, type VovkControllerYieldType, type VovkControllerOutput, type VovkBody, type VovkQuery, type VovkParams, type VovkYieldType, type VovkReturnType, type VovkClientOptions, type VovkControllerSchema, type VovkHandlerSchema, type VovkFullSchema, type VovkConfig, type VovkStrictConfig, type VovkEnv, StreamJSONResponse, HttpException, HttpStatus, HttpMethod, createVovkApp, createDecorator, createRPC, fetcher, generateStaticAPI, setHandlerValidation, };
8
+ import { withValidation } from './utils/withValidation';
9
+ export { type KnownAny, type VovkClient, type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkStreamAsyncIterable, type VovkValidateOnClient, type VovkSegmentSchema, type VovkErrorResponse, type VovkRequest, type VovkControllerBody, type VovkControllerQuery, type VovkControllerParams, type VovkControllerYieldType, type VovkControllerOutput, type VovkBody, type VovkQuery, type VovkParams, type VovkYieldType, type VovkReturnType, type VovkClientOptions, type VovkControllerSchema, type VovkHandlerSchema, type VovkFullSchema, type VovkConfig, type VovkStrictConfig, type VovkEnv, type VovkValidationType, StreamJSONResponse, HttpException, HttpStatus, HttpMethod, createVovkApp, createDecorator, createRPC, fetcher, generateStaticAPI, withValidation, };
10
10
  export declare const get: {
11
11
  (givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
12
12
  auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  var _a;
3
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.generateStaticAPI = exports.fetcher = exports.createRPC = exports.createDecorator = exports.createVovkApp = exports.HttpMethod = exports.HttpStatus = exports.HttpException = exports.StreamJSONResponse = void 0;
4
+ exports.initVovk = exports.prefix = exports.options = exports.head = exports.del = exports.patch = exports.put = exports.post = exports.get = exports.withValidation = exports.generateStaticAPI = exports.fetcher = exports.createRPC = exports.createDecorator = exports.createVovkApp = exports.HttpMethod = exports.HttpStatus = exports.HttpException = exports.StreamJSONResponse = void 0;
5
5
  const createVovkApp_1 = require("./createVovkApp");
6
6
  Object.defineProperty(exports, "createVovkApp", { enumerable: true, get: function () { return createVovkApp_1.createVovkApp; } });
7
7
  const types_1 = require("./types");
@@ -18,6 +18,6 @@ const StreamJSONResponse_1 = require("./StreamJSONResponse");
18
18
  Object.defineProperty(exports, "StreamJSONResponse", { enumerable: true, get: function () { return StreamJSONResponse_1.StreamJSONResponse; } });
19
19
  const generateStaticAPI_1 = require("./utils/generateStaticAPI");
20
20
  Object.defineProperty(exports, "generateStaticAPI", { enumerable: true, get: function () { return generateStaticAPI_1.generateStaticAPI; } });
21
- const setHandlerValidation_1 = require("./utils/setHandlerValidation");
22
- Object.defineProperty(exports, "setHandlerValidation", { enumerable: true, get: function () { return setHandlerValidation_1.setHandlerValidation; } });
21
+ const withValidation_1 = require("./utils/withValidation");
22
+ Object.defineProperty(exports, "withValidation", { enumerable: true, get: function () { return withValidation_1.withValidation; } });
23
23
  _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;
package/dist/types.d.ts CHANGED
@@ -10,8 +10,9 @@ export type VovkHandlerSchema = {
10
10
  validation?: {
11
11
  query?: KnownAny;
12
12
  body?: KnownAny;
13
- output?: KnownAny;
14
13
  params?: KnownAny;
14
+ output?: KnownAny;
15
+ iteration?: KnownAny;
15
16
  };
16
17
  openapi?: OperationObject;
17
18
  custom?: Record<string, KnownAny>;
@@ -82,6 +83,9 @@ export type VovkControllerYieldType<T extends (req: VovkRequest<KnownAny, KnownA
82
83
  export type VovkControllerOutput<T extends ((...args: KnownAny) => KnownAny) & {
83
84
  __output?: KnownAny;
84
85
  }> = T['__output'];
86
+ export type VovkControllerIteration<T extends ((...args: KnownAny) => KnownAny) & {
87
+ __iteration?: KnownAny;
88
+ }> = T['__iteration'];
85
89
  export type VovkBody<T extends (...args: KnownAny[]) => unknown> = Parameters<T>[0]['body'];
86
90
  export type VovkQuery<T extends (...args: KnownAny[]) => unknown> = Parameters<T>[0]['query'];
87
91
  export type VovkParams<T extends (...args: KnownAny[]) => unknown> = Parameters<T>[0]['params'];
@@ -150,6 +154,7 @@ export type VovkFullSchema = {
150
154
  config: Partial<VovkStrictConfig>;
151
155
  segments: Record<string, VovkSegmentSchema>;
152
156
  };
157
+ export type VovkValidationType = 'body' | 'query' | 'params' | 'output' | 'iteration';
153
158
  export declare enum HttpMethod {
154
159
  GET = "GET",
155
160
  POST = "POST",
@@ -0,0 +1,4 @@
1
+ import type { KnownAny, VovkController, VovkHandlerSchema } from '../types';
2
+ export declare function setHandlerSchema(h: ((...args: KnownAny[]) => KnownAny) & {
3
+ _getSchema?: (controller: VovkController) => Omit<VovkHandlerSchema, 'httpMethod' | 'path'>;
4
+ }, schema: Omit<VovkHandlerSchema, 'httpMethod' | 'path'>): Promise<void>;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.setHandlerValidation = setHandlerValidation;
4
- async function setHandlerValidation(h, validation) {
5
- h._getValidation = (controller) => {
3
+ exports.setHandlerSchema = setHandlerSchema;
4
+ async function setHandlerSchema(h, schema) {
5
+ h._getSchema = (controller) => {
6
6
  if (!controller) {
7
7
  throw new Error('Error setting client validators. Controller not found. Did you forget to use an HTTP decorator?');
8
8
  }
@@ -10,6 +10,6 @@ async function setHandlerValidation(h, validation) {
10
10
  if (!handlerName) {
11
11
  throw new Error('Error setting client validators. Handler not found.');
12
12
  }
13
- return validation;
13
+ return schema;
14
14
  };
15
15
  }
@@ -0,0 +1,20 @@
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;
@@ -0,0 +1,72 @@
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk",
3
- "version": "3.0.0-draft.93",
3
+ "version": "3.0.0-draft.96",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "description": "RESTful RPC for Next.js - Transforms Next.js into a powerful REST API platform with RPC capabilities.",
@@ -1,4 +0,0 @@
1
- import type { KnownAny, VovkController, VovkHandlerSchema } from '../types';
2
- export declare function setHandlerValidation(h: ((...args: KnownAny[]) => KnownAny) & {
3
- _getValidation?: (controller: VovkController) => void;
4
- }, validation: Exclude<VovkHandlerSchema['validation'], undefined>): Promise<void>;