Files
eosphoros-ai--db-gpt/web/lib/response.ts
T
wehub-resource-sync d13100ebf3
Update draft releases / main (push) Has been cancelled
Build and push docs image / build-image (push) Has been cancelled
Build Web Application / build-web (macos-latest) (push) Has been cancelled
Build Web Application / build-web (ubuntu-latest) (push) Has been cancelled
Python Code Quality Checks / build (push) Has been cancelled
Test Python / test-python (macos-latest, 3.10) (push) Has been cancelled
Test Python / test-python (macos-latest, 3.11) (push) Has been cancelled
Test Python / test-python (ubuntu-latest, 3.10) (push) Has been cancelled
Test Python / test-python (ubuntu-latest, 3.11) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:27:08 +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;
}