Skip to content

Commit 1cea0fc

Browse files
committed
fix client type
1 parent dd60bb2 commit 1cea0fc

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@creatorem/next-trpc",
3-
"version": "1.0.15",
3+
"version": "1.0.16",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/creatorem/next-trpc"

src/create-trpc-client.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import { kebabize } from "./utils";
33
import z from "zod";
44

55
export 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

1314
type 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

1920
function 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

7071
export 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

Comments
 (0)