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

ctx is untyped.. #2

Closed
kristiandupont opened this issue Feb 3, 2023 · 23 comments
Closed

ctx is untyped.. #2

kristiandupont opened this issue Feb 3, 2023 · 23 comments
Labels
bug Something isn't working

Comments

@kristiandupont
Copy link

Your library is exactly what I was hoping to find, thank you!

The one issue I have right now is that the ctx parameter is untyped. This is surprising to me as it appears that it should be correctly typed via the ExtractProcedureHandler type.

image

My mswTrpc instance is pretty straight forward:

const mswTrpc = createTRPCMsw<AppRouter>({
  transformer: { input: superjson, output: superjson },
});

..what am I missing?

@maloguertin
Copy link
Owner

Hi kristian, I'll try to get around to debugging your use case today!

@maloguertin maloguertin added the bug Something isn't working label Feb 3, 2023
@kristiandupont
Copy link
Author

That's awesome, thank you. I am happy to help as well, I just wanted to make sure I wasn't missing something simple first.

@maloguertin
Copy link
Owner

Could you give me a reproducible example of a trpc router

@kristiandupont
Copy link
Author

With this AppRouter definition:

const appRouter = router({
  user: publicProcedure
    .input(Number)
    .output(Object)
    .query((req) => ALL_USERS.find((user) => req.input === user.id)),
});

I get the same result. Let me know if I should try and set up a repository for you.

@maloguertin
Copy link
Owner

Here's my setup:

import createTRPCMsw from '../src/createTRPCMsw'
import { initTRPC } from '@trpc/server'
import superjson from 'superjson'

const t = initTRPC.create({ transformer: superjson })
export interface User {
  id: Number
  name: string
}
const ALL_USERS: User[] = [
  {
    id: 1,
    name: 'KATT',
  },
]
const appRouter = t.router({
  user: t.procedure
    .input(Number)
    .output(Object)
    .query(req => ALL_USERS.find(user => req.input === user.id)),
})
export type AppRouter = typeof appRouter

export const mswTrpc = createTRPCMsw<AppRouter>({ transformer: { input: superjson, output: superjson } })

mswTrpc.user.query((req, res, ctx) => {
  ctx.data
})

image

If you can give me a repo it would help :)

@kristiandupont
Copy link
Author

Interesting. Well it's good to know that it should work -- in that case I will first try and check that I didn't do something stupid. I'll report back here, either with the fix or with a reproduction repo :-)

@CUexter
Copy link
Contributor

CUexter commented Feb 14, 2023

I am having the same problem here

@maloguertin
Copy link
Owner

I am having the same problem here

Hi! Could you fill this template to help me narrow down the bug:

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Versions
typescript: vX.Y.Z
tRPC: vX.Y.Z
msw: vX.Y.Z

@CUexter
Copy link
Contributor

CUexter commented Feb 15, 2023

Here's my setup:

import createTRPCMsw from '../src/createTRPCMsw'
import { initTRPC } from '@trpc/server'
import superjson from 'superjson'

const t = initTRPC.create({ transformer: superjson })
export interface User {
  id: Number
  name: string
}
const ALL_USERS: User[] = [
  {
    id: 1,
    name: 'KATT',
  },
]
const appRouter = t.router({
  user: t.procedure
    .input(Number)
    .output(Object)
    .query(req => ALL_USERS.find(user => req.input === user.id)),
})
export type AppRouter = typeof appRouter

export const mswTrpc = createTRPCMsw<AppRouter>({ transformer: { input: superjson, output: superjson } })

mswTrpc.user.query((req, res, ctx) => {
  ctx.data
})

image

If you can give me a repo it would help :)

what would be the type of ctx in this example ?

@CUexter
Copy link
Contributor

CUexter commented Feb 15, 2023

I tried copy and paste your example and it doesn't work, can you tell me the version of msw you are using ?

@CUexter
Copy link
Contributor

CUexter commented Feb 15, 2023

wait reinstalling msw seems to solve the issue, not sure why

@kristiandupont
Copy link
Author

@CUexter sadly that doesn't seem to solve it for me. Which version of TS are you using?

@CUexter
Copy link
Contributor

CUexter commented Feb 15, 2023

@kristiandupont 4.9.4, try installing the old version of msw from 0.49.3 ?

@kristiandupont
Copy link
Author

That worked, thank you!!

@CUexter
Copy link
Contributor

CUexter commented Feb 15, 2023

i tried updating it back and it still works. This is weird, what package manager are u using, I am using pnpm

@kristiandupont
Copy link
Author

Weird indeed. I haven't tried updating after this, but I think I will leave it for now. I am using Yarn.

@maloguertin
Copy link
Owner

So it seems like the issue might be with latest version of MSW?

@kristiandupont
Copy link
Author

Well I tried first upgrading to the latest version which didn't do anything. I then downgraded to exactly version 0.49.3 as per CUexter's suggestion, and then it worked.

@CUexter
Copy link
Contributor

CUexter commented Feb 15, 2023 via email

@CUexter
Copy link
Contributor

CUexter commented Feb 17, 2023

@maloguertin I am sorry, it doesn't work for 1.0.1. I was wrong.

@maloguertin
Copy link
Owner

@maloguertin I am sorry, it doesn't work for 1.0.1. I was wrong.

Sorry I've been pretty busy. It's weird, I am using MSW 1.0.1 and ts 4.9.5 (tried with 5 too) and it works.

@maloguertin
Copy link
Owner

If you can give me a repo with the issue I'll try fixing it.

@maloguertin
Copy link
Owner

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

3 participants