@modern-js/bff-core 1.2.1-beta.2 → 1.2.2-beta.1

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.
@@ -41,7 +41,7 @@ export function Api(...args) {
41
41
  };
42
42
  const stack = [...validateHandlers, ...pipeHandlers];
43
43
  stack.push(async (helper, next) => {
44
- const res = await handler(inputs);
44
+ const res = await handler(helper.inputs);
45
45
  helper.result = res;
46
46
  return next();
47
47
  });
@@ -1,9 +1,15 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
1
7
  import { HttpMetadata, OperatorType, HttpMethod, TriggerType, ResponseMetaType } from "../types";
2
8
  import { ValidationError } from "../errors/http";
3
9
 
4
10
  const validateInput = async (schema, input) => {
5
11
  try {
6
- await schema.parseAsync(input);
12
+ return await schema.parseAsync(input);
7
13
  } catch (error) {
8
14
  const {
9
15
  z: zod
@@ -56,12 +62,13 @@ export const Data = schema => {
56
62
 
57
63
  async validate(helper, next) {
58
64
  const {
59
- inputs
65
+ inputs: {
66
+ data
67
+ }
60
68
  } = helper;
61
- const {
62
- data
63
- } = inputs;
64
- await validateInput(schema, data);
69
+ helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
70
+ data: await validateInput(schema, data)
71
+ });
65
72
  return next();
66
73
  }
67
74
 
@@ -79,12 +86,13 @@ export const Query = schema => {
79
86
 
80
87
  async validate(helper, next) {
81
88
  const {
82
- inputs
89
+ inputs: {
90
+ query
91
+ }
83
92
  } = helper;
84
- const {
85
- query
86
- } = inputs;
87
- await validateInput(schema, query);
93
+ helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
94
+ query: await validateInput(schema, query)
95
+ });
88
96
  return next();
89
97
  }
90
98
 
@@ -102,12 +110,13 @@ export const Params = schema => {
102
110
 
103
111
  async validate(helper, next) {
104
112
  const {
105
- inputs
113
+ inputs: {
114
+ params
115
+ }
106
116
  } = helper;
107
- const {
108
- params
109
- } = inputs;
110
- await validateInput(schema, params);
117
+ helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
118
+ params: await validateInput(schema, params)
119
+ });
111
120
  return next();
112
121
  }
113
122
 
@@ -125,12 +134,13 @@ export const Headers = schema => {
125
134
 
126
135
  async validate(helper, next) {
127
136
  const {
128
- inputs
137
+ inputs: {
138
+ headers
139
+ }
129
140
  } = helper;
130
- const {
131
- headers
132
- } = inputs;
133
- await validateInput(schema, headers);
141
+ helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
142
+ headers: await validateInput(schema, headers)
143
+ });
134
144
  return next();
135
145
  }
136
146
 
