Skip to content

Commit c4d3b30

Browse files
authored
feat: remove zod schema from dts type (#104)
1 parent 0702a2b commit c4d3b30

8 files changed

Lines changed: 169 additions & 34 deletions

File tree

example/bundle.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { createEndpoint, createRouter } from "better-call";
2+
import { z } from "zod";
3+
4+
export const a = 1;
5+
6+
export const hello = createEndpoint(
7+
"/hello",
8+
{
9+
method: "POST",
10+
body: z.object({
11+
name: z.string(),
12+
}),
13+
metadata: {
14+
openapi: {
15+
responses: {
16+
"200": {
17+
description: "Welcome Page",
18+
content: {
19+
"text/plain": {
20+
schema: {
21+
type: "string",
22+
},
23+
},
24+
},
25+
},
26+
},
27+
},
28+
},
29+
},
30+
async (c) => {
31+
c.setCookie("hello", "world");
32+
c.setCookie("test", "value");
33+
return "hello from better-call!";
34+
},
35+
);
36+
37+
export const router = createRouter({ hello });

example/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"name": "@better-auth/example",
33
"private": true,
4+
"scripts": {
5+
"build": "tsdown"
6+
},
47
"dependencies": {
58
"better-call": "workspace:*",
69
"zod": "^4.3.6"

example/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"extends": "../tsconfig.base.json",
3-
"include": ["./hello.ts"],
3+
"include": ["./hello.ts", "bundle.ts"],
4+
"compilerOptions": {
5+
"declaration": true
6+
},
47
"references": [
58
{
69
"path": "../packages/better-call"

example/tsdown.config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineConfig } from "tsdown";
2+
3+
export default defineConfig({
4+
entry: {
5+
bundle: "bundle.ts",
6+
},
7+
dts: { build: true, incremental: true },
8+
sourcemap: true,
9+
treeshake: true,
10+
clean: true,
11+
unbundle: true,
12+
});

packages/better-call/src/client.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,35 @@ import type { Router } from "./router";
77
import type { HasRequiredKeys, Prettify, UnionToIntersection } from "./helper";
88
import type { Endpoint } from "./endpoint";
99

10-
type HasRequired<
11-
T extends {
12-
body?: any;
13-
query?: any;
14-
params?: any;
15-
},
16-
> = T["body"] extends object
17-
? HasRequiredKeys<T["body"]> extends true
18-
? true
19-
: T["query"] extends object
20-
? HasRequiredKeys<T["query"]> extends true
10+
export type HasRequired<T extends object> = T extends {}
11+
? false
12+
: T extends {
13+
body?: any;
14+
query?: any;
15+
params?: any;
16+
}
17+
? T["body"] extends object
18+
? HasRequiredKeys<T["body"]> extends true
2119
? true
20+
: T["query"] extends object
21+
? HasRequiredKeys<T["query"]> extends true
22+
? true
23+
: T["params"] extends object
24+
? HasRequiredKeys<T["params"]>
25+
: false
26+
: T["params"] extends object
27+
? HasRequiredKeys<T["params"]>
28+
: false
29+
: T["query"] extends object
30+
? HasRequiredKeys<T["query"]> extends true
31+
? true
32+
: T["params"] extends object
33+
? HasRequiredKeys<T["params"]>
34+
: false
2235
: T["params"] extends object
2336
? HasRequiredKeys<T["params"]>
2437
: false
25-
: T["params"] extends object
26-
? HasRequiredKeys<T["params"]>
27-
: false
28-
: T["query"] extends object
29-
? HasRequiredKeys<T["query"]> extends true
30-
? true
31-
: T["params"] extends object
32-
? HasRequiredKeys<T["params"]>
33-
: false
34-
: T["params"] extends object
35-
? HasRequiredKeys<T["params"]>
36-
: false;
38+
: false;
3739

3840
type InferContext<T> = T extends (ctx: infer Ctx) => any
3941
? Ctx extends object

packages/better-call/src/endpoint.ts

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,82 @@ export type EndpointContext<
416416
) => APIError;
417417
};
418418

419-
type EndpointHandler<
419+
export type ExtractBody<E extends EndpointBodyMethodOptions> = E extends {
420+
method: infer M;
421+
body?: never;
422+
}
423+
? {
424+
method: M;
425+
}
426+
: E extends {
427+
method: ("POST" | "PUT" | "DELETE" | "PATCH" | "GET" | "HEAD")[];
428+
body?: StandardSchemaV1<infer B>;
429+
}
430+
? E extends {
431+
method: infer M;
432+
body?: StandardSchemaV1<B>;
433+
}
434+
? { method: M; body: StandardSchemaV1<B> }
435+
: never
436+
: E extends {
437+
method:
438+
| "POST"
439+
| "PUT"
440+
| "DELETE"
441+
| "PATCH"
442+
| ("POST" | "PUT" | "DELETE" | "PATCH")[];
443+
body?: StandardSchemaV1<infer B>;
444+
}
445+
? E extends {
446+
method: infer M;
447+
body?: StandardSchemaV1<B>;
448+
}
449+
? { method: M; body: StandardSchemaV1<B> }
450+
: never
451+
: E extends {
452+
method: "*";
453+
body?: StandardSchemaV1<infer B>;
454+
}
455+
? {
456+
method: "*";
457+
body?: StandardSchemaV1<B>;
458+
}
459+
: E extends {
460+
method: "GET" | "HEAD" | ("GET" | "HEAD")[];
461+
body?: never;
462+
}
463+
? E extends { method: infer M }
464+
? { method: M }
465+
: never
466+
: never;
467+
export type ExtractError<E extends EndpointOptions> = E extends {
468+
error?: StandardSchemaV1<infer Err>;
469+
}
470+
? {
471+
error: StandardSchemaV1<Err>;
472+
}
473+
: {};
474+
export type ExtractQuery<E extends EndpointOptions> = E extends {
475+
query?: StandardSchemaV1<infer Q>;
476+
}
477+
? {
478+
query: StandardSchemaV1<Q>;
479+
}
480+
: {};
481+
482+
export type ExtractOthers<E extends EndpointOptions> = Pick<
483+
E,
484+
Exclude<keyof E, "method" | "body" | "query" | "error">
485+
>;
486+
487+
export type ExtractStandSchema<E extends EndpointOptions> = Prettify<
488+
ExtractOthers<E>
489+
> &
490+
ExtractBody<E> &
491+
ExtractQuery<E> &
492+
ExtractError<E>;
493+
494+
export type EndpointHandler<
420495
Path extends string,
421496
Options extends EndpointOptions,
422497
R,
@@ -430,12 +505,12 @@ export function createEndpoint<
430505
path: Path,
431506
options: Options,
432507
handler: EndpointHandler<Path, Options, R>,
433-
): StrictEndpoint<Path, Options, R>;
508+
): StrictEndpoint<Path, ExtractStandSchema<Options>, R>;
434509

