Skip to content

Commit

Permalink
✨ feat: 更新入参配置,解决跨域兼容性的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 29, 2023
1 parent 6b9fa27 commit c02451c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
* web APIs. Feel free to use it in your own projects.
*/

type StaticOrigin = boolean | string | RegExp | (boolean | string | RegExp)[];
export type StaticOrigin = boolean | string | RegExp | (boolean | string | RegExp)[];

type OriginFn = (origin: string | undefined, req: Request) => StaticOrigin | Promise<StaticOrigin>;
export type OriginFn = (
origin: string | undefined,
req: Request,
) => StaticOrigin | Promise<StaticOrigin>;

export interface CorsOptions {
allowedHeaders?: string | string[];
Expand Down
24 changes: 16 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const DEFAULT_PLUGINS_INDEX_URL = 'https://chat-plugins.lobehub.com';

const createGateway = (pluginsIndexUrl: string = DEFAULT_PLUGINS_INDEX_URL) => {
return async (req: Request) => {
// ========== 1. 校验请求入参基础格式 ========== //
// ========== 1. 校验请求方法 ========== //
if (req.method !== 'POST')
return createErrorResponse(PluginErrorType.MethodNotAllowed, {
message: '[gateway] only allow POST method',
});

// ========== 2. 校验请求入参基础格式 ========== //
const requestPayload = (await req.json()) as PluginRequestPayload;

const payloadParseResult = pluginRequestPayloadSchema.safeParse(requestPayload);
Expand All @@ -37,7 +43,7 @@ const createGateway = (pluginsIndexUrl: string = DEFAULT_PLUGINS_INDEX_URL) => {
// 入参中如果没有 manifest,则从插件市场索引中获取
if (!manifest) {
const marketIndexUrl = indexUrl ?? pluginsIndexUrl;
// ========== 2. 获取插件市场索引 ========== //
// ========== 3. 获取插件市场索引 ========== //

let marketIndex: LobeChatPluginsMarketIndex | undefined;
try {
Expand Down Expand Up @@ -68,7 +74,7 @@ const createGateway = (pluginsIndexUrl: string = DEFAULT_PLUGINS_INDEX_URL) => {

console.info('marketIndex:', marketIndex);

// ========== 3. 校验插件 meta 完备性 ========== //
// ========== 4. 校验插件 meta 完备性 ========== //

const pluginMeta = marketIndex.plugins.find((i) => i.identifier === identifier);

Expand Down Expand Up @@ -100,7 +106,7 @@ const createGateway = (pluginsIndexUrl: string = DEFAULT_PLUGINS_INDEX_URL) => {
message: '[plugin] plugin meta is invalid',
pluginMeta,
});
// ========== 4. 校验插件 manifest 完备性 ========== //
// ========== 5. 校验插件 manifest 完备性 ========== //

// 获取插件的 manifest
try {
Expand Down Expand Up @@ -129,7 +135,7 @@ const createGateway = (pluginsIndexUrl: string = DEFAULT_PLUGINS_INDEX_URL) => {

console.log(`[${identifier}] plugin manifest:`, manifest);

// ========== 5. 校验是否按照 manifest 包含了 settings 配置 ========== //
// ========== 6. 校验是否按照 manifest 包含了 settings 配置 ========== //
const settings = getPluginSettingsStringFromRequest(req);

if (manifest.settings) {
Expand All @@ -143,7 +149,7 @@ const createGateway = (pluginsIndexUrl: string = DEFAULT_PLUGINS_INDEX_URL) => {
});
}

// ========== 6. 校验请求入参与 manifest 要求一致性 ========== //
// ========== 7. 校验请求入参与 manifest 要求一致性 ========== //
const api = manifest.api.find((i) => i.name === apiName);

if (!api)
Expand All @@ -170,7 +176,7 @@ const createGateway = (pluginsIndexUrl: string = DEFAULT_PLUGINS_INDEX_URL) => {
});
}

// ========== 7. 发送请求 ========== //
// ========== 8. 发送请求 ========== //

const response = await fetch(api.url, {
body: args,
Expand Down Expand Up @@ -202,7 +208,9 @@ export interface GatewayOptions {
* @param options {GatewayOptions}
*/
export const createLobeChatPluginGateway = (options: GatewayOptions = {}) => {
const handler = createGateway(options.pluginsIndexUrl ?? DEFAULT_PLUGINS_INDEX_URL);
const handler = createGateway(
!!options.pluginsIndexUrl ? options.pluginsIndexUrl : DEFAULT_PLUGINS_INDEX_URL,
);

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

0 comments on commit c02451c

Please sign in to comment.