elysiajs / elysia

Ergonomic Framework for Humans

Home Page:https://elysiajs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow `.derive` to take additional data

ThatOneCalculator opened this issue · comments

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 :(

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)
		};
	});

Yeah, for some reason using guard completely escaped my mind. 🤦