435510
export function createEndpoint<Options extends EndpointOptions, R>(
436511
options: Options,
437512
handler: EndpointHandler<never, Options, R>,
438-
): StrictEndpoint<never, Options, R>;
513+
): StrictEndpoint<never, ExtractStandSchema<Options>, R>;
439514

440515
export function createEndpoint<
441516
Path extends string,
@@ -445,7 +520,7 @@ export function createEndpoint<
445520
pathOrOptions: Path | Options,
446521
handlerOrOptions: EndpointHandler<Path, Options, R> | Options,
447522
handlerOrNever?: any,
448-
): StrictEndpoint<Path, Options, R> {
523+
): StrictEndpoint<Path, ExtractStandSchema<Options>, R> {
449524
const path: string | undefined =
450525
typeof pathOrOptions === "string" ? pathOrOptions : undefined;
451526
const options: Options =
@@ -574,7 +649,11 @@ export function createEndpoint<
574649
};
575650
internalHandler.options = options;
576651
internalHandler.path = path;
577-
return internalHandler as unknown as StrictEndpoint<Path, Options, R>;
652+
return internalHandler as unknown as StrictEndpoint<
653+
Path,
654+
ExtractStandSchema<Options>,
655+
R
656+
>;
578657
}
579658

580659
createEndpoint.create = <E extends { use?: Middleware[] }>(opts?: E) => {

packages/better-call/tsconfig.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"extends": "../../tsconfig.base.json",
33
"compilerOptions": {
4-
"rootDir": "./src",
5-
"outDir": "./dist"
6-
},
7-
"include": ["./src"]
4+
"lib": ["esnext", "dom", "dom.iterable"],
5+
"types": ["node", "bun"]
6+
}
87
}

packages/better-call/tsdown.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineConfig({
77
error: "src/error.ts",
88
node: "src/adapters/node/index.ts",
99
},
10-
dts: true,
10+
dts: { build: true, incremental: true },
1111
sourcemap: true,
1212
format: ["esm", "cjs"],
1313
unbundle: true,

0 commit comments

Comments
 (0)