Skip to content

Commit

Permalink
✨ feat: support node runtime (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Dec 13, 2023
1 parent 39aaefd commit 28ef918
Show file tree
Hide file tree
Showing 16 changed files with 1,715 additions and 324 deletions.
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ Dockerfile*

# misc
# add other ignore file below
tests
.env.example
.env
108 changes: 108 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,114 @@

# Changelog

## [Version 1.8.0-beta.4](https://github.com/lobehub/chat-plugins-gateway/compare/v1.8.0-beta.3...v1.8.0-beta.4)

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

#### 🐛 Bug Fixes

- **misc**: Close ajv strict mode.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's fixed

- **misc**: Close ajv strict mode ([a6eb5b6](https://github.com/lobehub/chat-plugins-gateway/commit/a6eb5b6))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>

## [Version 1.8.0-beta.3](https://github.com/lobehub/chat-plugins-gateway/compare/v1.8.0-beta.2...v1.8.0-beta.3)

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

#### 🐛 Bug Fixes

- **misc**: Fix ajv schema.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's fixed

- **misc**: Fix ajv schema ([40f22c5](https://github.com/lobehub/chat-plugins-gateway/commit/40f22c5))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>

## [Version 1.8.0-beta.2](https://github.com/lobehub/chat-plugins-gateway/compare/v1.8.0-beta.1...v1.8.0-beta.2)

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

#### 🐛 Bug Fixes

- **misc**: Fix request input.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's fixed

- **misc**: Fix request input ([1b896de](https://github.com/lobehub/chat-plugins-gateway/commit/1b896de))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>

## [Version 1.8.0-beta.1](https://github.com/lobehub/chat-plugins-gateway/compare/v1.7.2...v1.8.0-beta.1)

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

#### ♻ Code Refactoring

- **misc**: Refactor to the Gateway Class and add tests.

#### ✨ Features

- **misc**: Support node runtime.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### Code refactoring

- **misc**: Refactor to the Gateway Class and add tests ([e8c8cb5](https://github.com/lobehub/chat-plugins-gateway/commit/e8c8cb5))

#### What's improved

- **misc**: Support node runtime ([a8e2933](https://github.com/lobehub/chat-plugins-gateway/commit/a8e2933))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>

### [Version 1.7.2](https://github.com/lobehub/chat-plugins-gateway/compare/v1.7.1...v1.7.2)

<sup>Released on **2023-12-12**</sup>
Expand Down
7 changes: 7 additions & 0 deletions api/v1/edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createGatewayOnEdgeRuntime } from '../../src';

export const config = {
runtime: 'edge',
};

export default createGatewayOnEdgeRuntime();
3 changes: 3 additions & 0 deletions api/v1/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createGatewayOnNodeRuntime } from '../../src';

export default createGatewayOnNodeRuntime();
4 changes: 2 additions & 2 deletions api/v1/runner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createLobeChatPluginGateway } from '../../src';
import { createGatewayOnEdgeRuntime } from '../../src';

export const config = {
runtime: 'edge',
};

export default createLobeChatPluginGateway();
export default createGatewayOnEdgeRuntime();
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lobehub/chat-plugins-gateway",
"version": "1.7.2",
"version": "1.8.0-beta.4",
"description": "Lobe Chat Plugin Market",
"repository": "https://github.com/lobehub/chat-plugins-gateway.git",
"author": "LobeHub <i@lobehub.com>",
Expand All @@ -22,8 +22,9 @@
"prettier": "prettier -c --write \"**/**\"",
"release": "semantic-release",
"start": "vercel dev",
"test": "vitest --passWithNoTests",
"test:coverage": "vitest --coverage --passWithNoTests",
"test": "vitest",
"test:coverage": "vitest --coverage",
"test:update": "vitest -u",
"type-check": "tsc --noEmit"
},
"lint-staged": {
Expand All @@ -49,16 +50,18 @@
"not ie <= 10"
],
"dependencies": {
"@babel/runtime": "^7",
"@cfworker/json-schema": "^1",
"@lobehub/chat-plugin-sdk": "latest",
"swagger-client": "^3.24.5",
"@lobehub/chat-plugin-sdk": "^1",
"ajv": "^8",
"swagger-client": "^3",
"zod": "^3"
},
"devDependencies": {
"@commitlint/cli": "^17",
"@lobehub/lint": "latest",
"@vercel/node": "^2",
"@vitest/coverage-v8": "latest",
"@vitest/coverage-v8": "0.34.6",
"commitlint": "^17",
"cross-env": "^7",
"eslint": "^8",
Expand All @@ -70,8 +73,8 @@
"remark-cli": "^11",
"semantic-release": "^21",
"typescript": "^5",
"vercel": "^28.20.0",
"vitest": "latest"
"vercel": "^32.7.1",
"vitest": "0.34.6"
},
"publishConfig": {
"access": "public",
Expand Down
43 changes: 43 additions & 0 deletions src/edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
PluginErrorType,
PluginRequestPayload,
createErrorResponse,
getPluginSettingsFromRequest,
} from '@lobehub/chat-plugin-sdk';

import cors, { CorsOptions } from './cors';
import { Gateway, GatewayErrorResponse, GatewayOptions } from './gateway';

export interface EdgeRuntimeGatewayOptions extends GatewayOptions {
cors?: CorsOptions;
}

/**
* create Gateway Edge Function with plugins index url
* @param options {EdgeRuntimeGatewayOptions}
*/
export const createGatewayOnEdgeRuntime = (options: EdgeRuntimeGatewayOptions = {}) => {
const gateway = new Gateway(options);

const handler = async (req: Request): Promise<Response> => {
// ========== 1. 校验请求方法 ========== //
if (req.method !== 'POST')
return createErrorResponse(PluginErrorType.MethodNotAllowed, {
message: '[gateway] only allow POST method',
});

const requestPayload = (await req.json()) as PluginRequestPayload;
const settings = getPluginSettingsFromRequest(req);

try {
const res = await gateway.execute(requestPayload, settings);
return new Response(res.data);
} catch (error) {
const { errorType, body } = error as GatewayErrorResponse;

return createErrorResponse(errorType, body);
}
};

return async (req: Request) => cors(req, await handler(req), options.cors);
};
Loading

0 comments on commit 28ef918

Please sign in to comment.