@modern-js/bff-core 1.22.2-beta.0 → 1.22.2
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 +21 -132
- package/dist/js/modern/api.js +34 -49
- package/dist/js/modern/client/generate-client.js +34 -52
- package/dist/js/modern/client/index.js +1 -1
- package/dist/js/modern/client/result.js +8 -10
- package/dist/js/modern/errors/http.js +14 -8
- package/dist/js/modern/index.js +3 -19
- package/dist/js/modern/operators/http.js +116 -139
- package/dist/js/modern/router/constants.js +13 -31
- package/dist/js/modern/router/index.js +131 -68
- package/dist/js/modern/router/types.js +1 -0
- package/dist/js/modern/router/utils.js +54 -36
- package/dist/js/modern/types.js +47 -45
- package/dist/js/modern/utils/alias.js +50 -35
- package/dist/js/modern/utils/debug.js +2 -5
- package/dist/js/modern/utils/index.js +2 -5
- package/dist/js/modern/utils/meta.js +4 -8
- package/dist/js/modern/utils/storage.js +14 -8
- package/dist/js/modern/utils/validate.js +29 -23
- package/dist/js/node/api.js +46 -78
- package/dist/js/node/client/generate-client.js +55 -85
- package/dist/js/node/client/index.js +18 -17
- package/dist/js/node/client/result.js +18 -32
- package/dist/js/node/errors/http.js +22 -28
- package/dist/js/node/index.js +122 -45
- package/dist/js/node/operators/http.js +169 -184
- package/dist/js/node/router/constants.js +28 -60
- package/dist/js/node/router/index.js +201 -112
- package/dist/js/node/router/types.js +5 -15
- package/dist/js/node/router/utils.js +78 -71
- package/dist/js/node/types.js +57 -71
- package/dist/js/node/utils/alias.js +73 -65
- package/dist/js/node/utils/debug.js +10 -27
- package/dist/js/node/utils/index.js +68 -28
- package/dist/js/node/utils/meta.js +12 -30
- package/dist/js/node/utils/storage.js +24 -36
- package/dist/js/node/utils/validate.js +44 -50
- package/package.json +40 -12
|
@@ -1,76 +1,91 @@
|
|
|
1
|
-
import * as path from
|
|
2
|
-
import * as os from
|
|
3
|
-
import fs from
|
|
4
|
-
import Module from
|
|
5
|
-
const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
|
|
6
|
-
let relativeRuntimePath =
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
)}`;
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import * as os from 'os';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import Module from 'module';
|
|
5
|
+
export const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
|
|
6
|
+
let relativeRuntimePath = '';
|
|
7
|
+
|
|
8
|
+
if (os.platform() === 'win32') {
|
|
9
|
+
// isRelative function in babel-plugin-resolver plugin can't handle windows relative path correctly, see babel-plugin-resolver's utils.
|
|
10
|
+
relativeRuntimePath = `../${path.relative(appDirectory, serverRuntimePath)}`;
|
|
12
11
|
} else {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
path.relative(appDirectory, serverRuntimePath)
|
|
16
|
-
);
|
|
12
|
+
// Look up one level, because the artifacts after build have dist directories
|
|
13
|
+
relativeRuntimePath = path.join('../', path.relative(appDirectory, serverRuntimePath));
|
|
17
14
|
}
|
|
18
|
-
|
|
15
|
+
|
|
16
|
+
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
|
19
17
|
relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
|
|
20
18
|
}
|
|
19
|
+
|
|
21
20
|
return relativeRuntimePath;
|
|
22
21
|
};
|
|
23
|
-
|
|
22
|
+
|
|
23
|
+
const sortByLongestPrefix = arr => {
|
|
24
24
|
return arr.concat().sort((a, b) => b.length - a.length);
|
|
25
25
|
};
|
|
26
|
-
|
|
26
|
+
|
|
27
|
+
export const createMatchPath = paths => {
|
|
27
28
|
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
|
|
28
29
|
const sortedPaths = {};
|
|
29
|
-
sortedKeys.forEach(
|
|
30
|
+
sortedKeys.forEach(key => {
|
|
30
31
|
sortedPaths[key] = paths[key];
|
|
31
32
|
});
|
|
32
|
-
return
|
|
33
|
-
const found = Object.keys(sortedPaths).find(
|
|
33
|
+
return request => {
|
|
34
|
+
const found = Object.keys(sortedPaths).find(key => {
|
|
34
35
|
return request.startsWith(key);
|
|
35
36
|
});
|
|
37
|
+
|
|
36
38
|
if (found) {
|
|
37
39
|
let foundPaths = sortedPaths[found];
|
|
40
|
+
|
|
38
41
|
if (!Array.isArray(foundPaths)) {
|
|
39
42
|
foundPaths = [foundPaths];
|
|
40
43
|
}
|
|
41
|
-
|
|
44
|
+
|
|
45
|
+
foundPaths = foundPaths.filter(foundPath => path.isAbsolute(foundPath));
|
|
46
|
+
|
|
42
47
|
for (const p of foundPaths) {
|
|
43
48
|
const foundPath = request.replace(found, p);
|
|
49
|
+
|
|
44
50
|
if (fs.existsSync(foundPath)) {
|
|
45
51
|
return foundPath;
|
|
46
52
|
}
|
|
47
53
|
}
|
|
54
|
+
|
|
48
55
|
return request.replace(found, foundPaths[0]);
|
|
49
56
|
}
|
|
57
|
+
|
|
50
58
|
return null;
|
|
51
59
|
};
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
60
|
+
}; // every path must be a absolute path;
|
|
61
|
+
|
|
62
|
+
export const registerPaths = paths => {
|
|
63
|
+
const originalResolveFilename = Module._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
64
|
+
|
|
65
|
+
const {
|
|
66
|
+
builtinModules
|
|
67
|
+
} = Module;
|
|
56
68
|
const matchPath = createMatchPath(paths);
|
|
57
|
-
|
|
69
|
+
|
|
70
|
+
Module._resolveFilename = function (request, _parent) {
|
|
58
71
|
const isCoreModule = builtinModules.includes(request);
|
|
72
|
+
|
|
59
73
|
if (!isCoreModule) {
|
|
60
74
|
const matched = matchPath(request);
|
|
75
|
+
|
|
61
76
|
if (matched) {
|
|
62
|
-
|
|
77
|
+
// eslint-disable-next-line prefer-rest-params
|
|
78
|
+
const modifiedArguments = [matched, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
|
|
79
|
+
|
|
63
80
|
return originalResolveFilename.apply(this, modifiedArguments);
|
|
64
81
|
}
|
|
65
|
-
}
|
|
82
|
+
} // eslint-disable-next-line prefer-rest-params
|
|
83
|
+
|
|
84
|
+
|
|
66
85
|
return originalResolveFilename.apply(this, arguments);
|
|
67
86
|
};
|
|
87
|
+
|
|
68
88
|
return () => {
|
|
69
89
|
Module._resolveFilename = originalResolveFilename;
|
|
70
90
|
};
|
|
71
|
-
};
|
|
72
|
-
export {
|
|
73
|
-
createMatchPath,
|
|
74
|
-
getRelativeRuntimePath,
|
|
75
|
-
registerPaths
|
|
76
|
-
};
|
|
91
|
+
};
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
const HANDLER_WITH_META =
|
|
2
|
-
const isWithMetaHandler =
|
|
3
|
-
return typeof handler ===
|
|
4
|
-
};
|
|
5
|
-
export {
|
|
6
|
-
HANDLER_WITH_META,
|
|
7
|
-
isWithMetaHandler
|
|
8
|
-
};
|
|
1
|
+
export const HANDLER_WITH_META = 'HANDLER_WITH_META';
|
|
2
|
+
export const isWithMetaHandler = handler => {
|
|
3
|
+
return typeof handler === 'function' && handler[HANDLER_WITH_META];
|
|
4
|
+
};
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import * as ah from
|
|
1
|
+
import * as ah from 'async_hooks';
|
|
2
|
+
|
|
2
3
|
const createStorage = () => {
|
|
3
4
|
let storage;
|
|
4
|
-
|
|
5
|
+
|
|
6
|
+
if (typeof ah.AsyncLocalStorage !== 'undefined') {
|
|
5
7
|
storage = new ah.AsyncLocalStorage();
|
|
6
8
|
}
|
|
9
|
+
|
|
7
10
|
const run = (context, cb) => {
|
|
8
11
|
if (!storage) {
|
|
9
12
|
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
10
13
|
`);
|
|
11
14
|
}
|
|
15
|
+
|
|
12
16
|
return new Promise((resolve, reject) => {
|
|
13
17
|
storage.run(context, () => {
|
|
14
18
|
try {
|
|
@@ -19,24 +23,26 @@ const createStorage = () => {
|
|
|
19
23
|
});
|
|
20
24
|
});
|
|
21
25
|
};
|
|
26
|
+
|
|
22
27
|
const useContext = () => {
|
|
23
28
|
if (!storage) {
|
|
24
29
|
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
25
30
|
`);
|
|
26
31
|
}
|
|
32
|
+
|
|
27
33
|
const context = storage.getStore();
|
|
34
|
+
|
|
28
35
|
if (!context) {
|
|
29
|
-
throw new Error(
|
|
30
|
-
`Can't call useContext out of scope, it should be placed in the bff function`
|
|
31
|
-
);
|
|
36
|
+
throw new Error(`Can't call useContext out of scope, it should be placed in the bff function`);
|
|
32
37
|
}
|
|
38
|
+
|
|
33
39
|
return context;
|
|
34
40
|
};
|
|
41
|
+
|
|
35
42
|
return {
|
|
36
43
|
run,
|
|
37
44
|
useContext
|
|
38
45
|
};
|
|
39
46
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
};
|
|
47
|
+
|
|
48
|
+
export { createStorage };
|
|
@@ -1,43 +1,49 @@
|
|
|
1
|
-
import util from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
let msg =
|
|
1
|
+
import util from 'util'; // fork from https://github.com/nodejs/node/blob/master/lib/internal/errors.js
|
|
2
|
+
|
|
3
|
+
export const getTypeErrorMessage = actual => {
|
|
4
|
+
let msg = '';
|
|
5
|
+
|
|
5
6
|
if (actual == null) {
|
|
6
7
|
msg += `. Received ${actual}`;
|
|
7
|
-
} else if (typeof actual ===
|
|
8
|
+
} else if (typeof actual === 'function' && actual.name) {
|
|
8
9
|
msg += `. Received function ${actual.name}`;
|
|
9
|
-
} else if (typeof actual ===
|
|
10
|
-
|
|
10
|
+
} else if (typeof actual === 'object') {
|
|
11
|
+
var _actual$constructor;
|
|
12
|
+
|
|
13
|
+
if ((_actual$constructor = actual.constructor) !== null && _actual$constructor !== void 0 && _actual$constructor.name) {
|
|
11
14
|
msg += `. Received an instance of ${actual.constructor.name}`;
|
|
12
15
|
} else {
|
|
13
|
-
const inspected = util.inspect(actual, {
|
|
16
|
+
const inspected = util.inspect(actual, {
|
|
17
|
+
depth: -1
|
|
18
|
+
});
|
|
14
19
|
msg += `. Received ${inspected}`;
|
|
15
20
|
}
|
|
16
21
|
} else {
|
|
17
|
-
let inspected = util.inspect(actual, {
|
|
22
|
+
let inspected = util.inspect(actual, {
|
|
23
|
+
colors: false
|
|
24
|
+
});
|
|
25
|
+
|
|
18
26
|
if (inspected.length > 25) {
|
|
19
27
|
inspected = `${inspected.slice(0, 25)}...`;
|
|
20
28
|
}
|
|
29
|
+
|
|
21
30
|
msg += `. Received type ${typeof actual} (${inspected})`;
|
|
22
31
|
}
|
|
32
|
+
|
|
23
33
|
return msg;
|
|
24
|
-
};
|
|
25
|
-
|
|
34
|
+
}; // eslint-disable-next-line @typescript-eslint/naming-convention
|
|
35
|
+
|
|
36
|
+
export class ERR_INVALID_ARG_TYPE extends Error {
|
|
26
37
|
constructor(funcName, expectedType, actual) {
|
|
27
|
-
const message = `[ERR_INVALID_ARG_TYPE]: The '${funcName}' argument must be of type ${expectedType}${getTypeErrorMessage(
|
|
28
|
-
actual
|
|
29
|
-
)}`;
|
|
38
|
+
const message = `[ERR_INVALID_ARG_TYPE]: The '${funcName}' argument must be of type ${expectedType}${getTypeErrorMessage(actual)}`;
|
|
30
39
|
super(message);
|
|
31
40
|
}
|
|
41
|
+
|
|
32
42
|
}
|
|
33
|
-
const validateFunction = (maybeFunc, name) => {
|
|
34
|
-
if (typeof maybeFunc !==
|
|
35
|
-
throw new ERR_INVALID_ARG_TYPE(name,
|
|
43
|
+
export const validateFunction = (maybeFunc, name) => {
|
|
44
|
+
if (typeof maybeFunc !== 'function') {
|
|
45
|
+
throw new ERR_INVALID_ARG_TYPE(name, 'function', maybeFunc);
|
|
36
46
|
}
|
|
47
|
+
|
|
37
48
|
return true;
|
|
38
|
-
};
|
|
39
|
-
export {
|
|
40
|
-
ERR_INVALID_ARG_TYPE,
|
|
41
|
-
getTypeErrorMessage,
|
|
42
|
-
validateFunction
|
|
43
|
-
};
|
|
49
|
+
};
|
package/dist/js/node/api.js
CHANGED
|
@@ -1,98 +1,66 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
-
mod
|
|
22
|
-
));
|
|
23
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
-
var __async = (__this, __arguments, generator) => {
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
var fulfilled = (value) => {
|
|
27
|
-
try {
|
|
28
|
-
step(generator.next(value));
|
|
29
|
-
} catch (e) {
|
|
30
|
-
reject(e);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
var rejected = (value) => {
|
|
34
|
-
try {
|
|
35
|
-
step(generator.throw(value));
|
|
36
|
-
} catch (e) {
|
|
37
|
-
reject(e);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
var api_exports = {};
|
|
45
|
-
__export(api_exports, {
|
|
46
|
-
Api: () => Api
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
47
5
|
});
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
6
|
+
exports.Api = Api;
|
|
7
|
+
|
|
8
|
+
require("reflect-metadata");
|
|
9
|
+
|
|
10
|
+
var _koaCompose = _interopRequireDefault(require("koa-compose"));
|
|
11
|
+
|
|
12
|
+
var _utils = require("./utils");
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
|
|
52
16
|
function Api(...args) {
|
|
53
17
|
const handler = args.pop();
|
|
54
|
-
(0,
|
|
18
|
+
(0, _utils.validateFunction)(handler, 'Apihandler');
|
|
55
19
|
const operators = args;
|
|
56
20
|
const metadataHelper = {
|
|
57
21
|
getMetadata(key) {
|
|
58
22
|
return Reflect.getMetadata(key, runner);
|
|
59
23
|
},
|
|
24
|
+
|
|
60
25
|
setMetadata(key, value) {
|
|
61
26
|
return Reflect.defineMetadata(key, value, runner);
|
|
62
27
|
}
|
|
28
|
+
|
|
63
29
|
};
|
|
30
|
+
|
|
64
31
|
for (const operator of operators) {
|
|
65
32
|
if (operator.metadata) {
|
|
66
33
|
operator.metadata(metadataHelper);
|
|
67
34
|
}
|
|
68
35
|
}
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
36
|
+
|
|
37
|
+
const validateHandlers = operators.filter(operator => operator.validate).map(operator => operator.validate);
|
|
38
|
+
const pipeHandlers = operators.filter(operator => operator.execute).map(operator => operator.execute);
|
|
39
|
+
|
|
40
|
+
async function runner(inputs) {
|
|
41
|
+
const executeHelper = {
|
|
42
|
+
result: null,
|
|
43
|
+
|
|
44
|
+
get inputs() {
|
|
45
|
+
return inputs;
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
set inputs(val) {
|
|
49
|
+
// eslint-disable-next-line no-param-reassign
|
|
50
|
+
inputs = val;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
};
|
|
54
|
+
const stack = [...validateHandlers, ...pipeHandlers];
|
|
55
|
+
stack.push(async (helper, next) => {
|
|
56
|
+
const res = await handler(helper.inputs);
|
|
57
|
+
helper.result = res;
|
|
58
|
+
return next();
|
|
90
59
|
});
|
|
60
|
+
await (0, _koaCompose.default)(stack)(executeHelper);
|
|
61
|
+
return executeHelper.result;
|
|
91
62
|
}
|
|
92
|
-
|
|
63
|
+
|
|
64
|
+
runner[_utils.HANDLER_WITH_META] = true;
|
|
93
65
|
return runner;
|
|
94
|
-
}
|
|
95
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
96
|
-
0 && (module.exports = {
|
|
97
|
-
Api
|
|
98
|
-
});
|
|
66
|
+
}
|
|
@@ -1,57 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
-
mod
|
|
22
|
-
));
|
|
23
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
-
var __async = (__this, __arguments, generator) => {
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
var fulfilled = (value) => {
|
|
27
|
-
try {
|
|
28
|
-
step(generator.next(value));
|
|
29
|
-
} catch (e) {
|
|
30
|
-
reject(e);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
var rejected = (value) => {
|
|
34
|
-
try {
|
|
35
|
-
step(generator.throw(value));
|
|
36
|
-
} catch (e) {
|
|
37
|
-
reject(e);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
var generate_client_exports = {};
|
|
45
|
-
__export(generate_client_exports, {
|
|
46
|
-
DEFAULT_CLIENT_REQUEST_CREATOR: () => DEFAULT_CLIENT_REQUEST_CREATOR,
|
|
47
|
-
generateClient: () => generateClient
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
48
5
|
});
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
var
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
6
|
+
exports.generateClient = exports.DEFAULT_CLIENT_REQUEST_CREATOR = void 0;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
var _router = require("../router");
|
|
11
|
+
|
|
12
|
+
var _result = require("./result");
|
|
13
|
+
|
|
14
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
15
|
+
|
|
16
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
17
|
+
|
|
18
|
+
const DEFAULT_CLIENT_REQUEST_CREATOR = '@modern-js/create-request';
|
|
19
|
+
exports.DEFAULT_CLIENT_REQUEST_CREATOR = DEFAULT_CLIENT_REQUEST_CREATOR;
|
|
20
|
+
|
|
21
|
+
const generateClient = async ({
|
|
55
22
|
resourcePath,
|
|
56
23
|
apiDir,
|
|
57
24
|
prefix,
|
|
@@ -59,53 +26,56 @@ const generateClient = (_0) => __async(void 0, [_0], function* ({
|
|
|
59
26
|
target,
|
|
60
27
|
requestCreator,
|
|
61
28
|
fetcher,
|
|
62
|
-
requireResolve = require.resolve
|
|
63
|
-
}) {
|
|
29
|
+
requireResolve: _requireResolve = require.resolve
|
|
30
|
+
}) => {
|
|
64
31
|
if (!requestCreator) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
).replace(/\\/g, "/");
|
|
32
|
+
// eslint-disable-next-line no-param-reassign
|
|
33
|
+
requestCreator = _requireResolve(`${DEFAULT_CLIENT_REQUEST_CREATOR}${target ? `/${target}` : ''}`).replace(/\\/g, '/');
|
|
68
34
|
} else {
|
|
35
|
+
// 这里约束传入的 requestCreator 包也必须有两个导出 client 和 server,因为目前的机制 client 和 server 要导出不同的 configure 函数;该 api 不对使用者暴露,后续可优化
|
|
69
36
|
let resolvedPath = requestCreator;
|
|
37
|
+
|
|
70
38
|
try {
|
|
71
|
-
resolvedPath = path.dirname(
|
|
72
|
-
} catch (error) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
"/"
|
|
77
|
-
);
|
|
39
|
+
resolvedPath = path.dirname(_requireResolve(requestCreator));
|
|
40
|
+
} catch (error) {} // eslint-disable-next-line no-param-reassign
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
requestCreator = `${resolvedPath}${target ? `/${target}` : ''}`.replace(/\\/g, '/');
|
|
78
44
|
}
|
|
79
|
-
|
|
45
|
+
|
|
46
|
+
const apiRouter = new _router.ApiRouter({
|
|
80
47
|
apiDir,
|
|
81
48
|
prefix
|
|
82
49
|
});
|
|
83
50
|
const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
|
|
51
|
+
|
|
84
52
|
if (!handlerInfos) {
|
|
85
|
-
return (0,
|
|
53
|
+
return (0, _result.Err)(`generate client error: Cannot require module ${resourcePath}`);
|
|
86
54
|
}
|
|
87
|
-
|
|
55
|
+
|
|
56
|
+
let handlersCode = '';
|
|
57
|
+
|
|
88
58
|
for (const handlerInfo of handlerInfos) {
|
|
89
|
-
const {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
59
|
+
const {
|
|
60
|
+
name,
|
|
61
|
+
httpMethod,
|
|
62
|
+
routePath
|
|
63
|
+
} = handlerInfo;
|
|
64
|
+
let exportStatement = `const ${name} =`;
|
|
65
|
+
|
|
66
|
+
if (name.toLowerCase() === 'default') {
|
|
67
|
+
exportStatement = 'default';
|
|
93
68
|
}
|
|
69
|
+
|
|
94
70
|
const upperHttpMethod = httpMethod.toUpperCase();
|
|
95
71
|
const routeName = routePath;
|
|
96
|
-
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', process.env.PORT || ${String(
|
|
97
|
-
port
|
|
98
|
-
)}${fetcher ? `, fetch` : ""});
|
|
72
|
+
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', process.env.PORT || ${String(port)}${fetcher ? `, fetch` : ''});
|
|
99
73
|
`;
|
|
100
74
|
}
|
|
75
|
+
|
|
101
76
|
const importCode = `import { createRequest } from '${requestCreator}';
|
|
102
|
-
${fetcher ? `import { fetch } from '${fetcher}'
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
108
|
-
0 && (module.exports = {
|
|
109
|
-
DEFAULT_CLIENT_REQUEST_CREATOR,
|
|
110
|
-
generateClient
|
|
111
|
-
});
|
|
77
|
+
${fetcher ? `import { fetch } from '${fetcher}';\n` : ''}`;
|
|
78
|
+
return (0, _result.Ok)(`${importCode}\n${handlersCode}`);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
exports.generateClient = generateClient;
|
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _generateClient = require("./generate-client");
|
|
8
|
+
|
|
9
|
+
Object.keys(_generateClient).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (key in exports && exports[key] === _generateClient[key]) return;
|
|
12
|
+
Object.defineProperty(exports, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _generateClient[key];
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|