@jayfong/x-server 2.17.3 → 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.
@@ -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,23 @@ class Handler {
29
32
  };
30
33
  this.handleHttp = async (data, ctx) => {
31
34
  // 请求数据验证
32
- if (this.requestDataSchema) {
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) {
33
52
  try {
34
53
  data = await this.requestDataSchema.validate(data, {
35
54
  abortEarly: true,
@@ -113,7 +132,9 @@ class Handler {
113
132
  dispose.dispose();
114
133
  });
115
134
  };
116
- if (options.requestDataSchema) {
135
+ if (options.requestDataSchemaVae) {
136
+ this.requestDataSchemaVae = options.requestDataSchemaVae(vae.v);
137
+ } else if (options.requestDataSchema) {
117
138
  this.requestDataSchema = options.requestDataSchema(_validator.yup);
118
139
  }
119
140
  }
@@ -3,6 +3,7 @@ export type { HandlerMethodMap, HandlerPath, HandlerPayloadMap, HandlerResultMap
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;
@@ -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,23 @@ export class Handler {
25
27
  };
26
28
  this.handleHttp = async (data, ctx) => {
27
29
  // 请求数据验证
28
- if (this.requestDataSchema) {
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) {
29
47
  try {
30
48
  data = await this.requestDataSchema.validate(data, {
31
49
  abortEarly: true,
@@ -109,7 +127,9 @@ export class Handler {
109
127
  dispose.dispose();
110
128
  });
111
129
  };
112
- if (options.requestDataSchema) {
130
+ if (options.requestDataSchemaVae) {
131
+ this.requestDataSchemaVae = options.requestDataSchemaVae(vae.v);
132
+ } else if (options.requestDataSchema) {
113
133
  this.requestDataSchema = options.requestDataSchema(yup);
114
134
  }
115
135
  }
@@ -1,17 +1,18 @@
1
1
  /// <reference types="node" />
2
- import { BasePlugin } from '../plugins/base';
3
- import { BaseService } from '../services/base';
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 { yup } from 'vtils/validator';
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.17.3",
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.87.0",
70
+ "vtils": "^4.88.0",
71
71
  "yaml": "^2.3.1",
72
72
  "yargs": "^17.4.1"
73
73
  },