Skip to content

Commit 28ef918

Browse files
authored
✨ feat: support node runtime (#9)
1 parent 39aaefd commit 28ef918

File tree

16 files changed

+1715
-324
lines changed

16 files changed

+1715
-324
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

CHANGELOG.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,114 @@
22

33
# Changelog
44

5+
## [Version 1.8.0-beta.4](https://github.com/lobehub/chat-plugins-gateway/compare/v1.8.0-beta.3...v1.8.0-beta.4)
6+
7+
<sup>Released on **2023-12-13**</sup>
8+
9+
#### 🐛 Bug Fixes
10+
11+
- **misc**: Close ajv strict mode.
12+
13+
<br/>
14+
15+
<details>
16+
<summary><kbd>Improvements and Fixes</kbd></summary>
17+
18+
#### What's fixed
19+
20+
- **misc**: Close ajv strict mode ([a6eb5b6](https://github.com/lobehub/chat-plugins-gateway/commit/a6eb5b6))
21+
22+
</details>
23+
24+
<div align="right">
25+
26+
[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
27+
28+
</div>
29+
30+
## [Version 1.8.0-beta.3](https://github.com/lobehub/chat-plugins-gateway/compare/v1.8.0-beta.2...v1.8.0-beta.3)
31+
32+
<sup>Released on **2023-12-13**</sup>
33+
34+
#### 🐛 Bug Fixes
35+
36+
- **misc**: Fix ajv schema.
37+
38+
<br/>
39+
40+
<details>
41+
<summary><kbd>Improvements and Fixes</kbd></summary>
42+
43+
#### What's fixed
44+
45+
- **misc**: Fix ajv schema ([40f22c5](https://github.com/lobehub/chat-plugins-gateway/commit/40f22c5))
46+
47+
</details>
48+
49+
<div align="right">
50+
51+
[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
52+
53+
</div>
54+
55+
## [Version 1.8.0-beta.2](https://github.com/lobehub/chat-plugins-gateway/compare/v1.8.0-beta.1...v1.8.0-beta.2)
56+
57+
<sup>Released on **2023-12-13**</sup>
58+
59+
#### 🐛 Bug Fixes
60+
61+
- **misc**: Fix request input.
62+
63+
<br/>
64+
65+
<details>
66+
<summary><kbd>Improvements and Fixes</kbd></summary>
67+
68+
#### What's fixed
69+
70+
- **misc**: Fix request input ([1b896de](https://github.com/lobehub/chat-plugins-gateway/commit/1b896de))
71+
72+
</details>
73+
74+
<div align="right">
75+
76+
[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
77+
78+
</div>
79+
80+
## [Version 1.8.0-beta.1](https://github.com/lobehub/chat-plugins-gateway/compare/v1.7.2...v1.8.0-beta.1)
81+
82+
<sup>Released on **2023-12-13**</sup>
83+
84+
#### ♻ Code Refactoring
85+
86+
- **misc**: Refactor to the Gateway Class and add tests.
87+
88+
#### ✨ Features
89+
90+
- **misc**: Support node runtime.
91+
92+
<br/>
93+
94+
<details>
95+
<summary><kbd>Improvements and Fixes</kbd></summary>
96+
97+
#### Code refactoring
98+
99+
- **misc**: Refactor to the Gateway Class and add tests ([e8c8cb5](https://github.com/lobehub/chat-plugins-gateway/commit/e8c8cb5))
100+
101+
#### What's improved
102+
103+
- **misc**: Support node runtime ([a8e2933](https://github.com/lobehub/chat-plugins-gateway/commit/a8e2933))
104+
105+
</details>
106+
107+
<div align="right">
108+
109+
[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
110+
111+
</div>
112+
5113
### [Version 1.7.2](https://github.com/lobehub/chat-plugins-gateway/compare/v1.7.1...v1.7.2)
6114

7115
<sup>Released on **2023-12-12**</sup>

api/v1/edge.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createGatewayOnEdgeRuntime } from '../../src';
2+
3+
export const config = {
4+
runtime: 'edge',
5+
};
6+
7+
export default createGatewayOnEdgeRuntime();

api/v1/node.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createGatewayOnNodeRuntime } from '../../src';
2+
3+
export default createGatewayOnNodeRuntime();

api/v1/runner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { createLobeChatPluginGateway } from '../../src';
1+
import { createGatewayOnEdgeRuntime } from '../../src';
22

33
export const config = {
44
runtime: 'edge',
55
};
66

7-
export default createLobeChatPluginGateway();
7+
export default createGatewayOnEdgeRuntime();

package.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lobehub/chat-plugins-gateway",
3-
"version": "1.7.2",
3+
"version": "1.8.0-beta.4",
44
"description": "Lobe Chat Plugin Market",
55
"repository": "https://github.com/lobehub/chat-plugins-gateway.git",
66
"author": "LobeHub <i@lobehub.com>",
@@ -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": {
@@ -49,16 +50,18 @@
4950
"not ie <= 10"
5051
],
5152
"dependencies": {
53+
"@babel/runtime": "^7",
5254
"@cfworker/json-schema": "^1",
53-
"@lobehub/chat-plugin-sdk": "latest",
54-
"swagger-client": "^3.24.5",
55+
"@lobehub/chat-plugin-sdk": "^1",
56+
"ajv": "^8",
57+
"swagger-client": "^3",
5558
"zod": "^3"
5659
},
5760
"devDependencies": {
5861
"@commitlint/cli": "^17",
5962
"@lobehub/lint": "latest",
6063
"@vercel/node": "^2",
61-
"@vitest/coverage-v8": "latest",
64+
"@vitest/coverage-v8": "0.34.6",
6265
"commitlint": "^17",
6366
"cross-env": "^7",
6467
"eslint": "^8",
@@ -70,8 +73,8 @@
7073
"remark-cli": "^11",
7174
"semantic-release": "^21",
7275
"typescript": "^5",
73-
"vercel": "^28.20.0",
74-
"vitest": "latest"
76+
"vercel": "^32.7.1",
77+
"vitest": "0.34.6"
7578
},
7679
"publishConfig": {
7780
"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)