elysiajs/elysia

derive method doesn't work as a plugin

Closed this issue · 5 comments

What version of Elysia.JS is running?

1.0.7

What platform is your computer?

Linux 5.15.146.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

Property 'userDevice' does not exist on type '{ body: { username: string; password: string; }; query: Record<string, string | undefined>; params: never; headers: Record<string, string | undefined>; cookie: Record<string, Cookie<...>>; ... 5 more ...; jwt: { ...; }; }'.

// device-detect.ts
image

// authentication.ts
image

// sign-in-route.ts
image

What is the expected behavior?

I want to get user device to store information about user device. But derive method doesn't work as a plugin

What do you see instead?

No response

Additional information

No response

#513

Specify derive as global

Continues to happen for me with v1.0.9 and the { as: 'global' } config for derive(). I can only get rid of the red squiggly lines if the plugin is included (via app.use()) in every instance that should be able to access the derived properties.

Tried to quickly reproduce it with something simple but this worked fine in 1.0.11. Can you make something with { as: 'global' } failing?

const deviceDetect = new Elysia({ name: "device-detect" })
  .derive({ as: "global" }, ({ request }) => {
    let userAgent = request.headers.get('user-agent');
    return { userAgent }
  });

const auth = new Elysia({ name: "auth" })
  .use(deviceDetect);

const controller = new Elysia()
  .use(auth)
  .get('/', ({ userAgent }) => userAgent)

Close as #513 and no further discussion for 2 weeks.
Feels free to ping me to reopen this issue if necessary.

This seems to work for me:

// auth.ts
export const account = new Elysia()
  .use(bearer())
  .use(access)
  .derive({ as: 'scoped' }, async ({
    bearer, jwtAccess,
  }) => {
    if (!bearer)
      throw new ForbiddenError('Unauthorized.')
		
    const access = await jwtAccess.verify(bearer)
    if (!access)
      throw new ForbiddenError('Unauthorized.')

    return {
      account: JSON.parse(access.id),
    }
  })

// page.ts
export default new Elysia({ prefix: '/page' })
  .get('/', async ({ account }) => {
    console.log(account)
    return await Page.fetch(account)
  })
	
// api.ts
import { account } from '@/auth'
import page from './page'
	
export default new Elysia({ prefix: '/api' })
  .use(account)
  .use(page)

But for some reason i just couldn't get rid of the red squiggly in vscode under ({ account }) in page.ts