Skip to content

Commit

Permalink
✨ feat: 增加 manifest schema 、error 校验方法
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 19, 2023
1 parent 7e24401 commit 8888998
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
31 changes: 24 additions & 7 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export enum ErrorType {
// ******* 业务错误语义 ******* //
PluginMarketIndexNotFound = 'pluginMarketIndexNotFound', // 插件市场索引解析失败
PluginMarketIndexParseError = 'pluginMarketIndexParseError', // 插件市场索引解析失败
PluginMarketIndexInvalid = 'pluginMarketIndexInvalid', // 插件市场索引无效

PluginMetaNotFound = 'pluginMetaNotFound', // 没有找到插件元数据
PluginMetaInvalid = 'pluginMetaInvalid', // 插件元数据无效

PluginManifestNotFound = 'pluginManifestNotFound', // 插件描述文件不存在
PluginManifestInvalid = 'pluginManifestInvalid', // 插件描述文件不存在

// ******* 客户端错误 ******* //
BadRequest = 400,
Expand All @@ -18,21 +24,32 @@ export enum ErrorType {
GatewayTimeout = 504,
}

const getStatus = (errorType: ErrorType) => {
const getStatus = (errorType: ErrorType | string) => {
switch (errorType) {
case ErrorType.PluginMarketIndexNotFound:
case ErrorType.PluginMetaNotFound:
case ErrorType.PluginManifestNotFound:
return 404;

case ErrorType.PluginMarketIndexParseError:
case ErrorType.PluginMetaInvalid:
return 490;
case ErrorType.PluginManifestInvalid:
return 491;

case ErrorType.PluginMarketIndexNotFound:
return 590;

case ErrorType.PluginMarketIndexInvalid:
return 590;
}

return errorType;
if (typeof errorType === 'number') return errorType;

return 500;
};

export interface ErrorResponse {
body: any;
errorType: ErrorType;
errorType: ErrorType | string;
}

/**
Expand All @@ -41,7 +58,7 @@ export interface ErrorResponse {
* @param body - 响应体数据
* @returns {Response} - 错误响应对象
*/
export const createErrorResponse = (errorType: ErrorType, body?: string | object) => {
export const createErrorResponse = (errorType: ErrorType | string, body?: string | object) => {
// 获取错误类型对应的状态码
const statusCode = getStatus(errorType);

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './error';
export * from './manifest';
export * from './market';
export * from './types';
17 changes: 17 additions & 0 deletions src/manifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { z } from 'zod';

export const pluginManifestSchema = z.object({
name: z.string(),
schema: z.object({
properties: z.object({}),
type: z.enum(['object']),
}),
server: z.object({
url: z.string(),
}),
ui: z
.object({
url: z.string().optional(),
})
.optional(),
});
13 changes: 8 additions & 5 deletions src/market.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { z } from 'zod';

export const lobeChatPluginMetaSchema = z.object({
export const pluginMetaSchema = z.object({
createAt: z.string(),
homepage: z.string(),
manifest: z.string({}),
meta: z.object({}),
manifest: z.string(),
meta: z.object({
avatar: z.string().optional(),
tags: z.array(z.string()).optional(),
}),
name: z.string(),
schemaVersion: z.string(),
schemaVersion: z.enum(['v1']),
});

export const marketIndexSchema = z.object({
plugins: z.array(lobeChatPluginMetaSchema),
plugins: z.array(z.any()),
version: z.number(),
});

0 comments on commit 8888998

Please sign in to comment.