Files
sign/packages/ee/server-only/limits/handler.ts

38 lines
1013 B
TypeScript
Raw Normal View History

2023-10-15 20:26:32 +11:00
import { NextApiRequest, NextApiResponse } from 'next';
import { getToken } from 'next-auth/jwt';
import { match } from 'ts-pattern';
import { ERROR_CODES } from './errors';
import { TLimitsErrorResponseSchema, TLimitsResponseSchema } from './schema';
import { getServerLimits } from './server';
export const limitsHandler = async (
req: NextApiRequest,
res: NextApiResponse<TLimitsResponseSchema | TLimitsErrorResponseSchema>,
) => {
try {
const token = await getToken({ req });
2023-10-26 12:26:29 +11:00
const limits = await getServerLimits({ email: token?.email });
2023-10-15 20:26:32 +11:00
return res.status(200).json(limits);
2023-10-15 20:26:32 +11:00
} catch (err) {
console.error('error', err);
if (err instanceof Error) {
const status = match(err.message)
.with(ERROR_CODES.UNAUTHORIZED, () => 401)
.otherwise(() => 500);
return res.status(status).json({
error: ERROR_CODES[err.message] ?? ERROR_CODES.UNKNOWN,
});
}
2023-11-04 13:14:20 +11:00
return res.status(500).json({
2023-10-15 20:26:32 +11:00
error: ERROR_CODES.UNKNOWN,
});
}
};