vovk 3.0.0-draft.92 → 3.0.0-draft.95

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/VovkApp.js CHANGED
@@ -177,7 +177,7 @@ class VovkApp {
177
177
  console.error(onErrorError);
178
178
  }
179
179
  if (err.message !== 'NEXT_REDIRECT' && err.message !== 'NEXT_NOT_FOUND') {
180
- const statusCode = err.statusCode ?? types_1.HttpStatus.INTERNAL_SERVER_ERROR;
180
+ const statusCode = err.statusCode || types_1.HttpStatus.INTERNAL_SERVER_ERROR;
181
181
  return this.#respondWithError(statusCode, err.message, staticMethod._options, err.cause);
182
182
  }
183
183
  throw e; // if NEXT_REDIRECT or NEXT_NOT_FOUND, rethrow it
@@ -1,4 +1,3 @@
1
1
  import type { KnownAny, VovkFullSchema } from '../types';
2
2
  import type { VovkClientOptions, VovkClient, VovkDefaultFetcherOptions } from './types';
3
- declare const createRPC: <T, OPTS extends Record<string, KnownAny> = VovkDefaultFetcherOptions>(fullSchema: VovkFullSchema, segmentName: string, controllerName: string, options?: VovkClientOptions<OPTS>) => VovkClient<T, OPTS>;
4
- export default createRPC;
3
+ export declare const createRPC: <T, OPTS extends Record<string, KnownAny> = VovkDefaultFetcherOptions>(fullSchema: VovkFullSchema, segmentName: string, controllerName: string, options?: VovkClientOptions<OPTS>) => VovkClient<T, OPTS>;
@@ -3,7 +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
- const defaultFetcher_1 = __importDefault(require("./defaultFetcher"));
6
+ exports.createRPC = void 0;
7
+ const fetcher_1 = require("./fetcher");
7
8
  const defaultHandler_1 = require("./defaultHandler");
8
9
  const defaultStreamHandler_1 = require("./defaultStreamHandler");
9
10
  const serializeQuery_1 = __importDefault(require("../utils/serializeQuery"));
