vovk 3.0.0-draft.20 → 3.0.0-draft.21

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.
@@ -1,4 +1,4 @@
1
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: VovkControllerSchema, segmentName?: string, options?: VovkClientOptions<OPTS>) => VovkClient<T, OPTS>;
4
+ export declare const _clientizeController: <T, OPTS extends Record<string, KnownAny> = VovkDefaultFetcherOptions>(controllerSchema: VovkControllerSchema, segmentName?: string, options?: VovkClientOptions<OPTS>) => VovkClient<T, OPTS>;
@@ -36,17 +36,16 @@ const getHandlerPath = (endpoint, params, query) => {
36
36
  }
37
37
  return `${result}${hasQuery ? '?' : ''}${searchParams.toString()}`;
38
38
  };
39
- const _clientizeController = (givenController, segmentName, options) => {
40
- const controller = givenController;
39
+ const _clientizeController = (controllerSchema, segmentName, options) => {
40
+ const schema = controllerSchema;
41
41
  const client = {};
42
- if (!controller)
43
- throw new Error(`Unable to clientize. Controller schema is not provided`);
44
- const schema = controller._handlers;
45
42
  if (!schema)
46
- throw new Error(`Unable to clientize. No schema for controller ${String(controller?._controllerName)}`);
47
- const controllerPrefix = trimPath(controller._prefix ?? '');
43
+ throw new Error(`Unable to clientize. Controller schema is not provided`);
44
+ if (!schema.handlers)
45
+ throw new Error(`Unable to clientize. No schema for controller ${String(schema?.controllerName)}`);
46
+ const controllerPrefix = trimPath(schema.prefix ?? '');
48
47
  const { fetcher: settingsFetcher = defaultFetcher_1.default } = options ?? {};
49
- for (const [staticMethodName, { path, httpMethod, clientValidators }] of Object.entries(schema)) {
48
+ for (const [staticMethodName, { path, httpMethod, validation }] of Object.entries(schema.handlers)) {
50
49
  const getEndpoint = ({ apiRoot, params, query, }) => {
51
50
  const mainPrefix = (apiRoot.startsWith('http://') || apiRoot.startsWith('https://') || apiRoot.startsWith('/') ? '' : '/') +
52
51
  (apiRoot.endsWith('/') ? apiRoot : `${apiRoot}/`) +
@@ -56,7 +55,7 @@ const _clientizeController = (givenController, segmentName, options) => {
56
55
  const handler = (input = {}) => {
57
56
  const fetcher = input.fetcher ?? settingsFetcher;
58
57
  const validate = async ({ body, query, endpoint }) => {
59
- await (input.validateOnClient ?? options?.validateOnClient)?.({ body, query, endpoint }, clientValidators ?? {});
58
+ await (input.validateOnClient ?? options?.validateOnClient)?.({ body, query, endpoint }, validation ?? {});
60
59
  };
61
60
  const internalOptions = {
62
61
  name: staticMethodName,
@@ -18,8 +18,8 @@ function _createDecorator(handler, initHandler) {
18
18
  [propertyKey]: {
19
19
  ...handlerSchema,
20
20
  // avoid override of path and httpMethod
21
- ...(initResult?.clientValidators ? { clientValidators: initResult.clientValidators } : {}),
22
- ...(initResult?.customSchema ? { customSchema: initResult.customSchema } : {}),
21
+ ...(initResult?.validation ? { validation: initResult.validation } : {}),
22
+ ...(initResult?.custom ? { custom: initResult.custom } : {}),
23
23
  },
24
24
  };
25
25
  const method = function method(req, params) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk",
3
- "version": "3.0.0-draft.20",
3
+ "version": "3.0.0-draft.21",
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
@@ -17,33 +17,37 @@ export type _VovkErrorResponse = {
17
17
  export type _HandlerSchema = {
18
18
  path: string;
19
19
  httpMethod: _HttpMethod;
20
- clientValidators?: {
20
+ validation?: {
21
21
  query?: _KnownAny;
22
22
  body?: _KnownAny;
23
23
  };
24
- customSchema?: Record<string, _KnownAny>;
24
+ custom?: Record<string, _KnownAny>;
25
25
  };
26
26
  export type _VovkControllerSchema = {
27
- _controllerName: string;
28
- _originalControllerName: string;
29
- _prefix?: string;
30
- _handlers: Record<string, _HandlerSchema>;
27
+ controllerName: string;
28
+ originalControllerName: string;
29
+ prefix?: string;
30
+ handlers: Record<string, _HandlerSchema>;
31
31
  };
32
32
  export type _VovkWorkerSchema = {
33
- _workerName: string;
34
- _originalWorkerName: string;
35
- _handlers: Record<string, {
33
+ workerName: string;
34
+ originalWorkerName: string;
35
+ handlers: Record<string, {
36
36
  isGenerator?: true;
37
37
  }>;
38
38
  };
39
- export type _VovkControllerInternal = _VovkControllerSchema & {
39
+ export type _VovkControllerInternal = {
40
+ _controllerName?: _VovkControllerSchema['controllerName'];
41
+ _prefix?: _VovkControllerSchema['prefix'];
42
+ _handlers: _VovkControllerSchema['handlers'];
40
43
  _activated?: true;
41
44
  _onError?: (err: Error, req: _VovkRequest) => void | Promise<void>;
42
45
  };
43
46
  export type _VovkController = _StaticClass & _VovkControllerInternal & {
44
47
  [key: string]: unknown;
45
48
  };
46
- export type _VovkWorker = _StaticClass & _VovkWorkerSchema & {
49
+ export type _VovkWorker = _StaticClass & {
50
+ _handlers: _VovkWorkerSchema['handlers'];
47
51
  [key: string]: unknown;
48
52
  };
49
53
  export type _DecoratorOptions = {
@@ -14,24 +14,24 @@ function getSchema(options) {
14
14
  return schema;
15
15
  for (const [controllerName, controller] of Object.entries(options.controllers)) {
16
16
  schema.controllers[controllerName] = {
17
- _controllerName: controllerName,
18
- _originalControllerName: controller.name,
19
- _prefix: controller._prefix ?? '',
20
- _handlers: {
17
+ controllerName: controllerName,
18
+ originalControllerName: controller.name,
19
+ prefix: controller._prefix ?? '',
20
+ handlers: {
21
21
  ...(exposeValidation
22
22
  ? controller._handlers
23
23
  : Object.fromEntries(Object.entries(controller._handlers).map(([key, value]) => [
24
24
  key,
25
- { ...value, clientValidators: undefined },
25
+ { ...value, validation: undefined },
26
26
  ]))),
27
27
  },
28
28
  };
29
29
  }
30
30
  for (const [workerName, worker] of Object.entries(options.workers ?? {})) {
31
31
  schema.workers[workerName] = {
32
- _workerName: workerName,
33
- _originalWorkerName: worker.name,
34
- _handlers: { ...worker._handlers },
32
+ workerName,
33
+ originalWorkerName: worker.name,
34
+ handlers: { ...worker._handlers },
35
35
  };
36
36
  }
37
37
  return schema;
@@ -16,7 +16,7 @@ function setClientValidatorsForHandler(h, validators) {
16
16
  ...controller._handlers,
17
17
  [handlerName]: {
18
18
  ...controller._handlers[handlerName],
19
- clientValidators: {
19
+ validation: {
20
20
  body: validators.body,
21
21
  query: validators.query,
22
22
  },
@@ -1,2 +1,2 @@
1
1
  import type { _WorkerPromiseInstance as WorkerPromiseInstance } from './types';
2
- export declare function _promisifyWorker<T extends object>(currentWorker: Worker | null, givenWorkerService: object): WorkerPromiseInstance<T>;
2
+ export declare function _promisifyWorker<T extends object>(currentWorker: Worker | null, workerSchema: object): WorkerPromiseInstance<T>;
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._promisifyWorker = _promisifyWorker;
4
- function _promisifyWorker(currentWorker, givenWorkerService) {
5
- if (!givenWorkerService)
4
+ function _promisifyWorker(currentWorker, workerSchema) {
5
+ if (!workerSchema)
6
6
  throw new Error('Worker schema is not provided');
7
- const workerService = givenWorkerService;
7
+ const schema = workerSchema;
8
8
  const instance = {
9
9
  worker: currentWorker,
10
10
  };
@@ -23,11 +23,9 @@ function _promisifyWorker(currentWorker, givenWorkerService) {
23
23
  instance.worker = worker;
24
24
  return instance;
25
25
  };
26
- instance.fork = (worker) => {
27
- return _promisifyWorker(worker, givenWorkerService);
28
- };
29
- for (const methodName of Object.keys(workerService._handlers)) {
30
- const { isGenerator } = workerService._handlers[methodName];
26
+ instance.fork = (worker) => _promisifyWorker(worker, schema);
27
+ for (const methodName of Object.keys(schema.handlers)) {
28
+ const { isGenerator } = schema.handlers[methodName];
31
29
  if (isGenerator) {
32
30
  const method = (...args) => {
33
31
  const key = callsKey;
package/worker/worker.js CHANGED
@@ -5,7 +5,6 @@ function _worker() {
5
5
  return (t) => {
6
6
  const target = t;
7
7
  target._handlers = {};
8
- // TODO: Experimental: You can pass Worker Service instead of schema to prommisify worker
9
8
  for (const key of Object.getOwnPropertyNames(target)) {
10
9
  const member = target[key];
11
10
  if (typeof member === 'function') {