@modern-js/bff-core 1.21.2-beta.4 → 1.21.3-beta.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/CHANGELOG.md +9 -0
- package/package.json +6 -5
- package/dist/js/modern/api.js +0 -54
- package/dist/js/modern/client/generate-client.js +0 -63
- package/dist/js/modern/client/index.js +0 -1
- package/dist/js/modern/client/result.js +0 -20
- package/dist/js/modern/errors/http.js +0 -22
- package/dist/js/modern/index.js +0 -7
- package/dist/js/modern/operators/http.js +0 -187
- package/dist/js/modern/router/constants.js +0 -14
- package/dist/js/modern/router/index.js +0 -299
- package/dist/js/modern/router/types.js +0 -1
- package/dist/js/modern/router/utils.js +0 -101
- package/dist/js/modern/types.js +0 -47
- package/dist/js/modern/utils/alias.js +0 -91
- package/dist/js/modern/utils/debug.js +0 -2
- package/dist/js/modern/utils/index.js +0 -5
- package/dist/js/modern/utils/meta.js +0 -4
- package/dist/js/modern/utils/storage.js +0 -48
- package/dist/js/modern/utils/validate.js +0 -49
- package/dist/js/node/api.js +0 -66
- package/dist/js/node/client/generate-client.js +0 -81
- package/dist/js/node/client/index.js +0 -18
- package/dist/js/node/client/result.js +0 -32
- package/dist/js/node/errors/http.js +0 -34
- package/dist/js/node/index.js +0 -125
- package/dist/js/node/operators/http.js +0 -226
- package/dist/js/node/router/constants.js +0 -29
- package/dist/js/node/router/index.js +0 -345
- package/dist/js/node/router/types.js +0 -5
- package/dist/js/node/router/utils.js +0 -123
- package/dist/js/node/types.js +0 -59
- package/dist/js/node/utils/alias.js +0 -115
- package/dist/js/node/utils/debug.js +0 -11
- package/dist/js/node/utils/index.js +0 -72
- package/dist/js/node/utils/meta.js +0 -14
- package/dist/js/node/utils/storage.js +0 -59
- package/dist/js/node/utils/validate.js +0 -68
- package/dist/types/api.d.ts +0 -3
- package/dist/types/client/generate-client.d.ts +0 -24
- package/dist/types/client/index.d.ts +0 -1
- package/dist/types/client/result.d.ts +0 -15
- package/dist/types/errors/http.d.ts +0 -8
- package/dist/types/index.d.ts +0 -7
- package/dist/types/operators/http.d.ts +0 -39
- package/dist/types/router/constants.d.ts +0 -17
- package/dist/types/router/index.d.ts +0 -51
- package/dist/types/router/types.d.ts +0 -17
- package/dist/types/router/utils.d.ts +0 -10
- package/dist/types/types.d.ts +0 -58
- package/dist/types/utils/alias.d.ts +0 -7
- package/dist/types/utils/debug.d.ts +0 -1
- package/dist/types/utils/index.d.ts +0 -5
- package/dist/types/utils/meta.d.ts +0 -2
- package/dist/types/utils/storage.d.ts +0 -5
- package/dist/types/utils/validate.d.ts +0 -5
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.21.
|
|
14
|
+
"version": "1.21.3-beta.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -20,21 +20,22 @@
|
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
22
|
"jsnext:source": "./src/index.ts",
|
|
23
|
+
"types": "./dist/types/index.d.ts",
|
|
23
24
|
"default": "./dist/js/node/index.js"
|
|
24
25
|
}
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {
|
|
27
28
|
"@babel/runtime": "^7.18.0",
|
|
28
|
-
"@modern-js/bff-runtime": "1.21.
|
|
29
|
-
"@modern-js/utils": "1.21.
|
|
29
|
+
"@modern-js/bff-runtime": "1.21.2",
|
|
30
|
+
"@modern-js/utils": "1.21.2",
|
|
30
31
|
"esbuild": "^0.14.38",
|
|
31
32
|
"esbuild-register": "^3.3.3",
|
|
32
33
|
"koa-compose": "^4.1.0",
|
|
33
34
|
"reflect-metadata": "^0.1.13"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
|
-
"@scripts/build": "1.21.
|
|
37
|
-
"@scripts/jest-config": "1.21.
|
|
37
|
+
"@scripts/build": "1.21.2",
|
|
38
|
+
"@scripts/jest-config": "1.21.2",
|
|
38
39
|
"@types/jest": "^27",
|
|
39
40
|
"@types/koa-compose": "^3.2.5",
|
|
40
41
|
"@types/node": "^14",
|
package/dist/js/modern/api.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
import compose from 'koa-compose';
|
|
3
|
-
import { validateFunction, HANDLER_WITH_META } from "./utils";
|
|
4
|
-
export function Api(...args) {
|
|
5
|
-
const handler = args.pop();
|
|
6
|
-
validateFunction(handler, 'Apihandler');
|
|
7
|
-
const operators = args;
|
|
8
|
-
const metadataHelper = {
|
|
9
|
-
getMetadata(key) {
|
|
10
|
-
return Reflect.getMetadata(key, runner);
|
|
11
|
-
},
|
|
12
|
-
|
|
13
|
-
setMetadata(key, value) {
|
|
14
|
-
return Reflect.defineMetadata(key, value, runner);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
for (const operator of operators) {
|
|
20
|
-
if (operator.metadata) {
|
|
21
|
-
operator.metadata(metadataHelper);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const validateHandlers = operators.filter(operator => operator.validate).map(operator => operator.validate);
|
|
26
|
-
const pipeHandlers = operators.filter(operator => operator.execute).map(operator => operator.execute);
|
|
27
|
-
|
|
28
|
-
async function runner(inputs) {
|
|
29
|
-
const executeHelper = {
|
|
30
|
-
result: null,
|
|
31
|
-
|
|
32
|
-
get inputs() {
|
|
33
|
-
return inputs;
|
|
34
|
-
},
|
|
35
|
-
|
|
36
|
-
set inputs(val) {
|
|
37
|
-
// eslint-disable-next-line no-param-reassign
|
|
38
|
-
inputs = val;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
};
|
|
42
|
-
const stack = [...validateHandlers, ...pipeHandlers];
|
|
43
|
-
stack.push(async (helper, next) => {
|
|
44
|
-
const res = await handler(helper.inputs);
|
|
45
|
-
helper.result = res;
|
|
46
|
-
return next();
|
|
47
|
-
});
|
|
48
|
-
await compose(stack)(executeHelper);
|
|
49
|
-
return executeHelper.result;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
runner[HANDLER_WITH_META] = true;
|
|
53
|
-
return runner;
|
|
54
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import { ApiRouter } from "../router";
|
|
3
|
-
import { Ok, Err } from "./result";
|
|
4
|
-
export const DEFAULT_CLIENT_REQUEST_CREATOR = '@modern-js/create-request';
|
|
5
|
-
export const generateClient = async ({
|
|
6
|
-
resourcePath,
|
|
7
|
-
apiDir,
|
|
8
|
-
prefix,
|
|
9
|
-
port,
|
|
10
|
-
target,
|
|
11
|
-
requestCreator,
|
|
12
|
-
fetcher,
|
|
13
|
-
requireResolve: _requireResolve = require.resolve
|
|
14
|
-
}) => {
|
|
15
|
-
if (!requestCreator) {
|
|
16
|
-
// eslint-disable-next-line no-param-reassign
|
|
17
|
-
requestCreator = _requireResolve(`${DEFAULT_CLIENT_REQUEST_CREATOR}${target ? `/${target}` : ''}`).replace(/\\/g, '/');
|
|
18
|
-
} else {
|
|
19
|
-
// 这里约束传入的 requestCreator 包也必须有两个导出 client 和 server,因为目前的机制 client 和 server 要导出不同的 configure 函数;该 api 不对使用者暴露,后续可优化
|
|
20
|
-
let resolvedPath = requestCreator;
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
resolvedPath = path.dirname(_requireResolve(requestCreator));
|
|
24
|
-
} catch (error) {} // eslint-disable-next-line no-param-reassign
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
requestCreator = `${resolvedPath}${target ? `/${target}` : ''}`.replace(/\\/g, '/');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const apiRouter = new ApiRouter({
|
|
31
|
-
apiDir,
|
|
32
|
-
prefix
|
|
33
|
-
});
|
|
34
|
-
const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
|
|
35
|
-
|
|
36
|
-
if (!handlerInfos) {
|
|
37
|
-
return Err(`generate client error: Cannot require module ${resourcePath}`);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
let handlersCode = '';
|
|
41
|
-
|
|
42
|
-
for (const handlerInfo of handlerInfos) {
|
|
43
|
-
const {
|
|
44
|
-
name,
|
|
45
|
-
httpMethod,
|
|
46
|
-
routePath
|
|
47
|
-
} = handlerInfo;
|
|
48
|
-
let exportStatement = `const ${name} =`;
|
|
49
|
-
|
|
50
|
-
if (name.toLowerCase() === 'default') {
|
|
51
|
-
exportStatement = 'default';
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const upperHttpMethod = httpMethod.toUpperCase();
|
|
55
|
-
const routeName = routePath;
|
|
56
|
-
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', process.env.PORT || ${String(port)}${fetcher ? `, fetch` : ''});
|
|
57
|
-
`;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const importCode = `import { createRequest } from '${requestCreator}';
|
|
61
|
-
${fetcher ? `import { fetch } from '${fetcher}';\n` : ''}`;
|
|
62
|
-
return Ok(`${importCode}\n${handlersCode}`);
|
|
63
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./generate-client";
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
2
|
-
export const Err = value => {
|
|
3
|
-
const err = {
|
|
4
|
-
kind: 'Err',
|
|
5
|
-
value,
|
|
6
|
-
isErr: true,
|
|
7
|
-
isOk: false
|
|
8
|
-
};
|
|
9
|
-
return err;
|
|
10
|
-
}; // eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
11
|
-
|
|
12
|
-
export const Ok = value => {
|
|
13
|
-
const ok = {
|
|
14
|
-
kind: 'Ok',
|
|
15
|
-
value,
|
|
16
|
-
isErr: false,
|
|
17
|
-
isOk: true
|
|
18
|
-
};
|
|
19
|
-
return ok;
|
|
20
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
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; }
|
|
2
|
-
|
|
3
|
-
export class HttpError extends Error {
|
|
4
|
-
constructor(status, message) {
|
|
5
|
-
super(message);
|
|
6
|
-
|
|
7
|
-
_defineProperty(this, "status", void 0);
|
|
8
|
-
|
|
9
|
-
this.status = status;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
}
|
|
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
|
-
}
|
package/dist/js/modern/index.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { Api } from "./api";
|
|
2
|
-
export { HttpError, ValidationError } from "./errors/http";
|
|
3
|
-
export * from "./router";
|
|
4
|
-
export * from "./types";
|
|
5
|
-
export * from "./client";
|
|
6
|
-
export * from "./operators/http";
|
|
7
|
-
export { getRelativeRuntimePath, HANDLER_WITH_META, isWithMetaHandler, createStorage, registerPaths } from "./utils";
|
|
@@ -1,187 +0,0 @@
|
|
|
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
|
-
|
|
7
|
-
import { HttpMetadata, OperatorType, HttpMethod, TriggerType, ResponseMetaType } from "../types";
|
|
8
|
-
import { ValidationError } from "../errors/http";
|
|
9
|
-
|
|
10
|
-
const validateInput = async (schema, input) => {
|
|
11
|
-
try {
|
|
12
|
-
return await schema.parseAsync(input);
|
|
13
|
-
} catch (error) {
|
|
14
|
-
const {
|
|
15
|
-
z: zod
|
|
16
|
-
} = require('zod');
|
|
17
|
-
|
|
18
|
-
if (error instanceof zod.ZodError) {
|
|
19
|
-
throw new ValidationError(400, error.message);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
throw error;
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export const createHttpOperator = method => {
|
|
27
|
-
return urlPath => {
|
|
28
|
-
return {
|
|
29
|
-
name: method,
|
|
30
|
-
|
|
31
|
-
metadata({
|
|
32
|
-
setMetadata
|
|
33
|
-
}) {
|
|
34
|
-
setMetadata(OperatorType.Trigger, {
|
|
35
|
-
type: TriggerType.Http,
|
|
36
|
-
path: urlPath,
|
|
37
|
-
method
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
export const Get = createHttpOperator(HttpMethod.Get);
|
|
45
|
-
export const Post = createHttpOperator(HttpMethod.Post);
|
|
46
|
-
export const Put = createHttpOperator(HttpMethod.Put);
|
|
47
|
-
export const Delete = createHttpOperator(HttpMethod.Delete);
|
|
48
|
-
export const Connect = createHttpOperator(HttpMethod.Connect);
|
|
49
|
-
export const Trace = createHttpOperator(HttpMethod.Trace);
|
|
50
|
-
export const Patch = createHttpOperator(HttpMethod.Patch);
|
|
51
|
-
export const Option = createHttpOperator(HttpMethod.Option);
|
|
52
|
-
export const Head = createHttpOperator(HttpMethod.Head);
|
|
53
|
-
export const Data = schema => {
|
|
54
|
-
return {
|
|
55
|
-
name: HttpMetadata.Data,
|
|
56
|
-
|
|
57
|
-
metadata({
|
|
58
|
-
setMetadata
|
|
59
|
-
}) {
|
|
60
|
-
setMetadata(HttpMetadata.Data, schema);
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
async validate(helper, next) {
|
|
64
|
-
const {
|
|
65
|
-
inputs: {
|
|
66
|
-
data
|
|
67
|
-
}
|
|
68
|
-
} = helper;
|
|
69
|
-
helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
|
|
70
|
-
data: await validateInput(schema, data)
|
|
71
|
-
});
|
|
72
|
-
return next();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
};
|
|
76
|
-
};
|
|
77
|
-
export const Query = schema => {
|
|
78
|
-
return {
|
|
79
|
-
name: HttpMetadata.Query,
|
|
80
|
-
|
|
81
|
-
metadata({
|
|
82
|
-
setMetadata
|
|
83
|
-
}) {
|
|
84
|
-
setMetadata(HttpMetadata.Query, schema);
|
|
85
|
-
},
|
|
86
|
-
|
|
87
|
-
async validate(helper, next) {
|
|
88
|
-
const {
|
|
89
|
-
inputs: {
|
|
90
|
-
query
|
|
91
|
-
}
|
|
92
|
-
} = helper;
|
|
93
|
-
helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
|
|
94
|
-
query: await validateInput(schema, query)
|
|
95
|
-
});
|
|
96
|
-
return next();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
};
|
|
100
|
-
};
|
|
101
|
-
export const Params = schema => {
|
|
102
|
-
return {
|
|
103
|
-
name: HttpMetadata.Params,
|
|
104
|
-
|
|
105
|
-
metadata({
|
|
106
|
-
setMetadata
|
|
107
|
-
}) {
|
|
108
|
-
setMetadata(HttpMetadata.Params, schema);
|
|
109
|
-
},
|
|
110
|
-
|
|
111
|
-
async validate(helper, next) {
|
|
112
|
-
const {
|
|
113
|
-
inputs: {
|
|
114
|
-
params
|
|
115
|
-
}
|
|
116
|
-
} = helper;
|
|
117
|
-
helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
|
|
118
|
-
params: await validateInput(schema, params)
|
|
119
|
-
});
|
|
120
|
-
return next();
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
};
|
|
124
|
-
};
|
|
125
|
-
export const Headers = schema => {
|
|
126
|
-
return {
|
|
127
|
-
name: HttpMetadata.Headers,
|
|
128
|
-
|
|
129
|
-
metadata({
|
|
130
|
-
setMetadata
|
|
131
|
-
}) {
|
|
132
|
-
setMetadata(HttpMetadata.Headers, schema);
|
|
133
|
-
},
|
|
134
|
-
|
|
135
|
-
async validate(helper, next) {
|
|
136
|
-
const {
|
|
137
|
-
inputs: {
|
|
138
|
-
headers
|
|
139
|
-
}
|
|
140
|
-
} = helper;
|
|
141
|
-
helper.inputs = _objectSpread(_objectSpread({}, helper.inputs), {}, {
|
|
142
|
-
headers: await validateInput(schema, headers)
|
|
143
|
-
});
|
|
144
|
-
return next();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
};
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
const setResponseMeta = (helper, type, value) => {
|
|
151
|
-
const responseMetaData = helper.getMetadata(HttpMetadata.Response) || [];
|
|
152
|
-
helper.setMetadata(HttpMetadata.Response, [...responseMetaData, {
|
|
153
|
-
type,
|
|
154
|
-
value
|
|
155
|
-
}]);
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
export const HttpCode = statusCode => {
|
|
159
|
-
return {
|
|
160
|
-
name: 'HttpCode',
|
|
161
|
-
|
|
162
|
-
metadata(helper) {
|
|
163
|
-
setResponseMeta(helper, ResponseMetaType.StatusCode, statusCode);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
};
|
|
167
|
-
};
|
|
168
|
-
export const SetHeaders = headers => {
|
|
169
|
-
return {
|
|
170
|
-
name: 'SetHeaders',
|
|
171
|
-
|
|
172
|
-
metadata(helper) {
|
|
173
|
-
setResponseMeta(helper, ResponseMetaType.Headers, headers);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
};
|
|
177
|
-
};
|
|
178
|
-
export const Redirect = url => {
|
|
179
|
-
return {
|
|
180
|
-
name: 'Redirect',
|
|
181
|
-
|
|
182
|
-
metadata(helper) {
|
|
183
|
-
setResponseMeta(helper, ResponseMetaType.Redirect, url);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
};
|
|
187
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { HttpMethod } from "../types";
|
|
2
|
-
export const AllHttpMethods = Object.values(HttpMethod);
|
|
3
|
-
export let APIMode;
|
|
4
|
-
|
|
5
|
-
(function (APIMode) {
|
|
6
|
-
APIMode["FARMEWORK"] = "framework";
|
|
7
|
-
APIMode["FUNCTION"] = "function";
|
|
8
|
-
})(APIMode || (APIMode = {}));
|
|
9
|
-
|
|
10
|
-
export const FRAMEWORK_MODE_LAMBDA_DIR = 'lambda';
|
|
11
|
-
export const FRAMEWORK_MODE_APP_DIR = 'app';
|
|
12
|
-
export const INDEX_SUFFIX = 'index';
|
|
13
|
-
export const API_DIR = 'api';
|
|
14
|
-
export const API_FILE_RULES = ['**/*.[tj]s', '!**/_*', '!**/_*/**/*.[tj]s', '!**/*.test.js', '!**/*.test.ts', '!**/*.d.ts', '!__test__/*.ts', '!__tests__/*.ts', '!node_modules/**', '!bootstrap.js'];
|