You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.2 KiB
45 lines
1.2 KiB
import { AuthConflictError, AuthValidationError, registerUser } from "../../service/auth";
|
|
|
|
type RegisterBody = {
|
|
username: string;
|
|
password: string;
|
|
};
|
|
|
|
function hasStatusCode(err: unknown): err is { statusCode: number } {
|
|
return typeof err === "object" && err !== null && "statusCode" in err
|
|
&& typeof (err as { statusCode?: unknown }).statusCode === "number";
|
|
}
|
|
|
|
function toPublicError(err: unknown) {
|
|
if (hasStatusCode(err)) {
|
|
return err;
|
|
}
|
|
if (err instanceof AuthValidationError) {
|
|
return createError({
|
|
statusCode: 400,
|
|
statusMessage: err.message,
|
|
});
|
|
}
|
|
if (err instanceof AuthConflictError) {
|
|
return createError({
|
|
statusCode: 409,
|
|
statusMessage: err.message,
|
|
});
|
|
}
|
|
return createError({
|
|
statusCode: 500,
|
|
statusMessage: "服务器繁忙,请稍后重试",
|
|
});
|
|
}
|
|
|
|
export default defineWrappedResponseHandler(async (event) => {
|
|
try {
|
|
const body = await readBody<RegisterBody>(event);
|
|
const user = await registerUser(body);
|
|
return R.success({
|
|
user,
|
|
});
|
|
} catch (err) {
|
|
throw toPublicError(err);
|
|
}
|
|
});
|
|
|