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

Support for V11 #37

Open
PupoSDC opened this issue Jan 18, 2024 · 4 comments · May be fixed by #41
Open

Support for V11 #37

PupoSDC opened this issue Jan 18, 2024 · 4 comments · May be fixed by #41

Comments

@PupoSDC
Copy link

PupoSDC commented Jan 18, 2024

Is your feature request related to a problem? Please describe.

V11 has now entered Beta. Unfortunately this package does not work out of the box with V11 and some work might be needed.

Describe the solution you'd like

support for MSW v11.

Describe alternatives you've considered

Remaining on V10 for the time being :P

Additional context

you can find a PR on my public project trying to make this bump:

PupoSDC/chair-flight#77

Thank you very much for this package! ❤️

@tomitrescak
Copy link

tomitrescak commented Apr 18, 2024

It seems to work with v11, just the types are borked.

I tried to give it a go, but that typescript magic is out of my reach amd ChatGPT did not help:

https://github.com/maloguertin/msw-trpc/blob/main/src/types.ts

@carlhopf
Copy link

It seems to work with v11, just the types are borked.

I tried to give it a go, but that typescript magic is out of my reach amd ChatGPT did not help:

https://github.com/maloguertin/msw-trpc/blob/main/src/types.ts

can confirm 👍

@tomitrescak
Copy link

Took a bit, but here are the working types for v11

import type {
  MutationProcedure,
  QueryProcedure,
  SubscriptionProcedure,
  inferProcedureInput,
  inferProcedureOutput
} from "@trpc/server/unstable-core-do-not-import";

type Any = any;
type Router = Record<string, QueryProcedure<Any> | MutationProcedure<Any> | SubscriptionProcedure<Any>>;

export type ExtractKeys<T, K extends keyof T = keyof T> = T[K] extends
  | QueryProcedure<Any>
  | MutationProcedure<Any>
  | Router
  ? K
  : never;

export type Query<T, K extends keyof T> = T[K] extends QueryProcedure<Any>
  ? {
      query(input: inferProcedureInput<T[K]>): inferProcedureOutput<T[K]>; // : SetQueryHandler<T, K>;
    }
  : never;

export type Mutation<T, K extends keyof T> = T[K] extends MutationProcedure<Any>
  ? {
      mutation(input: inferProcedureInput<T[K]>): inferProcedureOutput<T[K]>;
    }
  : never;

type ExtractProcedureHandler<
  T,
  K extends keyof T
> = T[K] extends MutationProcedure<Any>
  ? Mutation<T, K>
  : T[K] extends QueryProcedure<Any>
  ? Query<T, K>
  : T[K] extends Router
  ? MswTrpc<T[K]>
  : never;

export type MswTrpc<T> = {
  [key in keyof T as ExtractKeys<T, key>]: ExtractProcedureHandler<T, key>;
};
export const trpcMsw: MswTrpc<AppRouter> = createTRPCMsw<AppRouter>() as Any;

WARNING

The api is clearly marked as "unstable", but it works for me now.

@vafanassieff
Copy link

I have addressed this in #40 FYI @tomitrescak @carlhopf

@vafanassieff vafanassieff mentioned this issue Apr 24, 2024
2 tasks
@vafanassieff vafanassieff linked a pull request May 8, 2024 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants