vovk 3.0.0-beta.21 → 3.0.0-beta.26

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/Segment.js CHANGED
@@ -128,7 +128,7 @@ class _Segment {
128
128
  req.vovk = {
129
129
  body: () => req.json(),
130
130
  query: () => (0, reqQuery_1.default)(req),
131
- meta: (metadata) => (0, reqMeta_1.default)(req, metadata),
131
+ meta: (meta) => (0, reqMeta_1.default)(req, meta),
132
132
  };
133
133
  try {
134
134
  const result = await staticMethod.call(controller, req, methodParams);
@@ -1,4 +1,4 @@
1
- import { type _VovkControllerMetadata as VovkControllerMetadata, type _KnownAny as KnownAny } from '../types';
1
+ import { type _VovkControllerSchema as VovkControllerSchema, type _KnownAny as KnownAny } from '../types';
2
2
  import { type _VovkClientOptions as VovkClientOptions, type _VovkClient as VovkClient, type _VovkDefaultFetcherOptions as VovkDefaultFetcherOptions } from './types';
3
3
  export declare const ARRAY_QUERY_KEY = "_vovkarr";
4
- export declare const _clientizeController: <T, OPTS extends Record<string, KnownAny> = VovkDefaultFetcherOptions>(givenController: VovkControllerMetadata, segmentName?: string, options?: VovkClientOptions<OPTS>) => VovkClient<T, OPTS>;
4
+ export declare const _clientizeController: <T, OPTS extends Record<string, KnownAny> = VovkDefaultFetcherOptions>(givenController: VovkControllerSchema, segmentName?: string, options?: VovkClientOptions<OPTS>) => VovkClient<T, OPTS>;
@@ -38,13 +38,13 @@ const _clientizeController = (givenController, segmentName, options) => {
38
38
  const controller = givenController;
39
39
  const client = {};
40
40
  if (!controller)
41
- throw new Error(`Unable to clientize. Controller metadata is not provided`);
42
- const metadata = controller._handlers;
43
- if (!metadata)
44
- throw new Error(`Unable to clientize. No metadata for controller ${String(controller?._controllerName)}`);
41
+ throw new Error(`Unable to clientize. Controller schema is not provided`);
42
+ const schema = controller._handlers;
43
+ if (!schema)
44
+ throw new Error(`Unable to clientize. No schema for controller ${String(controller?._controllerName)}`);
45
45
  const controllerPrefix = trimPath(controller._prefix ?? '');
46
46
  const { fetcher: settingsFetcher = defaultFetcher_1.default } = options ?? {};
47
- for (const [staticMethodName, { path, httpMethod, clientValidators }] of Object.entries(metadata)) {
47
+ for (const [staticMethodName, { path, httpMethod, clientValidators }] of Object.entries(schema)) {
48
48
  const getEndpoint = ({ prefix, params, query, }) => {
49
49
  const mainPrefix = (prefix.startsWith('http://') || prefix.startsWith('https://') || prefix.startsWith('/') ? '' : '/') +
50
50
  (prefix.endsWith('/') ? prefix : `${prefix}/`) +
@@ -70,7 +70,6 @@ const _clientizeController = (givenController, segmentName, options) => {
70
70
  body: input.body ?? null,
71
71
  query: input.query ?? {},
72
72
  params: input.params ?? {},
73
- segmentName,
74
73
  // TS workaround
75
74
  fetcher: undefined,
76
75
  validateOnClient: undefined,
package/client/types.d.ts CHANGED
@@ -51,7 +51,6 @@ export type _VovkClientFetcher<OPTS extends Record<string, KnownAny> = Record<st
51
51
  httpMethod: HttpMethod;
52
52
  getEndpoint: (data: {
53
53
  prefix: string;
54
- segmentName?: string;
55
54
  params: {
56
55
  [key: string]: string;
57
56
  };
@@ -1,4 +1,4 @@
1
- import type { _HandlerMetadata as HandlerMetadata, _KnownAny as KnownAny, _VovkController as VovkController, _VovkRequest as VovkRequest } from './types';
1
+ import type { _HandlerSchema as HandlerSchema, _KnownAny as KnownAny, _VovkController as VovkController, _VovkRequest as VovkRequest } from './types';
2
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<HandlerMetadata, 'path' | 'httpMethod'> | ((handlerMetadata: HandlerMetadata | null) => Omit<HandlerMetadata, 'path' | 'httpMethod'>) | null | undefined): (...args: ARGS) => (target: KnownAny, propertyKey: string) => void;
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<HandlerSchema, 'path' | 'httpMethod'> | ((handlerSchema: HandlerSchema | null) => Omit<HandlerSchema, 'path' | 'httpMethod'>) | null | undefined): (...args: ARGS) => (target: KnownAny, propertyKey: string) => void;
4
4
  export {};
@@ -10,16 +10,16 @@ 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 handlerMetadata = controller._handlers?.[propertyKey] ?? null;
13
+ const handlerSchema = controller._handlers?.[propertyKey] ?? null;
14
14
  const initResultReturn = initHandler?.call(controller, ...args);
15
- const initResult = typeof initResultReturn === 'function' ? initResultReturn(handlerMetadata) : initResultReturn;
15
+ const initResult = typeof initResultReturn === 'function' ? initResultReturn(handlerSchema) : initResultReturn;
16
16
  controller._handlers = {
17
17
  ...controller._handlers,
18
18
  [propertyKey]: {
19
- ...handlerMetadata,
19
+ ...handlerSchema,
20
20
  // avoid override of path and httpMethod
21
21
  ...(initResult?.clientValidators ? { clientValidators: initResult.clientValidators } : {}),
22
- ...(initResult?.customMetadata ? { customMetadata: initResult.customMetadata } : {}),
22
+ ...(initResult?.customSchema ? { customSchema: initResult.customSchema } : {}),
23
23
  },
24
24
  };
25
25
  const method = function method(req, params) {
@@ -34,7 +34,7 @@ export declare function _createSegment(): {
34
34
  controllers: Record<string, Function>;
35
35
  workers?: Record<string, Function>;
36
36
  exposeValidation?: boolean;
37
- emitMetadata?: boolean;
37
+ emitSchema?: boolean;
38
38
  onError?: (err: Error, req: VovkRequest) => void | Promise<void>;
39
39
  }) => {
40
40
  GET: (req: VovkRequest, data: {
package/createSegment.js CHANGED
@@ -1,9 +1,13 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports._createSegment = _createSegment;
4
7
  /* eslint-disable no-console */
5
8
  const Segment_1 = require("./Segment");
6
9
  const types_1 = require("./types");
10
+ const getSchema_1 = __importDefault(require("./utils/getSchema"));
7
11
  const trimPath = (path) => path.trim().replace(/^\/|\/$/g, '');
8
12
  const isClass = (func) => typeof func === 'function' && /class/.test(func.toString());
9
13
  const toKebabCase = (str) => {
@@ -17,7 +21,7 @@ const toKebabCase = (str) => {
17
21
  function _createSegment() {
18
22
  const segment = new Segment_1._Segment();
19
23
  const getDecoratorCreator = (httpMethod) => {
20
- const assignMetadata = (controller, propertyKey, path, options) => {
24
+ const assignSchema = (controller, propertyKey, path, options) => {
21
25
  if (typeof window !== 'undefined') {
22
26
  throw new Error('Decorators are intended for server-side use only. You have probably imported a controller on the client-side.');
23
27
  }
@@ -47,7 +51,7 @@ function _createSegment() {
47
51
  const path = trimPath(givenPath);
48
52
  function decorator(givenTarget, propertyKey) {
49
53
  const target = givenTarget;
50
- assignMetadata(target, propertyKey, path, options);
54
+ assignSchema(target, propertyKey, path, options);
51
55
  }
52
56
  return decorator;
53
57
  }
@@ -63,7 +67,7 @@ function _createSegment() {
63
67
  httpMethod,
64
68
  },
65
69
  };
66
- assignMetadata(controller, propertyKey, toKebabCase(propertyKey), options);
70
+ assignSchema(controller, propertyKey, toKebabCase(propertyKey), options);
67
71
  }
68
72
  return decorator;
69
73
  };
@@ -80,39 +84,6 @@ function _createSegment() {
80
84
  return givenTarget;
81
85
  };
82
86
  };
83
- const getMetadata = (options) => {
84
- const exposeValidation = options?.exposeValidation ?? true;
85
- const emitMetadata = options.emitMetadata ?? true;
86
- const metadata = {
87
- emitMetadata,
88
- segmentName: options.segmentName ?? '',
89
- controllers: {},
90
- workers: {},
91
- };
92
- if (!emitMetadata)
93
- return metadata;
94
- for (const [controllerName, controller] of Object.entries(options.controllers)) {
95
- metadata.controllers[controllerName] = {
96
- _controllerName: controllerName,
97
- _prefix: controller._prefix ?? '',
98
- _handlers: {
99
- ...(exposeValidation
100
- ? controller._handlers
101
- : Object.fromEntries(Object.entries(controller._handlers).map(([key, value]) => [
102
- key,
103
- { ...value, clientValidators: undefined },
104
- ]))),
105
- },
106
- };
107
- }
108
- for (const [workerName, worker] of Object.entries(options.workers ?? {})) {
109
- metadata.workers[workerName] = {
110
- _workerName: workerName,
111
- _handlers: { ...worker._handlers },
112
- };
113
- }
114
- return metadata;
115
- };
116
87
  const initVovk = (options) => {
117
88
  for (const [controllerName, controller] of Object.entries(options.controllers)) {
118
89
  controller._controllerName = controllerName;
@@ -122,10 +93,10 @@ function _createSegment() {
122
93
  async function GET_DEV(req, data) {
123
94
  const { params } = data;
124
95
  if (params[Object.keys(params)[0]]?.[0] === '_schema_') {
125
- // Wait for metadata to be set (it can be set after decorators are called with another setTimeout)
96
+ // Wait for schema to be set (it can be set after decorators are called with another setTimeout)
126
97
  await new Promise((resolve) => setTimeout(resolve, 10));
127
- const metadata = getMetadata(options);
128
- return segment.respond(200, { metadata });
98
+ const schema = (0, getSchema_1.default)(options);
99
+ return segment.respond(200, { schema });
129
100
  }
130
101
  return segment.GET(req, data);
131
102
  }
package/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import { _createSegment as createSegment } from './createSegment';
2
- import { type _VovkErrorResponse as VovkErrorResponse, type _VovkRequest as VovkRequest, type _VovkControllerBody as VovkControllerBody, type _VovkControllerQuery as VovkControllerQuery, type _VovkControllerParams as VovkControllerParams, type _VovkControllerReturnType as VovkControllerReturnType, type _VovkControlerYieldType as VovkControlerYieldType, type _VovkBody as VovkBody, type _VovkQuery as VovkQuery, type _VovkParams as VovkParams, type _VovkReturnType as VovkReturnType, type _VovkYieldType as VovkYieldType, type _VovkMetadata as VovkMetadata, _HttpStatus as HttpStatus, _HttpMethod as HttpMethod } from './types';
2
+ import { type _VovkErrorResponse as VovkErrorResponse, type _VovkRequest as VovkRequest, type _VovkControllerBody as VovkControllerBody, type _VovkControllerQuery as VovkControllerQuery, type _VovkControllerParams as VovkControllerParams, type _VovkControllerReturnType as VovkControllerReturnType, type _VovkControlerYieldType as VovkControlerYieldType, type _VovkBody as VovkBody, type _VovkQuery as VovkQuery, type _VovkParams as VovkParams, type _VovkReturnType as VovkReturnType, type _VovkYieldType as VovkYieldType, type _VovkSchema as VovkSchema, _HttpStatus as HttpStatus, _HttpMethod as HttpMethod } from './types';
3
3
  import type { _VovkClientOptions as VovkClientOptions, _VovkClientFetcher as VovkClientFetcher, _VovkDefaultFetcherOptions as VovkDefaultFetcherOptions, _VovkValidateOnClient as VovkValidateOnClient } from './client/types';
4
4
  import { _HttpException as HttpException } from './HttpException';
5
5
  import { _createDecorator as createDecorator } from './createDecorator';
6
6
  import { _StreamResponse as StreamResponse } from './StreamResponse';
7
7
  import { worker } from './worker';
8
8
  import { _generateStaticAPI as generateStaticAPI } from './generateStaticAPI';
9
- export { type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkValidateOnClient, type VovkMetadata, type VovkErrorResponse, type VovkRequest, type VovkControllerBody, type VovkControllerQuery, type VovkControllerParams, type VovkControllerReturnType, type VovkControlerYieldType, type VovkBody, type VovkQuery, type VovkParams, type VovkReturnType, type VovkYieldType, type VovkClientOptions, StreamResponse, HttpException, HttpStatus, HttpMethod, createSegment, createDecorator, worker, generateStaticAPI, };
9
+ export { type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkValidateOnClient, type VovkSchema, type VovkErrorResponse, type VovkRequest, type VovkControllerBody, type VovkControllerQuery, type VovkControllerParams, type VovkControllerReturnType, type VovkControlerYieldType, type VovkBody, type VovkQuery, type VovkParams, type VovkReturnType, type VovkYieldType, type VovkClientOptions, StreamResponse, HttpException, HttpStatus, HttpMethod, createSegment, createDecorator, worker, generateStaticAPI, };
10
10
  export declare const get: {
11
11
  (givenPath?: string | undefined, options?: import("./types")._DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types")._DecoratorOptions) => (givenTarget: import("./types")._KnownAny, propertyKey: string) => void>;
12
12
  auto: (options?: import("./types")._DecoratorOptions) => (givenTarget: import("./types")._KnownAny, propertyKey: string) => void;
@@ -33,7 +33,7 @@ export declare const get: {
33
33
  controllers: Record<string, Function>;
34
34
  workers?: Record<string, Function>;
35
35
  exposeValidation?: boolean;
36
- emitMetadata?: boolean;
36
+ emitSchema?: boolean;
37
37
  onError?: (err: Error, req: VovkRequest) => void | Promise<void>;
38
38
  }) => {
39
39
  GET: (req: VovkRequest, data: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk",
3
- "version": "3.0.0-beta.21",
3
+ "version": "3.0.0-beta.26",
4
4
  "description": "RESTful RPC for Next.js - Transforms Next.js into a powerful REST API platform with RPC capabilities.",
5
5
  "repository": {
6
6
  "type": "git",
package/types.d.ts CHANGED
@@ -2,45 +2,47 @@ import type { NextRequest } from 'next/server';
2
2
  import type { _StreamResponse as StreamResponse } from './StreamResponse';
3
3
  import { _StreamAsyncIterator as StreamAsyncIterator } from './client/types';
4
4
  export type _KnownAny = any;
5
- export type _VovkMetadata = {
6
- emitMetadata: boolean;
5
+ export type _VovkSchema = {
6
+ emitSchema: boolean;
7
7
  segmentName: string;
8
- workers: Record<string, _VovkWorkerMetadata>;
9
- controllers: Record<string, _VovkControllerMetadata>;
8
+ workers: Record<string, _VovkWorkerSchema>;
9
+ controllers: Record<string, _VovkControllerSchema>;
10
10
  };
11
11
  export type _VovkErrorResponse = {
12
12
  statusCode: _HttpStatus;
13
13
  message: string;
14
14
  isError: true;
15
15
  };
16
- export type _HandlerMetadata = {
16
+ export type _HandlerSchema = {
17
17
  path: string;
18
18
  httpMethod: _HttpMethod;
19
19
  clientValidators?: {
20
20
  query?: _KnownAny;
21
21
  body?: _KnownAny;
22
22
  };
23
- customMetadata?: Record<string, _KnownAny>;
23
+ customSchema?: Record<string, _KnownAny>;
24
24
  };
25
- export type _VovkControllerMetadata = {
25
+ export type _VovkControllerSchema = {
26
26
  _controllerName: string;
27
+ _originalControllerName: string;
27
28
  _prefix?: string;
28
- _handlers: Record<string, _HandlerMetadata>;
29
+ _handlers: Record<string, _HandlerSchema>;
29
30
  };
30
- export type _VovkWorkerMetadata = {
31
+ export type _VovkWorkerSchema = {
31
32
  _workerName: string;
33
+ _originalWorkerName: string;
32
34
  _handlers: Record<string, {
33
35
  isGenerator?: true;
34
36
  }>;
35
37
  };
36
- export type _VovkControllerInternal = _VovkControllerMetadata & {
38
+ export type _VovkControllerInternal = _VovkControllerSchema & {
37
39
  _activated?: true;
38
40
  _onError?: (err: Error, req: _VovkRequest) => void | Promise<void>;
39
41
  };
40
42
  export type _VovkController = Function & _VovkControllerInternal & {
41
43
  [key: string]: unknown;
42
44
  };
43
- export type _VovkWorker = Function & _VovkWorkerMetadata & {
45
+ export type _VovkWorker = Function & _VovkWorkerSchema & {
44
46
  [key: string]: unknown;
45
47
  };
46
48
  export type _DecoratorOptions = {
@@ -66,7 +68,7 @@ export interface _VovkRequest<BODY = undefined, QUERY extends object | undefined
66
68
  vovk: {
67
69
  body: () => Promise<BODY>;
68
70
  query: () => QUERY;
69
- meta: <T = Record<_KnownAny, _KnownAny>>(metadata?: T | null) => T;
71
+ meta: <T = Record<_KnownAny, _KnownAny>>(meta?: T | null) => T;
70
72
  };
71
73
  }
72
74
  export type _ControllerStaticMethod<REQ extends _VovkRequest<undefined, _KnownAny> = _VovkRequest<undefined, Record<string, string | string[]>>, PARAMS extends {
@@ -0,0 +1,8 @@
1
+ import { _VovkSchema as VovkSchema } from '../types';
2
+ export default function (options: {
3
+ emitSchema?: boolean;
4
+ segmentName?: string;
5
+ controllers: Record<string, Function>;
6
+ workers?: Record<string, Function>;
7
+ exposeValidation?: boolean;
8
+ }): VovkSchema;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = default_1;
4
+ function default_1(options) {
5
+ const exposeValidation = options?.exposeValidation ?? true;
6
+ const emitSchema = options.emitSchema ?? true;
7
+ const schema = {
8
+ emitSchema,
9
+ segmentName: options.segmentName ?? '',
10
+ controllers: {},
11
+ workers: {},
12
+ };
13
+ if (!emitSchema)
14
+ return schema;
15
+ for (const [controllerName, controller] of Object.entries(options.controllers)) {
16
+ schema.controllers[controllerName] = {
17
+ _controllerName: controllerName,
18
+ _originalControllerName: controller.name,
19
+ _prefix: controller._prefix ?? '',
20
+ _handlers: {
21
+ ...(exposeValidation
22
+ ? controller._handlers
23
+ : Object.fromEntries(Object.entries(controller._handlers).map(([key, value]) => [
24
+ key,
25
+ { ...value, clientValidators: undefined },
26
+ ]))),
27
+ },
28
+ };
29
+ }
30
+ for (const [workerName, worker] of Object.entries(options.workers ?? {})) {
31
+ schema.workers[workerName] = {
32
+ _workerName: workerName,
33
+ _originalWorkerName: worker.name,
34
+ _handlers: { ...worker._handlers },
35
+ };
36
+ }
37
+ return schema;
38
+ }
@@ -1,3 +1,3 @@
1
1
  import { VovkRequest } from 'vovk';
2
2
  import { _KnownAny as KnownAny } from '../types';
3
- export default function reqMeta<T = Record<KnownAny, KnownAny>>(req: VovkRequest<KnownAny, KnownAny>, metadata?: T | null): T;
3
+ export default function reqMeta<T = Record<KnownAny, KnownAny>>(req: VovkRequest<KnownAny, KnownAny>, meta?: T | null): T;
package/utils/reqMeta.js CHANGED
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = reqMeta;
4
- const metadataMap = new WeakMap();
5
- function reqMeta(req, metadata) {
6
- if (metadata) {
7
- metadataMap.set(req, { ...metadataMap.get(req), ...metadata });
4
+ const metaMap = new WeakMap();
5
+ function reqMeta(req, meta) {
6
+ if (meta) {
7
+ metaMap.set(req, { ...metaMap.get(req), ...meta });
8
8
  }
9
- else if (metadata === null) {
10
- metadataMap.delete(req);
9
+ else if (meta === null) {
10
+ metaMap.delete(req);
11
11
  }
12
- return (metadataMap.get(req) ?? {});
12
+ return (metaMap.get(req) ?? {});
13
13
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._promisifyWorker = _promisifyWorker;
4
4
  function _promisifyWorker(currentWorker, givenWorkerService) {
5
5
  if (!givenWorkerService)
6
- throw new Error('Worker metadata is not provided');
6
+ throw new Error('Worker schema is not provided');
7
7
  const workerService = givenWorkerService;
8
8
  const instance = {
9
9
  worker: currentWorker,
package/worker/worker.js CHANGED
@@ -5,7 +5,7 @@ function _worker() {
5
5
  return (t) => {
6
6
  const target = t;
7
7
  target._handlers = {};
8
- // Experimental: You can pass Worker Service instead of metadata to prommisify worker
8
+ // TODO: Experimental: You can pass Worker Service instead of schema to prommisify worker
9
9
  for (const key of Object.getOwnPropertyNames(target)) {
10
10
  const member = target[key];
11
11
  if (typeof member === 'function') {