@modern-js/bff-core 2.60.4 → 2.60.6
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/cjs/client/generateClient.js +5 -3
- package/dist/cjs/operators/http.js +26 -0
- package/dist/cjs/router/index.js +21 -8
- package/dist/cjs/types.js +1 -0
- package/dist/esm/client/generateClient.js +5 -3
- package/dist/esm/operators/http.js +25 -0
- package/dist/esm/router/index.js +21 -8
- package/dist/esm/types.js +1 -0
- package/dist/types/operators/http.d.ts +6 -1
- package/dist/types/router/index.d.ts +1 -0
- package/dist/types/router/types.d.ts +1 -0
- package/dist/types/types.d.ts +3 -2
- package/package.json +5 -5
|
@@ -60,14 +60,16 @@ const generateClient = async ({ appDir, resourcePath, apiDir, lambdaDir, prefix,
|
|
|
60
60
|
}
|
|
61
61
|
let handlersCode = "";
|
|
62
62
|
for (const handlerInfo of handlerInfos) {
|
|
63
|
-
const { name, httpMethod, routePath } = handlerInfo;
|
|
63
|
+
const { name, httpMethod, routePath, action } = handlerInfo;
|
|
64
64
|
let exportStatement = `var ${name} =`;
|
|
65
65
|
if (name.toLowerCase() === "default") {
|
|
66
66
|
exportStatement = "default";
|
|
67
67
|
}
|
|
68
68
|
const upperHttpMethod = httpMethod.toUpperCase();
|
|
69
69
|
const routeName = routePath;
|
|
70
|
-
if (
|
|
70
|
+
if (action) {
|
|
71
|
+
handlersCode += `export ${exportStatement} createUploader('${routeName}');`;
|
|
72
|
+
} else if (target === "server") {
|
|
71
73
|
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', process.env.PORT || ${String(port)}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
|
|
72
74
|
`;
|
|
73
75
|
} else {
|
|
@@ -75,7 +77,7 @@ const generateClient = async ({ appDir, resourcePath, apiDir, lambdaDir, prefix,
|
|
|
75
77
|
`;
|
|
76
78
|
}
|
|
77
79
|
}
|
|
78
|
-
const importCode = `import { createRequest } from '${requestCreator}';
|
|
80
|
+
const importCode = `import { createRequest${handlerInfos.find((i) => i.action) ? ", createUploader" : ""} } from '${requestCreator}';
|
|
79
81
|
${fetcher ? `import { fetch } from '${fetcher}';
|
|
80
82
|
` : ""}`;
|
|
81
83
|
return (0, import_result.Ok)(`${importCode}
|
|
@@ -34,9 +34,11 @@ __export(http_exports, {
|
|
|
34
34
|
Redirect: () => Redirect,
|
|
35
35
|
SetHeaders: () => SetHeaders,
|
|
36
36
|
Trace: () => Trace,
|
|
37
|
+
Upload: () => Upload,
|
|
37
38
|
createHttpOperator: () => createHttpOperator
|
|
38
39
|
});
|
|
39
40
|
module.exports = __toCommonJS(http_exports);
|
|
41
|
+
var import_zod = require("zod");
|
|
40
42
|
var import_http = require("../errors/http");
|
|
41
43
|
var import_types = require("../types");
|
|
42
44
|
const validateInput = async (schema, input) => {
|
|
@@ -170,6 +172,29 @@ const Redirect = (url) => {
|
|
|
170
172
|
}
|
|
171
173
|
};
|
|
172
174
|
};
|
|
175
|
+
const Upload = (urlPath, schema) => {
|
|
176
|
+
const finalSchema = schema || import_zod.z.any();
|
|
177
|
+
return {
|
|
178
|
+
name: "Upload",
|
|
179
|
+
metadata({ setMetadata }) {
|
|
180
|
+
setMetadata(import_types.OperatorType.Trigger, {
|
|
181
|
+
type: import_types.TriggerType.Http,
|
|
182
|
+
path: urlPath,
|
|
183
|
+
method: import_types.HttpMethod.Post,
|
|
184
|
+
action: "upload"
|
|
185
|
+
});
|
|
186
|
+
setMetadata(import_types.HttpMetadata.Files, finalSchema);
|
|
187
|
+
},
|
|
188
|
+
async validate(helper, next) {
|
|
189
|
+
const { inputs: { formData: files } } = helper;
|
|
190
|
+
helper.inputs = {
|
|
191
|
+
...helper.inputs,
|
|
192
|
+
files: await validateInput(finalSchema, files)
|
|
193
|
+
};
|
|
194
|
+
return next();
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
};
|
|
173
198
|
// Annotate the CommonJS export names for ESM import in node:
|
|
174
199
|
0 && (module.exports = {
|
|
175
200
|
Connect,
|
|
@@ -188,5 +213,6 @@ const Redirect = (url) => {
|
|
|
188
213
|
Redirect,
|
|
189
214
|
SetHeaders,
|
|
190
215
|
Trace,
|
|
216
|
+
Upload,
|
|
191
217
|
createHttpOperator
|
|
192
218
|
});
|
package/dist/cjs/router/index.js
CHANGED
|
@@ -67,15 +67,20 @@ class ApiRouter {
|
|
|
67
67
|
getHandlerInfo(filename, originFuncName, handler) {
|
|
68
68
|
const httpMethod = this.getHttpMethod(originFuncName, handler);
|
|
69
69
|
const routeName = this.getRouteName(filename, handler);
|
|
70
|
+
const action = this.getAction(handler);
|
|
71
|
+
const responseObj = {
|
|
72
|
+
handler,
|
|
73
|
+
name: originFuncName,
|
|
74
|
+
httpMethod,
|
|
75
|
+
routeName,
|
|
76
|
+
filename,
|
|
77
|
+
routePath: this.getRoutePath(this.prefix, routeName)
|
|
78
|
+
};
|
|
79
|
+
if (action) {
|
|
80
|
+
responseObj.action = action;
|
|
81
|
+
}
|
|
70
82
|
if (httpMethod && routeName) {
|
|
71
|
-
return
|
|
72
|
-
handler,
|
|
73
|
-
name: originFuncName,
|
|
74
|
-
httpMethod,
|
|
75
|
-
routeName,
|
|
76
|
-
filename,
|
|
77
|
-
routePath: this.getRoutePath(this.prefix, routeName)
|
|
78
|
-
};
|
|
83
|
+
return responseObj;
|
|
79
84
|
}
|
|
80
85
|
return null;
|
|
81
86
|
}
|
|
@@ -151,6 +156,14 @@ class ApiRouter {
|
|
|
151
156
|
return import_types.HttpMethod.Get;
|
|
152
157
|
}
|
|
153
158
|
}
|
|
159
|
+
getAction(handler) {
|
|
160
|
+
if (handler) {
|
|
161
|
+
const trigger = Reflect.getMetadata(import_types.OperatorType.Trigger, handler);
|
|
162
|
+
if (trigger === null || trigger === void 0 ? void 0 : trigger.action) {
|
|
163
|
+
return trigger.action;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
154
167
|
loadApiFiles() {
|
|
155
168
|
if (!this.existLambdaDir) {
|
|
156
169
|
return [];
|
package/dist/cjs/types.js
CHANGED
|
@@ -43,6 +43,7 @@ var HttpMetadata;
|
|
|
43
43
|
HttpMetadata2["Params"] = "PARAMS";
|
|
44
44
|
HttpMetadata2["Headers"] = "HEADERS";
|
|
45
45
|
HttpMetadata2["Response"] = "RESPONSE";
|
|
46
|
+
HttpMetadata2["Files"] = "Files";
|
|
46
47
|
})(HttpMetadata || (HttpMetadata = {}));
|
|
47
48
|
var ResponseMetaType;
|
|
48
49
|
(function(ResponseMetaType2) {
|
|
@@ -26,14 +26,16 @@ const generateClient = async ({ appDir, resourcePath, apiDir, lambdaDir, prefix,
|
|
|
26
26
|
}
|
|
27
27
|
let handlersCode = "";
|
|
28
28
|
for (const handlerInfo of handlerInfos) {
|
|
29
|
-
const { name, httpMethod, routePath } = handlerInfo;
|
|
29
|
+
const { name, httpMethod, routePath, action } = handlerInfo;
|
|
30
30
|
let exportStatement = `var ${name} =`;
|
|
31
31
|
if (name.toLowerCase() === "default") {
|
|
32
32
|
exportStatement = "default";
|
|
33
33
|
}
|
|
34
34
|
const upperHttpMethod = httpMethod.toUpperCase();
|
|
35
35
|
const routeName = routePath;
|
|
36
|
-
if (
|
|
36
|
+
if (action) {
|
|
37
|
+
handlersCode += `export ${exportStatement} createUploader('${routeName}');`;
|
|
38
|
+
} else if (target === "server") {
|
|
37
39
|
handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', process.env.PORT || ${String(port)}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
|
|
38
40
|
`;
|
|
39
41
|
} else {
|
|
@@ -41,7 +43,7 @@ const generateClient = async ({ appDir, resourcePath, apiDir, lambdaDir, prefix,
|
|
|
41
43
|
`;
|
|
42
44
|
}
|
|
43
45
|
}
|
|
44
|
-
const importCode = `import { createRequest } from '${requestCreator}';
|
|
46
|
+
const importCode = `import { createRequest${handlerInfos.find((i) => i.action) ? ", createUploader" : ""} } from '${requestCreator}';
|
|
45
47
|
${fetcher ? `import { fetch } from '${fetcher}';
|
|
46
48
|
` : ""}`;
|
|
47
49
|
return Ok(`${importCode}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { ValidationError } from "../errors/http";
|
|
2
3
|
import { HttpMetadata, HttpMethod, OperatorType, ResponseMetaType, TriggerType } from "../types";
|
|
3
4
|
const validateInput = async (schema, input) => {
|
|
@@ -131,6 +132,29 @@ const Redirect = (url) => {
|
|
|
131
132
|
}
|
|
132
133
|
};
|
|
133
134
|
};
|
|
135
|
+
const Upload = (urlPath, schema) => {
|
|
136
|
+
const finalSchema = schema || z.any();
|
|
137
|
+
return {
|
|
138
|
+
name: "Upload",
|
|
139
|
+
metadata({ setMetadata }) {
|
|
140
|
+
setMetadata(OperatorType.Trigger, {
|
|
141
|
+
type: TriggerType.Http,
|
|
142
|
+
path: urlPath,
|
|
143
|
+
method: HttpMethod.Post,
|
|
144
|
+
action: "upload"
|
|
145
|
+
});
|
|
146
|
+
setMetadata(HttpMetadata.Files, finalSchema);
|
|
147
|
+
},
|
|
148
|
+
async validate(helper, next) {
|
|
149
|
+
const { inputs: { formData: files } } = helper;
|
|
150
|
+
helper.inputs = {
|
|
151
|
+
...helper.inputs,
|
|
152
|
+
files: await validateInput(finalSchema, files)
|
|
153
|
+
};
|
|
154
|
+
return next();
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
};
|
|
134
158
|
export {
|
|
135
159
|
Connect,
|
|
136
160
|
Data,
|
|
@@ -148,5 +172,6 @@ export {
|
|
|
148
172
|
Redirect,
|
|
149
173
|
SetHeaders,
|
|
150
174
|
Trace,
|
|
175
|
+
Upload,
|
|
151
176
|
createHttpOperator
|
|
152
177
|
};
|
package/dist/esm/router/index.js
CHANGED
|
@@ -33,15 +33,20 @@ class ApiRouter {
|
|
|
33
33
|
getHandlerInfo(filename, originFuncName, handler) {
|
|
34
34
|
const httpMethod = this.getHttpMethod(originFuncName, handler);
|
|
35
35
|
const routeName = this.getRouteName(filename, handler);
|
|
36
|
+
const action = this.getAction(handler);
|
|
37
|
+
const responseObj = {
|
|
38
|
+
handler,
|
|
39
|
+
name: originFuncName,
|
|
40
|
+
httpMethod,
|
|
41
|
+
routeName,
|
|
42
|
+
filename,
|
|
43
|
+
routePath: this.getRoutePath(this.prefix, routeName)
|
|
44
|
+
};
|
|
45
|
+
if (action) {
|
|
46
|
+
responseObj.action = action;
|
|
47
|
+
}
|
|
36
48
|
if (httpMethod && routeName) {
|
|
37
|
-
return
|
|
38
|
-
handler,
|
|
39
|
-
name: originFuncName,
|
|
40
|
-
httpMethod,
|
|
41
|
-
routeName,
|
|
42
|
-
filename,
|
|
43
|
-
routePath: this.getRoutePath(this.prefix, routeName)
|
|
44
|
-
};
|
|
49
|
+
return responseObj;
|
|
45
50
|
}
|
|
46
51
|
return null;
|
|
47
52
|
}
|
|
@@ -117,6 +122,14 @@ class ApiRouter {
|
|
|
117
122
|
return HttpMethod.Get;
|
|
118
123
|
}
|
|
119
124
|
}
|
|
125
|
+
getAction(handler) {
|
|
126
|
+
if (handler) {
|
|
127
|
+
const trigger = Reflect.getMetadata(OperatorType.Trigger, handler);
|
|
128
|
+
if (trigger === null || trigger === void 0 ? void 0 : trigger.action) {
|
|
129
|
+
return trigger.action;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
120
133
|
loadApiFiles() {
|
|
121
134
|
if (!this.existLambdaDir) {
|
|
122
135
|
return [];
|
package/dist/esm/types.js
CHANGED
|
@@ -15,6 +15,7 @@ var HttpMetadata;
|
|
|
15
15
|
HttpMetadata2["Params"] = "PARAMS";
|
|
16
16
|
HttpMetadata2["Headers"] = "HEADERS";
|
|
17
17
|
HttpMetadata2["Response"] = "RESPONSE";
|
|
18
|
+
HttpMetadata2["Files"] = "Files";
|
|
18
19
|
})(HttpMetadata || (HttpMetadata = {}));
|
|
19
20
|
var ResponseMetaType;
|
|
20
21
|
(function(ResponseMetaType2) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { z } from 'zod';
|
|
2
2
|
import { HttpMethod, type Operator, ResponseMetaType } from '../types';
|
|
3
3
|
export interface ResponseMeta {
|
|
4
4
|
type: ResponseMetaType;
|
|
@@ -37,3 +37,8 @@ export declare const Headers: <Schema extends z.ZodType<any, z.ZodTypeDef, any>>
|
|
|
37
37
|
export declare const HttpCode: (statusCode: number) => Operator<void>;
|
|
38
38
|
export declare const SetHeaders: (headers: Record<string, string>) => Operator<void>;
|
|
39
39
|
export declare const Redirect: (url: string) => Operator<void>;
|
|
40
|
+
export declare const Upload: <Schema extends z.ZodType<any, z.ZodTypeDef, any>>(urlPath: string, schema?: Schema | undefined) => Operator<{
|
|
41
|
+
files: z.input<Schema>;
|
|
42
|
+
}, {
|
|
43
|
+
formData: z.output<Schema>;
|
|
44
|
+
}>;
|
|
@@ -32,6 +32,7 @@ export declare class ApiRouter {
|
|
|
32
32
|
getSafeRoutePath(filename: string, handler?: ApiHandler): string;
|
|
33
33
|
getRouteName(filename: string, handler?: ApiHandler): string;
|
|
34
34
|
getHttpMethod(originHandlerName: string, handler?: ApiHandler): HttpMethod | null;
|
|
35
|
+
getAction(handler?: ApiHandler): string | undefined;
|
|
35
36
|
loadApiFiles(): string[];
|
|
36
37
|
getApiFiles(): string[];
|
|
37
38
|
getApiHandlers(): Promise<APIHandlerInfo[]>;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -12,7 +12,8 @@ export declare enum HttpMetadata {
|
|
|
12
12
|
Query = "QUERY",
|
|
13
13
|
Params = "PARAMS",
|
|
14
14
|
Headers = "HEADERS",
|
|
15
|
-
Response = "RESPONSE"
|
|
15
|
+
Response = "RESPONSE",
|
|
16
|
+
Files = "Files"
|
|
16
17
|
}
|
|
17
18
|
export declare enum ResponseMetaType {
|
|
18
19
|
StatusCode = 0,
|
|
@@ -30,7 +31,7 @@ export declare enum HttpMethod {
|
|
|
30
31
|
Options = "OPTIONS",
|
|
31
32
|
Head = "HEAD"
|
|
32
33
|
}
|
|
33
|
-
export type InputSchemaMeata = Extract<HttpMetadata, HttpMetadata.Data | HttpMetadata.Query | HttpMetadata.Headers | HttpMetadata.Params>;
|
|
34
|
+
export type InputSchemaMeata = Extract<HttpMetadata, HttpMetadata.Data | HttpMetadata.Query | HttpMetadata.Headers | HttpMetadata.Params | HttpMetadata.Files>;
|
|
34
35
|
export type ExecuteFunc<Outputs> = (helper: ExecuteHelper<Outputs>, next: () => Promise<any>) => Promise<any>;
|
|
35
36
|
export type ExecuteHelper<Outputs> = {
|
|
36
37
|
result?: any;
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "2.60.
|
|
18
|
+
"version": "2.60.6",
|
|
19
19
|
"jsnext:source": "./src/index.ts",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"main": "./dist/cjs/index.js",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"koa-compose": "^4.1.0",
|
|
33
33
|
"reflect-metadata": "^0.1.13",
|
|
34
34
|
"type-fest": "2.15.0",
|
|
35
|
-
"@modern-js/utils": "2.60.
|
|
35
|
+
"@modern-js/utils": "2.60.6"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/jest": "^29",
|
|
@@ -43,9 +43,9 @@
|
|
|
43
43
|
"tsconfig-paths": "^4.1.2",
|
|
44
44
|
"typescript": "^5",
|
|
45
45
|
"zod": "^3.22.3",
|
|
46
|
-
"@
|
|
47
|
-
"@
|
|
48
|
-
"@scripts/jest-config": "2.60.
|
|
46
|
+
"@scripts/build": "2.60.6",
|
|
47
|
+
"@modern-js/types": "2.60.6",
|
|
48
|
+
"@scripts/jest-config": "2.60.6"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"ts-node": "^10.9.1",
|