@modern-js/bff-core 1.0.1-beta.3 → 1.0.1-beta.6
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/js/modern/errors/http.js +10 -1
- package/dist/js/modern/operators/http.js +5 -2
- package/dist/js/modern/router/index.js +0 -1
- package/dist/js/modern/router/utils.js +22 -8
- package/dist/js/node/errors/http.js +10 -1
- package/dist/js/node/operators/http.js +5 -3
- package/dist/js/node/router/index.js +0 -2
- package/dist/js/node/router/utils.js +21 -8
- package/dist/types/client/result.d.ts +3 -3
- package/dist/types/errors/http.d.ts +5 -2
- package/dist/types/operators/http.d.ts +1 -1
- package/dist/types/router/index.d.ts +0 -1
- package/dist/types/router/utils.d.ts +3 -2
- package/package.json +1 -2
- package/CHANGELOG.md +0 -224
|
@@ -10,4 +10,13 @@ export class HttpError extends Error {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
}
|
|
13
|
-
export class ValidationError extends HttpError {
|
|
13
|
+
export class ValidationError extends HttpError {
|
|
14
|
+
constructor(status, message) {
|
|
15
|
+
super(status, message);
|
|
16
|
+
|
|
17
|
+
_defineProperty(this, "code", void 0);
|
|
18
|
+
|
|
19
|
+
this.code = 'VALIDATION_ERROR';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
1
|
import { HttpMetadata, OperatorType, HttpMethod, TriggerType, ResponseMetaType } from "../types";
|
|
3
2
|
import { ValidationError } from "../errors/http";
|
|
4
3
|
|
|
@@ -6,7 +5,11 @@ const validateInput = async (schema, input) => {
|
|
|
6
5
|
try {
|
|
7
6
|
await schema.parseAsync(input);
|
|
8
7
|
} catch (error) {
|
|
9
|
-
|
|
8
|
+
const {
|
|
9
|
+
z: zod
|
|
10
|
+
} = require('zod');
|
|
11
|
+
|
|
12
|
+
if (error instanceof zod.ZodError) {
|
|
10
13
|
throw new ValidationError(400, error.message);
|
|
11
14
|
}
|
|
12
15
|
|
|
@@ -3,7 +3,6 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
|
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { fs, logger } from '@modern-js/utils';
|
|
5
5
|
import 'reflect-metadata';
|
|
6
|
-
import 'esbuild-register';
|
|
7
6
|
import { HttpMethod, httpMethods, OperatorType, TriggerType } from "../types";
|
|
8
7
|
import { debug } from "../utils";
|
|
9
8
|
import { APIMode, FRAMEWORK_MODE_LAMBDA_DIR, API_FILE_RULES } from "./constants";
|
|
@@ -41,7 +41,16 @@ export const isHandler = input => input && typeof input === 'function';
|
|
|
41
41
|
const isFunction = input => input && {}.toString.call(input) === '[object Function]';
|
|
42
42
|
|
|
43
43
|
export const requireHandlerModule = modulePath => {
|
|
44
|
-
const
|
|
44
|
+
const {
|
|
45
|
+
register
|
|
46
|
+
} = require('esbuild-register/dist/node');
|
|
47
|
+
|
|
48
|
+
const {
|
|
49
|
+
unregister
|
|
50
|
+
} = register({}); // 测试环境不走缓存,因为缓存的 handler 文件,会被 mockAPI 函数进行 mock,升级 jest28,setupFilesAfterEnv 能做异步操作的话,可解此问题
|
|
51
|
+
|
|
52
|
+
const originRequire = process.env.NODE_ENV === 'test' ? jest.requireActual : require;
|
|
53
|
+
const module = originRequire(modulePath);
|
|
45
54
|
|
|
46
55
|
if (isFunction(module)) {
|
|
47
56
|
return {
|
|
@@ -49,15 +58,20 @@ export const requireHandlerModule = modulePath => {
|
|
|
49
58
|
};
|
|
50
59
|
}
|
|
51
60
|
|
|
61
|
+
unregister();
|
|
52
62
|
return module;
|
|
53
63
|
};
|
|
64
|
+
|
|
65
|
+
const routeValue = routePath => {
|
|
66
|
+
if (routePath.includes(':')) {
|
|
67
|
+
return 11;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return 1;
|
|
71
|
+
};
|
|
72
|
+
|
|
54
73
|
export const sortRoutes = apiHandlers => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (apiHandler.routeName.includes(':')) {
|
|
58
|
-
sortedHandlers.splice(handlerIndex, 1);
|
|
59
|
-
sortedHandlers.push(apiHandler);
|
|
60
|
-
}
|
|
74
|
+
return apiHandlers.sort((handlerA, handlerB) => {
|
|
75
|
+
return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
|
|
61
76
|
});
|
|
62
|
-
return sortedHandlers;
|
|
63
77
|
};
|
|
@@ -20,6 +20,15 @@ class HttpError extends Error {
|
|
|
20
20
|
|
|
21
21
|
exports.HttpError = HttpError;
|
|
22
22
|
|
|
23
|
-
class ValidationError extends HttpError {
|
|
23
|
+
class ValidationError extends HttpError {
|
|
24
|
+
constructor(status, message) {
|
|
25
|
+
super(status, message);
|
|
26
|
+
|
|
27
|
+
_defineProperty(this, "code", void 0);
|
|
28
|
+
|
|
29
|
+
this.code = 'VALIDATION_ERROR';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
}
|
|
24
33
|
|
|
25
34
|
exports.ValidationError = ValidationError;
|
|
@@ -5,8 +5,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.createHttpOperator = exports.Trace = exports.SetHeaders = exports.Redirect = exports.Query = exports.Put = exports.Post = exports.Patch = exports.Params = exports.Option = exports.Middleware = exports.HttpCode = exports.Headers = exports.Head = exports.Get = exports.Delete = exports.Data = exports.Connect = void 0;
|
|
7
7
|
|
|
8
|
-
var _zod = require("zod");
|
|
9
|
-
|
|
10
8
|
var _types = require("../types");
|
|
11
9
|
|
|
12
10
|
var _http = require("../errors/http");
|
|
@@ -15,7 +13,11 @@ const validateInput = async (schema, input) => {
|
|
|
15
13
|
try {
|
|
16
14
|
await schema.parseAsync(input);
|
|
17
15
|
} catch (error) {
|
|
18
|
-
|
|
16
|
+
const {
|
|
17
|
+
z: zod
|
|
18
|
+
} = require('zod');
|
|
19
|
+
|
|
20
|
+
if (error instanceof zod.ZodError) {
|
|
19
21
|
throw new _http.ValidationError(400, error.message);
|
|
20
22
|
}
|
|
21
23
|
|
|
@@ -60,7 +60,16 @@ exports.isHandler = isHandler;
|
|
|
60
60
|
const isFunction = input => input && {}.toString.call(input) === '[object Function]';
|
|
61
61
|
|
|
62
62
|
const requireHandlerModule = modulePath => {
|
|
63
|
-
const
|
|
63
|
+
const {
|
|
64
|
+
register
|
|
65
|
+
} = require('esbuild-register/dist/node');
|
|
66
|
+
|
|
67
|
+
const {
|
|
68
|
+
unregister
|
|
69
|
+
} = register({}); // 测试环境不走缓存,因为缓存的 handler 文件,会被 mockAPI 函数进行 mock,升级 jest28,setupFilesAfterEnv 能做异步操作的话,可解此问题
|
|
70
|
+
|
|
71
|
+
const originRequire = process.env.NODE_ENV === 'test' ? jest.requireActual : require;
|
|
72
|
+
const module = originRequire(modulePath);
|
|
64
73
|
|
|
65
74
|
if (isFunction(module)) {
|
|
66
75
|
return {
|
|
@@ -68,20 +77,24 @@ const requireHandlerModule = modulePath => {
|
|
|
68
77
|
};
|
|
69
78
|
}
|
|
70
79
|
|
|
80
|
+
unregister();
|
|
71
81
|
return module;
|
|
72
82
|
};
|
|
73
83
|
|
|
74
84
|
exports.requireHandlerModule = requireHandlerModule;
|
|
75
85
|
|
|
86
|
+
const routeValue = routePath => {
|
|
87
|
+
if (routePath.includes(':')) {
|
|
88
|
+
return 11;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return 1;
|
|
92
|
+
};
|
|
93
|
+
|
|
76
94
|
const sortRoutes = apiHandlers => {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (apiHandler.routeName.includes(':')) {
|
|
80
|
-
sortedHandlers.splice(handlerIndex, 1);
|
|
81
|
-
sortedHandlers.push(apiHandler);
|
|
82
|
-
}
|
|
95
|
+
return apiHandlers.sort((handlerA, handlerB) => {
|
|
96
|
+
return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
|
|
83
97
|
});
|
|
84
|
-
return sortedHandlers;
|
|
85
98
|
};
|
|
86
99
|
|
|
87
100
|
exports.sortRoutes = sortRoutes;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
export declare type Err<T =
|
|
1
|
+
export declare type Err<T = unknown> = {
|
|
2
2
|
kind: 'Err';
|
|
3
3
|
value: T;
|
|
4
4
|
isErr: true;
|
|
5
5
|
isOk: false;
|
|
6
6
|
};
|
|
7
|
-
export declare type Ok<T =
|
|
7
|
+
export declare type Ok<T = unknown> = {
|
|
8
8
|
kind: 'Ok';
|
|
9
9
|
value: T;
|
|
10
10
|
isErr: false;
|
|
11
11
|
isOk: true;
|
|
12
12
|
};
|
|
13
|
-
export declare type Result<T =
|
|
13
|
+
export declare type Result<T = unknown, E = string> = Err<E> | Ok<T>;
|
|
14
14
|
export declare const Err: <E = string>(value: E) => Err<E>;
|
|
15
15
|
export declare const Ok: <T, E = string>(value: T) => Result<T, E>;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export declare class HttpError extends Error {
|
|
2
|
-
status: number;
|
|
2
|
+
protected status: number;
|
|
3
3
|
constructor(status: number, message: string);
|
|
4
4
|
}
|
|
5
|
-
export declare class ValidationError extends HttpError {
|
|
5
|
+
export declare class ValidationError extends HttpError {
|
|
6
|
+
private code;
|
|
7
|
+
constructor(status: number, message: string);
|
|
8
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { MaybeAsync } from '@modern-js/bff-runtime';
|
|
2
1
|
import { APIHandlerInfo } from './types';
|
|
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
8
|
export declare const requireHandlerModule: (modulePath: string) => any;
|
|
9
|
-
export declare const sortRoutes: (apiHandlers: APIHandlerInfo[]) => APIHandlerInfo[];
|
|
9
|
+
export declare const sortRoutes: (apiHandlers: APIHandlerInfo[]) => APIHandlerInfo[];
|
|
10
|
+
export {};
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.0.1-beta.
|
|
14
|
+
"version": "1.0.1-beta.6",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -29,7 +29,6 @@
|
|
|
29
29
|
"@modern-js/utils": "^1.7.6",
|
|
30
30
|
"esbuild": "^0.14.38",
|
|
31
31
|
"esbuild-register": "^3.3.3",
|
|
32
|
-
"http-errors": "^2.0.0",
|
|
33
32
|
"koa-compose": "^4.1.0",
|
|
34
33
|
"reflect-metadata": "^0.1.13"
|
|
35
34
|
},
|
package/CHANGELOG.md
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
# @modern-js/adapter-helpers
|
|
2
|
-
|
|
3
|
-
## 1.2.4
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- d32f35134: chore: add modern/jest/eslint/ts config files to .npmignore
|
|
8
|
-
|
|
9
|
-
## 1.2.3
|
|
10
|
-
|
|
11
|
-
### Patch Changes
|
|
12
|
-
|
|
13
|
-
- 6cffe99d: chore:
|
|
14
|
-
remove react eslint rules for `modern-js` rule set.
|
|
15
|
-
add .eslintrc for each package to speed up linting
|
|
16
|
-
- 60f7d8bf: feat: add tests dir to npmignore
|
|
17
|
-
- befd9e5b: fix: compatible with babel-plugin-resolver's handling of relative paths on windows
|
|
18
|
-
- 3bf4f8b0: feat: support start api server only
|
|
19
|
-
|
|
20
|
-
## 1.2.2
|
|
21
|
-
|
|
22
|
-
### Patch Changes
|
|
23
|
-
|
|
24
|
-
- 55e18278: chore: remove unused dependencies and devDependencies
|
|
25
|
-
|
|
26
|
-
## 1.2.1
|
|
27
|
-
|
|
28
|
-
### Patch Changes
|
|
29
|
-
|
|
30
|
-
- 83166714: change .npmignore
|
|
31
|
-
|
|
32
|
-
## 1.2.0
|
|
33
|
-
|
|
34
|
-
### Minor Changes
|
|
35
|
-
|
|
36
|
-
- cfe11628: Make Modern.js self bootstraping
|
|
37
|
-
|
|
38
|
-
## 1.1.1
|
|
39
|
-
|
|
40
|
-
### Patch Changes
|
|
41
|
-
|
|
42
|
-
- 0fa83663: support more .env files
|
|
43
|
-
|
|
44
|
-
## 1.1.0
|
|
45
|
-
|
|
46
|
-
### Minor Changes
|
|
47
|
-
|
|
48
|
-
- 96119db2: Relese v1.1.0
|
|
49
|
-
|
|
50
|
-
## 1.0.0
|
|
51
|
-
|
|
52
|
-
### Patch Changes
|
|
53
|
-
|
|
54
|
-
- 224f7fe: fix server route match
|
|
55
|
-
- 30ac27c: feat: add generator package description
|
|
56
|
-
- 0fd196e: feat: fix bugs
|
|
57
|
-
- 204c626: feat: initial
|
|
58
|
-
- 63be0a5: fix: #118 #104
|
|
59
|
-
|
|
60
|
-
## 1.0.0-rc.23
|
|
61
|
-
|
|
62
|
-
### Patch Changes
|
|
63
|
-
|
|
64
|
-
- 224f7fe: fix server route match
|
|
65
|
-
- 30ac27c: feat: add generator package description
|
|
66
|
-
- 0fd196e: feat: fix bugs
|
|
67
|
-
- 204c626: feat: initial
|
|
68
|
-
- 63be0a5: fix: #118 #104
|
|
69
|
-
|
|
70
|
-
## 1.0.0-rc.22
|
|
71
|
-
|
|
72
|
-
### Patch Changes
|
|
73
|
-
|
|
74
|
-
- 224f7fe: fix server route match
|
|
75
|
-
- 30ac27c: feat: add generator package description
|
|
76
|
-
- 0fd196e: feat: fix bugs
|
|
77
|
-
- 204c626: feat: initial
|
|
78
|
-
- 63be0a5: fix: #118 #104
|
|
79
|
-
|
|
80
|
-
## 1.0.0-rc.21
|
|
81
|
-
|
|
82
|
-
### Patch Changes
|
|
83
|
-
|
|
84
|
-
- 224f7fe: fix server route match
|
|
85
|
-
- 30ac27c: feat: add generator package description
|
|
86
|
-
- 0fd196e: feat: fix bugs
|
|
87
|
-
- 204c626: feat: initial
|
|
88
|
-
- 63be0a5: fix: #118 #104
|
|
89
|
-
|
|
90
|
-
## 1.0.0-rc.20
|
|
91
|
-
|
|
92
|
-
### Patch Changes
|
|
93
|
-
|
|
94
|
-
- 224f7fe: fix server route match
|
|
95
|
-
- 30ac27c: feat: add generator package description
|
|
96
|
-
- feat: fix bugs
|
|
97
|
-
- 204c626: feat: initial
|
|
98
|
-
- 63be0a5: fix: #118 #104
|
|
99
|
-
|
|
100
|
-
## 1.0.0-rc.19
|
|
101
|
-
|
|
102
|
-
### Patch Changes
|
|
103
|
-
|
|
104
|
-
- 224f7fe: fix server route match
|
|
105
|
-
- 30ac27c: feat: add generator package description
|
|
106
|
-
- 204c626: feat: initial
|
|
107
|
-
- 63be0a5: fix: #118 #104
|
|
108
|
-
|
|
109
|
-
## 1.0.0-rc.18
|
|
110
|
-
|
|
111
|
-
### Patch Changes
|
|
112
|
-
|
|
113
|
-
- 224f7fe: fix server route match
|
|
114
|
-
- 30ac27c: feat: add generator package description
|
|
115
|
-
- 204c626: feat: initial
|
|
116
|
-
- 63be0a5: fix: #118 #104
|
|
117
|
-
|
|
118
|
-
## 1.0.0-rc.17
|
|
119
|
-
|
|
120
|
-
### Patch Changes
|
|
121
|
-
|
|
122
|
-
- 224f7fe: fix server route match
|
|
123
|
-
- 30ac27c: feat: add generator package description
|
|
124
|
-
- 204c626: feat: initial
|
|
125
|
-
- fix: #118 #104
|
|
126
|
-
|
|
127
|
-
## 1.0.0-rc.16
|
|
128
|
-
|
|
129
|
-
### Patch Changes
|
|
130
|
-
|
|
131
|
-
- 224f7fe: fix server route match
|
|
132
|
-
- 30ac27c: feat: add generator package description
|
|
133
|
-
- 204c626: feat: initial
|
|
134
|
-
|
|
135
|
-
## 1.0.0-rc.15
|
|
136
|
-
|
|
137
|
-
### Patch Changes
|
|
138
|
-
|
|
139
|
-
- 224f7fe: fix server route match
|
|
140
|
-
- 30ac27c: feat: add generator package description
|
|
141
|
-
- 204c626: feat: initial
|
|
142
|
-
|
|
143
|
-
## 1.0.0-rc.14
|
|
144
|
-
|
|
145
|
-
### Patch Changes
|
|
146
|
-
|
|
147
|
-
- 224f7fe: fix server route match
|
|
148
|
-
- 204c626: feat: initial
|
|
149
|
-
|
|
150
|
-
## 1.0.0-rc.13
|
|
151
|
-
|
|
152
|
-
### Patch Changes
|
|
153
|
-
|
|
154
|
-
- 224f7fe: fix server route match
|
|
155
|
-
- 204c626: feat: initial
|
|
156
|
-
|
|
157
|
-
## 1.0.0-rc.12
|
|
158
|
-
|
|
159
|
-
### Patch Changes
|
|
160
|
-
|
|
161
|
-
- 224f7fe: fix server route match
|
|
162
|
-
- 204c626: feat: initial
|
|
163
|
-
|
|
164
|
-
## 1.0.0-rc.11
|
|
165
|
-
|
|
166
|
-
### Patch Changes
|
|
167
|
-
|
|
168
|
-
- 224f7fe: fix server route match
|
|
169
|
-
- 204c626: feat: initial
|
|
170
|
-
|
|
171
|
-
## 1.0.0-rc.10
|
|
172
|
-
|
|
173
|
-
### Patch Changes
|
|
174
|
-
|
|
175
|
-
- 224f7fe: fix server route match
|
|
176
|
-
- 204c626: feat: initial
|
|
177
|
-
|
|
178
|
-
## 1.0.0-rc.9
|
|
179
|
-
|
|
180
|
-
### Patch Changes
|
|
181
|
-
|
|
182
|
-
- 224f7fe: fix server route match
|
|
183
|
-
- 204c626: feat: initial
|
|
184
|
-
|
|
185
|
-
## 1.0.0-rc.8
|
|
186
|
-
|
|
187
|
-
### Patch Changes
|
|
188
|
-
|
|
189
|
-
- 224f7fe: fix server route match
|
|
190
|
-
- 204c626: feat: initial
|
|
191
|
-
|
|
192
|
-
## 1.0.0-rc.7
|
|
193
|
-
|
|
194
|
-
### Patch Changes
|
|
195
|
-
|
|
196
|
-
- 224f7fe: fix server route match
|
|
197
|
-
- 204c626: feat: initial
|
|
198
|
-
|
|
199
|
-
## 1.0.0-rc.6
|
|
200
|
-
|
|
201
|
-
### Patch Changes
|
|
202
|
-
|
|
203
|
-
- 224f7fe: fix server route match
|
|
204
|
-
- 204c626: feat: initial
|
|
205
|
-
|
|
206
|
-
## 1.0.0-rc.5
|
|
207
|
-
|
|
208
|
-
### Patch Changes
|
|
209
|
-
|
|
210
|
-
- 224f7fe: fix server route match
|
|
211
|
-
- 204c626: feat: initial
|
|
212
|
-
|
|
213
|
-
## 1.0.0-rc.4
|
|
214
|
-
|
|
215
|
-
### Patch Changes
|
|
216
|
-
|
|
217
|
-
- fix server route match
|
|
218
|
-
- 204c626: feat: initial
|
|
219
|
-
|
|
220
|
-
## 1.0.0-rc.3
|
|
221
|
-
|
|
222
|
-
### Patch Changes
|
|
223
|
-
|
|
224
|
-
- feat: initial
|