@hyper-fetch/hono 9.8.1 → 9.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -40,6 +40,7 @@ types only, so nothing from your server ends up in the client bundle.
40
40
  - 🧭 **Paths become properties** - `/users/:userId/posts` is `sdk.users.$userId.posts.$get`, with typed `setParams()`
41
41
  - 🛂 **Validators drive the payload** - `json` and `query` validators type `setPayload()` and `setQueryParams()`
42
42
  - 🧩 **Mounted routers included** - `app.route()` and `basePath()` prefixes land in the endpoint and the nesting
43
+ - 🧷 **Every path shape** - Optional `:id?` and regex `:id{[0-9]+}` params, `.all()` and `app.on()` get typed accessors
43
44
  - 🪶 **Zero runtime** - A single type export, so it adds no code to your bundle
44
45
  - ⚡ **Every HyperFetch feature** - Cache, retries, deduplication, offline and React hooks work on every route
45
46
 
@@ -0,0 +1,113 @@
1
+ import type { ServerType } from "@hono/node-server";
2
+ import { createClient, createSdk } from "@hyper-fetch/core";
3
+
4
+ import type { HonoAppToHyperFetch } from "../../../src";
5
+ import { inputsApp } from "../../utils/inputs.app";
6
+ import { startServer, stopServer } from "../../utils/server";
7
+
8
+ type Schema = HonoAppToHyperFetch<typeof inputsApp>;
9
+
10
+ describe("HonoAppToHyperFetch [ Validator inputs against a Hono server ]", () => {
11
+ let server: ServerType;
12
+ let url: string;
13
+
14
+ const createAppSdk = () => {
15
+ const client = createClient({ url });
16
+ return { client, sdk: createSdk<typeof client, Schema>(client) };
17
+ };
18
+
19
+ beforeAll(async () => {
20
+ ({ server, url } = await startServer(inputsApp));
21
+ });
22
+
23
+ afterAll(async () => {
24
+ await stopServer(server);
25
+ });
26
+
27
+ it("should send the payload as the JSON body the json validator reads", async () => {
28
+ const { sdk } = createAppSdk();
29
+
30
+ const { data, status } = await sdk.posts.$post.setPayload({ title: "Hello", draft: true }).send();
31
+
32
+ expect(status).toBe(201);
33
+ expect(data).toStrictEqual({ title: "Hello", draft: true });
34
+ });
35
+
36
+ it("should send array query params in the bracket format by default", async () => {
37
+ const { sdk } = createAppSdk();
38
+
39
+ const { data } = await sdk.search.$get.setQueryParams({ q: "hono", tags: ["a", "b"] }).send();
40
+
41
+ expect(data).toStrictEqual({ query: { q: "hono", "tags[]": ["a", "b"] }, tags: [] });
42
+ });
43
+
44
+ it("should send array query params as repeated keys with the none array format", async () => {
45
+ const { client, sdk } = createAppSdk();
46
+ client.adapter.setQueryParamsMapperConfig({ arrayFormat: "none" });
47
+
48
+ const { data } = await sdk.search.$get.setQueryParams({ q: "hono", tags: ["a", "b"] }).send();
49
+
50
+ expect(data).toStrictEqual({ query: { q: "hono", tags: ["a", "b"] }, tags: ["a", "b"] });
51
+ });
52
+
53
+ it("should send array query params as one comma separated value with the comma array format", async () => {
54
+ const { client, sdk } = createAppSdk();
55
+ client.adapter.setQueryParamsMapperConfig({ arrayFormat: "comma" });
56
+
57
+ const { data } = await sdk.search.$get.setQueryParams({ tags: ["a", "b"] }).send();
58
+
59
+ expect(data).toStrictEqual({ query: { tags: "a,b" }, tags: ["a,b"] });
60
+ });
61
+
62
+ it("should send no body to a form route, so the form validator reads an empty form", async () => {
63
+ const { sdk } = createAppSdk();
64
+
65
+ const { data, status } = await sdk.upload.$post.send();
66
+
67
+ expect(status).toBe(200);
68
+ expect(data).toStrictEqual({ title: null, contentType: null });
69
+ });
70
+
71
+ it("should reach a form route with a FormData payload", async () => {
72
+ const { client } = createAppSdk();
73
+ const request = client.createRequest<{ payload: FormData }>()({ endpoint: "/upload", method: "POST" });
74
+ const form = new FormData();
75
+ form.append("title", "Hello");
76
+
77
+ const { data } = await request.setPayload(form).send();
78
+
79
+ expect(data).toMatchObject({ title: "Hello", contentType: expect.stringContaining("multipart/form-data") });
80
+ });
81
+
82
+ it("should pass header and cookie validators with headers set on the request", async () => {
83
+ const { sdk } = createAppSdk();
84
+
85
+ const { data } = await sdk.whoami.$get.setHeaders({ "x-request-id": "req-1", cookie: "session=abc" }).send();
86
+
87
+ expect(data).toStrictEqual({ requestId: "req-1", session: "abc", raw: "abc" });
88
+ });
89
+
90
+ it("should combine params, payload and query params on one route", async () => {
91
+ const { sdk } = createAppSdk();
92
+
93
+ const { data } = await sdk.posts.$postId.$put
94
+ .setParams({ postId: "7" })
95
+ .setPayload({ title: "Edited" })
96
+ .setQueryParams({ notify: "true" })
97
+ .send();
98
+
99
+ expect(data).toStrictEqual({ postId: 7, title: "Edited", notify: true });
100
+ });
101
+
102
+ it("should resolve the response a validator returns as the error", async () => {
103
+ const { client } = createAppSdk();
104
+ const request = client.createRequest<{ payload: { title: string } }>()({ endpoint: "/strict", method: "POST" });
105
+
106
+ const { data, error, status, success } = await request.setPayload({ title: "No name" }).send();
107
+
108
+ expect(success).toBe(false);
109
+ expect(status).toBe(400);
110
+ expect(data).toBeNull();
111
+ expect(error).toStrictEqual({ error: "Name is required" });
112
+ });
113
+ });
@@ -0,0 +1,87 @@
1
+ import { createClient, createSdk } from "@hyper-fetch/core";
2
+ import type { ExtractPayloadType, ExtractQueryParamsType, ExtractResponseType } from "@hyper-fetch/core";
3
+ import { expectTypeOf } from "vitest";
4
+
5
+ import type { HonoAppToHyperFetch } from "../../../src";
6
+ import type { inputsApp } from "../../utils/inputs.app";
7
+
8
+ type Schema = HonoAppToHyperFetch<typeof inputsApp>;
9
+
10
+ describe("HonoAppToHyperFetch [ Validator inputs ]", () => {
11
+ it("should take the payload from the json validator, optional fields included", () => {
12
+ expectTypeOf<ExtractPayloadType<Schema["posts"]["$post"]>>().toEqualTypeOf<{ title: string; draft?: boolean }>();
13
+ expect(true).toBe(true);
14
+ });
15
+
16
+ it("should take the query params from an untyped query validator", () => {
17
+ expectTypeOf<ExtractQueryParamsType<Schema["search"]["$get"]>>().toEqualTypeOf<{
18
+ [key: string]: string | string[];
19
+ }>();
20
+ expect(true).toBe(true);
21
+ });
22
+
23
+ it("should leave the payload undefined for a form validator", () => {
24
+ expectTypeOf<ExtractPayloadType<Schema["upload"]["$post"]>>().toEqualTypeOf<undefined>();
25
+ expect(true).toBe(true);
26
+ });
27
+
28
+ it("should leave header and cookie validators out of the request", () => {
29
+ expectTypeOf<ExtractPayloadType<Schema["whoami"]["$get"]>>().toEqualTypeOf<undefined>();
30
+ expectTypeOf<ExtractQueryParamsType<Schema["whoami"]["$get"]>>().toEqualTypeOf<undefined>();
31
+ expect(true).toBe(true);
32
+ });
33
+
34
+ it("should combine param, json and query validators on one route", () => {
35
+ type Update = Schema["posts"]["$postId"]["$put"];
36
+
37
+ expectTypeOf<ExtractPayloadType<Update>>().toEqualTypeOf<{ title: string; draft?: boolean }>();
38
+ expectTypeOf<ExtractQueryParamsType<Update>>().toEqualTypeOf<{ notify: string | string[] }>();
39
+ expectTypeOf<ExtractResponseType<Update>>().toEqualTypeOf<{
40
+ title: string;
41
+ draft?: boolean;
42
+ postId: number;
43
+ notify: boolean;
44
+ }>();
45
+ expect(true).toBe(true);
46
+ });
47
+
48
+ it("should add the response a validator returns to the route response", () => {
49
+ expectTypeOf<ExtractResponseType<Schema["strict"]["$post"]>>().toEqualTypeOf<
50
+ { name: string } | { error: "Name is required" }
51
+ >();
52
+ expect(true).toBe(true);
53
+ });
54
+ });
55
+
56
+ describe("HonoAppToHyperFetch [ Validator inputs on createSdk ]", () => {
57
+ const client = createClient({ url: "http://localhost" });
58
+ const sdk = createSdk<typeof client, Schema>(client);
59
+
60
+ it("should accept a payload without its optional fields", () => {
61
+ sdk.posts.$post.setPayload({ title: "Hello" });
62
+ sdk.posts.$post.setPayload({ title: "Hello", draft: true });
63
+ // @ts-expect-error - draft is a boolean
64
+ sdk.posts.$post.setPayload({ title: "Hello", draft: "yes" });
65
+ expect(true).toBe(true);
66
+ });
67
+
68
+ it("should accept arrays in the query params", () => {
69
+ sdk.search.$get.setQueryParams({ q: "hono", tags: ["a", "b"] });
70
+ // @ts-expect-error - query values are strings or string arrays
71
+ sdk.search.$get.setQueryParams({ page: 1 });
72
+ expect(true).toBe(true);
73
+ });
74
+
75
+ it("should take the params of a param validator from the Hono path", () => {
76
+ sdk.posts.$postId.$put.setParams({ postId: "1" });
77
+ // @ts-expect-error - the Hono path names the param postId
78
+ sdk.posts.$postId.$put.setParams({ id: "1" });
79
+ expect(true).toBe(true);
80
+ });
81
+
82
+ it("should reject a payload on a form route", () => {
83
+ // @ts-expect-error - the payload of a form route is undefined
84
+ sdk.upload.$post.setPayload(new FormData());
85
+ expect(true).toBe(true);
86
+ });
87
+ });
@@ -0,0 +1,159 @@
1
+ import type { ServerType } from "@hono/node-server";
2
+ import { createClient, createSdk } from "@hyper-fetch/core";
3
+
4
+ import type { HonoAppToHyperFetch } from "../../../src";
5
+ import { pathsApp } from "../../utils/paths.app";
6
+ import { startServer, stopServer } from "../../utils/server";
7
+
8
+ type Schema = HonoAppToHyperFetch<typeof pathsApp>;
9
+
10
+ describe("HonoAppToHyperFetch [ Paths against a Hono server ]", () => {
11
+ let server: ServerType;
12
+ let url: string;
13
+
14
+ const createAppSdk = () => {
15
+ const client = createClient({ url });
16
+ return createSdk<typeof client, Schema>(client);
17
+ };
18
+
19
+ beforeAll(async () => {
20
+ ({ server, url } = await startServer(pathsApp));
21
+ });
22
+
23
+ afterAll(async () => {
24
+ await stopServer(server);
25
+ });
26
+
27
+ it("should call a route with an optional param both without and with the param", async () => {
28
+ const sdk = createAppSdk();
29
+
30
+ const without = await sdk.files.$get.send();
31
+ const withParam = await sdk.files.$fileId.$get.setParams({ fileId: "report" }).send();
32
+
33
+ expect(sdk.files.$get.endpoint).toBe("/files");
34
+ expect(sdk.files.$fileId.$get.endpoint).toBe("/files/:fileId");
35
+ expect(without.data).toStrictEqual({ fileId: null });
36
+ expect(withParam.data).toStrictEqual({ fileId: "report" });
37
+ });
38
+
39
+ it("should call every prefix of a route with several optional params", async () => {
40
+ const sdk = createAppSdk();
41
+
42
+ const major = await sdk.versions.$major.$get.setParams({ major: "1" }).send();
43
+ const minor = await sdk.versions.$major.$minor.$get.setParams({ major: "1", minor: "2" }).send();
44
+ const patch = await sdk.versions.$major.$minor.$patch.$get.setParams({ major: "1", minor: "2", patch: "3" }).send();
45
+
46
+ expect(major.data).toStrictEqual({ major: "1" });
47
+ expect(minor.data).toStrictEqual({ major: "1", minor: "2" });
48
+ expect(patch.data).toStrictEqual({ major: "1", minor: "2", patch: "3" });
49
+ });
50
+
51
+ it("should fill a regex param by its name", async () => {
52
+ const sdk = createAppSdk();
53
+ const request = sdk.posts.$postId.$get;
54
+
55
+ const { data } = await request.setParams({ postId: "42" }).send();
56
+
57
+ expect(request.endpoint).toBe("/posts/:postId");
58
+ expect(data).toStrictEqual({ postId: "42" });
59
+ });
60
+
61
+ it("should fill a regex param whose pattern holds braces", async () => {
62
+ const sdk = createAppSdk();
63
+
64
+ const { data } = await sdk.archive.$year.$slug.$get.setParams({ year: "2024", slug: "launch" }).send();
65
+
66
+ expect(data).toStrictEqual({ year: "2024", slug: "launch" });
67
+ });
68
+
69
+ it("should not match a regex param with a value the pattern rejects", async () => {
70
+ const sdk = createAppSdk();
71
+
72
+ const { status, success } = await sdk.posts.$postId.$get.setParams({ postId: "latest" }).send();
73
+
74
+ expect(success).toBe(false);
75
+ expect(status).toBe(404);
76
+ });
77
+
78
+ it("should send every standard method to an .all() route", async () => {
79
+ const sdk = createAppSdk();
80
+
81
+ const get = await sdk.echo.$get.send();
82
+ const post = await sdk.echo.$post.send();
83
+ const del = await sdk.echo.$delete.send();
84
+
85
+ expect(get.data).toStrictEqual({ method: "GET" });
86
+ expect(post.data).toStrictEqual({ method: "POST" });
87
+ expect(del.data).toStrictEqual({ method: "DELETE" });
88
+ });
89
+
90
+ it("should send each method of an app.on() route with several methods", async () => {
91
+ const sdk = createAppSdk();
92
+
93
+ const put = await sdk.items.$itemId.$put.setParams({ itemId: "1" }).send();
94
+ const patch = await sdk.items.$itemId.$patch.setParams({ itemId: "1" }).send();
95
+
96
+ expect(put.data).toStrictEqual({ method: "PUT", itemId: "1" });
97
+ expect(patch.data).toStrictEqual({ method: "PATCH", itemId: "1" });
98
+ });
99
+
100
+ it("should send to each path of an app.on() route with several paths", async () => {
101
+ const sdk = createAppSdk();
102
+
103
+ const health = await sdk.health.$get.send();
104
+ const status = await sdk.status.$get.send();
105
+
106
+ expect(health.data).toStrictEqual({ up: true });
107
+ expect(status.data).toStrictEqual({ up: true });
108
+ });
109
+
110
+ it("should send to a path registered without a leading slash", async () => {
111
+ const sdk = createAppSdk();
112
+ const request = sdk.plain.$get;
113
+
114
+ const { data } = await request.send();
115
+
116
+ expect(request.endpoint).toBe("/plain");
117
+ expect(data).toStrictEqual({ plain: true });
118
+ });
119
+
120
+ it("should send to a path registered with a trailing slash without the slash", async () => {
121
+ const sdk = createAppSdk();
122
+ const request = sdk.slash.$get;
123
+
124
+ const { data } = await request.send();
125
+
126
+ expect(request.endpoint).toBe("/slash");
127
+ expect(data).toStrictEqual({ slash: true });
128
+ });
129
+
130
+ it("should send to a wildcard route through its literal path", async () => {
131
+ const sdk = createAppSdk();
132
+ const request = sdk.assets["*"].$get;
133
+
134
+ const { data } = await request.send();
135
+
136
+ expect(request.endpoint).toBe("/assets/*");
137
+ expect(data).toStrictEqual({ path: "/assets/*" });
138
+ });
139
+
140
+ it("should send each method of two routers mounted on one path", async () => {
141
+ const sdk = createAppSdk();
142
+
143
+ const read = await sdk.shared.$get.send();
144
+ const written = await sdk.shared.$post.send();
145
+
146
+ expect(read.data).toStrictEqual({ read: true });
147
+ expect(written.data).toStrictEqual({ written: true });
148
+ });
149
+
150
+ it("should send to a route nested deeper than createSdk injects its client", async () => {
151
+ const sdk = createAppSdk();
152
+ const request = sdk.l1.l2.l3.l4.l5.l6.l7.l8.l9.l10.l11.$leaf.$get;
153
+
154
+ const { data } = await request.setParams({ leaf: "end" }).send();
155
+
156
+ expect(request.endpoint).toBe("/l1/l2/l3/l4/l5/l6/l7/l8/l9/l10/l11/:leaf");
157
+ expect(data).toStrictEqual({ leaf: "end" });
158
+ });
159
+ });
@@ -0,0 +1,223 @@
1
+ import { createClient, createSdk } from "@hyper-fetch/core";
2
+ import type { ClientInstance, ExtractClientType, ExtractEndpointType, ExtractResponseType } from "@hyper-fetch/core";
3
+ import { Hono } from "hono";
4
+ import { expectTypeOf } from "vitest";
5
+
6
+ import type { HonoAppToHyperFetch } from "../../../src";
7
+ import type { pathsApp, rootOptionalApp } from "../../utils/paths.app";
8
+
9
+ type Schema = HonoAppToHyperFetch<typeof pathsApp>;
10
+
11
+ describe("HonoAppToHyperFetch [ Optional params ]", () => {
12
+ it("should add a route with an optional param both without and with the param", () => {
13
+ expectTypeOf<keyof Schema["files"]>().toEqualTypeOf<"$get" | "$fileId">();
14
+ expectTypeOf<ExtractEndpointType<Schema["files"]["$get"]>>().toEqualTypeOf<"/files">();
15
+ expectTypeOf<ExtractEndpointType<Schema["files"]["$fileId"]["$get"]>>().toEqualTypeOf<"/files/:fileId">();
16
+ expectTypeOf<ExtractResponseType<Schema["files"]["$get"]>>().toEqualTypeOf<{ fileId: string | null }>();
17
+ expect(true).toBe(true);
18
+ });
19
+
20
+ it("should add every prefix of a route with several optional params", () => {
21
+ type Major = Schema["versions"]["$major"];
22
+
23
+ expectTypeOf<keyof Schema["versions"]>().toEqualTypeOf<"$major">();
24
+ expectTypeOf<keyof Major>().toEqualTypeOf<"$get" | "$minor">();
25
+ expectTypeOf<keyof Major["$minor"]>().toEqualTypeOf<"$get" | "$patch">();
26
+ expectTypeOf<ExtractEndpointType<Major["$get"]>>().toEqualTypeOf<"/versions/:major">();
27
+ expectTypeOf<ExtractEndpointType<Major["$minor"]["$get"]>>().toEqualTypeOf<"/versions/:major/:minor">();
28
+ expectTypeOf<
29
+ ExtractEndpointType<Major["$minor"]["$patch"]["$get"]>
30
+ >().toEqualTypeOf<"/versions/:major/:minor/:patch">();
31
+ expect(true).toBe(true);
32
+ });
33
+
34
+ it("should put the route without an optional root param at the top level", () => {
35
+ type RootSchema = HonoAppToHyperFetch<typeof rootOptionalApp>;
36
+
37
+ expectTypeOf<keyof RootSchema>().toEqualTypeOf<"$get" | "$lang">();
38
+ expectTypeOf<ExtractEndpointType<RootSchema["$get"]>>().toEqualTypeOf<"/">();
39
+ expectTypeOf<ExtractEndpointType<RootSchema["$lang"]["$get"]>>().toEqualTypeOf<"/:lang">();
40
+ expect(true).toBe(true);
41
+ });
42
+
43
+ it("should not keep keys or params with the optional marker", () => {
44
+ // @ts-expect-error - the optional param is split into two routes
45
+ type OptionalKey = Schema["files"]["$fileId?"];
46
+
47
+ expectTypeOf<OptionalKey>().toBeAny();
48
+ expect(true).toBe(true);
49
+ });
50
+ });
51
+
52
+ describe("HonoAppToHyperFetch [ Regex params ]", () => {
53
+ it("should drop the pattern from the key and the endpoint", () => {
54
+ expectTypeOf<keyof Schema["posts"]>().toEqualTypeOf<"$postId">();
55
+ expectTypeOf<ExtractEndpointType<Schema["posts"]["$postId"]["$get"]>>().toEqualTypeOf<"/posts/:postId">();
56
+ expect(true).toBe(true);
57
+ });
58
+
59
+ it("should drop a pattern that holds braces of its own", () => {
60
+ expectTypeOf<keyof Schema["archive"]>().toEqualTypeOf<"$year">();
61
+ expectTypeOf<
62
+ ExtractEndpointType<Schema["archive"]["$year"]["$slug"]["$get"]>
63
+ >().toEqualTypeOf<"/archive/:year/:slug">();
64
+ expect(true).toBe(true);
65
+ });
66
+
67
+ it("should drop a pattern that holds a slash", () => {
68
+ const app = new Hono().get("/docs/:path{.+/.+}/raw", (c) => c.json({ path: c.req.param("path") }));
69
+ type DocsSchema = HonoAppToHyperFetch<typeof app>;
70
+
71
+ expectTypeOf<keyof DocsSchema["docs"]>().toEqualTypeOf<"$path">();
72
+ expectTypeOf<ExtractEndpointType<DocsSchema["docs"]["$path"]["raw"]["$get"]>>().toEqualTypeOf<"/docs/:path/raw">();
73
+ expect(true).toBe(true);
74
+ });
75
+
76
+ it("should split an optional regex param into two routes", () => {
77
+ const app = new Hono().get("/pages/:page{[0-9]+}?", (c) => c.json({ page: c.req.param("page") ?? "1" }));
78
+ type PagesSchema = HonoAppToHyperFetch<typeof app>;
79
+
80
+ expectTypeOf<keyof PagesSchema["pages"]>().toEqualTypeOf<"$get" | "$page">();
81
+ expectTypeOf<ExtractEndpointType<PagesSchema["pages"]["$page"]["$get"]>>().toEqualTypeOf<"/pages/:page">();
82
+ expect(true).toBe(true);
83
+ });
84
+ });
85
+
86
+ describe("HonoAppToHyperFetch [ Methods ]", () => {
87
+ it("should expand an .all() route into every standard method", () => {
88
+ expectTypeOf<keyof Schema["echo"]>().toEqualTypeOf<
89
+ "$get" | "$post" | "$put" | "$delete" | "$options" | "$patch" | "$query"
90
+ >();
91
+ expectTypeOf<ExtractResponseType<Schema["echo"]["$post"]>>().toEqualTypeOf<{ method: string }>();
92
+ expectTypeOf<ExtractEndpointType<Schema["echo"]["$delete"]>>().toEqualTypeOf<"/echo">();
93
+ expect(true).toBe(true);
94
+ });
95
+
96
+ it("should not keep the $all key Hono stores .all() routes under", () => {
97
+ // @ts-expect-error - ALL is not an HTTP method a client can send
98
+ type AllKey = Schema["echo"]["$all"];
99
+
100
+ expectTypeOf<AllKey>().toBeAny();
101
+ expect(true).toBe(true);
102
+ });
103
+
104
+ it("should keep a method registered next to an .all() route on the same path", () => {
105
+ const app = new Hono()
106
+ .all("/hook", (c) => c.json({ any: true as const }))
107
+ .get("/hook", (c) => c.json({ read: true as const }));
108
+ type HookSchema = HonoAppToHyperFetch<typeof app>;
109
+
110
+ expectTypeOf<ExtractResponseType<HookSchema["hook"]["$get"]>>().toEqualTypeOf<{ read: true }>();
111
+ expectTypeOf<ExtractResponseType<HookSchema["hook"]["$post"]>>().toEqualTypeOf<{ any: true }>();
112
+ expect(true).toBe(true);
113
+ });
114
+
115
+ it("should add one key per method of an app.on() route with several methods", () => {
116
+ expectTypeOf<keyof Schema["items"]["$itemId"]>().toEqualTypeOf<"$put" | "$patch">();
117
+ expectTypeOf<ExtractEndpointType<Schema["items"]["$itemId"]["$patch"]>>().toEqualTypeOf<"/items/:itemId">();
118
+ expect(true).toBe(true);
119
+ });
120
+
121
+ it("should add one route per path of an app.on() route with several paths", () => {
122
+ expectTypeOf<ExtractEndpointType<Schema["health"]["$get"]>>().toEqualTypeOf<"/health">();
123
+ expectTypeOf<ExtractEndpointType<Schema["status"]["$get"]>>().toEqualTypeOf<"/status">();
124
+ expectTypeOf<ExtractResponseType<Schema["status"]["$get"]>>().toEqualTypeOf<{ up: true }>();
125
+ expect(true).toBe(true);
126
+ });
127
+
128
+ it("should keep a custom method of app.on()", () => {
129
+ const app = new Hono().on("PURGE", "/cache", (c) => c.json({ purged: true as const }));
130
+ type CacheSchema = HonoAppToHyperFetch<typeof app>;
131
+
132
+ expectTypeOf<keyof CacheSchema["cache"]>().toEqualTypeOf<"$purge">();
133
+ expect(true).toBe(true);
134
+ });
135
+
136
+ it("should merge the methods of two routers mounted on one path", () => {
137
+ expectTypeOf<keyof Schema["shared"]>().toEqualTypeOf<"$get" | "$post">();
138
+ expectTypeOf<ExtractResponseType<Schema["shared"]["$post"]>>().toEqualTypeOf<{ written: true }>();
139
+ expect(true).toBe(true);
140
+ });
141
+ });
142
+
143
+ describe("HonoAppToHyperFetch [ Path shapes ]", () => {
144
+ it("should give a path registered without a leading slash the endpoint createSdk sends", () => {
145
+ expectTypeOf<ExtractEndpointType<Schema["plain"]["$get"]>>().toEqualTypeOf<"/plain">();
146
+ expect(true).toBe(true);
147
+ });
148
+
149
+ it("should give a path registered with a trailing slash the endpoint createSdk sends", () => {
150
+ expectTypeOf<keyof Schema["slash"]>().toEqualTypeOf<"$get">();
151
+ expectTypeOf<ExtractEndpointType<Schema["slash"]["$get"]>>().toEqualTypeOf<"/slash">();
152
+ expect(true).toBe(true);
153
+ });
154
+
155
+ it("should keep a wildcard segment as a literal * key", () => {
156
+ expectTypeOf<keyof Schema["assets"]>().toEqualTypeOf<"*">();
157
+ expectTypeOf<ExtractEndpointType<Schema["assets"]["*"]["$get"]>>().toEqualTypeOf<"/assets/*">();
158
+ expect(true).toBe(true);
159
+ });
160
+
161
+ it("should nest a route deeper than ten segments", () => {
162
+ type Leaf = Schema["l1"]["l2"]["l3"]["l4"]["l5"]["l6"]["l7"]["l8"]["l9"]["l10"]["l11"]["$leaf"]["$get"];
163
+
164
+ expectTypeOf<ExtractEndpointType<Leaf>>().toEqualTypeOf<"/l1/l2/l3/l4/l5/l6/l7/l8/l9/l10/l11/:leaf">();
165
+ expectTypeOf<ExtractResponseType<Leaf>>().toEqualTypeOf<{ leaf: string }>();
166
+ expect(true).toBe(true);
167
+ });
168
+
169
+ it("should keep the default client below the ten levels createSdk injects its client into", () => {
170
+ const client = createClient({ url: "http://localhost" });
171
+ const sdk = createSdk<typeof client, Schema>(client);
172
+ const shallow = sdk.files.$get;
173
+ const deep = sdk.l1.l2.l3.l4.l5.l6.l7.l8.l9.l10.l11.$leaf.$get;
174
+
175
+ expectTypeOf<ExtractClientType<typeof shallow>>().toEqualTypeOf<typeof client>();
176
+ expectTypeOf<ExtractClientType<typeof deep>>().toEqualTypeOf<ClientInstance>();
177
+ deep.setParams({ leaf: "end" });
178
+ // @ts-expect-error - the param is named by the Hono path
179
+ deep.setParams({ id: "end" });
180
+ expect(true).toBe(true);
181
+ });
182
+
183
+ it("should flatten routers mounted with an empty path", () => {
184
+ const app = new Hono().basePath("/api/v1").route(
185
+ "/me",
186
+ new Hono().route(
187
+ "",
188
+ new Hono().get("", (c) => c.json({ name: "hono" })),
189
+ ),
190
+ );
191
+ type MeSchema = HonoAppToHyperFetch<typeof app>;
192
+
193
+ expectTypeOf<ExtractEndpointType<MeSchema["api"]["v1"]["me"]["$get"]>>().toEqualTypeOf<"/api/v1/me">();
194
+ expect(true).toBe(true);
195
+ });
196
+
197
+ it("should prefix routes added after basePath()", () => {
198
+ const app = new Hono().basePath("/api").get("/search", (c) => c.json({ ok: true as const }));
199
+ type SearchSchema = HonoAppToHyperFetch<typeof app>;
200
+
201
+ expectTypeOf<ExtractEndpointType<SearchSchema["api"]["search"]["$get"]>>().toEqualTypeOf<"/api/search">();
202
+ expect(true).toBe(true);
203
+ });
204
+
205
+ it("should prefix a router mounted after basePath()", () => {
206
+ const book = new Hono().get("/", (c) => c.json({ ok: true as const }));
207
+ const app = new Hono().basePath("/v1").route("/book", book);
208
+ type BookSchema = HonoAppToHyperFetch<typeof app>;
209
+
210
+ expectTypeOf<ExtractEndpointType<BookSchema["v1"]["book"]["$get"]>>().toEqualTypeOf<"/v1/book">();
211
+ expect(true).toBe(true);
212
+ });
213
+
214
+ it("should accept an app with a typed environment", () => {
215
+ type Env = { Bindings: { TOKEN: string }; Variables: { userId: string } };
216
+ const api = new Hono<Env>().get("/me", (c) => c.json({ userId: c.get("userId") }));
217
+ const app = new Hono<Env>().route("/api", api);
218
+ type EnvSchema = HonoAppToHyperFetch<typeof app>;
219
+
220
+ expectTypeOf<ExtractResponseType<EnvSchema["api"]["me"]["$get"]>>().toEqualTypeOf<{ userId: string }>();
221
+ expect(true).toBe(true);
222
+ });
223
+ });