@modern-js/bff-core 2.7.0 → 2.7.1-alpha.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/dist/js/modern/api.js +69 -0
- package/dist/js/modern/client/generate-client.js +79 -0
- package/dist/js/modern/client/index.js +1 -0
- package/dist/js/modern/client/result.js +22 -0
- package/dist/js/modern/errors/http.js +16 -0
- package/dist/js/modern/index.js +23 -0
- package/dist/js/modern/operators/http.js +210 -0
- package/dist/js/modern/router/constants.js +32 -0
- package/dist/js/modern/router/index.js +236 -0
- package/dist/js/modern/router/types.js +0 -0
- package/dist/js/modern/router/utils.js +83 -0
- package/dist/js/modern/types.js +45 -0
- package/dist/js/modern/utils/alias.js +76 -0
- package/dist/js/modern/utils/debug.js +5 -0
- package/dist/js/modern/utils/index.js +8 -0
- package/dist/js/modern/utils/meta.js +8 -0
- package/dist/js/modern/utils/storage.js +42 -0
- package/dist/js/modern/utils/validate.js +43 -0
- package/dist/js/node/api.js +98 -0
- package/dist/js/node/client/generate-client.js +109 -0
- package/dist/js/node/client/index.js +17 -0
- package/dist/js/node/client/result.js +46 -0
- package/dist/js/node/errors/http.js +40 -0
- package/dist/js/node/index.js +48 -0
- package/dist/js/node/operators/http.js +241 -0
- package/dist/js/node/router/constants.js +61 -0
- package/dist/js/node/router/index.js +256 -0
- package/dist/js/node/router/types.js +15 -0
- package/dist/js/node/router/utils.js +116 -0
- package/dist/js/node/types.js +73 -0
- package/dist/js/node/utils/alias.js +107 -0
- package/dist/js/node/utils/debug.js +28 -0
- package/dist/js/node/utils/index.js +32 -0
- package/dist/js/node/utils/meta.js +32 -0
- package/dist/js/node/utils/storage.js +71 -0
- package/dist/js/node/utils/validate.js +74 -0
- package/dist/types/types.d.ts +1 -1
- package/package.json +8 -8
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { globby } from "@modern-js/utils";
|
|
3
|
+
import { INDEX_SUFFIX } from "./constants";
|
|
4
|
+
const getFiles = (lambdaDir, rules) => globby.sync(rules, {
|
|
5
|
+
cwd: lambdaDir,
|
|
6
|
+
gitignore: true
|
|
7
|
+
}).map((file) => path.resolve(lambdaDir, file));
|
|
8
|
+
const getPathFromFilename = (baseDir, filename) => {
|
|
9
|
+
const relativeName = filename.substring(baseDir.length);
|
|
10
|
+
const relativePath = relativeName.split(".").slice(0, -1).join(".");
|
|
11
|
+
const nameSplit = relativePath.split(path.sep).map((item) => {
|
|
12
|
+
if (item.length > 2) {
|
|
13
|
+
if (item.startsWith("[") && item.endsWith("]")) {
|
|
14
|
+
return `:${item.substring(1, item.length - 1)}`;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return item;
|
|
18
|
+
});
|
|
19
|
+
const name = nameSplit.join("/");
|
|
20
|
+
const finalName = name.endsWith(INDEX_SUFFIX) ? name.substring(0, name.length - INDEX_SUFFIX.length) : name;
|
|
21
|
+
return clearRouteName(finalName);
|
|
22
|
+
};
|
|
23
|
+
const clearRouteName = (routeName) => {
|
|
24
|
+
let finalRouteName = routeName.trim();
|
|
25
|
+
if (!finalRouteName.startsWith("/")) {
|
|
26
|
+
finalRouteName = `/${finalRouteName}`;
|
|
27
|
+
}
|
|
28
|
+
if (finalRouteName.length > 1 && finalRouteName.endsWith("/")) {
|
|
29
|
+
finalRouteName = finalRouteName.substring(0, finalRouteName.length - 1);
|
|
30
|
+
}
|
|
31
|
+
return finalRouteName;
|
|
32
|
+
};
|
|
33
|
+
const isHandler = (input) => input && typeof input === "function";
|
|
34
|
+
const enableRegister = (requireFn) => {
|
|
35
|
+
let existTsLoader = false;
|
|
36
|
+
let firstCall = true;
|
|
37
|
+
return (modulePath) => {
|
|
38
|
+
if (firstCall) {
|
|
39
|
+
existTsLoader = Boolean(require.extensions[".ts"]);
|
|
40
|
+
firstCall = false;
|
|
41
|
+
}
|
|
42
|
+
if (!existTsLoader) {
|
|
43
|
+
const {
|
|
44
|
+
register
|
|
45
|
+
} = require("esbuild-register/dist/node");
|
|
46
|
+
const { unregister } = register({
|
|
47
|
+
extensions: [".ts"]
|
|
48
|
+
});
|
|
49
|
+
const requiredModule2 = requireFn(modulePath);
|
|
50
|
+
unregister();
|
|
51
|
+
return requiredModule2;
|
|
52
|
+
}
|
|
53
|
+
const requiredModule = requireFn(modulePath);
|
|
54
|
+
return requiredModule;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
const isFunction = (input) => input && {}.toString.call(input) === "[object Function]";
|
|
58
|
+
const requireHandlerModule = enableRegister((modulePath) => {
|
|
59
|
+
const originRequire = process.env.NODE_ENV === "test" ? jest.requireActual : require;
|
|
60
|
+
const module = originRequire(modulePath);
|
|
61
|
+
if (isFunction(module)) {
|
|
62
|
+
return { default: module };
|
|
63
|
+
}
|
|
64
|
+
return module;
|
|
65
|
+
});
|
|
66
|
+
const routeValue = (routePath) => {
|
|
67
|
+
if (routePath.includes(":")) {
|
|
68
|
+
return 11;
|
|
69
|
+
}
|
|
70
|
+
return 1;
|
|
71
|
+
};
|
|
72
|
+
const sortRoutes = (apiHandlers) => {
|
|
73
|
+
return apiHandlers.sort((handlerA, handlerB) => {
|
|
74
|
+
return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
export {
|
|
78
|
+
getFiles,
|
|
79
|
+
getPathFromFilename,
|
|
80
|
+
isHandler,
|
|
81
|
+
requireHandlerModule,
|
|
82
|
+
sortRoutes
|
|
83
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
var OperatorType = /* @__PURE__ */ ((OperatorType2) => {
|
|
2
|
+
OperatorType2[OperatorType2["Trigger"] = 0] = "Trigger";
|
|
3
|
+
OperatorType2[OperatorType2["Middleware"] = 1] = "Middleware";
|
|
4
|
+
return OperatorType2;
|
|
5
|
+
})(OperatorType || {});
|
|
6
|
+
var TriggerType = /* @__PURE__ */ ((TriggerType2) => {
|
|
7
|
+
TriggerType2[TriggerType2["Http"] = 0] = "Http";
|
|
8
|
+
return TriggerType2;
|
|
9
|
+
})(TriggerType || {});
|
|
10
|
+
var HttpMetadata = /* @__PURE__ */ ((HttpMetadata2) => {
|
|
11
|
+
HttpMetadata2["Method"] = "METHOD";
|
|
12
|
+
HttpMetadata2["Data"] = "DATA";
|
|
13
|
+
HttpMetadata2["Query"] = "QUERY";
|
|
14
|
+
HttpMetadata2["Params"] = "PARAMS";
|
|
15
|
+
HttpMetadata2["Headers"] = "HEADERS";
|
|
16
|
+
HttpMetadata2["Response"] = "RESPONSE";
|
|
17
|
+
return HttpMetadata2;
|
|
18
|
+
})(HttpMetadata || {});
|
|
19
|
+
var ResponseMetaType = /* @__PURE__ */ ((ResponseMetaType2) => {
|
|
20
|
+
ResponseMetaType2[ResponseMetaType2["StatusCode"] = 0] = "StatusCode";
|
|
21
|
+
ResponseMetaType2[ResponseMetaType2["Redirect"] = 1] = "Redirect";
|
|
22
|
+
ResponseMetaType2[ResponseMetaType2["Headers"] = 2] = "Headers";
|
|
23
|
+
return ResponseMetaType2;
|
|
24
|
+
})(ResponseMetaType || {});
|
|
25
|
+
var HttpMethod = /* @__PURE__ */ ((HttpMethod2) => {
|
|
26
|
+
HttpMethod2["Get"] = "GET";
|
|
27
|
+
HttpMethod2["Post"] = "POST";
|
|
28
|
+
HttpMethod2["Put"] = "PUT";
|
|
29
|
+
HttpMethod2["Delete"] = "DELETE";
|
|
30
|
+
HttpMethod2["Connect"] = "CONNECT";
|
|
31
|
+
HttpMethod2["Trace"] = "TRACE";
|
|
32
|
+
HttpMethod2["Patch"] = "PATCH";
|
|
33
|
+
HttpMethod2["Option"] = "OPTION";
|
|
34
|
+
HttpMethod2["Head"] = "HEAD";
|
|
35
|
+
return HttpMethod2;
|
|
36
|
+
})(HttpMethod || {});
|
|
37
|
+
const httpMethods = Object.values(HttpMethod);
|
|
38
|
+
export {
|
|
39
|
+
HttpMetadata,
|
|
40
|
+
HttpMethod,
|
|
41
|
+
OperatorType,
|
|
42
|
+
ResponseMetaType,
|
|
43
|
+
TriggerType,
|
|
44
|
+
httpMethods
|
|
45
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import * as os from "os";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import Module from "module";
|
|
5
|
+
const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
|
|
6
|
+
let relativeRuntimePath = "";
|
|
7
|
+
if (os.platform() === "win32") {
|
|
8
|
+
relativeRuntimePath = `../${path.relative(
|
|
9
|
+
appDirectory,
|
|
10
|
+
serverRuntimePath
|
|
11
|
+
)}`;
|
|
12
|
+
} else {
|
|
13
|
+
relativeRuntimePath = path.join(
|
|
14
|
+
"../",
|
|
15
|
+
path.relative(appDirectory, serverRuntimePath)
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {
|
|
19
|
+
relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
|
|
20
|
+
}
|
|
21
|
+
return relativeRuntimePath;
|
|
22
|
+
};
|
|
23
|
+
const sortByLongestPrefix = (arr) => {
|
|
24
|
+
return arr.concat().sort((a, b) => b.length - a.length);
|
|
25
|
+
};
|
|
26
|
+
const createMatchPath = (paths) => {
|
|
27
|
+
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
|
|
28
|
+
const sortedPaths = {};
|
|
29
|
+
sortedKeys.forEach((key) => {
|
|
30
|
+
sortedPaths[key] = paths[key];
|
|
31
|
+
});
|
|
32
|
+
return (request) => {
|
|
33
|
+
const found = Object.keys(sortedPaths).find((key) => {
|
|
34
|
+
return request.startsWith(key);
|
|
35
|
+
});
|
|
36
|
+
if (found) {
|
|
37
|
+
let foundPaths = sortedPaths[found];
|
|
38
|
+
if (!Array.isArray(foundPaths)) {
|
|
39
|
+
foundPaths = [foundPaths];
|
|
40
|
+
}
|
|
41
|
+
foundPaths = foundPaths.filter((foundPath) => path.isAbsolute(foundPath));
|
|
42
|
+
for (const p of foundPaths) {
|
|
43
|
+
const foundPath = request.replace(found, p);
|
|
44
|
+
if (fs.existsSync(foundPath)) {
|
|
45
|
+
return foundPath;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return request.replace(found, foundPaths[0]);
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
const registerPaths = (paths) => {
|
|
54
|
+
const originalResolveFilename = Module._resolveFilename;
|
|
55
|
+
const { builtinModules } = Module;
|
|
56
|
+
const matchPath = createMatchPath(paths);
|
|
57
|
+
Module._resolveFilename = function(request, _parent) {
|
|
58
|
+
const isCoreModule = builtinModules.includes(request);
|
|
59
|
+
if (!isCoreModule) {
|
|
60
|
+
const matched = matchPath(request);
|
|
61
|
+
if (matched) {
|
|
62
|
+
const modifiedArguments = [matched, ...[].slice.call(arguments, 1)];
|
|
63
|
+
return originalResolveFilename.apply(this, modifiedArguments);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return originalResolveFilename.apply(this, arguments);
|
|
67
|
+
};
|
|
68
|
+
return () => {
|
|
69
|
+
Module._resolveFilename = originalResolveFilename;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
export {
|
|
73
|
+
createMatchPath,
|
|
74
|
+
getRelativeRuntimePath,
|
|
75
|
+
registerPaths
|
|
76
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as ah from "async_hooks";
|
|
2
|
+
const createStorage = () => {
|
|
3
|
+
let storage;
|
|
4
|
+
if (typeof ah.AsyncLocalStorage !== "undefined") {
|
|
5
|
+
storage = new ah.AsyncLocalStorage();
|
|
6
|
+
}
|
|
7
|
+
const run = (context, cb) => {
|
|
8
|
+
if (!storage) {
|
|
9
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
10
|
+
`);
|
|
11
|
+
}
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
storage.run(context, () => {
|
|
14
|
+
try {
|
|
15
|
+
return resolve(cb());
|
|
16
|
+
} catch (error) {
|
|
17
|
+
return reject(error);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
const useContext = () => {
|
|
23
|
+
if (!storage) {
|
|
24
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
25
|
+
`);
|
|
26
|
+
}
|
|
27
|
+
const context = storage.getStore();
|
|
28
|
+
if (!context) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Can't call useContext out of scope, it should be placed in the bff function`
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
return context;
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
run,
|
|
37
|
+
useContext
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
export {
|
|
41
|
+
createStorage
|
|
42
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import util from "util";
|
|
2
|
+
const getTypeErrorMessage = (actual) => {
|
|
3
|
+
var _a;
|
|
4
|
+
let msg = "";
|
|
5
|
+
if (actual == null) {
|
|
6
|
+
msg += `. Received ${actual}`;
|
|
7
|
+
} else if (typeof actual === "function" && actual.name) {
|
|
8
|
+
msg += `. Received function ${actual.name}`;
|
|
9
|
+
} else if (typeof actual === "object") {
|
|
10
|
+
if ((_a = actual.constructor) == null ? void 0 : _a.name) {
|
|
11
|
+
msg += `. Received an instance of ${actual.constructor.name}`;
|
|
12
|
+
} else {
|
|
13
|
+
const inspected = util.inspect(actual, { depth: -1 });
|
|
14
|
+
msg += `. Received ${inspected}`;
|
|
15
|
+
}
|
|
16
|
+
} else {
|
|
17
|
+
let inspected = util.inspect(actual, { colors: false });
|
|
18
|
+
if (inspected.length > 25) {
|
|
19
|
+
inspected = `${inspected.slice(0, 25)}...`;
|
|
20
|
+
}
|
|
21
|
+
msg += `. Received type ${typeof actual} (${inspected})`;
|
|
22
|
+
}
|
|
23
|
+
return msg;
|
|
24
|
+
};
|
|
25
|
+
class ERR_INVALID_ARG_TYPE extends Error {
|
|
26
|
+
constructor(funcName, expectedType, actual) {
|
|
27
|
+
const message = `[ERR_INVALID_ARG_TYPE]: The '${funcName}' argument must be of type ${expectedType}${getTypeErrorMessage(
|
|
28
|
+
actual
|
|
29
|
+
)}`;
|
|
30
|
+
super(message);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const validateFunction = (maybeFunc, name) => {
|
|
34
|
+
if (typeof maybeFunc !== "function") {
|
|
35
|
+
throw new ERR_INVALID_ARG_TYPE(name, "function", maybeFunc);
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
};
|
|
39
|
+
export {
|
|
40
|
+
ERR_INVALID_ARG_TYPE,
|
|
41
|
+
getTypeErrorMessage,
|
|
42
|
+
validateFunction
|
|
43
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
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
|
|
47
|
+
});
|
|
48
|
+
module.exports = __toCommonJS(api_exports);
|
|
49
|
+
var import_reflect_metadata = require("reflect-metadata");
|
|
50
|
+
var import_koa_compose = __toESM(require("koa-compose"));
|
|
51
|
+
var import_utils = require("./utils");
|
|
52
|
+
function Api(...args) {
|
|
53
|
+
const handler = args.pop();
|
|
54
|
+
(0, import_utils.validateFunction)(handler, "Apihandler");
|
|
55
|
+
const operators = args;
|
|
56
|
+
const metadataHelper = {
|
|
57
|
+
getMetadata(key) {
|
|
58
|
+
return Reflect.getMetadata(key, runner);
|
|
59
|
+
},
|
|
60
|
+
setMetadata(key, value) {
|
|
61
|
+
return Reflect.defineMetadata(key, value, runner);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
for (const operator of operators) {
|
|
65
|
+
if (operator.metadata) {
|
|
66
|
+
operator.metadata(metadataHelper);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const validateHandlers = operators.filter((operator) => operator.validate).map((operator) => operator.validate);
|
|
70
|
+
const pipeHandlers = operators.filter((operator) => operator.execute).map((operator) => operator.execute);
|
|
71
|
+
function runner(inputs) {
|
|
72
|
+
return __async(this, null, function* () {
|
|
73
|
+
const executeHelper = {
|
|
74
|
+
result: null,
|
|
75
|
+
get inputs() {
|
|
76
|
+
return inputs;
|
|
77
|
+
},
|
|
78
|
+
set inputs(val) {
|
|
79
|
+
inputs = val;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const stack = [...validateHandlers, ...pipeHandlers];
|
|
83
|
+
stack.push((helper, next) => __async(this, null, function* () {
|
|
84
|
+
const res = yield handler(helper.inputs);
|
|
85
|
+
helper.result = res;
|
|
86
|
+
return next();
|
|
87
|
+
}));
|
|
88
|
+
yield (0, import_koa_compose.default)(stack)(executeHelper);
|
|
89
|
+
return executeHelper.result;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
runner[import_utils.HANDLER_WITH_META] = true;
|
|
93
|
+
return runner;
|
|
94
|
+
}
|
|
95
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
96
|
+
0 && (module.exports = {
|
|
97
|
+
Api
|
|
98
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
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
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(generate_client_exports);
|
|
50
|
+
var path = __toESM(require("path"));
|
|
51
|
+
var import_router = require("../router");
|
|
52
|
+
var import_result = require("./result");
|
|
53
|
+
const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
|
|
54
|
+
const generateClient = (_0) => __async(void 0, [_0], function* ({
|
|
55
|
+
resourcePath,
|
|
56
|
+
apiDir,
|
|
57
|
+
prefix,
|
|
58
|
+
port,
|
|
59
|
+
target,
|
|
60
|
+
requestCreator,
|
|
61
|
+
fetcher,
|
|
62
|
+
requireResolve = require.resolve
|
|
63
|
+
}) {
|
|
64
|
+
if (!requestCreator) {
|
|
65
|
+
requestCreator = requireResolve(
|
|
66
|
+
`${DEFAULT_CLIENT_REQUEST_CREATOR}${target ? `/${target}` : ""}`
|
|
67
|
+
).replace(/\\/g, "/");
|
|
68
|
+
} else {
|
|
69
|
+
let resolvedPath = requestCreator;
|
|
70
|
+
try {
|
|
71
|
+
resolvedPath = path.dirname(requireResolve(requestCreator));
|
|
72
|
+
} catch (error) {
|
|
73
|
+
}
|
|
74
|
+
requestCreator = `${resolvedPath}${target ? `/${target}` : ""}`.replace(
|
|
75
|
+
/\\/g,
|
|
76
|
+
"/"
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const apiRouter = new import_router.ApiRouter({
|
|
80
|
+
apiDir,
|
|
81
|
+
prefix
|
|
82
|
+
});
|
|
83
|
+
const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
|
|
84
|
+
if (!handlerInfos) {
|
|
85
|
+
return (0, import_result.Err)(`generate client error: Cannot require module ${resourcePath}`);
|
|
86
|
+
}
|
|
87
|
+
let handlersCode = "";
|
|
88
|
+
for (const handlerInfo of handlerInfos) {
|
|
89
|
+
const { name, httpMethod, routePath } = handlerInfo;
|
|
90
|
+
let exportStatement = `const ${name} =`;
|
|
91
|
+
if (name.toLowerCase() === "default") {
|
|
92
|
+
exportStatement = "default";
|
|
93
|
+
}
|
|
94
|
+
const upperHttpMethod = httpMethod.toUpperCase();
|
|
95
|
+
const routeName = routePath;
|
|
96
|
+
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${process.env.PORT || String(port)}${fetcher ? `, fetch` : ""});
|
|
97
|
+
`;
|
|
98
|
+
}
|
|
99
|
+
const importCode = `import { createRequest } from '${requestCreator}';
|
|
100
|
+
${fetcher ? `import { fetch } from '${fetcher}';
|
|
101
|
+
` : ""}`;
|
|
102
|
+
return (0, import_result.Ok)(`${importCode}
|
|
103
|
+
${handlersCode}`);
|
|
104
|
+
});
|
|
105
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
106
|
+
0 && (module.exports = {
|
|
107
|
+
DEFAULT_CLIENT_REQUEST_CREATOR,
|
|
108
|
+
generateClient
|
|
109
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var client_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(client_exports);
|
|
17
|
+
__reExport(client_exports, require("./generate-client"), module.exports);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var result_exports = {};
|
|
19
|
+
__export(result_exports, {
|
|
20
|
+
Err: () => Err,
|
|
21
|
+
Ok: () => Ok
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(result_exports);
|
|
24
|
+
const Err = (value) => {
|
|
25
|
+
const err = {
|
|
26
|
+
kind: "Err",
|
|
27
|
+
value,
|
|
28
|
+
isErr: true,
|
|
29
|
+
isOk: false
|
|
30
|
+
};
|
|
31
|
+
return err;
|
|
32
|
+
};
|
|
33
|
+
const Ok = (value) => {
|
|
34
|
+
const ok = {
|
|
35
|
+
kind: "Ok",
|
|
36
|
+
value,
|
|
37
|
+
isErr: false,
|
|
38
|
+
isOk: true
|
|
39
|
+
};
|
|
40
|
+
return ok;
|
|
41
|
+
};
|
|
42
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
+
0 && (module.exports = {
|
|
44
|
+
Err,
|
|
45
|
+
Ok
|
|
46
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var http_exports = {};
|
|
19
|
+
__export(http_exports, {
|
|
20
|
+
HttpError: () => HttpError,
|
|
21
|
+
ValidationError: () => ValidationError
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(http_exports);
|
|
24
|
+
class HttpError extends Error {
|
|
25
|
+
constructor(status, message) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.status = status;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
class ValidationError extends HttpError {
|
|
31
|
+
constructor(status, message) {
|
|
32
|
+
super(status, message);
|
|
33
|
+
this.code = "VALIDATION_ERROR";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
37
|
+
0 && (module.exports = {
|
|
38
|
+
HttpError,
|
|
39
|
+
ValidationError
|
|
40
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var src_exports = {};
|
|
20
|
+
__export(src_exports, {
|
|
21
|
+
Api: () => import_api.Api,
|
|
22
|
+
HANDLER_WITH_META: () => import_utils.HANDLER_WITH_META,
|
|
23
|
+
HttpError: () => import_http.HttpError,
|
|
24
|
+
ValidationError: () => import_http.ValidationError,
|
|
25
|
+
createStorage: () => import_utils.createStorage,
|
|
26
|
+
getRelativeRuntimePath: () => import_utils.getRelativeRuntimePath,
|
|
27
|
+
isWithMetaHandler: () => import_utils.isWithMetaHandler,
|
|
28
|
+
registerPaths: () => import_utils.registerPaths
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(src_exports);
|
|
31
|
+
var import_api = require("./api");
|
|
32
|
+
var import_http = require("./errors/http");
|
|
33
|
+
__reExport(src_exports, require("./router"), module.exports);
|
|
34
|
+
__reExport(src_exports, require("./types"), module.exports);
|
|
35
|
+
__reExport(src_exports, require("./client"), module.exports);
|
|
36
|
+
__reExport(src_exports, require("./operators/http"), module.exports);
|
|
37
|
+
var import_utils = require("./utils");
|
|
38
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
39
|
+
0 && (module.exports = {
|
|
40
|
+
Api,
|
|
41
|
+
HANDLER_WITH_META,
|
|
42
|
+
HttpError,
|
|
43
|
+
ValidationError,
|
|
44
|
+
createStorage,
|
|
45
|
+
getRelativeRuntimePath,
|
|
46
|
+
isWithMetaHandler,
|
|
47
|
+
registerPaths
|
|
48
|
+
});
|