generated from arvinxx/vercel-serverless-api-template
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,715 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,5 @@ Dockerfile* | |
|
||
# misc | ||
# add other ignore file below | ||
tests | ||
.env.example | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createGatewayOnNodeRuntime } from '../../src'; | ||
|
||
export default createGatewayOnNodeRuntime(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
Oops, something went wrong.