Skip to content

Commit

Permalink
✨ feat: 增加插件市场索引校验方法
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 19, 2023
1 parent f140633 commit 37d56e9
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 29 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
rules: {
...config.rules,
'unicorn/prefer-string-replace-all': 0,
'unicorn/switch-case-braces': 0,
},
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 29 additions & 3 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -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 - 错误类型
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './error';
export * from './market';
export * from './types';
15 changes: 15 additions & 0 deletions src/market.ts
Original file line number Diff line number Diff line change
@@ -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(),
});
22 changes: 0 additions & 22 deletions src/types/error.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './chatGPT';
export * from './error';
export * from './manifest';
export * from './market';
4 changes: 2 additions & 2 deletions src/types/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface LobeChatPluginsMarketIndex {
* @nameCN 插件列表
* @descCN 插件项列表
*/
plugins: LobeChatPluginManifest[];
plugins: LobeChatPluginMeta[];
/**
* version
* @desc Version of the plugins
Expand All @@ -27,7 +27,7 @@ export interface LobeChatPluginsMarketIndex {
* @nameCN 插件项
* @descCN 插件项接口
*/
export interface LobeChatPluginManifest {
export interface LobeChatPluginMeta {
/**
* createAt
* @desc Creation date of the plugin
Expand Down

0 comments on commit 37d56e9

Please sign in to comment.