@jayfong/x-server 2.17.2 → 2.18.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 +28 -5
- package/lib/core/handler.d.ts +2 -1
- package/lib/core/handler.js +27 -4
- package/lib/core/types.d.ts +14 -9
- package/package.json +2 -2
package/lib/_cjs/core/handler.js
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
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");
|
|
8
|
-
var
|
|
9
|
-
var _http_error = require("../core/http_error");
|
|
9
|
+
var vae = _interopRequireWildcard(require("vtils/vae"));
|
|
10
10
|
var _validator = require("vtils/validator");
|
|
11
|
+
var _http_error = require("../core/http_error");
|
|
12
|
+
var _dispose = require("../services/dispose");
|
|
11
13
|
// @ts-ignore
|
|
12
14
|
// prettier-ignore
|
|
13
|
-
|
|
15
|
+
_validator.yup.setLocale((0, _validator.getZhCN)({
|
|
16
|
+
getLabel: params => params.label || params.path
|
|
17
|
+
}));
|
|
14
18
|
class Handler {
|
|
15
19
|
constructor(options) {
|
|
16
20
|
this.options = options;
|
|
17
21
|
this.requestDataSchema = void 0;
|
|
22
|
+
this.requestDataSchemaVae = void 0;
|
|
18
23
|
this.handle = async (data, ctx) => {
|
|
19
24
|
// 前置 hook
|
|
20
25
|
if (Handler.hooks.length) {
|
|
@@ -27,7 +32,23 @@ class Handler {
|
|
|
27
32
|
};
|
|
28
33
|
this.handleHttp = async (data, ctx) => {
|
|
29
34
|
// 请求数据验证
|
|
30
|
-
|
|
35
|
+
// vae
|
|
36
|
+
if (this.requestDataSchemaVae) {
|
|
37
|
+
const res = this.requestDataSchemaVae.parse(data, {
|
|
38
|
+
abortEarly: true,
|
|
39
|
+
preserveUnknownKeys: false
|
|
40
|
+
});
|
|
41
|
+
if (res.success) {
|
|
42
|
+
data = res.data;
|
|
43
|
+
} else {
|
|
44
|
+
throw new _http_error.HttpError.BadRequest(
|
|
45
|
+
// TODO: 暂时兼容编译bug
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
res.issues[0].message);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// yup
|
|
51
|
+
else if (this.requestDataSchema) {
|
|
31
52
|
try {
|
|
32
53
|
data = await this.requestDataSchema.validate(data, {
|
|
33
54
|
abortEarly: true,
|
|
@@ -111,7 +132,9 @@ class Handler {
|
|
|
111
132
|
dispose.dispose();
|
|
112
133
|
});
|
|
113
134
|
};
|
|
114
|
-
if (options.
|
|
135
|
+
if (options.requestDataSchemaVae) {
|
|
136
|
+
this.requestDataSchemaVae = options.requestDataSchemaVae(vae.v);
|
|
137
|
+
} else if (options.requestDataSchema) {
|
|
115
138
|
this.requestDataSchema = options.requestDataSchema(_validator.yup);
|
|
116
139
|
}
|
|
117
140
|
}
|
package/lib/core/handler.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { XHandler } from './types';
|
|
2
|
-
export type { HandlerPath, HandlerPayloadMap, HandlerResultMap
|
|
2
|
+
export type { HandlerMethodMap, HandlerPath, HandlerPayloadMap, HandlerResultMap } from '.x/routes';
|
|
3
3
|
export declare class Handler<TReqData extends any = void, TResData extends any = void, TReqMethod extends XHandler.Method = XHandler.Method> {
|
|
4
4
|
readonly options: XHandler.Options<TReqData, TResData, TReqMethod>;
|
|
5
5
|
private requestDataSchema;
|
|
6
|
+
private requestDataSchemaVae;
|
|
6
7
|
constructor(options: XHandler.Options<TReqData, TResData, TReqMethod>);
|
|
7
8
|
handle: XHandler.Handle<TReqData, TResData, TReqMethod>;
|
|
8
9
|
private handleHttp;
|
package/lib/core/handler.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import LZString from 'lz-string';
|
|
2
2
|
import { DataPacker } from 'vtils';
|
|
3
|
-
import
|
|
3
|
+
import * as vae from 'vtils/vae';
|
|
4
|
+
import { getZhCN, yup } from 'vtils/validator';
|
|
4
5
|
import { HttpError } from "../core/http_error";
|
|
5
|
-
import {
|
|
6
|
+
import { DisposeService } from "../services/dispose";
|
|
6
7
|
|
|
7
8
|
// @ts-ignore
|
|
8
9
|
// prettier-ignore
|
|
10
|
+
yup.setLocale(getZhCN({
|
|
11
|
+
getLabel: params => params.label || params.path
|
|
12
|
+
}));
|
|
9
13
|
export class Handler {
|
|
10
14
|
constructor(options) {
|
|
11
15
|
this.options = options;
|
|
12
16
|
this.requestDataSchema = void 0;
|
|
17
|
+
this.requestDataSchemaVae = void 0;
|
|
13
18
|
this.handle = async (data, ctx) => {
|
|
14
19
|
// 前置 hook
|
|
15
20
|
if (Handler.hooks.length) {
|
|
@@ -22,7 +27,23 @@ export class Handler {
|
|
|
22
27
|
};
|
|
23
28
|
this.handleHttp = async (data, ctx) => {
|
|
24
29
|
// 请求数据验证
|
|
25
|
-
|
|
30
|
+
// vae
|
|
31
|
+
if (this.requestDataSchemaVae) {
|
|
32
|
+
const res = this.requestDataSchemaVae.parse(data, {
|
|
33
|
+
abortEarly: true,
|
|
34
|
+
preserveUnknownKeys: false
|
|
35
|
+
});
|
|
36
|
+
if (res.success) {
|
|
37
|
+
data = res.data;
|
|
38
|
+
} else {
|
|
39
|
+
throw new HttpError.BadRequest(
|
|
40
|
+
// TODO: 暂时兼容编译bug
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
res.issues[0].message);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// yup
|
|
46
|
+
else if (this.requestDataSchema) {
|
|
26
47
|
try {
|
|
27
48
|
data = await this.requestDataSchema.validate(data, {
|
|
28
49
|
abortEarly: true,
|
|
@@ -106,7 +127,9 @@ export class Handler {
|
|
|
106
127
|
dispose.dispose();
|
|
107
128
|
});
|
|
108
129
|
};
|
|
109
|
-
if (options.
|
|
130
|
+
if (options.requestDataSchemaVae) {
|
|
131
|
+
this.requestDataSchemaVae = options.requestDataSchemaVae(vae.v);
|
|
132
|
+
} else if (options.requestDataSchema) {
|
|
110
133
|
this.requestDataSchema = options.requestDataSchema(yup);
|
|
111
134
|
}
|
|
112
135
|
}
|
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?: (_: 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.18.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.88.0",
|
|
71
71
|
"yaml": "^2.3.1",
|
|
72
72
|
"yargs": "^17.4.1"
|
|
73
73
|
},
|