DB-GPT/web/lib/response.ts
lhwan 3a32344380
feat: web update (#1860)
Co-authored-by: 夏姜 <wenfengjiang.jwf@digital-engine.com>
Co-authored-by: yhjun1026 <460342015@qq.com>
Co-authored-by: aries_ckt <916701291@qq.com>
Co-authored-by: wb-lh513319 <wb-lh513319@alibaba-inc.com>
2024-08-22 11:05:18 +08:00

40 lines
1.2 KiB
TypeScript

import { ERROR_CODE } from '@/utils/constants';
import type { NextApiRequest, NextApiResponse } from 'next';
type IApiResponse<T = any> = {
statusCode?: number;
success: boolean;
message: string;
data: T;
errorCode?: ERROR_CODE | (string & {});
};
type Methods = 'GET' | 'OPTIONS' | 'PUT' | 'POST' | 'PATCH' | 'DELETE' | (string & {});
export function response<T = any>(res: NextApiResponse, options?: Partial<IApiResponse<T>>) {
const { statusCode = 200, message, data = null, errorCode } = options ?? {};
res.status(statusCode).json({
success: statusCode === 200 && !errorCode,
err_msg: message ?? (statusCode === 200 && !errorCode ? 'successful' : 'failed'),
data,
err_code: errorCode ?? (statusCode === 200 ? undefined : statusCode),
});
}
export function checkAllowMethods(res: NextApiResponse, method: string = '', methods: Methods[]) {
if (!method || !methods.includes(method)) {
response(res, { statusCode: 405 });
return false;
}
return true;
}
export function checkAuthorized(req: NextApiRequest, res: NextApiResponse) {
if (!req.session) {
response(res, { statusCode: 401 });
return false;
}
return true;
}