아래와 같은 플러그인 코드에서 headers에 적용된 스키마가 정상적으로 동작하지 않음.
export const adminGuard = new Elysia({ name: "admin-guard" })
.state({
admin: {} as AdminPayload,
})
.guard({
// Here
headers: adminGuardHeaderSchema,
})
.macro(({ onBeforeHandle }) => ({
adminGuard(options?: { privileges: string[] }) {
if (!options) return;
onBeforeHandle(async ({ store, headers }) => {
if (!Value.Check(adminGuardHeaderSchema, headers)) {
throw new AppError(401, "Failed to authenticate admin");
}
store.admin = await extractAdminPayload(headers["authorization"]);
});
const { privileges } = options;
if (privileges) {
onBeforeHandle(async ({ store: { admin }, headers }) => {
const hasPrivilege = checkPrivilege(
headers["target-category"],
privileges,
admin.privilegeNames,
);
if (!hasPrivilege) {
throw new AppError(403, `You are not allowed to access this api`);
}
});
}
},
}));
그래서 Value.Check를 따로 불러주도록해서 대응함