@@ -70,8 +70,10 @@ const enableRegister = requireFn => {
70
70
 
71
71
  const isFunction = input => input && {}.toString.call(input) === '[object Function]';
72
72
 
73
- export const requireHandlerModule = enableRegister(modulePath => {
74
- const module = require(modulePath);
73
+ export const requireHandlerModule = modulePath => {
74
+ // 测试环境不走缓存,因为缓存的 handler 文件,会被 mockAPI 函数进行 mock,升级 jest28,setupFilesAfterEnv 能做异步操作的话,可解此问题
75
+ const originRequire = process.env.NODE_ENV === 'test' ? jest.requireActual : require;
76
+ const module = originRequire(modulePath);
75
77
 
76
78
  if (isFunction(module)) {
77
79
  return {
@@ -80,7 +82,7 @@ export const requireHandlerModule = enableRegister(modulePath => {
80
82
  }
81
83
 
82
84
  return module;
83
- });
85
+ };
84
86
 
85
87
  const routeValue = routePath => {
86
88
  if (routePath.includes(':')) {
@@ -53,7 +53,7 @@ function Api(...args) {
53
53
  };
54
54
  const stack = [...validateHandlers, ...pipeHandlers];
55
55
  stack.push(async (helper, next) => {
56
- const res = await handler(inputs);
56
+ const res = await handler(helper.inputs);
57
57
  helper.result = res;
58
58
  return next();
59
59
  });
@@ -9,9 +9,15 @@ var _types = require("../types");
9
9
 
10
10
  var _http = require("../errors/http");
11
11
 
12
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
13
+
14
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
15
+
16
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
+
12
18
  const validateInput = async (schema, input) => {
13
19
  try {
14
- await schema.parseAsync(input);
20
+ return await schema.parseAsync(input);
15
21
  } catch (error) {
16
22
  const {
17
23
  z: zod
@@ -76,12 +82,13 @@ const Data = schema => {
76
82
 
77
83
  async validate(helper, next) {
78
84
  const {
79
- inputs
85
+ inputs: {
86
+ data
87
+ }
80
88
  } = helper;
81
- const {
82
- data
83
- } = inputs;
84
- await validateInput(schema, data);
89
+ helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
90
+ data: await validateInput(schema, data)
91
+ });
85
92
  return next();
86
93
  }
87
94
 
@@ -102,12 +109,13 @@ const Query = schema => {
102
109
 
103
110
  async validate(helper, next) {
104
111
  const {
105
- inputs
112
+ inputs: {
113
+ query
114
+ }
106
115
  } = helper;
107
- const {
108
- query
109
- } = inputs;
110
- await validateInput(schema, query);
116
+ helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
117
+ query: await validateInput(schema, query)
118
+ });
111
119
  return next();
112
120
  }
113
121
 
@@ -128,12 +136,13 @@ const Params = schema => {
128
136
 
129
137
  async validate(helper, next) {
130
138
  const {
131
- inputs
139
+ inputs: {
140
+ params
141
+ }
132
142
  } = helper;
133
- const {
134
- params
135
- } = inputs;
136
- await validateInput(schema, params);
143
+ helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
144
+ params: await validateInput(schema, params)
145
+ });
137
146
  return next();
138
147
  }
139
148
 
@@ -154,12 +163,13 @@ const Headers = schema => {
154
163
 
155
164
  async validate(helper, next) {
156
165
  const {
157
- inputs
166
+ inputs: {
167
+ headers
168
+ }
158
169
  } = helper;
159
- const {
160
- headers
161
- } = inputs;
162
- await validateInput(schema, headers);
170
+ helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
171
+ headers: await validateInput(schema, headers)
172
+ });
163
173
  return next();
164
174
  }
165
175
 
@@ -89,8 +89,10 @@ const enableRegister = requireFn => {
89
89
 
90
90
  const isFunction = input => input && {}.toString.call(input) === '[object Function]';
91
91
 
92
- const requireHandlerModule = enableRegister(modulePath => {
93
- const module = require(modulePath);
92
+ const requireHandlerModule = modulePath => {
93
+ // 测试环境不走缓存,因为缓存的 handler 文件,会被 mockAPI 函数进行 mock,升级 jest28,setupFilesAfterEnv 能做异步操作的话,可解此问题
94
+ const originRequire = process.env.NODE_ENV === 'test' ? jest.requireActual : require;
95
+ const module = originRequire(modulePath);
94
96
 
95
97
  if (isFunction(module)) {
96
98
  return {
@@ -99,7 +101,8 @@ const requireHandlerModule = enableRegister(modulePath => {
99
101
  }
100
102
 
101
103
  return module;
102
- });
104
+ };
105
+
103
106
  exports.requireHandlerModule = requireHandlerModule;
104
107
 
105
108
  const routeValue = routePath => {
@@ -1,3 +1,3 @@
1
1
  import 'reflect-metadata';
2
- import type { ApiRunner, ArrayToObject, ExtractInputType, Operator, MaybeAsync } from './types';
3
- export declare function Api<Operators extends Operator<any>[], Res extends MaybeAsync<any>>(...args: [...operators: Operators, handler: (arg: ArrayToObject<ExtractInputType<Operators>>) => Res]): ApiRunner<ExtractInputType<Operators> extends void[] ? void : ArrayToObject<ExtractInputType<Operators>>, Res>;
2
+ import type { ApiRunner, ArrayToObject, ExtractInputType, ExtractOuputType, Operator, MaybeAsync } from './types';
3
+ export declare function Api<Operators extends Operator<any, any>[], Res extends MaybeAsync<any>>(...args: [...operators: Operators, handler: (arg: ArrayToObject<ExtractOuputType<Operators>>) => Res]): ApiRunner<ExtractInputType<Operators> extends void[] ? void : ArrayToObject<ExtractInputType<Operators>>, Res>;
@@ -14,17 +14,25 @@ export declare const Trace: (urlPath: string) => Operator<void>;
14
14
  export declare const Patch: (urlPath: string) => Operator<void>;
15
15
  export declare const Option: (urlPath: string) => Operator<void>;
16
16
  export declare const Head: (urlPath: string) => Operator<void>;
17
- export declare const Data: <T>(schema: z.ZodType<T, z.ZodTypeDef, T>) => Operator<{
18
- data: T;
17
+ export declare const Data: <Schema extends z.ZodType<any, z.ZodTypeDef, any>>(schema: Schema) => Operator<{
18
+ data: z.input<Schema>;
19
+ }, {
20
+ data: z.output<Schema>;
19
21
  }>;
20
- export declare const Query: <T>(schema: z.ZodType<T, z.ZodTypeDef, T>) => Operator<{
21
- query: T;
22
+ export declare const Query: <Schema extends z.ZodType<any, z.ZodTypeDef, any>>(schema: Schema) => Operator<{
23
+ query: z.input<Schema>;
24
+ }, {
25
+ query: z.output<Schema>;
22
26
  }>;
23
- export declare const Params: <T>(schema: z.ZodType<T, z.ZodTypeDef, T>) => Operator<{
24
- params: T;
27
+ export declare const Params: <Schema extends z.ZodType<any, z.ZodTypeDef, any>>(schema: Schema) => Operator<{
28
+ params: z.input<Schema>;
29
+ }, {
30
+ params: z.output<Schema>;
25
31
  }>;
26
- export declare const Headers: <T>(schema: z.ZodType<T, z.ZodTypeDef, T>) => Operator<{
27
- headers: T;
32
+ export declare const Headers: <Schema extends z.ZodType<any, z.ZodTypeDef, any>>(schema: Schema) => Operator<{
33
+ headers: z.input<Schema>;
34
+ }, {
35
+ headers: z.output<Schema>;
28
36
  }>;
29
37
  export declare const HttpCode: (statusCode: number) => Operator<void>;
30
38
  export declare const SetHeaders: (headers: Record<string, string>) => Operator<void>;
@@ -1,10 +1,10 @@
1
- import { APIHandlerInfo, HandlerModule } from './types';
1
+ import { APIHandlerInfo } from './types';
2
2
  declare type MaybeAsync<I> = I | Promise<I>;
3
3
  export declare type NormalHandler = (...args: any[]) => any;
4
4
  export declare type Handler<I, O> = (input: I) => MaybeAsync<O>;
5
5
  export declare const getFiles: (lambdaDir: string, rules: string | string[]) => string[];
6
6
  export declare const getPathFromFilename: (baseDir: string, filename: string) => string;
7
7
  export declare const isHandler: (input: any) => input is Handler<any, any>;
8
- export declare const requireHandlerModule: (modulePath: string) => HandlerModule;
8
+ export declare const requireHandlerModule: (modulePath: string) => any;
9
9
  export declare const sortRoutes: (apiHandlers: APIHandlerInfo[]) => APIHandlerInfo[];
10
10
  export {};
@@ -31,26 +31,28 @@ export declare enum HttpMethod {
31
31
  Head = "HEAD",
32
32
  }
33
33
  export declare type InputSchemaMeata = Extract<HttpMetadata, HttpMetadata.Data | HttpMetadata.Query | HttpMetadata.Headers | HttpMetadata.Params>;
34
- export declare type ExecuteFunc = (helper: ExecuteHelper, next: () => Promise<any>) => Promise<any>;
35
- export declare type ExecuteHelper = {
34
+ export declare type ExecuteFunc<Outputs> = (helper: ExecuteHelper<Outputs>, next: () => Promise<any>) => Promise<any>;
35
+ export declare type ExecuteHelper<Outputs> = {
36
36
  result?: any;
37
- inputs: any;
37
+ inputs: Outputs;
38
38
  };
39
39
  export declare type MetadataHelper = {
40
40
  setMetadata: <T = any>(key: any, value: T) => void;
41
41
  getMetadata: <T = any>(key: any) => T;
42
42
  };
43
- export declare type Operator<Input> = {
43
+ export declare type Operator<Input = any, Output = Input> = {
44
44
  name: string;
45
45
  inputType?: Input;
46
+ outputType?: Output;
46
47
  metadata?: (helper: MetadataHelper) => void;
47
- validate?: ExecuteFunc;
48
- execute?: ExecuteFunc;
48
+ validate?: ExecuteFunc<Output>;
49
+ execute?: ExecuteFunc<Output>;
49
50
  };
50
51
  export declare type MaybeAsync<T> = Promise<T> | T;
51
52
  export declare type ApiRunner<Input extends object | void | unknown, Output extends MaybeAsync<any>> = (...args: Input extends void ? never : [input: Input]) => Output;
52
53
  export declare type NonNullable<T> = Exclude<T, null | undefined>;
53
- export declare type ExtractInputType<T> = { [key in keyof T]: T[key] extends Operator<any> ? NonNullable<T[key]['inputType']> : void };
54
+ export declare type ExtractInputType<T> = { [key in keyof T]: T[key] extends Operator<any, any> ? NonNullable<T[key]['inputType']> : void };
55
+ export declare type ExtractOuputType<T> = { [key in keyof T]: T[key] extends Operator<any, any> ? NonNullable<T[key]['outputType']> : void };
54
56
  export declare type ArrayToObject<T, R = {}> = T extends [infer First, ...infer Rest] ? First extends PromiseLike<infer PromiseValue> ? PromiseValue : First extends object ? Merge<First, ArrayToObject<Rest, R>> : ArrayToObject<Rest, R> : R;
55
57
  export declare type AsyncFunction = (...args: any[]) => Promise<any>;
56
58
  export declare const httpMethods: HttpMethod[];
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.2.1-beta.2",
14
+ "version": "1.2.2-beta.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",