vovk 3.0.0-draft.3 → 3.0.0-draft.5

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/client/types.d.ts CHANGED
@@ -41,7 +41,9 @@ type OmitNever<T> = {
41
41
  type _VovkClientWithNever<T, OPTS extends {
42
42
  [key: string]: KnownAny;
43
43
  }> = {
44
- [K in keyof T]: T[K] extends (...args: KnownAny) => KnownAny ? ClientMethod<T[K], OPTS> : never;
44
+ [K in keyof T]: T[K] extends (...args: KnownAny) => KnownAny ? ClientMethod<T[K], OPTS> & {
45
+ __isClientMethod: boolean;
46
+ } : never;
45
47
  };
46
48
  export type _VovkClient<T, OPTS extends {
47
49
  [key: string]: KnownAny;
@@ -1,4 +1,4 @@
1
- import { type _KnownAny as KnownAny, type _DecoratorOptions as DecoratorOptions, type _VovkRequest as VovkRequest } from './types';
1
+ import { type _KnownAny as KnownAny, type _DecoratorOptions as DecoratorOptions, type _VovkRequest as VovkRequest, type _StaticClass as StaticClass } from './types';
2
2
  export declare function _createSegment(): {
3
3
  get: {
4
4
  (givenPath?: string | undefined, options?: DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
@@ -31,8 +31,8 @@ export declare function _createSegment(): {
31
31
  prefix: (givenPath?: string) => (givenTarget: KnownAny) => any;
32
32
  initVovk: (options: {
33
33
  segmentName?: string;
34
- controllers: Record<string, Function>;
35
- workers?: Record<string, Function>;
34
+ controllers: Record<string, StaticClass>;
35
+ workers?: Record<string, StaticClass>;
36
36
  exposeValidation?: boolean;
37
37
  emitSchema?: boolean;
38
38
  onError?: (err: Error, req: VovkRequest) => void | Promise<void>;
package/createSegment.js CHANGED
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports._createSegment = _createSegment;
7
- /* eslint-disable no-console */
8
7
  const Segment_1 = require("./Segment");
9
8
  const types_1 = require("./types");
10
9
  const getSchema_1 = __importDefault(require("./utils/getSchema"));
@@ -76,7 +75,6 @@ function _createSegment() {
76
75
  return (givenTarget) => {
77
76
  const controller = givenTarget;
78
77
  controller._prefix = path;
79
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
80
78
  return givenTarget;
81
79
  };
82
80
  };
@@ -1,3 +1,4 @@
1
- export declare function _generateStaticAPI(c: Record<string, Function>, slug?: string): {
1
+ import { _StaticClass as StaticClass } from './types';
2
+ export declare function _generateStaticAPI(c: Record<string, StaticClass>, slug?: string): {
2
3
  [x: string]: string[];
3
4
  }[];
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._generateStaticAPI = _generateStaticAPI;
4
- // eslint-disable-next-line @typescript-eslint/ban-types
5
4
  function _generateStaticAPI(c, slug = 'vovk') {
6
5
  const controllers = c;
7
6
  return [
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 _VovkSchema as VovkSchema, _HttpStatus as HttpStatus, _HttpMethod as HttpMethod } from './types';
2
+ import { type _VovkErrorResponse as VovkErrorResponse, type _VovkRequest as VovkRequest, 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 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, };
9
+ export { type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkValidateOnClient, type VovkSchema, type VovkErrorResponse, type VovkRequest, 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;
@@ -30,8 +30,8 @@ export declare const get: {
30
30
  auto: (options?: import("./types")._DecoratorOptions) => (givenTarget: import("./types")._KnownAny, propertyKey: string) => void;
31
31
  }, prefix: (givenPath?: string) => (givenTarget: import("./types")._KnownAny) => any, initVovk: (options: {
32
32
  segmentName?: string;
33
- controllers: Record<string, Function>;
34
- workers?: Record<string, Function>;
33
+ controllers: Record<string, import("./types")._StaticClass>;
34
+ workers?: Record<string, import("./types")._StaticClass>;
35
35
  exposeValidation?: boolean;
36
36
  emitSchema?: boolean;
37
37
  onError?: (err: Error, req: VovkRequest) => void | Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk",
3
- "version": "3.0.0-draft.3",
3
+ "version": "3.0.0-draft.5",
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,6 +2,7 @@ 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 _StaticClass = Function;
5
6
  export type _VovkSchema = {
6
7
  emitSchema: boolean;
7
8
  segmentName: string;
@@ -39,10 +40,10 @@ export type _VovkControllerInternal = _VovkControllerSchema & {
39
40
  _activated?: true;
40
41
  _onError?: (err: Error, req: _VovkRequest) => void | Promise<void>;
41
42
  };
42
- export type _VovkController = Function & _VovkControllerInternal & {
43
+ export type _VovkController = _StaticClass & _VovkControllerInternal & {
43
44
  [key: string]: unknown;
44
45
  };
45
- export type _VovkWorker = Function & _VovkWorkerSchema & {
46
+ export type _VovkWorker = _StaticClass & _VovkWorkerSchema & {
46
47
  [key: string]: unknown;
47
48
  };
48
49
  export type _DecoratorOptions = {
@@ -85,26 +86,44 @@ export type _VovkControllerQuery<T extends _ControllerStaticMethod<REQ, PARAMS>,
85
86
  export type _VovkControllerParams<T extends _ControllerStaticMethod<REQ, PARAMS>, REQ extends _VovkRequest<undefined, _KnownAny> = Parameters<T>[0], PARAMS extends {
86
87
  [key: string]: string;
87
88
  } = _KnownAny> = Parameters<T>[1];
88
- export type _VovkBody<T extends (options: OPTIONS) => _KnownAny, OPTIONS extends {
89
+ export type _VovkControllerReturnType<T extends _ControllerStaticMethod<REQ, PARAMS>, REQ extends _VovkRequest<undefined, _KnownAny> = Parameters<T>[0], PARAMS extends {
90
+ [key: string]: string;
91
+ } = _KnownAny> = Awaited<ReturnType<T>>;
92
+ export type _VovkControlerYieldType<T extends _ControllerStaticMethod<REQ, PARAMS>, REQ extends _VovkRequest<undefined, _KnownAny> = Parameters<T>[0], PARAMS extends {
93
+ [key: string]: string;
94
+ } = _KnownAny> = T extends (...args: _KnownAny[]) => AsyncGenerator<infer Y, _KnownAny, _KnownAny> ? Y : T extends (...args: _KnownAny[]) => Generator<infer Y, _KnownAny, _KnownAny> ? Y : T extends (...args: _KnownAny[]) => Promise<StreamResponse<infer Y>> | StreamResponse<infer Y> ? Y : never;
95
+ export type _VovkClientBody<T extends (options: OPTIONS) => _KnownAny, OPTIONS extends {
89
96
  body: B;
90
97
  [key: string]: _KnownAny;
91
98
  } = Parameters<T>[0], B = _KnownAny> = Parameters<T>[0]['body'];
92
- export type _VovkQuery<T extends (options: OPTIONS) => _KnownAny, OPTIONS extends {
99
+ export type _VovkClientQuery<T extends (options: OPTIONS) => _KnownAny, OPTIONS extends {
93
100
  query: Q;
94
101
  [key: string]: _KnownAny;
95
102
  } = Parameters<T>[0], Q = _KnownAny> = Parameters<T>[0]['query'];
96
- export type _VovkParams<T extends (options: OPTIONS) => _KnownAny, OPTIONS extends {
103
+ export type _VovkClientParams<T extends (options: OPTIONS) => _KnownAny, OPTIONS extends {
97
104
  params: P;
98
105
  [key: string]: _KnownAny;
99
106
  } = Parameters<T>[0], P = _KnownAny> = Parameters<T>[0]['params'];
100
- export type _VovkControllerReturnType<T extends _ControllerStaticMethod<REQ, PARAMS>, REQ extends _VovkRequest<undefined, _KnownAny> = Parameters<T>[0], PARAMS extends {
107
+ export type _VovkClientYieldType<T extends (...args: _KnownAny[]) => unknown> = T extends (...args: _KnownAny[]) => Promise<StreamAsyncIterator<infer Y>> ? Y : never;
108
+ export type _VovkClientReturnType<T extends (...args: _KnownAny) => unknown> = Awaited<ReturnType<T>>;
109
+ type ClientStaticMethod = ((...args: _KnownAny[]) => _KnownAny) & {
110
+ __isClientMethod: boolean;
111
+ };
112
+ export type _VovkBody<T extends ClientStaticMethod | _ControllerStaticMethod<REQ, PARAMS>, REQ extends _VovkRequest<undefined, _KnownAny> = Parameters<T>[0], PARAMS extends {
101
113
  [key: string]: string;
102
- } = _KnownAny> = Awaited<ReturnType<T>>;
103
- export type _VovkControlerYieldType<T extends _ControllerStaticMethod<REQ, PARAMS>, REQ extends _VovkRequest<undefined, _KnownAny> = Parameters<T>[0], PARAMS extends {
114
+ } = _KnownAny> = T extends ClientStaticMethod ? _VovkClientBody<T> : T extends _ControllerStaticMethod<REQ, PARAMS> ? _VovkControllerBody<T> : never;
115
+ export type _VovkQuery<T extends ClientStaticMethod | _ControllerStaticMethod<REQ, PARAMS>, REQ extends _VovkRequest<undefined, _KnownAny> = Parameters<T>[0], PARAMS extends {
104
116
  [key: string]: string;
105
- } = _KnownAny> = T extends (...args: _KnownAny[]) => AsyncGenerator<infer Y, _KnownAny, _KnownAny> ? Y : T extends (...args: _KnownAny[]) => Generator<infer Y, _KnownAny, _KnownAny> ? Y : T extends (...args: _KnownAny[]) => Promise<StreamResponse<infer Y>> | StreamResponse<infer Y> ? Y : never;
106
- export type _VovkReturnType<T extends (...args: _KnownAny) => unknown> = Awaited<ReturnType<T>>;
107
- export type _VovkYieldType<T extends (...args: _KnownAny[]) => unknown> = T extends (...args: _KnownAny[]) => Promise<StreamAsyncIterator<infer Y>> ? Y : never;
117
+ } = _KnownAny> = T extends ClientStaticMethod ? _VovkClientQuery<T> : T extends _ControllerStaticMethod<REQ, PARAMS> ? _VovkControllerQuery<T> : never;
118
+ export type _VovkParams<T extends ClientStaticMethod | _ControllerStaticMethod<REQ, PARAMS>, REQ extends _VovkRequest<undefined, _KnownAny> = Parameters<T>[0], PARAMS extends {
119
+ [key: string]: string;
120
+ } = _KnownAny> = T extends ClientStaticMethod ? _VovkClientParams<T> : T extends _ControllerStaticMethod<REQ, PARAMS> ? _VovkControllerParams<T> : never;
121
+ export type _VovkYieldType<T extends ClientStaticMethod | _ControllerStaticMethod<REQ, PARAMS>, REQ extends _VovkRequest<undefined, _KnownAny> = Parameters<T>[0], PARAMS extends {
122
+ [key: string]: string;
123
+ } = _KnownAny> = T extends ClientStaticMethod ? _VovkClientYieldType<T> : T extends _ControllerStaticMethod<REQ, PARAMS> ? _VovkControlerYieldType<T> : never;
124
+ export type _VovkReturnType<T extends ClientStaticMethod | _ControllerStaticMethod<REQ, PARAMS>, REQ extends _VovkRequest<undefined, _KnownAny> = Parameters<T>[0], PARAMS extends {
125
+ [key: string]: string;
126
+ } = _KnownAny> = T extends ClientStaticMethod ? _VovkClientReturnType<T> : T extends _ControllerStaticMethod<REQ, PARAMS> ? _VovkControllerReturnType<T> : never;
108
127
  export type _StreamAbortMessage = {
109
128
  isError: true;
110
129
  reason: _KnownAny;
@@ -169,3 +188,4 @@ export declare enum _HttpStatus {
169
188
  GATEWAY_TIMEOUT = 504,
170
189
  HTTP_VERSION_NOT_SUPPORTED = 505
171
190
  }
191
+ export {};
@@ -1,8 +1,8 @@
1
- import { _VovkSchema as VovkSchema } from '../types';
2
- export default function (options: {
1
+ import { _VovkSchema as VovkSchema, _StaticClass as StaticClass } from '../types';
2
+ export default function getSchema(options: {
3
3
  emitSchema?: boolean;
4
4
  segmentName?: string;
5
- controllers: Record<string, Function>;
6
- workers?: Record<string, Function>;
5
+ controllers: Record<string, StaticClass>;
6
+ workers?: Record<string, StaticClass>;
7
7
  exposeValidation?: boolean;
8
8
  }): VovkSchema;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = default_1;
4
- function default_1(options) {
3
+ exports.default = getSchema;
4
+ function getSchema(options) {
5
5
  const exposeValidation = options?.exposeValidation ?? true;
6
6
  const emitSchema = options.emitSchema ?? true;
7
7
  const schema = {
@@ -26,12 +26,10 @@ function _promisifyWorker(currentWorker, givenWorkerService) {
26
26
  instance.fork = (worker) => {
27
27
  return _promisifyWorker(worker, givenWorkerService);
28
28
  };
29
- for (const method of Object.keys(workerService._handlers)) {
30
- const { isGenerator } = workerService._handlers[method];
31
- const value = workerService[method];
29
+ for (const methodName of Object.keys(workerService._handlers)) {
30
+ const { isGenerator } = workerService._handlers[methodName];
32
31
  if (isGenerator) {
33
- // @ts-expect-error TODO Fix this
34
- instance[method] = (...args) => {
32
+ const method = (...args) => {
35
33
  const key = callsKey;
36
34
  callsKey += 1;
37
35
  return {
@@ -43,8 +41,8 @@ function _promisifyWorker(currentWorker, givenWorkerService) {
43
41
  const messageQueue = [];
44
42
  let messageResolver = null;
45
43
  const onMessage = (e) => {
46
- const { method: m, key: k } = e.data;
47
- if (k !== key || m !== method) {
44
+ const { methodName: m, key: k } = e.data;
45
+ if (k !== key || m !== methodName) {
48
46
  return;
49
47
  }
50
48
  if (messageResolver) {
@@ -69,7 +67,7 @@ function _promisifyWorker(currentWorker, givenWorkerService) {
69
67
  };
70
68
  w.addEventListener('message', onMessage);
71
69
  w.addEventListener('error', onError);
72
- w.postMessage({ key, args, method });
70
+ w.postMessage({ key, args, methodName });
73
71
  try {
74
72
  while (true) {
75
73
  let message = null;
@@ -101,10 +99,11 @@ function _promisifyWorker(currentWorker, givenWorkerService) {
101
99
  },
102
100
  };
103
101
  };
102
+ // @ts-expect-error TODO
103
+ instance[methodName] = method;
104
104
  }
105
105
  else {
106
- // @ts-expect-error TODO Fix this
107
- instance[method] = (...args) => {
106
+ const method = (...args) => {
108
107
  if (!instance.worker) {
109
108
  throw new Error('Worker is not provided or terminated');
110
109
  }
@@ -118,8 +117,8 @@ function _promisifyWorker(currentWorker, givenWorkerService) {
118
117
  reject(e);
119
118
  };
120
119
  const onMessage = (e) => {
121
- const { result, error, key: k, method: m } = e.data;
122
- if (k !== key || m !== method) {
120
+ const { result, error, key: k, methodName: m } = e.data;
121
+ if (k !== key || m !== methodName) {
123
122
  return;
124
123
  }
125
124
  w.removeEventListener('message', onMessage);
@@ -133,9 +132,11 @@ function _promisifyWorker(currentWorker, givenWorkerService) {
133
132
  };
134
133
  w.addEventListener('message', onMessage);
135
134
  w.addEventListener('error', onError);
136
- w.postMessage({ key, args, method });
135
+ w.postMessage({ key, args, methodName });
137
136
  });
138
137
  };
138
+ // @ts-expect-error TODO
139
+ instance[methodName] = method;
139
140
  }
140
141
  }
141
142
  return instance;
package/worker/types.d.ts CHANGED
@@ -17,15 +17,15 @@ export type _WorkerPromiseInstance<T> = OmitNever<_WorkerPromiseInstanceWithNeve
17
17
  [Symbol.dispose]: () => void;
18
18
  };
19
19
  export interface _WorkerInput {
20
- method: string;
20
+ methodName: string;
21
21
  args: unknown[];
22
22
  key: number;
23
23
  }
24
24
  export interface _WorkerOutput {
25
+ methodName: string;
25
26
  result?: unknown;
26
27
  error?: unknown;
27
28
  done?: true;
28
29
  key: number;
29
- method: string;
30
30
  }
31
31
  export {};
package/worker/worker.js CHANGED
@@ -20,25 +20,24 @@ function _worker() {
20
20
  }
21
21
  if (typeof self === 'undefined')
22
22
  return; // no-op in non-worker environment
23
- // eslint-disable-next-line no-undef
24
23
  const w = self;
25
24
  w.onmessage = async (evt) => {
26
- const { method, args, key } = evt.data;
25
+ const { methodName, args, key } = evt.data;
27
26
  try {
28
- const result = await target[method](...args);
27
+ const result = await target[methodName](...args);
29
28
  if (result && typeof result === 'object' && 'next' in result && typeof result.next === 'function') {
30
29
  const iterable = result;
31
30
  for await (const result of iterable) {
32
- w.postMessage({ result, key, method });
31
+ w.postMessage({ result, key, methodName });
33
32
  }
34
- w.postMessage({ done: true, key, method });
33
+ w.postMessage({ done: true, key, methodName });
35
34
  }
36
35
  else {
37
- w.postMessage({ result, key, method });
36
+ w.postMessage({ result, key, methodName });
38
37
  }
39
38
  }
40
39
  catch (e) {
41
- w.postMessage({ error: e, key, method });
40
+ w.postMessage({ error: e, key, methodName });
42
41
  }
43
42
  };
44
43
  };