@@ -3,17 +3,18 @@ import { kebabize } from "./utils";
33import z from "zod" ;
44
55export type EndpointClient < Input , Output > = Input extends undefined
6- ? { fetch : ( ) => Promise < Output > }
6+ ? { fetch : ( ) => Promise < Output > , key : string }
77 : {
8- fetch : (
9- input : Input extends import ( "zod" ) . Schema ? z . infer < Input > : Input
10- ) => Promise < Output > ;
11- } ;
8+ fetch : (
9+ input : Input extends import ( "zod" ) . Schema ? z . infer < Input > : Input
10+ ) => Promise < Output > ;
11+ key : string
12+ } ;
1213
1314type TrpcClient < R extends Router < any > > = {
1415 [ K in keyof R ] : R [ K ] extends Endpoint < infer Output , infer Input , any >
15- ? EndpointClient < Input , Output >
16- : never ;
16+ ? EndpointClient < Input , Output >
17+ : never ;
1718} ;
1819
1920function serialize ( str : string ) : string {
@@ -39,33 +40,33 @@ export const getTrpcFetch =
3940 } : {
4041 endpointSlug : string ;
4142 } & createTrpcClientOptions ) =>
42- async ( input ?: any ) => {
43- const endpointName = kebabize ( endpointSlug ) ;
43+ async ( input ?: any ) => {
44+ const endpointName = kebabize ( endpointSlug ) ;
4445
45- // Build URL with search params if input exists
46- let requestUrl = `${ url } /${ endpointName } ` ;
47- if ( input ) {
48- requestUrl += `?input=${ serialize ( input ) } ` ;
49- }
46+ // Build URL with search params if input exists
47+ let requestUrl = `${ url } /${ endpointName } ` ;
48+ if ( input ) {
49+ requestUrl += `?input=${ serialize ( input ) } ` ;
50+ }
5051
51- const headerObject =
52- typeof headers === "function" ? await headers ( ) : headers || { } ;
53- const response = await fetch ( requestUrl , {
54- method : "GET" ,
55- headers : {
56- "Content-Type" : "application/json" ,
57- ...headerObject ,
58- } ,
59- } ) ;
52+ const headerObject =
53+ typeof headers === "function" ? await headers ( ) : headers || { } ;
54+ const response = await fetch ( requestUrl , {
55+ method : "GET" ,
56+ headers : {
57+ "Content-Type" : "application/json" ,
58+ ...headerObject ,
59+ } ,
60+ } ) ;
6061
61- if ( ! response . ok ) {
62- const errorData = await response . json ( ) ;
63- throw new Error ( errorData . error || "Request failed" ) ;
64- }
62+ if ( ! response . ok ) {
63+ const errorData = await response . json ( ) ;
64+ throw new Error ( errorData . error || "Request failed" ) ;
65+ }
6566
66- const result = await response . json ( ) ;
67- return result . data ;
68- } ;
67+ const result = await response . json ( ) ;
68+ return result . data ;
69+ } ;
6970
7071export interface createTrpcClientOptions {
7172 url : string ;
@@ -85,6 +86,7 @@ export const createTrpcClient = <
8586 endpointSlug : prop ,
8687 ...opts ,
8788 } ) ,
89+ key : kebabize ( prop )
8890 } ;
8991 }
9092 return undefined ;
0 commit comments