@jayfong/x-server 2.17.3 → 2.19.0
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/lib/_cjs/core/handler.js +26 -2
- package/lib/core/handler.d.ts +2 -0
- package/lib/core/handler.js +25 -2
- package/lib/core/types.d.ts +14 -9
- package/package.json +2 -2
package/lib/_cjs/core/handler.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
3
4
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
5
|
exports.__esModule = true;
|
|
5
6
|
exports.Handler = void 0;
|
|
6
7
|
var _lzString = _interopRequireDefault(require("lz-string"));
|
|
7
8
|
var _vtils = require("vtils");
|
|
9
|
+
var vae = _interopRequireWildcard(require("vtils/vae"));
|
|
8
10
|
var _validator = require("vtils/validator");
|
|
9
11
|
var _http_error = require("../core/http_error");
|
|
10
12
|
var _dispose = require("../services/dispose");
|
|
@@ -17,6 +19,7 @@ class Handler {
|
|
|
17
19
|
constructor(options) {
|
|
18
20
|
this.options = options;
|
|
19
21
|
this.requestDataSchema = void 0;
|
|
22
|
+
this.requestDataSchemaVae = void 0;
|
|
20
23
|
this.handle = async (data, ctx) => {
|
|
21
24
|
// 前置 hook
|
|
22
25
|
if (Handler.hooks.length) {
|
|
@@ -29,7 +32,26 @@ class Handler {
|
|
|
29
32
|
};
|
|
30
33
|
this.handleHttp = async (data, ctx) => {
|
|
31
34
|
// 请求数据验证
|
|
32
|
-
|
|
35
|
+
// vae
|
|
36
|
+
if (this.requestDataSchemaVae) {
|
|
37
|
+
const res = this.requestDataSchemaVae.parse(
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
data, {
|
|
40
|
+
abortEarly: true,
|
|
41
|
+
preserveUnknownKeys: false
|
|
42
|
+
});
|
|
43
|
+
if (res.success) {
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
data = res.data;
|
|
46
|
+
} else {
|
|
47
|
+
throw new _http_error.HttpError.BadRequest(
|
|
48
|
+
// TODO: 暂时兼容编译bug
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
res.issues[0].message);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// yup
|
|
54
|
+
else if (this.requestDataSchema) {
|
|
33
55
|
try {
|
|
34
56
|
data = await this.requestDataSchema.validate(data, {
|
|
35
57
|
abortEarly: true,
|
|
@@ -113,7 +135,9 @@ class Handler {
|
|
|
113
135
|
dispose.dispose();
|
|
114
136
|
});
|
|
115
137
|
};
|
|
116
|
-
if (options.
|
|
138
|
+
if (options.requestDataSchemaVae) {
|
|
139
|
+
this.requestDataSchemaVae = typeof options.requestDataSchemaVae === 'function' ? options.requestDataSchemaVae(vae.v) : options.requestDataSchemaVae;
|
|
140
|
+
} else if (options.requestDataSchema) {
|
|
117
141
|
this.requestDataSchema = options.requestDataSchema(_validator.yup);
|
|
118
142
|
}
|
|
119
143
|
}
|
package/lib/core/handler.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import * as vae from 'vtils/vae';
|
|
1
2
|
import type { XHandler } from './types';
|
|
2
3
|
export type { HandlerMethodMap, HandlerPath, HandlerPayloadMap, HandlerResultMap } from '.x/routes';
|
|
3
4
|
export declare class Handler<TReqData extends any = void, TResData extends any = void, TReqMethod extends XHandler.Method = XHandler.Method> {
|
|
4
5
|
readonly options: XHandler.Options<TReqData, TResData, TReqMethod>;
|
|
5
6
|
private requestDataSchema;
|
|
7
|
+
readonly requestDataSchemaVae: vae.VaeSchemaOf<TReqData> | undefined;
|
|
6
8
|
constructor(options: XHandler.Options<TReqData, TResData, TReqMethod>);
|
|
7
9
|
handle: XHandler.Handle<TReqData, TResData, TReqMethod>;
|
|
8
10
|
private handleHttp;
|
package/lib/core/handler.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import LZString from 'lz-string';
|
|
2
2
|
import { DataPacker } from 'vtils';
|
|
3
|
+
import * as vae from 'vtils/vae';
|
|
3
4
|
import { getZhCN, yup } from 'vtils/validator';
|
|
4
5
|
import { HttpError } from "../core/http_error";
|
|
5
6
|
import { DisposeService } from "../services/dispose";
|
|
@@ -13,6 +14,7 @@ export class Handler {
|
|
|
13
14
|
constructor(options) {
|
|
14
15
|
this.options = options;
|
|
15
16
|
this.requestDataSchema = void 0;
|
|
17
|
+
this.requestDataSchemaVae = void 0;
|
|
16
18
|
this.handle = async (data, ctx) => {
|
|
17
19
|
// 前置 hook
|
|
18
20
|
if (Handler.hooks.length) {
|
|
@@ -25,7 +27,26 @@ export class Handler {
|
|
|
25
27
|
};
|
|
26
28
|
this.handleHttp = async (data, ctx) => {
|
|
27
29
|
// 请求数据验证
|
|
28
|
-
|
|
30
|
+
// vae
|
|
31
|
+
if (this.requestDataSchemaVae) {
|
|
32
|
+
const res = this.requestDataSchemaVae.parse(
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
data, {
|
|
35
|
+
abortEarly: true,
|
|
36
|
+
preserveUnknownKeys: false
|
|
37
|
+
});
|
|
38
|
+
if (res.success) {
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
data = res.data;
|
|
41
|
+
} else {
|
|
42
|
+
throw new HttpError.BadRequest(
|
|
43
|
+
// TODO: 暂时兼容编译bug
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
res.issues[0].message);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// yup
|
|
49
|
+
else if (this.requestDataSchema) {
|
|
29
50
|
try {
|
|
30
51
|
data = await this.requestDataSchema.validate(data, {
|
|
31
52
|
abortEarly: true,
|
|
@@ -109,7 +130,9 @@ export class Handler {
|
|
|
109
130
|
dispose.dispose();
|
|
110
131
|
});
|
|
111
132
|
};
|
|
112
|
-
if (options.
|
|
133
|
+
if (options.requestDataSchemaVae) {
|
|
134
|
+
this.requestDataSchemaVae = typeof options.requestDataSchemaVae === 'function' ? options.requestDataSchemaVae(vae.v) : options.requestDataSchemaVae;
|
|
135
|
+
} else if (options.requestDataSchema) {
|
|
113
136
|
this.requestDataSchema = options.requestDataSchema(yup);
|
|
114
137
|
}
|
|
115
138
|
}
|
package/lib/core/types.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import type { MultipartFile } from '@fastify/multipart';
|
|
3
|
+
import type { SocketStream } from '@fastify/websocket';
|
|
4
|
+
import type { Queue } from 'bull';
|
|
4
5
|
import { CronJob } from 'cron';
|
|
5
6
|
import { FastifyReply, FastifyRequest, FastifyServerOptions } from 'fastify';
|
|
6
|
-
import {
|
|
7
|
+
import type { IncomingHttpHeaders } from 'http';
|
|
8
|
+
import type { MsValue } from 'vtils';
|
|
7
9
|
import type { AsyncOrSync, LiteralUnion, OneOrMore, RequiredDeep } from 'vtils/types';
|
|
10
|
+
import * as vae from 'vtils/vae';
|
|
11
|
+
import { yup } from 'vtils/validator';
|
|
12
|
+
import { BasePlugin } from '../plugins/base';
|
|
13
|
+
import { BaseService } from '../services/base';
|
|
8
14
|
import type { DisposeService } from '../services/dispose';
|
|
9
15
|
import type { Handler } from './handler';
|
|
10
|
-
import type { IncomingHttpHeaders } from 'http';
|
|
11
|
-
import type { MsValue } from 'vtils';
|
|
12
|
-
import type { MultipartFile } from '@fastify/multipart';
|
|
13
|
-
import type { Queue } from 'bull';
|
|
14
|
-
import type { SocketStream } from '@fastify/websocket';
|
|
15
16
|
export declare namespace XServer {
|
|
16
17
|
type Mode = 'development' | 'production';
|
|
17
18
|
interface Route {
|
|
@@ -128,9 +129,13 @@ export declare namespace XHandler {
|
|
|
128
129
|
*/
|
|
129
130
|
responseContentType?: LiteralUnion<'text/html', string>;
|
|
130
131
|
/**
|
|
131
|
-
* 请求数据验证结构
|
|
132
|
+
* 请求数据验证结构(yup)
|
|
132
133
|
*/
|
|
133
134
|
requestDataSchema?: (_: typeof yup) => yup.GetSchema<RequiredDeep<TReqData>>;
|
|
135
|
+
/**
|
|
136
|
+
* 请求数据验证结构(vae)
|
|
137
|
+
*/
|
|
138
|
+
requestDataSchemaVae?: vae.VaeSchemaOf<TReqData> | ((_: typeof vae.v) => vae.VaeSchemaOf<TReqData>);
|
|
134
139
|
/**
|
|
135
140
|
* 处理器
|
|
136
141
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jayfong/x-server",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.19.0",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "lib/_cjs/index.js",
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"tsx": "^3.12.7",
|
|
68
68
|
"utf-8-validate": "^5.0.9",
|
|
69
69
|
"vscode-generate-index-standalone": "^1.7.1",
|
|
70
|
-
"vtils": "^4.
|
|
70
|
+
"vtils": "^4.90.0",
|
|
71
71
|
"yaml": "^2.3.1",
|
|
72
72
|
"yargs": "^17.4.1"
|
|
73
73
|
},
|