@modern-js/bff-core 2.6.1-alpha.1 → 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/CHANGELOG.md +27 -0
- package/dist/cjs/client/generateClient.js +3 -3
- package/dist/cjs/operators/http.js +3 -3
- package/dist/cjs/router/index.js +2 -2
- package/dist/cjs/types.js +1 -1
- package/dist/esm/client/generateClient.js +3 -3
- package/dist/esm/operators/http.js +2 -2
- package/dist/esm/router/index.js +2 -2
- package/dist/esm/types.js +1 -1
- 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/operators/http.d.ts +1 -1
- package/dist/types/types.d.ts +2 -2
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
# @modern-js/bff-core
|
|
2
2
|
|
|
3
|
+
## 2.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 84bfb439b8: feat: support custom apiDir, lambdaDir and style of writing for bff
|
|
8
|
+
feat: 支持定制 api 目录,lambda 目录,bff 的写法
|
|
9
|
+
|
|
10
|
+
### Patch Changes
|
|
11
|
+
|
|
12
|
+
- 7bb1554194: fix: remove process.env.PORT from the bff generate client code
|
|
13
|
+
fix: 从 bff 的 generate client code 中移除 process.env.PORT
|
|
14
|
+
- 7fff9020e1: chore: make file naming consistent
|
|
15
|
+
|
|
16
|
+
chore: 统一文件命名为小驼峰格式
|
|
17
|
+
|
|
18
|
+
- 1eea234fdd: chore: make test files naming consistent
|
|
19
|
+
|
|
20
|
+
chore: 统一测试文件命名为小驼峰格式
|
|
21
|
+
|
|
22
|
+
- Updated dependencies [0f15fc597c]
|
|
23
|
+
- Updated dependencies [dcad887024]
|
|
24
|
+
- Updated dependencies [a4672f7c16]
|
|
25
|
+
- Updated dependencies [7fff9020e1]
|
|
26
|
+
- Updated dependencies [84bfb439b8]
|
|
27
|
+
- @modern-js/utils@2.7.0
|
|
28
|
+
- @modern-js/bff-runtime@2.7.0
|
|
29
|
+
|
|
3
30
|
## 2.6.0
|
|
4
31
|
|
|
5
32
|
### Patch Changes
|
|
@@ -81,13 +81,13 @@ const generateClient = async ({
|
|
|
81
81
|
}
|
|
82
82
|
const upperHttpMethod = httpMethod.toUpperCase();
|
|
83
83
|
const routeName = routePath;
|
|
84
|
-
if (target === "
|
|
85
|
-
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${String(
|
|
84
|
+
if (target === "server") {
|
|
85
|
+
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', process.env.PORT || ${String(
|
|
86
86
|
port
|
|
87
87
|
)}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
|
|
88
88
|
`;
|
|
89
89
|
} else {
|
|
90
|
-
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}',
|
|
90
|
+
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${String(
|
|
91
91
|
port
|
|
92
92
|
)}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
|
|
93
93
|
`;
|
|
@@ -34,7 +34,7 @@ __export(http_exports, {
|
|
|
34
34
|
Head: () => Head,
|
|
35
35
|
Headers: () => Headers,
|
|
36
36
|
HttpCode: () => HttpCode,
|
|
37
|
-
|
|
37
|
+
Options: () => Options,
|
|
38
38
|
Params: () => Params,
|
|
39
39
|
Patch: () => Patch,
|
|
40
40
|
Post: () => Post,
|
|
@@ -80,7 +80,7 @@ const Delete = createHttpOperator(import_types.HttpMethod.Delete);
|
|
|
80
80
|
const Connect = createHttpOperator(import_types.HttpMethod.Connect);
|
|
81
81
|
const Trace = createHttpOperator(import_types.HttpMethod.Trace);
|
|
82
82
|
const Patch = createHttpOperator(import_types.HttpMethod.Patch);
|
|
83
|
-
const
|
|
83
|
+
const Options = createHttpOperator(import_types.HttpMethod.Options);
|
|
84
84
|
const Head = createHttpOperator(import_types.HttpMethod.Head);
|
|
85
85
|
const Data = (schema) => {
|
|
86
86
|
return {
|
|
@@ -197,7 +197,7 @@ const Redirect = (url) => {
|
|
|
197
197
|
Head,
|
|
198
198
|
Headers,
|
|
199
199
|
HttpCode,
|
|
200
|
-
|
|
200
|
+
Options,
|
|
201
201
|
Params,
|
|
202
202
|
Patch,
|
|
203
203
|
Post,
|
package/dist/cjs/router/index.js
CHANGED
|
@@ -165,8 +165,8 @@ class ApiRouter {
|
|
|
165
165
|
return import_types.HttpMethod.Trace;
|
|
166
166
|
case "PATCH":
|
|
167
167
|
return import_types.HttpMethod.Patch;
|
|
168
|
-
case "
|
|
169
|
-
return import_types.HttpMethod.
|
|
168
|
+
case "OPTIONS":
|
|
169
|
+
return import_types.HttpMethod.Options;
|
|
170
170
|
case "DEFAULT": {
|
|
171
171
|
return import_types.HttpMethod.Get;
|
|
172
172
|
}
|
package/dist/cjs/types.js
CHANGED
|
@@ -57,7 +57,7 @@ var HttpMethod = /* @__PURE__ */ ((HttpMethod2) => {
|
|
|
57
57
|
HttpMethod2["Connect"] = "CONNECT";
|
|
58
58
|
HttpMethod2["Trace"] = "TRACE";
|
|
59
59
|
HttpMethod2["Patch"] = "PATCH";
|
|
60
|
-
HttpMethod2["
|
|
60
|
+
HttpMethod2["Options"] = "OPTIONS";
|
|
61
61
|
HttpMethod2["Head"] = "HEAD";
|
|
62
62
|
return HttpMethod2;
|
|
63
63
|
})(HttpMethod || {});
|
|
@@ -48,13 +48,13 @@ const generateClient = async ({
|
|
|
48
48
|
}
|
|
49
49
|
const upperHttpMethod = httpMethod.toUpperCase();
|
|
50
50
|
const routeName = routePath;
|
|
51
|
-
if (target === "
|
|
52
|
-
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${String(
|
|
51
|
+
if (target === "server") {
|
|
52
|
+
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', process.env.PORT || ${String(
|
|
53
53
|
port
|
|
54
54
|
)}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
|
|
55
55
|
`;
|
|
56
56
|
} else {
|
|
57
|
-
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}',
|
|
57
|
+
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${String(
|
|
58
58
|
port
|
|
59
59
|
)}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
|
|
60
60
|
`;
|
|
@@ -38,7 +38,7 @@ const Delete = createHttpOperator(HttpMethod.Delete);
|
|
|
38
38
|
const Connect = createHttpOperator(HttpMethod.Connect);
|
|
39
39
|
const Trace = createHttpOperator(HttpMethod.Trace);
|
|
40
40
|
const Patch = createHttpOperator(HttpMethod.Patch);
|
|
41
|
-
const
|
|
41
|
+
const Options = createHttpOperator(HttpMethod.Options);
|
|
42
42
|
const Head = createHttpOperator(HttpMethod.Head);
|
|
43
43
|
const Data = (schema) => {
|
|
44
44
|
return {
|
|
@@ -154,7 +154,7 @@ export {
|
|
|
154
154
|
Head,
|
|
155
155
|
Headers,
|
|
156
156
|
HttpCode,
|
|
157
|
-
|
|
157
|
+
Options,
|
|
158
158
|
Params,
|
|
159
159
|
Patch,
|
|
160
160
|
Post,
|
package/dist/esm/router/index.js
CHANGED
|
@@ -142,8 +142,8 @@ class ApiRouter {
|
|
|
142
142
|
return HttpMethod.Trace;
|
|
143
143
|
case "PATCH":
|
|
144
144
|
return HttpMethod.Patch;
|
|
145
|
-
case "
|
|
146
|
-
return HttpMethod.
|
|
145
|
+
case "OPTIONS":
|
|
146
|
+
return HttpMethod.Options;
|
|
147
147
|
case "DEFAULT": {
|
|
148
148
|
return HttpMethod.Get;
|
|
149
149
|
}
|
package/dist/esm/types.js
CHANGED
|
@@ -30,7 +30,7 @@ var HttpMethod = /* @__PURE__ */ ((HttpMethod2) => {
|
|
|
30
30
|
HttpMethod2["Connect"] = "CONNECT";
|
|
31
31
|
HttpMethod2["Trace"] = "TRACE";
|
|
32
32
|
HttpMethod2["Patch"] = "PATCH";
|
|
33
|
-
HttpMethod2["
|
|
33
|
+
HttpMethod2["Options"] = "OPTIONS";
|
|
34
34
|
HttpMethod2["Head"] = "HEAD";
|
|
35
35
|
return HttpMethod2;
|
|
36
36
|
})(HttpMethod || {});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import "reflect-metadata";
|
|
22
|
+
import compose from "koa-compose";
|
|
23
|
+
import { validateFunction, HANDLER_WITH_META } from "./utils";
|
|
24
|
+
function Api(...args) {
|
|
25
|
+
const handler = args.pop();
|
|
26
|
+
validateFunction(handler, "Apihandler");
|
|
27
|
+
const operators = args;
|
|
28
|
+
const metadataHelper = {
|
|
29
|
+
getMetadata(key) {
|
|
30
|
+
return Reflect.getMetadata(key, runner);
|
|
31
|
+
},
|
|
32
|
+
setMetadata(key, value) {
|
|
33
|
+
return Reflect.defineMetadata(key, value, runner);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
for (const operator of operators) {
|
|
37
|
+
if (operator.metadata) {
|
|
38
|
+
operator.metadata(metadataHelper);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const validateHandlers = operators.filter((operator) => operator.validate).map((operator) => operator.validate);
|
|
42
|
+
const pipeHandlers = operators.filter((operator) => operator.execute).map((operator) => operator.execute);
|
|
43
|
+
function runner(inputs) {
|
|
44
|
+
return __async(this, null, function* () {
|
|
45
|
+
const executeHelper = {
|
|
46
|
+
result: null,
|
|
47
|
+
get inputs() {
|
|
48
|
+
return inputs;
|
|
49
|
+
},
|
|
50
|
+
set inputs(val) {
|
|
51
|
+
inputs = val;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const stack = [...validateHandlers, ...pipeHandlers];
|
|
55
|
+
stack.push((helper, next) => __async(this, null, function* () {
|
|
56
|
+
const res = yield handler(helper.inputs);
|
|
57
|
+
helper.result = res;
|
|
58
|
+
return next();
|
|
59
|
+
}));
|
|
60
|
+
yield compose(stack)(executeHelper);
|
|
61
|
+
return executeHelper.result;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
runner[HANDLER_WITH_META] = true;
|
|
65
|
+
return runner;
|
|
66
|
+
}
|
|
67
|
+
export {
|
|
68
|
+
Api
|
|
69
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import * as path from "path";
|
|
22
|
+
import { ApiRouter } from "../router";
|
|
23
|
+
import { Ok, Err } from "./result";
|
|
24
|
+
const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
|
|
25
|
+
const generateClient = (_0) => __async(void 0, [_0], function* ({
|
|
26
|
+
resourcePath,
|
|
27
|
+
apiDir,
|
|
28
|
+
prefix,
|
|
29
|
+
port,
|
|
30
|
+
target,
|
|
31
|
+
requestCreator,
|
|
32
|
+
fetcher,
|
|
33
|
+
requireResolve = require.resolve
|
|
34
|
+
}) {
|
|
35
|
+
if (!requestCreator) {
|
|
36
|
+
requestCreator = requireResolve(
|
|
37
|
+
`${DEFAULT_CLIENT_REQUEST_CREATOR}${target ? `/${target}` : ""}`
|
|
38
|
+
).replace(/\\/g, "/");
|
|
39
|
+
} else {
|
|
40
|
+
let resolvedPath = requestCreator;
|
|
41
|
+
try {
|
|
42
|
+
resolvedPath = path.dirname(requireResolve(requestCreator));
|
|
43
|
+
} catch (error) {
|
|
44
|
+
}
|
|
45
|
+
requestCreator = `${resolvedPath}${target ? `/${target}` : ""}`.replace(
|
|
46
|
+
/\\/g,
|
|
47
|
+
"/"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
const apiRouter = new ApiRouter({
|
|
51
|
+
apiDir,
|
|
52
|
+
prefix
|
|
53
|
+
});
|
|
54
|
+
const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
|
|
55
|
+
if (!handlerInfos) {
|
|
56
|
+
return Err(`generate client error: Cannot require module ${resourcePath}`);
|
|
57
|
+
}
|
|
58
|
+
let handlersCode = "";
|
|
59
|
+
for (const handlerInfo of handlerInfos) {
|
|
60
|
+
const { name, httpMethod, routePath } = handlerInfo;
|
|
61
|
+
let exportStatement = `const ${name} =`;
|
|
62
|
+
if (name.toLowerCase() === "default") {
|
|
63
|
+
exportStatement = "default";
|
|
64
|
+
}
|
|
65
|
+
const upperHttpMethod = httpMethod.toUpperCase();
|
|
66
|
+
const routeName = routePath;
|
|
67
|
+
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${process.env.PORT || String(port)}${fetcher ? `, fetch` : ""});
|
|
68
|
+
`;
|
|
69
|
+
}
|
|
70
|
+
const importCode = `import { createRequest } from '${requestCreator}';
|
|
71
|
+
${fetcher ? `import { fetch } from '${fetcher}';
|
|
72
|
+
` : ""}`;
|
|
73
|
+
return Ok(`${importCode}
|
|
74
|
+
${handlersCode}`);
|
|
75
|
+
});
|
|
76
|
+
export {
|
|
77
|
+
DEFAULT_CLIENT_REQUEST_CREATOR,
|
|
78
|
+
generateClient
|
|
79
|
+
};
|
|
@@ -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,23 @@
|
|
|
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
|
+
createStorage,
|
|
12
|
+
registerPaths
|
|
13
|
+
} from "./utils";
|
|
14
|
+
export {
|
|
15
|
+
Api,
|
|
16
|
+
HANDLER_WITH_META,
|
|
17
|
+
HttpError,
|
|
18
|
+
ValidationError,
|
|
19
|
+
createStorage,
|
|
20
|
+
getRelativeRuntimePath,
|
|
21
|
+
isWithMetaHandler,
|
|
22
|
+
registerPaths
|
|
23
|
+
};
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
import {
|
|
41
|
+
HttpMetadata,
|
|
42
|
+
OperatorType,
|
|
43
|
+
HttpMethod,
|
|
44
|
+
TriggerType,
|
|
45
|
+
ResponseMetaType
|
|
46
|
+
} from "../types";
|
|
47
|
+
import { ValidationError } from "../errors/http";
|
|
48
|
+
const validateInput = (schema, input) => __async(void 0, null, function* () {
|
|
49
|
+
try {
|
|
50
|
+
return yield schema.parseAsync(input);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
const { z: zod } = require("zod");
|
|
53
|
+
if (error instanceof zod.ZodError) {
|
|
54
|
+
throw new ValidationError(400, error.message);
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
const createHttpOperator = (method) => {
|
|
60
|
+
return (urlPath) => {
|
|
61
|
+
return {
|
|
62
|
+
name: method,
|
|
63
|
+
metadata({ setMetadata }) {
|
|
64
|
+
setMetadata(OperatorType.Trigger, {
|
|
65
|
+
type: TriggerType.Http,
|
|
66
|
+
path: urlPath,
|
|
67
|
+
method
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
const Get = createHttpOperator(HttpMethod.Get);
|
|
74
|
+
const Post = createHttpOperator(HttpMethod.Post);
|
|
75
|
+
const Put = createHttpOperator(HttpMethod.Put);
|
|
76
|
+
const Delete = createHttpOperator(HttpMethod.Delete);
|
|
77
|
+
const Connect = createHttpOperator(HttpMethod.Connect);
|
|
78
|
+
const Trace = createHttpOperator(HttpMethod.Trace);
|
|
79
|
+
const Patch = createHttpOperator(HttpMethod.Patch);
|
|
80
|
+
const Option = createHttpOperator(HttpMethod.Option);
|
|
81
|
+
const Head = createHttpOperator(HttpMethod.Head);
|
|
82
|
+
const Data = (schema) => {
|
|
83
|
+
return {
|
|
84
|
+
name: HttpMetadata.Data,
|
|
85
|
+
metadata({ setMetadata }) {
|
|
86
|
+
setMetadata(HttpMetadata.Data, schema);
|
|
87
|
+
},
|
|
88
|
+
validate(helper, next) {
|
|
89
|
+
return __async(this, null, function* () {
|
|
90
|
+
const {
|
|
91
|
+
inputs: { data }
|
|
92
|
+
} = helper;
|
|
93
|
+
helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
|
|
94
|
+
data: yield validateInput(schema, data)
|
|
95
|
+
});
|
|
96
|
+
return next();
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
const Query = (schema) => {
|
|
102
|
+
return {
|
|
103
|
+
name: HttpMetadata.Query,
|
|
104
|
+
metadata({ setMetadata }) {
|
|
105
|
+
setMetadata(HttpMetadata.Query, schema);
|
|
106
|
+
},
|
|
107
|
+
validate(helper, next) {
|
|
108
|
+
return __async(this, null, function* () {
|
|
109
|
+
const {
|
|
110
|
+
inputs: { query }
|
|
111
|
+
} = helper;
|
|
112
|
+
helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
|
|
113
|
+
query: yield validateInput(schema, query)
|
|
114
|
+
});
|
|
115
|
+
return next();
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const Params = (schema) => {
|
|
121
|
+
return {
|
|
122
|
+
name: HttpMetadata.Params,
|
|
123
|
+
metadata({ setMetadata }) {
|
|
124
|
+
setMetadata(HttpMetadata.Params, schema);
|
|
125
|
+
},
|
|
126
|
+
validate(helper, next) {
|
|
127
|
+
return __async(this, null, function* () {
|
|
128
|
+
const {
|
|
129
|
+
inputs: { params }
|
|
130
|
+
} = helper;
|
|
131
|
+
helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
|
|
132
|
+
params: yield validateInput(schema, params)
|
|
133
|
+
});
|
|
134
|
+
return next();
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
const Headers = (schema) => {
|
|
140
|
+
return {
|
|
141
|
+
name: HttpMetadata.Headers,
|
|
142
|
+
metadata({ setMetadata }) {
|
|
143
|
+
setMetadata(HttpMetadata.Headers, schema);
|
|
144
|
+
},
|
|
145
|
+
validate(helper, next) {
|
|
146
|
+
return __async(this, null, function* () {
|
|
147
|
+
const {
|
|
148
|
+
inputs: { headers }
|
|
149
|
+
} = helper;
|
|
150
|
+
helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
|
|
151
|
+
headers: yield validateInput(schema, headers)
|
|
152
|
+
});
|
|
153
|
+
return next();
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
const setResponseMeta = (helper, type, value) => {
|
|
159
|
+
const responseMetaData = helper.getMetadata(HttpMetadata.Response) || [];
|
|
160
|
+
helper.setMetadata(HttpMetadata.Response, [
|
|
161
|
+
...responseMetaData,
|
|
162
|
+
{
|
|
163
|
+
type,
|
|
164
|
+
value
|
|
165
|
+
}
|
|
166
|
+
]);
|
|
167
|
+
};
|
|
168
|
+
const HttpCode = (statusCode) => {
|
|
169
|
+
return {
|
|
170
|
+
name: "HttpCode",
|
|
171
|
+
metadata(helper) {
|
|
172
|
+
setResponseMeta(helper, ResponseMetaType.StatusCode, statusCode);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
const SetHeaders = (headers) => {
|
|
177
|
+
return {
|
|
178
|
+
name: "SetHeaders",
|
|
179
|
+
metadata(helper) {
|
|
180
|
+
setResponseMeta(helper, ResponseMetaType.Headers, headers);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
const Redirect = (url) => {
|
|
185
|
+
return {
|
|
186
|
+
name: "Redirect",
|
|
187
|
+
metadata(helper) {
|
|
188
|
+
setResponseMeta(helper, ResponseMetaType.Redirect, url);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
export {
|
|
193
|
+
Connect,
|
|
194
|
+
Data,
|
|
195
|
+
Delete,
|
|
196
|
+
Get,
|
|
197
|
+
Head,
|
|
198
|
+
Headers,
|
|
199
|
+
HttpCode,
|
|
200
|
+
Option,
|
|
201
|
+
Params,
|
|
202
|
+
Patch,
|
|
203
|
+
Post,
|
|
204
|
+
Put,
|
|
205
|
+
Query,
|
|
206
|
+
Redirect,
|
|
207
|
+
SetHeaders,
|
|
208
|
+
Trace,
|
|
209
|
+
createHttpOperator
|
|
210
|
+
};
|
|
@@ -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
|
+
};
|