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.
- package/client/clientizeController.d.ts +1 -1
- package/client/clientizeController.js +8 -9
- package/createDecorator.js +2 -2
- package/package.json +1 -1
- package/types.d.ts +15 -11
- package/utils/getSchema.js +8 -8
- package/utils/setClientValidatorsForHandler.js +1 -1
- package/worker/promisifyWorker.d.ts +1 -1
- package/worker/promisifyWorker.js +6 -8
- package/worker/worker.js +0 -1
|
@@ -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>(
|
|
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 = (
|
|
40
|
-
const
|
|
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.
|
|
47
|
-
|
|
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,
|
|
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 },
|
|
58
|
+
await (input.validateOnClient ?? options?.validateOnClient)?.({ body, query, endpoint }, validation ?? {});
|
|
60
59
|
};
|
|
61
60
|
const internalOptions = {
|
|
62
61
|
name: staticMethodName,
|
package/createDecorator.js
CHANGED
|
@@ -18,8 +18,8 @@ function _createDecorator(handler, initHandler) {
|
|
|
18
18
|
[propertyKey]: {
|
|
19
19
|
...handlerSchema,
|
|
20
20
|
// avoid override of path and httpMethod
|
|
21
|
-
...(initResult?.
|
|
22
|
-
...(initResult?.
|
|
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
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
|
-
|
|
20
|
+
validation?: {
|
|
21
21
|
query?: _KnownAny;
|
|
22
22
|
body?: _KnownAny;
|
|
23
23
|
};
|
|
24
|
-
|
|
24
|
+
custom?: Record<string, _KnownAny>;
|
|
25
25
|
};
|
|
26
26
|
export type _VovkControllerSchema = {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
controllerName: string;
|
|
28
|
+
originalControllerName: string;
|
|
29
|
+
prefix?: string;
|
|
30
|
+
handlers: Record<string, _HandlerSchema>;
|
|
31
31
|
};
|
|
32
32
|
export type _VovkWorkerSchema = {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
workerName: string;
|
|
34
|
+
originalWorkerName: string;
|
|
35
|
+
handlers: Record<string, {
|
|
36
36
|
isGenerator?: true;
|
|
37
37
|
}>;
|
|
38
38
|
};
|
|
39
|
-
export type _VovkControllerInternal =
|
|
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 &
|
|
49
|
+
export type _VovkWorker = _StaticClass & {
|
|
50
|
+
_handlers: _VovkWorkerSchema['handlers'];
|
|
47
51
|
[key: string]: unknown;
|
|
48
52
|
};
|
|
49
53
|
export type _DecoratorOptions = {
|
package/utils/getSchema.js
CHANGED
|
@@ -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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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,
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
workerName,
|
|
33
|
+
originalWorkerName: worker.name,
|
|
34
|
+
handlers: { ...worker._handlers },
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
37
|
return schema;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { _WorkerPromiseInstance as WorkerPromiseInstance } from './types';
|
|
2
|
-
export declare function _promisifyWorker<T extends object>(currentWorker: Worker | null,
|
|
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,
|
|
5
|
-
if (!
|
|
4
|
+
function _promisifyWorker(currentWorker, workerSchema) {
|
|
5
|
+
if (!workerSchema)
|
|
6
6
|
throw new Error('Worker schema is not provided');
|
|
7
|
-
const
|
|
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
|
-
|
|
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') {
|