Allow `.derive` to take additional data
ThatOneCalculator opened this issue · comments
Kainoa Kanter commented
What is the problem this feature would solve?
Currently, this is possible:
export const privy_user = new Elysia()
.use(bearer())
.derive({ as: "scoped" }, async ({ bearer, cookie }) => {
/* logic that uses `cookie["privy-id-token"]` to get `payload` */
return { user: await getUser(payload.sub) };
} catch (error) {
throw new AuthenticationError("Auth token could not be verified.");
}
});
But this isn't:
export const privy_user = new Elysia()
.use(bearer())
.derive({ as: "scoped" }, async ({ bearer, cookie }) => {
/* logic that uses `cookie["privy-id-token"]` to get `payload` */
return { user: await getUser(payload.sub) };
} catch (error) {
throw new AuthenticationError("Auth token could not be verified.");
}
}, {
cookie: t.Cookie({
"privy-id-token": t.String(),
}),
});
What is the feature you are proposing to solve the problem?
Allow things like
}, {
cookie: t.Cookie({
"privy-id-token": t.String(),
}),
});
on derrive
What alternatives have you considered?
No type-safety :(
Panda commented
Maybe you need to use guard ?
const user = new Elysia()
.guard({ cookie: t.Cookie({ session: t.String() }) })
.derive({ as: 'scoped' }, async ({ cookie }) => {
return {
user: await getUser(cookie.session.value)
};
});
Kainoa Kanter commented
Yeah, for some reason using guard completely escaped my mind. 🤦