@modern-js/bff-core 2.4.1-beta.0 → 2.5.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/CHANGELOG.md +12 -2
- package/dist/cjs/api.js +76 -0
- package/dist/cjs/client/generate-client.js +91 -0
- package/dist/cjs/client/index.js +17 -0
- package/dist/cjs/client/result.js +46 -0
- package/dist/cjs/errors/http.js +40 -0
- package/dist/cjs/index.js +52 -0
- package/dist/cjs/operators/http.js +206 -0
- package/dist/cjs/router/constants.js +61 -0
- package/dist/cjs/router/index.js +280 -0
- package/dist/cjs/router/types.js +15 -0
- package/dist/cjs/router/utils.js +116 -0
- package/dist/cjs/types.js +73 -0
- package/dist/cjs/utils/alias.js +107 -0
- package/dist/cjs/utils/debug.js +28 -0
- package/dist/cjs/utils/index.js +32 -0
- package/dist/cjs/utils/meta.js +40 -0
- package/dist/cjs/utils/storage.js +71 -0
- package/dist/cjs/utils/validate.js +74 -0
- package/dist/esm/api.js +47 -0
- package/dist/esm/client/generate-client.js +61 -0
- package/dist/esm/client/index.js +1 -0
- package/dist/esm/client/result.js +22 -0
- package/dist/esm/errors/http.js +16 -0
- package/dist/esm/index.js +27 -0
- package/dist/esm/operators/http.js +167 -0
- package/dist/esm/router/constants.js +32 -0
- package/dist/esm/router/index.js +260 -0
- package/dist/esm/router/types.js +0 -0
- package/dist/esm/router/utils.js +83 -0
- package/dist/esm/types.js +45 -0
- package/dist/esm/utils/alias.js +76 -0
- package/dist/esm/utils/debug.js +5 -0
- package/dist/esm/utils/index.js +8 -0
- package/dist/esm/utils/meta.js +14 -0
- package/dist/esm/utils/storage.js +42 -0
- package/dist/esm/utils/validate.js +43 -0
- package/dist/types/client/generate-client.d.ts +4 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/router/index.d.ts +5 -1
- package/dist/types/utils/meta.d.ts +3 -1
- package/package.json +11 -11
|
@@ -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 meta_exports = {};
|
|
19
|
+
__export(meta_exports, {
|
|
20
|
+
HANDLER_WITH_META: () => HANDLER_WITH_META,
|
|
21
|
+
INPUT_PARAMS_DECIDER: () => INPUT_PARAMS_DECIDER,
|
|
22
|
+
isInputParamsDeciderHandler: () => isInputParamsDeciderHandler,
|
|
23
|
+
isWithMetaHandler: () => isWithMetaHandler
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(meta_exports);
|
|
26
|
+
const HANDLER_WITH_META = "HANDLER_WITH_META";
|
|
27
|
+
const INPUT_PARAMS_DECIDER = "INPUT_PARAMS_DECIDER";
|
|
28
|
+
const isWithMetaHandler = (handler) => {
|
|
29
|
+
return typeof handler === "function" && handler[HANDLER_WITH_META];
|
|
30
|
+
};
|
|
31
|
+
const isInputParamsDeciderHandler = (handler) => {
|
|
32
|
+
return typeof handler === "function" && handler[INPUT_PARAMS_DECIDER];
|
|
33
|
+
};
|
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
+
0 && (module.exports = {
|
|
36
|
+
HANDLER_WITH_META,
|
|
37
|
+
INPUT_PARAMS_DECIDER,
|
|
38
|
+
isInputParamsDeciderHandler,
|
|
39
|
+
isWithMetaHandler
|
|
40
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
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 storage_exports = {};
|
|
25
|
+
__export(storage_exports, {
|
|
26
|
+
createStorage: () => createStorage
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(storage_exports);
|
|
29
|
+
var ah = __toESM(require("async_hooks"));
|
|
30
|
+
const createStorage = () => {
|
|
31
|
+
let storage;
|
|
32
|
+
if (typeof ah.AsyncLocalStorage !== "undefined") {
|
|
33
|
+
storage = new ah.AsyncLocalStorage();
|
|
34
|
+
}
|
|
35
|
+
const run = (context, cb) => {
|
|
36
|
+
if (!storage) {
|
|
37
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
38
|
+
`);
|
|
39
|
+
}
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
storage.run(context, () => {
|
|
42
|
+
try {
|
|
43
|
+
return resolve(cb());
|
|
44
|
+
} catch (error) {
|
|
45
|
+
return reject(error);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
const useContext = () => {
|
|
51
|
+
if (!storage) {
|
|
52
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
53
|
+
`);
|
|
54
|
+
}
|
|
55
|
+
const context = storage.getStore();
|
|
56
|
+
if (!context) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Can't call useContext out of scope, it should be placed in the bff function`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return context;
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
run,
|
|
65
|
+
useContext
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
69
|
+
0 && (module.exports = {
|
|
70
|
+
createStorage
|
|
71
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
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 validate_exports = {};
|
|
25
|
+
__export(validate_exports, {
|
|
26
|
+
ERR_INVALID_ARG_TYPE: () => ERR_INVALID_ARG_TYPE,
|
|
27
|
+
getTypeErrorMessage: () => getTypeErrorMessage,
|
|
28
|
+
validateFunction: () => validateFunction
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(validate_exports);
|
|
31
|
+
var import_util = __toESM(require("util"));
|
|
32
|
+
const getTypeErrorMessage = (actual) => {
|
|
33
|
+
var _a;
|
|
34
|
+
let msg = "";
|
|
35
|
+
if (actual == null) {
|
|
36
|
+
msg += `. Received ${actual}`;
|
|
37
|
+
} else if (typeof actual === "function" && actual.name) {
|
|
38
|
+
msg += `. Received function ${actual.name}`;
|
|
39
|
+
} else if (typeof actual === "object") {
|
|
40
|
+
if ((_a = actual.constructor) == null ? void 0 : _a.name) {
|
|
41
|
+
msg += `. Received an instance of ${actual.constructor.name}`;
|
|
42
|
+
} else {
|
|
43
|
+
const inspected = import_util.default.inspect(actual, { depth: -1 });
|
|
44
|
+
msg += `. Received ${inspected}`;
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
let inspected = import_util.default.inspect(actual, { colors: false });
|
|
48
|
+
if (inspected.length > 25) {
|
|
49
|
+
inspected = `${inspected.slice(0, 25)}...`;
|
|
50
|
+
}
|
|
51
|
+
msg += `. Received type ${typeof actual} (${inspected})`;
|
|
52
|
+
}
|
|
53
|
+
return msg;
|
|
54
|
+
};
|
|
55
|
+
class ERR_INVALID_ARG_TYPE extends Error {
|
|
56
|
+
constructor(funcName, expectedType, actual) {
|
|
57
|
+
const message = `[ERR_INVALID_ARG_TYPE]: The '${funcName}' argument must be of type ${expectedType}${getTypeErrorMessage(
|
|
58
|
+
actual
|
|
59
|
+
)}`;
|
|
60
|
+
super(message);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const validateFunction = (maybeFunc, name) => {
|
|
64
|
+
if (typeof maybeFunc !== "function") {
|
|
65
|
+
throw new ERR_INVALID_ARG_TYPE(name, "function", maybeFunc);
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
};
|
|
69
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
70
|
+
0 && (module.exports = {
|
|
71
|
+
ERR_INVALID_ARG_TYPE,
|
|
72
|
+
getTypeErrorMessage,
|
|
73
|
+
validateFunction
|
|
74
|
+
});
|
package/dist/esm/api.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import compose from "koa-compose";
|
|
3
|
+
import { validateFunction, HANDLER_WITH_META } from "./utils";
|
|
4
|
+
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
|
+
setMetadata(key, value) {
|
|
13
|
+
return Reflect.defineMetadata(key, value, runner);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
for (const operator of operators) {
|
|
17
|
+
if (operator.metadata) {
|
|
18
|
+
operator.metadata(metadataHelper);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const validateHandlers = operators.filter((operator) => operator.validate).map((operator) => operator.validate);
|
|
22
|
+
const pipeHandlers = operators.filter((operator) => operator.execute).map((operator) => operator.execute);
|
|
23
|
+
async function runner(inputs) {
|
|
24
|
+
const executeHelper = {
|
|
25
|
+
result: null,
|
|
26
|
+
get inputs() {
|
|
27
|
+
return inputs;
|
|
28
|
+
},
|
|
29
|
+
set inputs(val) {
|
|
30
|
+
inputs = val;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const stack = [...validateHandlers, ...pipeHandlers];
|
|
34
|
+
stack.push(async (helper, next) => {
|
|
35
|
+
const res = await handler(helper.inputs);
|
|
36
|
+
helper.result = res;
|
|
37
|
+
return next();
|
|
38
|
+
});
|
|
39
|
+
await compose(stack)(executeHelper);
|
|
40
|
+
return executeHelper.result;
|
|
41
|
+
}
|
|
42
|
+
runner[HANDLER_WITH_META] = true;
|
|
43
|
+
return runner;
|
|
44
|
+
}
|
|
45
|
+
export {
|
|
46
|
+
Api
|
|
47
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import { ApiRouter } from "../router";
|
|
3
|
+
import { Ok, Err } from "./result";
|
|
4
|
+
const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
|
|
5
|
+
const generateClient = async ({
|
|
6
|
+
resourcePath,
|
|
7
|
+
apiDir,
|
|
8
|
+
prefix,
|
|
9
|
+
port,
|
|
10
|
+
target,
|
|
11
|
+
requestCreator,
|
|
12
|
+
fetcher,
|
|
13
|
+
requireResolve = require.resolve,
|
|
14
|
+
httpMethodDecider
|
|
15
|
+
}) => {
|
|
16
|
+
if (!requestCreator) {
|
|
17
|
+
requestCreator = requireResolve(
|
|
18
|
+
`${DEFAULT_CLIENT_REQUEST_CREATOR}${target ? `/${target}` : ""}`
|
|
19
|
+
).replace(/\\/g, "/");
|
|
20
|
+
} else {
|
|
21
|
+
let resolvedPath = requestCreator;
|
|
22
|
+
try {
|
|
23
|
+
resolvedPath = path.dirname(requireResolve(requestCreator));
|
|
24
|
+
} catch (error) {
|
|
25
|
+
}
|
|
26
|
+
requestCreator = `${resolvedPath}${target ? `/${target}` : ""}`.replace(
|
|
27
|
+
/\\/g,
|
|
28
|
+
"/"
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
const apiRouter = new ApiRouter({
|
|
32
|
+
apiDir,
|
|
33
|
+
prefix,
|
|
34
|
+
httpMethodDecider
|
|
35
|
+
});
|
|
36
|
+
const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
|
|
37
|
+
if (!handlerInfos) {
|
|
38
|
+
return Err(`generate client error: Cannot require module ${resourcePath}`);
|
|
39
|
+
}
|
|
40
|
+
let handlersCode = "";
|
|
41
|
+
for (const handlerInfo of handlerInfos) {
|
|
42
|
+
const { name, httpMethod, routePath } = handlerInfo;
|
|
43
|
+
let exportStatement = `const ${name} =`;
|
|
44
|
+
if (name.toLowerCase() === "default") {
|
|
45
|
+
exportStatement = "default";
|
|
46
|
+
}
|
|
47
|
+
const upperHttpMethod = httpMethod.toUpperCase();
|
|
48
|
+
const routeName = routePath;
|
|
49
|
+
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', '${httpMethodDecider}', ${process.env.PORT || String(port)}${fetcher ? `, fetch` : ""});
|
|
50
|
+
`;
|
|
51
|
+
}
|
|
52
|
+
const importCode = `import { createRequest } from '${requestCreator}';
|
|
53
|
+
${fetcher ? `import { fetch } from '${fetcher}';
|
|
54
|
+
` : ""}`;
|
|
55
|
+
return Ok(`${importCode}
|
|
56
|
+
${handlersCode}`);
|
|
57
|
+
};
|
|
58
|
+
export {
|
|
59
|
+
DEFAULT_CLIENT_REQUEST_CREATOR,
|
|
60
|
+
generateClient
|
|
61
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./generate-client";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const Err = (value) => {
|
|
2
|
+
const err = {
|
|
3
|
+
kind: "Err",
|
|
4
|
+
value,
|
|
5
|
+
isErr: true,
|
|
6
|
+
isOk: false
|
|
7
|
+
};
|
|
8
|
+
return err;
|
|
9
|
+
};
|
|
10
|
+
const Ok = (value) => {
|
|
11
|
+
const ok = {
|
|
12
|
+
kind: "Ok",
|
|
13
|
+
value,
|
|
14
|
+
isErr: false,
|
|
15
|
+
isOk: true
|
|
16
|
+
};
|
|
17
|
+
return ok;
|
|
18
|
+
};
|
|
19
|
+
export {
|
|
20
|
+
Err,
|
|
21
|
+
Ok
|
|
22
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class HttpError extends Error {
|
|
2
|
+
constructor(status, message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.status = status;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
class ValidationError extends HttpError {
|
|
8
|
+
constructor(status, message) {
|
|
9
|
+
super(status, message);
|
|
10
|
+
this.code = "VALIDATION_ERROR";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
HttpError,
|
|
15
|
+
ValidationError
|
|
16
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Api } from "./api";
|
|
2
|
+
import { HttpError, ValidationError } from "./errors/http";
|
|
3
|
+
export * from "./router";
|
|
4
|
+
export * from "./types";
|
|
5
|
+
export * from "./client";
|
|
6
|
+
export * from "./operators/http";
|
|
7
|
+
import {
|
|
8
|
+
getRelativeRuntimePath,
|
|
9
|
+
HANDLER_WITH_META,
|
|
10
|
+
isWithMetaHandler,
|
|
11
|
+
INPUT_PARAMS_DECIDER,
|
|
12
|
+
isInputParamsDeciderHandler,
|
|
13
|
+
createStorage,
|
|
14
|
+
registerPaths
|
|
15
|
+
} from "./utils";
|
|
16
|
+
export {
|
|
17
|
+
Api,
|
|
18
|
+
HANDLER_WITH_META,
|
|
19
|
+
HttpError,
|
|
20
|
+
INPUT_PARAMS_DECIDER,
|
|
21
|
+
ValidationError,
|
|
22
|
+
createStorage,
|
|
23
|
+
getRelativeRuntimePath,
|
|
24
|
+
isInputParamsDeciderHandler,
|
|
25
|
+
isWithMetaHandler,
|
|
26
|
+
registerPaths
|
|
27
|
+
};
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HttpMetadata,
|
|
3
|
+
OperatorType,
|
|
4
|
+
HttpMethod,
|
|
5
|
+
TriggerType,
|
|
6
|
+
ResponseMetaType
|
|
7
|
+
} from "../types";
|
|
8
|
+
import { ValidationError } from "../errors/http";
|
|
9
|
+
const validateInput = async (schema, input) => {
|
|
10
|
+
try {
|
|
11
|
+
return await schema.parseAsync(input);
|
|
12
|
+
} catch (error) {
|
|
13
|
+
const { z: zod } = await import("zod");
|
|
14
|
+
if (error instanceof zod.ZodError) {
|
|
15
|
+
throw new ValidationError(400, error.message);
|
|
16
|
+
}
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const createHttpOperator = (method) => {
|
|
21
|
+
return (urlPath) => {
|
|
22
|
+
return {
|
|
23
|
+
name: method,
|
|
24
|
+
metadata({ setMetadata }) {
|
|
25
|
+
setMetadata(OperatorType.Trigger, {
|
|
26
|
+
type: TriggerType.Http,
|
|
27
|
+
path: urlPath,
|
|
28
|
+
method
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
const Get = createHttpOperator(HttpMethod.Get);
|
|
35
|
+
const Post = createHttpOperator(HttpMethod.Post);
|
|
36
|
+
const Put = createHttpOperator(HttpMethod.Put);
|
|
37
|
+
const Delete = createHttpOperator(HttpMethod.Delete);
|
|
38
|
+
const Connect = createHttpOperator(HttpMethod.Connect);
|
|
39
|
+
const Trace = createHttpOperator(HttpMethod.Trace);
|
|
40
|
+
const Patch = createHttpOperator(HttpMethod.Patch);
|
|
41
|
+
const Option = createHttpOperator(HttpMethod.Option);
|
|
42
|
+
const Head = createHttpOperator(HttpMethod.Head);
|
|
43
|
+
const Data = (schema) => {
|
|
44
|
+
return {
|
|
45
|
+
name: HttpMetadata.Data,
|
|
46
|
+
metadata({ setMetadata }) {
|
|
47
|
+
setMetadata(HttpMetadata.Data, schema);
|
|
48
|
+
},
|
|
49
|
+
async validate(helper, next) {
|
|
50
|
+
const {
|
|
51
|
+
inputs: { data }
|
|
52
|
+
} = helper;
|
|
53
|
+
helper.inputs = {
|
|
54
|
+
...helper.inputs,
|
|
55
|
+
data: await validateInput(schema, data)
|
|
56
|
+
};
|
|
57
|
+
return next();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
const Query = (schema) => {
|
|
62
|
+
return {
|
|
63
|
+
name: HttpMetadata.Query,
|
|
64
|
+
metadata({ setMetadata }) {
|
|
65
|
+
setMetadata(HttpMetadata.Query, schema);
|
|
66
|
+
},
|
|
67
|
+
async validate(helper, next) {
|
|
68
|
+
const {
|
|
69
|
+
inputs: { query }
|
|
70
|
+
} = helper;
|
|
71
|
+
helper.inputs = {
|
|
72
|
+
...helper.inputs,
|
|
73
|
+
query: await validateInput(schema, query)
|
|
74
|
+
};
|
|
75
|
+
return next();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
const Params = (schema) => {
|
|
80
|
+
return {
|
|
81
|
+
name: HttpMetadata.Params,
|
|
82
|
+
metadata({ setMetadata }) {
|
|
83
|
+
setMetadata(HttpMetadata.Params, schema);
|
|
84
|
+
},
|
|
85
|
+
async validate(helper, next) {
|
|
86
|
+
const {
|
|
87
|
+
inputs: { params }
|
|
88
|
+
} = helper;
|
|
89
|
+
helper.inputs = {
|
|
90
|
+
...helper.inputs,
|
|
91
|
+
params: await validateInput(schema, params)
|
|
92
|
+
};
|
|
93
|
+
return next();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
const Headers = (schema) => {
|
|
98
|
+
return {
|
|
99
|
+
name: HttpMetadata.Headers,
|
|
100
|
+
metadata({ setMetadata }) {
|
|
101
|
+
setMetadata(HttpMetadata.Headers, schema);
|
|
102
|
+
},
|
|
103
|
+
async validate(helper, next) {
|
|
104
|
+
const {
|
|
105
|
+
inputs: { headers }
|
|
106
|
+
} = helper;
|
|
107
|
+
helper.inputs = {
|
|
108
|
+
...helper.inputs,
|
|
109
|
+
headers: await validateInput(schema, headers)
|
|
110
|
+
};
|
|
111
|
+
return next();
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
const setResponseMeta = (helper, type, value) => {
|
|
116
|
+
const responseMetaData = helper.getMetadata(HttpMetadata.Response) || [];
|
|
117
|
+
helper.setMetadata(HttpMetadata.Response, [
|
|
118
|
+
...responseMetaData,
|
|
119
|
+
{
|
|
120
|
+
type,
|
|
121
|
+
value
|
|
122
|
+
}
|
|
123
|
+
]);
|
|
124
|
+
};
|
|
125
|
+
const HttpCode = (statusCode) => {
|
|
126
|
+
return {
|
|
127
|
+
name: "HttpCode",
|
|
128
|
+
metadata(helper) {
|
|
129
|
+
setResponseMeta(helper, ResponseMetaType.StatusCode, statusCode);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
const SetHeaders = (headers) => {
|
|
134
|
+
return {
|
|
135
|
+
name: "SetHeaders",
|
|
136
|
+
metadata(helper) {
|
|
137
|
+
setResponseMeta(helper, ResponseMetaType.Headers, headers);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
const Redirect = (url) => {
|
|
142
|
+
return {
|
|
143
|
+
name: "Redirect",
|
|
144
|
+
metadata(helper) {
|
|
145
|
+
setResponseMeta(helper, ResponseMetaType.Redirect, url);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
export {
|
|
150
|
+
Connect,
|
|
151
|
+
Data,
|
|
152
|
+
Delete,
|
|
153
|
+
Get,
|
|
154
|
+
Head,
|
|
155
|
+
Headers,
|
|
156
|
+
HttpCode,
|
|
157
|
+
Option,
|
|
158
|
+
Params,
|
|
159
|
+
Patch,
|
|
160
|
+
Post,
|
|
161
|
+
Put,
|
|
162
|
+
Query,
|
|
163
|
+
Redirect,
|
|
164
|
+
SetHeaders,
|
|
165
|
+
Trace,
|
|
166
|
+
createHttpOperator
|
|
167
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { HttpMethod } from "../types";
|
|
2
|
+
const AllHttpMethods = Object.values(HttpMethod);
|
|
3
|
+
var APIMode = /* @__PURE__ */ ((APIMode2) => {
|
|
4
|
+
APIMode2["FARMEWORK"] = "framework";
|
|
5
|
+
APIMode2["FUNCTION"] = "function";
|
|
6
|
+
return APIMode2;
|
|
7
|
+
})(APIMode || {});
|
|
8
|
+
const FRAMEWORK_MODE_LAMBDA_DIR = "lambda";
|
|
9
|
+
const FRAMEWORK_MODE_APP_DIR = "app";
|
|
10
|
+
const INDEX_SUFFIX = "index";
|
|
11
|
+
const API_DIR = "api";
|
|
12
|
+
const API_FILE_RULES = [
|
|
13
|
+
"**/*.[tj]s",
|
|
14
|
+
"!**/_*",
|
|
15
|
+
"!**/_*/**/*.[tj]s",
|
|
16
|
+
"!**/*.test.js",
|
|
17
|
+
"!**/*.test.ts",
|
|
18
|
+
"!**/*.d.ts",
|
|
19
|
+
"!__test__/*.ts",
|
|
20
|
+
"!__tests__/*.ts",
|
|
21
|
+
"!node_modules/**",
|
|
22
|
+
"!bootstrap.js"
|
|
23
|
+
];
|
|
24
|
+
export {
|
|
25
|
+
APIMode,
|
|
26
|
+
API_DIR,
|
|
27
|
+
API_FILE_RULES,
|
|
28
|
+
AllHttpMethods,
|
|
29
|
+
FRAMEWORK_MODE_APP_DIR,
|
|
30
|
+
FRAMEWORK_MODE_LAMBDA_DIR,
|
|
31
|
+
INDEX_SUFFIX
|
|
32
|
+
};
|