Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

derive method doesn't work as a plugin #566

Closed
nodirbekrajaboff opened this issue Mar 23, 2024 · 5 comments
Closed

derive method doesn't work as a plugin #566

nodirbekrajaboff opened this issue Mar 23, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@nodirbekrajaboff
Copy link

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

@nodirbekrajaboff nodirbekrajaboff added the bug Something isn't working label Mar 23, 2024
@kravetsone
Copy link
Contributor

#513

Specify derive as global

@JZubero
Copy link

JZubero commented Mar 27, 2024

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.

@nmquebb
Copy link
Contributor

nmquebb commented Apr 6, 2024

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)

@SaltyAom
Copy link
Member

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

@russism
Copy link

russism commented May 20, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants