From 37d56e96d47b8e94f50752a3cc3a110fa7f2b0b2 Mon Sep 17 00:00:00 2001 From: arvinxx Date: Sat, 19 Aug 2023 14:48:03 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20feat:=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=B8=82=E5=9C=BA=E7=B4=A2=E5=BC=95=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.js | 1 + package.json | 3 ++- src/error.ts | 32 +++++++++++++++++++++++++++++--- src/index.ts | 1 + src/market.ts | 15 +++++++++++++++ src/types/error.ts | 22 ---------------------- src/types/index.ts | 1 - src/types/market.ts | 4 ++-- 8 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 src/market.ts delete mode 100644 src/types/error.ts diff --git a/.eslintrc.js b/.eslintrc.js index 12aca84..0efbc60 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -5,5 +5,6 @@ module.exports = { rules: { ...config.rules, 'unicorn/prefer-string-replace-all': 0, + 'unicorn/switch-case-braces': 0, }, }; diff --git a/package.json b/package.json index f614c6d..92351a8 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,8 @@ ] }, "dependencies": { - "@types/json-schema": "^7.0.12" + "@types/json-schema": "^7.0.12", + "zod": "^3.22.2" }, "devDependencies": { "@commitlint/cli": "^17", diff --git a/src/error.ts b/src/error.ts index acc6444..861bcc8 100644 --- a/src/error.ts +++ b/src/error.ts @@ -1,14 +1,40 @@ -import { ErrorResponse, ErrorType } from '@/types/error'; +export enum ErrorType { + // ******* 业务错误语义 ******* // + PluginMarketIndexNotFound = 'pluginMarketIndexNotFound', // 插件市场索引解析失败 + PluginMarketIndexParseError = 'pluginMarketIndexParseError', // 插件市场索引解析失败 + + // ******* 客户端错误 ******* // + BadRequest = 400, + Unauthorized = 401, + Forbidden = 403, + ContentNotFound = 404, // 没找到接口 + MethodNotAllowed = 405, // 不支持 + TooManyRequests = 429, + + // ******* 服务端错误 ******* // + InternalServerError = 500, + BadGateway = 502, + ServiceUnavailable = 503, + GatewayTimeout = 504, +} const getStatus = (errorType: ErrorType) => { - // TODO: Add ErrorSwitch - // eslint-disable-next-line no-empty switch (errorType) { + case ErrorType.PluginMarketIndexNotFound: + return 404; + + case ErrorType.PluginMarketIndexParseError: + return 590; } return errorType; }; +export interface ErrorResponse { + body: any; + errorType: ErrorType; +} + /** * 创建一个错误响应对象 * @param {ErrorType} errorType - 错误类型 diff --git a/src/index.ts b/src/index.ts index c852b1f..e9ff49b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export * from './error'; +export * from './market'; export * from './types'; diff --git a/src/market.ts b/src/market.ts new file mode 100644 index 0000000..da71a87 --- /dev/null +++ b/src/market.ts @@ -0,0 +1,15 @@ +import { z } from 'zod'; + +export const lobeChatPluginMetaSchema = z.object({ + createAt: z.string(), + homepage: z.string(), + manifest: z.string({}), + meta: z.object({}), + name: z.string(), + schemaVersion: z.string(), +}); + +export const marketIndexSchema = z.object({ + plugins: z.array(lobeChatPluginMetaSchema), + version: z.number(), +}); diff --git a/src/types/error.ts b/src/types/error.ts deleted file mode 100644 index cafa70e..0000000 --- a/src/types/error.ts +++ /dev/null @@ -1,22 +0,0 @@ -export enum ErrorType { - // ******* 业务错误语义 ******* // - - // ******* 客户端错误 ******* // - BadRequest = 400, - Unauthorized = 401, - Forbidden = 403, - ContentNotFound = 404, // 没找到接口 - MethodNotAllowed = 405, // 不支持 - TooManyRequests = 429, - - // ******* 服务端错误 ******* // - InternalServerError = 500, - BadGateway = 502, - ServiceUnavailable = 503, - GatewayTimeout = 504, -} - -export interface ErrorResponse { - body: any; - errorType: ErrorType; -} diff --git a/src/types/index.ts b/src/types/index.ts index bdb19ce..0fc0dff 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,3 @@ export * from './chatGPT'; -export * from './error'; export * from './manifest'; export * from './market'; diff --git a/src/types/market.ts b/src/types/market.ts index 7b0017b..5a650ab 100644 --- a/src/types/market.ts +++ b/src/types/market.ts @@ -11,7 +11,7 @@ export interface LobeChatPluginsMarketIndex { * @nameCN 插件列表 * @descCN 插件项列表 */ - plugins: LobeChatPluginManifest[]; + plugins: LobeChatPluginMeta[]; /** * version * @desc Version of the plugins @@ -27,7 +27,7 @@ export interface LobeChatPluginsMarketIndex { * @nameCN 插件项 * @descCN 插件项接口 */ -export interface LobeChatPluginManifest { +export interface LobeChatPluginMeta { /** * createAt * @desc Creation date of the plugin