vovk 3.0.0-draft.93 → 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/createVovkApp.js +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/dist/utils/setHandlerSchema.d.ts +4 -0
- package/dist/utils/{setHandlerValidation.js → setHandlerSchema.js} +4 -4
- package/dist/utils/withValidation.d.ts +13 -0
- package/dist/utils/withValidation.js +42 -0
- package/package.json +1 -1
- package/dist/utils/setHandlerValidation.d.ts +0 -4
package/dist/createVovkApp.js
CHANGED
|
@@ -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
|
|
31
|
+
const schema = originalMethod._sourceMethod._getSchema?.(controller);
|
|
32
32
|
controller._handlers = {
|
|
33
33
|
...controller._handlers,
|
|
34
34
|
[propertyKey]: {
|
|
35
|
-
...
|
|
35
|
+
...schema,
|
|
36
36
|
...(controller._handlers ?? {})[propertyKey],
|
|
37
37
|
path,
|
|
38
38
|
httpMethod,
|
package/dist/index.d.ts
CHANGED
|
@@ -5,8 +5,8 @@ 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 {
|
|
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,
|
|
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.
|
|
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");
|
|
@@ -18,6 +18,6 @@ const StreamJSONResponse_1 = require("./StreamJSONResponse");
|
|
|
18
18
|
Object.defineProperty(exports, "StreamJSONResponse", { enumerable: true, get: function () { return StreamJSONResponse_1.StreamJSONResponse; } });
|
|
19
19
|
const generateStaticAPI_1 = require("./utils/generateStaticAPI");
|
|
20
20
|
Object.defineProperty(exports, "generateStaticAPI", { enumerable: true, get: function () { return generateStaticAPI_1.generateStaticAPI; } });
|
|
21
|
-
const
|
|
22
|
-
Object.defineProperty(exports, "
|
|
21
|
+
const withValidation_1 = require("./utils/withValidation");
|
|
22
|
+
Object.defineProperty(exports, "withValidation", { enumerable: true, get: function () { return withValidation_1.withValidation; } });
|
|
23
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.
|
|
4
|
-
async function
|
|
5
|
-
h.
|
|
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
|
|
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,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>;
|