@modern-js/bff-core 2.60.5 → 2.61.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/cjs/client/generateClient.js +5 -3
- package/dist/cjs/operators/http.js +27 -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 +26 -0
- package/dist/esm/router/index.js +21 -8
- package/dist/esm/types.js +1 -0
- package/dist/types/operators/http.d.ts +5 -0
- 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,6 +34,7 @@ __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);
|
|
@@ -170,6 +171,31 @@ const Redirect = (url) => {
|
|
|
170
171
|
}
|
|
171
172
|
};
|
|
172
173
|
};
|
|
174
|
+
const Upload = (urlPath, schema) => {
|
|
175
|
+
return {
|
|
176
|
+
name: "Upload",
|
|
177
|
+
metadata({ setMetadata }) {
|
|
178
|
+
setMetadata(import_types.OperatorType.Trigger, {
|
|
179
|
+
type: import_types.TriggerType.Http,
|
|
180
|
+
path: urlPath,
|
|
181
|
+
method: import_types.HttpMethod.Post,
|
|
182
|
+
action: "upload"
|
|
183
|
+
});
|
|
184
|
+
setMetadata(import_types.HttpMetadata.Files, schema);
|
|
185
|
+
},
|
|
186
|
+
async validate(helper, next) {
|
|
187
|
+
if (!schema) {
|
|
188
|
+
return next();
|
|
189
|
+
}
|
|
190
|
+
const { inputs: { formData: files } } = helper;
|
|
191
|
+
helper.inputs = {
|
|
192
|
+
...helper.inputs,
|
|
193
|
+
files: await validateInput(schema, files)
|
|
194
|
+
};
|
|
195
|
+
return next();
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
};
|
|
173
199
|
// Annotate the CommonJS export names for ESM import in node:
|
|
174
200
|
0 && (module.exports = {
|
|
175
201
|
Connect,
|
|
@@ -188,5 +214,6 @@ const Redirect = (url) => {
|
|
|
188
214
|
Redirect,
|
|
189
215
|
SetHeaders,
|
|
190
216
|
Trace,
|
|
217
|
+
Upload,
|
|
191
218
|
createHttpOperator
|
|
192
219
|
});
|
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}
|
|
@@ -131,6 +131,31 @@ const Redirect = (url) => {
|
|
|
131
131
|
}
|
|
132
132
|
};
|
|
133
133
|
};
|
|
134
|
+
const Upload = (urlPath, schema) => {
|
|
135
|
+
return {
|
|
136
|
+
name: "Upload",
|
|
137
|
+
metadata({ setMetadata }) {
|
|
138
|
+
setMetadata(OperatorType.Trigger, {
|
|
139
|
+
type: TriggerType.Http,
|
|
140
|
+
path: urlPath,
|
|
141
|
+
method: HttpMethod.Post,
|
|
142
|
+
action: "upload"
|
|
143
|
+
});
|
|
144
|
+
setMetadata(HttpMetadata.Files, schema);
|
|
145
|
+
},
|
|
146
|
+
async validate(helper, next) {
|
|
147
|
+
if (!schema) {
|
|
148
|
+
return next();
|
|
149
|
+
}
|
|
150
|
+
const { inputs: { formData: files } } = helper;
|
|
151
|
+
helper.inputs = {
|
|
152
|
+
...helper.inputs,
|
|
153
|
+
files: await validateInput(schema, files)
|
|
154
|
+
};
|
|
155
|
+
return next();
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
};
|
|
134
159
|
export {
|
|
135
160
|
Connect,
|
|
136
161
|
Data,
|
|
@@ -148,5 +173,6 @@ export {
|
|
|
148
173
|
Redirect,
|
|
149
174
|
SetHeaders,
|
|
150
175
|
Trace,
|
|
176
|
+
Upload,
|
|
151
177
|
createHttpOperator
|
|
152
178
|
};
|
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) {
|
|
@@ -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.
|
|
18
|
+
"version": "2.61.0",
|
|
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.
|
|
35
|
+
"@modern-js/utils": "2.61.0"
|
|
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
|
-
"@modern-js/types": "2.
|
|
47
|
-
"@scripts/
|
|
48
|
-
"@scripts/
|
|
46
|
+
"@modern-js/types": "2.61.0",
|
|
47
|
+
"@scripts/jest-config": "2.61.0",
|
|
48
|
+
"@scripts/build": "2.61.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"ts-node": "^10.9.1",
|