Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,58 @@ describe("client", () => {
});
expectTypeOf<Parameters<typeof client>[0]>().toMatchTypeOf<"@post/test">();
});

it("should infer response depending on options.throw", async () => {
const endpoint = createEndpoint(
"/test",
{
method: "POST",
body: z.object({
hello: z.string(),
}),
},
async () => {
return {
status: 200,
body: {
hello: "world",
},
};
},
);

const router = createRouter({
endpoint,
});

const client = createClient<typeof router>({
baseURL: "http://localhost:3000",
customFetchImpl: async () => {
return new Response(null);
},
});

const throwingClient = createClient<typeof router>({
baseURL: "http://localhost:3000",
customFetchImpl: async () => {
return new Response(null);
},
throw: true,
});

const response = await client("@post/test", {
body: {
hello: "world",
},
});

const throwingResponse = await throwingClient("@post/test", {
body: {
hello: "world",
},
});

expectTypeOf(response.data).toMatchTypeOf<{ body: { hello: string } } | null>();
expectTypeOf(throwingResponse).toMatchTypeOf<{ body: { hello: string } }>();
});
});
85 changes: 57 additions & 28 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,66 @@ export type RequiredOptionKeys<
params: true;
});

export const createClient = <R extends Router | Router["endpoints"]>(options: ClientOptions) => {
export function createClient<R extends Router | Router["endpoints"]>(
options: ClientOptions & { throw: true },
): FetchClientReturn<R, true>;

export function createClient<R extends Router | Router["endpoints"]>(
options: ClientOptions & { throw: false },
): FetchClientReturn<R, false>;

export function createClient<R extends Router | Router["endpoints"]>(
options: ClientOptions,
): FetchClientReturn<R, false>;

export function createClient<R extends Router | Router["endpoints"]>(
options: ClientOptions,
): FetchClientReturn<R, any> {
const fetch = createFetch(options);
type API = R extends { endpoints: Record<string, Endpoint> } ? R["endpoints"] : R;
type Options = API extends {
[key: string]: infer T;
}
? T extends Endpoint
? {
[key in T["options"]["method"] extends "GET"
? T["path"]
: `@${T["options"]["method"] extends string ? Lowercase<T["options"]["method"]> : never}${T["path"]}`]: T;
}
: {}
: {};

type O = Prettify<UnionToIntersection<Options>>;
return async <OPT extends O, K extends keyof OPT, C extends InferContext<OPT[K]>>(
path: K,
...options: HasRequired<C> extends true
? [
WithRequired<
BetterFetchOption<C["body"], C["query"], C["params"]>,
keyof RequiredOptionKeys<C>
>,
]
: [BetterFetchOption<C["body"], C["query"], C["params"]>?]
): Promise<
BetterFetchResponse<Awaited<ReturnType<OPT[K] extends Endpoint ? OPT[K] : never>>>
> => {
return async (path, ...options) => {
return (await fetch(path as string, {
...options[0],
})) as any;
};
};
}

type FetchClientAPI<R extends Router | Router["endpoints"]> = R extends {
endpoints: Record<string, Endpoint>;
}
? R["endpoints"]
: R;

type FetchClientOptions<R extends Router | Router["endpoints"]> = FetchClientAPI<R> extends {
[key: string]: infer T;
}
? T extends Endpoint
? {
[key in T["options"]["method"] extends "GET"
? T["path"]
: `@${T["options"]["method"] extends string ? Lowercase<T["options"]["method"]> : never}${T["path"]}`]: T;
}
: {}
: {};

type FetchClientReturn<R extends Router | Router["endpoints"], Throw extends boolean> = <
OPT extends Prettify<UnionToIntersection<FetchClientOptions<R>>>,
K extends keyof OPT,
C extends InferContext<OPT[K]>,
>(
path: K,
...options: HasRequired<C> extends true
? [
WithRequired<
BetterFetchOption<C["body"], C["query"], C["params"]>,
keyof RequiredOptionKeys<C>
>,
]
: [BetterFetchOption<C["body"], C["query"], C["params"]>?]
) => Promise<
BetterFetchResponse<
Awaited<ReturnType<OPT[K] extends Endpoint ? OPT[K] : never>>,
unknown,
Throw
>
>;