UNPKG

@sword-code-practice/sword-cors

Version:

sword框架下的cors插件,不会被预设在sword中,需要手动添加插件

46 lines (40 loc) 1.16 kB
// 运行时的log类型, 分为info, success, error type LogType = { err: (v: string | Error) => void; info: (v: string) => void; success: (v: string) => void; }; interface ContextData { query?: unknown; params?: unknown; res?: unknown; } interface HttpContext<T extends ContextData = ContextData> { readonly key: string; // context的key由api构造 readonly method: HttpInstructMethod[]; readonly proto: Record<string, unknown>; readonly reqHeaders: Record<string, unknown>; resHeaders: Record<string, unknown>; query: T['query']; params: T['params']; return?: { data: T['res']; // 返回对象 }; } type HttpInstructMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE'; type Plugin = { name: string; // 提供几个钩子用来定义函数,作为框架runtime的shim server?: { start: (...args: any[]) => Promise<void> | void; }; log?: LogType; context?: (context: HttpContext) => HttpContext; }; /** * 定义cors插件 * @preset true * @return {*} */ declare const useCorsPlugin: (resHeaders?: Partial<Record<string, unknown>>) => Plugin; export { useCorsPlugin };