@@ -25,7 +26,7 @@ const createRPC = (fullSchema, segmentName, controllerName, options) => {
25
26
  if (!controllerSchema)
26
27
  throw new Error(`Unable to create RPC object. Controller schema is missing. Check client template.`);
27
28
  const controllerPrefix = trimPath(controllerSchema.prefix ?? '');
28
- const { fetcher: settingsFetcher = defaultFetcher_1.default } = options ?? {};
29
+ const { fetcher: settingsFetcher = fetcher_1.fetcher } = options ?? {};
29
30
  for (const [staticMethodName, handlerSchema] of Object.entries(controllerSchema.handlers)) {
30
31
  const { path, httpMethod, validation } = handlerSchema;
31
32
  const getEndpoint = ({ apiRoot, params, query, }) => {
@@ -81,4 +82,4 @@ const createRPC = (fullSchema, segmentName, controllerName, options) => {
81
82
  }
82
83
  return client;
83
84
  };
84
- exports.default = createRPC;
85
+ exports.createRPC = createRPC;
@@ -1,4 +1,3 @@
1
1
  import type { VovkDefaultFetcherOptions, VovkClientFetcher } from './types';
2
2
  export declare const DEFAULT_ERROR_MESSAGE = "Unknown error at the defaultFetcher";
3
- declare const defaultFetcher: VovkClientFetcher<VovkDefaultFetcherOptions>;
4
- export default defaultFetcher;
3
+ export declare const fetcher: VovkClientFetcher<VovkDefaultFetcherOptions>;
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_ERROR_MESSAGE = void 0;
3
+ exports.fetcher = exports.DEFAULT_ERROR_MESSAGE = void 0;
4
4
  const types_1 = require("../types");
5
5
  const HttpException_1 = require("../HttpException");
6
6
  exports.DEFAULT_ERROR_MESSAGE = 'Unknown error at the defaultFetcher';
7
7
  // defaultFetcher uses HttpException class to throw errors of fake HTTP status 0 if client-side error occurs
8
8
  // For normal HTTP errors, it uses message and status code from the response of VovkErrorResponse type
9
- const defaultFetcher = async ({ httpMethod, getEndpoint, validate, defaultHandler, defaultStreamHandler }, { params, query, body, apiRoot = '/api', ...options }) => {
9
+ const fetcher = async ({ httpMethod, getEndpoint, validate, defaultHandler, defaultStreamHandler }, { params, query, body, apiRoot = '/api', ...options }) => {
10
10
  const endpoint = getEndpoint({ apiRoot, params, query });
11
11
  if (!options.disableClientValidation) {
12
12
  try {
@@ -57,4 +57,4 @@ const defaultFetcher = async ({ httpMethod, getEndpoint, validate, defaultHandle
57
57
  }
58
58
  return response;
59
59
  };
60
- exports.default = defaultFetcher;
60
+ exports.fetcher = fetcher;
@@ -1,2 +1,3 @@
1
- export { default as createRPC } from './createRPC';
1
+ export { createRPC } from './createRPC';
2
+ export { fetcher } from './fetcher';
2
3
  export type { VovkClient, VovkClientFetcher, VovkClientOptions, VovkDefaultFetcherOptions, VovkValidateOnClient, VovkStreamAsyncIterable, } from './types';
@@ -1,8 +1,7 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createRPC = void 0;
3
+ exports.fetcher = exports.createRPC = void 0;
7
4
  var createRPC_1 = require("./createRPC");
8
- Object.defineProperty(exports, "createRPC", { enumerable: true, get: function () { return __importDefault(createRPC_1).default; } });
5
+ Object.defineProperty(exports, "createRPC", { enumerable: true, get: function () { return createRPC_1.createRPC; } });
6
+ var fetcher_1 = require("./fetcher");
7
+ Object.defineProperty(exports, "fetcher", { enumerable: true, get: function () { return fetcher_1.fetcher; } });
@@ -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
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';
3
- import { type VovkClient, type VovkClientOptions, type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkValidateOnClient, type VovkStreamAsyncIterable, createRPC } from './client';
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, 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, 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.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");
@@ -9,6 +9,7 @@ Object.defineProperty(exports, "HttpStatus", { enumerable: true, get: function (
9
9
  Object.defineProperty(exports, "HttpMethod", { enumerable: true, get: function () { return types_1.HttpMethod; } });
10
10
  const client_1 = require("./client");
11
11
  Object.defineProperty(exports, "createRPC", { enumerable: true, get: function () { return client_1.createRPC; } });
12
+ Object.defineProperty(exports, "fetcher", { enumerable: true, get: function () { return client_1.fetcher; } });
12
13
  const HttpException_1 = require("./HttpException");
13
14
  Object.defineProperty(exports, "HttpException", { enumerable: true, get: function () { return HttpException_1.HttpException; } });
14
15
  const createDecorator_1 = require("./createDecorator");
@@ -17,6 +18,6 @@ const StreamJSONResponse_1 = require("./StreamJSONResponse");
17
18
  Object.defineProperty(exports, "StreamJSONResponse", { enumerable: true, get: function () { return StreamJSONResponse_1.StreamJSONResponse; } });
18
19
  const generateStaticAPI_1 = require("./utils/generateStaticAPI");
19
20
  Object.defineProperty(exports, "generateStaticAPI", { enumerable: true, get: function () { return generateStaticAPI_1.generateStaticAPI; } });
20
- const setHandlerValidation_1 = require("./utils/setHandlerValidation");
21
- 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; } });
22
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;
@@ -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,13 @@
1
+ import { VovkHandlerSchema, 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>({ body, query, params, output, handle, getHandlerSchema, validate, }: {
3
+ body?: BODY_MODEL;
4
+ query?: QUERY_MODEL;
5
+ params?: PARAMS_MODEL;
6
+ output?: OUTPUT_MODEL;
7
+ handle: T;
8
+ getHandlerSchema?: () => Omit<VovkHandlerSchema, 'httpMethod' | 'path'>;
9
+ validate: (data: KnownAny, model: NonNullable<BODY_MODEL | QUERY_MODEL | PARAMS_MODEL | OUTPUT_MODEL>, meta: {
10
+ type: 'body' | 'query' | 'params' | 'output';
11
+ req: VovkRequest<KnownAny, KnownAny>;
12
+ }) => KnownAny;
13
+ }): T;
@@ -0,0 +1,42 @@
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
+ function withValidation({ body, query, params, output, handle, getHandlerSchema, validate, }) {
8
+ const outputHandler = async (req, handlerParams) => {
9
+ const data = await handle(req, handlerParams);
10
+ if (output) {
11
+ if (!data) {
12
+ throw new HttpException_1.HttpException(types_1.HttpStatus.INTERNAL_SERVER_ERROR, 'Output is required. You probably forgot to return something from your handler.');
13
+ }
14
+ await validate(data, output, { type: 'output', req });
15
+ }
16
+ return data;
17
+ };
18
+ const resultHandler = async (req, handlerParams) => {
19
+ if (body) {
20
+ const data = await req.json();
21
+ const instance = (await validate(data, body, { type: 'body', req })) ?? data;
22
+ // redeclare to add ability to call req.json() again
23
+ req.json = () => Promise.resolve(data);
24
+ req.vovk.body = () => Promise.resolve(instance);
25
+ }
26
+ if (query) {
27
+ const data = req.vovk.query();
28
+ const instance = (await validate(data, query, { type: 'query', req })) ?? data;
29
+ req.vovk.query = () => instance;
30
+ }
31
+ if (params) {
32
+ const data = req.vovk.params();
33
+ const instance = (await validate(data, params, { type: 'params', req })) ?? data;
34
+ req.vovk.params = () => instance;
35
+ }
36
+ return outputHandler(req, handlerParams);
37
+ };
38
+ if (getHandlerSchema) {
39
+ (0, setHandlerSchema_1.setHandlerSchema)(resultHandler, getHandlerSchema());
40
+ }
41
+ return resultHandler;
42
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk",
3
- "version": "3.0.0-draft.92",
3
+ "version": "3.0.0-draft.95",
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>;