Skip to content

Commit e8c8cb5

Browse files
committed
♻️ refactor: refactor to the Gateway Class and add tests
1 parent 39aaefd commit e8c8cb5

File tree

10 files changed

+1338
-320
lines changed

10 files changed

+1338
-320
lines changed

.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,5 @@ Dockerfile*
5959

6060
# misc
6161
# add other ignore file below
62-
tests
6362
.env.example
6463
.env

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
"prettier": "prettier -c --write \"**/**\"",
2323
"release": "semantic-release",
2424
"start": "vercel dev",
25-
"test": "vitest --passWithNoTests",
26-
"test:coverage": "vitest --coverage --passWithNoTests",
25+
"test": "vitest",
26+
"test:coverage": "vitest --coverage",
27+
"test:update": "vitest -u",
2728
"type-check": "tsc --noEmit"
2829
},
2930
"lint-staged": {
@@ -58,7 +59,7 @@
5859
"@commitlint/cli": "^17",
5960
"@lobehub/lint": "latest",
6061
"@vercel/node": "^2",
61-
"@vitest/coverage-v8": "latest",
62+
"@vitest/coverage-v8": "0.34.6",
6263
"commitlint": "^17",
6364
"cross-env": "^7",
6465
"eslint": "^8",
@@ -71,7 +72,7 @@
7172
"semantic-release": "^21",
7273
"typescript": "^5",
7374
"vercel": "^28.20.0",
74-
"vitest": "latest"
75+
"vitest": "0.34.6"
7576
},
7677
"publishConfig": {
7778
"access": "public",

src/edge.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
PluginErrorType,
3+
PluginRequestPayload,
4+
createErrorResponse,
5+
getPluginSettingsFromRequest,
6+
} from '@lobehub/chat-plugin-sdk';
7+
8+
import cors, { CorsOptions } from './cors';
9+
import { Gateway, GatewayErrorResponse, GatewayOptions } from './gateway';
10+
11+
export interface EdgeRuntimeGatewayOptions extends GatewayOptions {
12+
cors?: CorsOptions;
13+
}
14+
15+
/**
16+
* create Gateway Edge Function with plugins index url
17+
* @param options {EdgeRuntimeGatewayOptions}
18+
*/
19+
export const createGatewayOnEdgeRuntime = (options: EdgeRuntimeGatewayOptions = {}) => {
20+
const gateway = new Gateway(options);
21+
22+
const handler = async (req: Request): Promise<Response> => {
23+
// ========== 1. 校验请求方法 ========== //
24+
if (req.method !== 'POST')
25+
return createErrorResponse(PluginErrorType.MethodNotAllowed, {
26+
message: '[gateway] only allow POST method',
27+
});
28+
29+
const requestPayload = (await req.json()) as PluginRequestPayload;
30+
const settings = getPluginSettingsFromRequest(req);
31+
32+
try {
33+
const res = await gateway.execute(requestPayload, settings);
34+
return new Response(res.data);
35+
} catch (error) {
36+
const { errorType, body } = error as GatewayErrorResponse;
37+
38+
return createErrorResponse(errorType, body);
39+
}
40+
};
41+
42+
return async (req: Request) => cors(req, await handler(req), options.cors);
43+
};

0 commit comments

Comments
 (0)