generated from arvinxx/vercel-serverless-api-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
8 changed files
with
120 additions
and
18 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 |
---|---|---|
@@ -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
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,5 +1,10 @@ | ||
export { | ||
createGatewayOnEdgeRuntime, | ||
/** | ||
* please use `createGatewayOnEdgeRuntime` instead | ||
* @deprecated | ||
*/ | ||
createGatewayOnEdgeRuntime as createLobeChatPluginGateway, | ||
} from './edge'; | ||
export * from './gateway'; | ||
export * from './node'; |
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,54 @@ | ||
import { | ||
PluginErrorType, | ||
PluginRequestPayload, | ||
getPluginErrorStatus, | ||
getPluginSettingsFromHeaders, | ||
} from '@lobehub/chat-plugin-sdk'; | ||
import { VercelRequest, VercelResponse } from '@vercel/node'; | ||
import Ajv from 'ajv'; | ||
|
||
import { Gateway, GatewayErrorResponse, GatewayOptions } from './gateway'; | ||
|
||
export type NodeRuntimeGatewayOptions = Pick<GatewayOptions, 'pluginsIndexUrl'>; | ||
|
||
export const createGatewayOnNodeRuntime = (options: NodeRuntimeGatewayOptions = {}) => { | ||
const gateway = new Gateway({ | ||
...options, | ||
Validator: (schema, value) => { | ||
const ajv = new Ajv(); | ||
const validate = ajv.compile(schema); | ||
|
||
const valid = validate(value); | ||
return { | ||
errors: validate.errors, | ||
valid, | ||
}; | ||
}, | ||
}); | ||
|
||
return async (req: VercelRequest, res: VercelResponse) => { | ||
// ========== 1. 校验请求方法 ========== // | ||
if (req.method !== 'POST') { | ||
res.status(PluginErrorType.MethodNotAllowed).send({ | ||
message: '[gateway] only allow POST method', | ||
}); | ||
|
||
return; | ||
} | ||
|
||
const requestPayload = req.body as PluginRequestPayload; | ||
|
||
const settings = getPluginSettingsFromHeaders(req.headers as any); | ||
|
||
try { | ||
const { data } = await gateway.execute(requestPayload, settings); | ||
|
||
res.send(data); | ||
} catch (error) { | ||
console.log(error); | ||
const { errorType, body } = error as GatewayErrorResponse; | ||
|
||
res.status(getPluginErrorStatus(errorType)).send({ body, errorType }); | ||
} | ||
}; | ||
}; |
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,10 @@ | ||
import { Gateway } from '@lobehub/chat-plugins-gateway'; | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
describe('Gateway', () => { | ||
it('should init with pluginIndexUrl', () => { | ||
const gateway = new Gateway({ pluginsIndexUrl: 'https://test-market-index-url.com' }); | ||
|
||
expect(gateway['pluginIndexUrl']).toBe('https://test-market-index-url.com'); | ||
}); | ||
}); |