@hyper-fetch/hono 9.8.1 → 10.0.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 +70 -3
- package/__tests__/features/inputs/inputs.sdk.spec.ts +113 -0
- package/__tests__/features/inputs/inputs.types.spec.ts +87 -0
- package/__tests__/features/paths/paths.sdk.spec.ts +159 -0
- package/__tests__/features/paths/paths.types.spec.ts +223 -0
- package/__tests__/features/realtime/realtime.react.spec.tsx +226 -0
- package/__tests__/features/realtime/realtime.sse.spec.ts +340 -0
- package/__tests__/features/realtime/realtime.types.spec.ts +140 -0
- package/__tests__/features/realtime/realtime.websocket.spec.ts +414 -0
- package/__tests__/features/responses/responses.sdk.spec.ts +149 -0
- package/__tests__/features/responses/responses.types.spec.ts +107 -0
- package/__tests__/features/schema/schema.types.spec.ts +42 -0
- package/__tests__/utils/inputs.app.ts +57 -0
- package/__tests__/utils/paths.app.ts +31 -0
- package/__tests__/utils/realtime-server.ts +333 -0
- package/__tests__/utils/responses.app.ts +54 -0
- package/__tests__/utils/server.ts +18 -0
- package/dist/hono.types.d.ts +105 -10
- package/dist/hono.types.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/package.json +6 -2
- package/src/hono.types.ts +162 -26
- package/src/index.ts +1 -1
- package/vite.config.ts +7 -0
package/dist/hono.types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ClientInstance, Request } from '@hyper-fetch/core';
|
|
2
2
|
import { HonoBase } from 'hono/hono-base';
|
|
3
|
+
import { METHODS } from 'hono/router';
|
|
3
4
|
type Prettify<T> = {
|
|
4
5
|
[K in keyof T]: T[K];
|
|
5
6
|
} & {};
|
|
@@ -18,17 +19,57 @@ type ExtractJson<T> = "json" extends keyof T ? T["json"] : undefined;
|
|
|
18
19
|
type ExtractQuery<T> = "query" extends keyof T ? T["query"] : undefined;
|
|
19
20
|
/** Hono encodes methods as `$get`, `$post`, ... on every path entry of the schema. */
|
|
20
21
|
type ExtractMethodKeys<T> = Extract<keyof T, `$${string}`>;
|
|
22
|
+
type StandardMethodKey = `$${(typeof METHODS)[number]}`;
|
|
23
|
+
/** Hono marks the routes handled by `upgradeWebSocket()` with the `ws` output format. */
|
|
24
|
+
type IsWebSocketEndpoint<T> = T extends {
|
|
25
|
+
outputFormat: "ws";
|
|
26
|
+
} ? true : false;
|
|
27
|
+
/** Method keys of a path entry that answer over HTTP - WebSocket upgrades are left out. */
|
|
28
|
+
type ExtractHttpMethodKeys<T> = {
|
|
29
|
+
[K in ExtractMethodKeys<T>]: IsWebSocketEndpoint<T[K]> extends true ? never : K;
|
|
30
|
+
}[ExtractMethodKeys<T>];
|
|
21
31
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
32
|
+
* Hono stores `.all()` routes under `$all`, and `ALL` is not a method a client can send. Like Hono's
|
|
33
|
+
* own client, the route gets every standard method instead. A method registered on the same path
|
|
34
|
+
* keeps its own handler.
|
|
24
35
|
*/
|
|
25
|
-
type
|
|
26
|
-
[
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
type ExpandAllMethod<Methods> = "$all" extends keyof Methods ? Omit<Methods, "$all"> & {
|
|
37
|
+
[M in Exclude<StandardMethodKey, keyof Methods>]: Methods["$all"];
|
|
38
|
+
} : Methods;
|
|
39
|
+
/**
|
|
40
|
+
* Turns the `{ $method: endpoint }` entry of a Hono path into Hyper Fetch `Request`s. The endpoint
|
|
41
|
+
* is the one `createSdk` builds from the accessors, so `:param` segments type `setParams()`.
|
|
42
|
+
* WebSocket upgrades are not requests, so they are skipped.
|
|
43
|
+
*/
|
|
44
|
+
type ConvertMethods<Methods, Endpoint extends string, Client extends ClientInstance> = {
|
|
45
|
+
[K in ExtractHttpMethodKeys<Methods> as K extends `$${infer M extends string}` ? `$${Lowercase<M>}` : never]: Request<ExtractOutput<Methods[K]>, ExtractJson<ExtractInput<Methods[K]>>, ExtractQuery<ExtractInput<Methods[K]>>, undefined, Endpoint, Client>;
|
|
29
46
|
};
|
|
30
47
|
type TrimStartSlash<S extends string> = S extends `/${infer R}` ? R : S;
|
|
48
|
+
/** Skips a regex pattern up to its closing brace, counting the braces nested inside it. */
|
|
49
|
+
type SkipPattern<S extends string, Depth extends unknown[] = []> = S extends `${infer Char}${infer Rest}` ? Char extends "}" ? Depth extends [unknown, ...infer Outer] ? SkipPattern<Rest, Outer> : Rest : Char extends "{" ? SkipPattern<Rest, [...Depth, unknown]> : SkipPattern<Rest, Depth> : S;
|
|
50
|
+
/**
|
|
51
|
+
* Hono restricts a param with a regex, as in `/posts/:id{[0-9]+}`. The client fills the param by its
|
|
52
|
+
* name only, so the pattern is dropped from the path, even when it holds a slash or braces.
|
|
53
|
+
*/
|
|
54
|
+
type StripPatterns<S extends string> = S extends `${infer Head}{${infer Rest}` ? `${Head}${StripPatterns<SkipPattern<Rest>>}` : S;
|
|
31
55
|
type SplitPath<S extends string> = S extends "" ? [] : S extends `${infer Head}/${infer Tail}` ? [Head, ...SplitPath<Tail>] : [S];
|
|
56
|
+
type JoinSegments<Segments extends string[]> = Segments extends [infer Head extends string] ? Head : Segments extends [infer Head extends string, ...infer Rest extends string[]] ? `${Head}/${JoinSegments<Rest>}` : "";
|
|
57
|
+
/**
|
|
58
|
+
* `createSdk` joins the accessors with `/` behind a leading slash, so a Hono path without a leading
|
|
59
|
+
* slash, or with a trailing one, gets the endpoint the request is really sent to.
|
|
60
|
+
*/
|
|
61
|
+
type JoinPath<Segments extends string[]> = `/${JoinSegments<Segments>}`;
|
|
62
|
+
type StripOptionalMarkers<Segments extends string[]> = {
|
|
63
|
+
[I in keyof Segments]: Segments[I] extends `:${infer Param}?` ? `:${Param}` : Segments[I];
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Hono serves `/files/:id?` both at `/files` and at `/files/:id`, and a chain of trailing optional
|
|
67
|
+
* params at every prefix. Hyper Fetch params are always required, so each prefix is its own route.
|
|
68
|
+
*/
|
|
69
|
+
type ExpandOptionalParams<Segments extends string[]> = Segments extends [
|
|
70
|
+
...infer Init extends string[],
|
|
71
|
+
`:${infer Param}?`
|
|
72
|
+
] ? ExpandOptionalParams<Init> | [...StripOptionalMarkers<Init>, `:${Param}`] : Segments;
|
|
32
73
|
type CamelCase<S extends string> = S extends `${infer H}-${infer T}` ? `${H}${Capitalize<CamelCase<T>>}` : S;
|
|
33
74
|
/**
|
|
34
75
|
* `:userId` becomes `$userId` and `google-login` becomes `googleLogin`, matching the accessors
|
|
@@ -41,9 +82,13 @@ type BuildNestedObject<Segments extends readonly string[], Value> = Segments ext
|
|
|
41
82
|
] ? {
|
|
42
83
|
[K in NormalizeSegment<Head>]: BuildNestedObject<Rest, Value>;
|
|
43
84
|
} : Value;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
85
|
+
/** Distributes over the routes one Hono path expands to. */
|
|
86
|
+
type ConvertRoutes<Methods, Routes extends string[], Client extends ClientInstance> = Routes extends string[] ? BuildNestedObject<Routes, Prettify<ConvertMethods<Methods, JoinPath<Routes>, Client>>> : never;
|
|
87
|
+
/** The routes one Hono path expands to - optional params give every prefix, regex patterns are dropped */
|
|
88
|
+
type ExpandPath<Path extends string> = ExpandOptionalParams<SplitPath<TrimStartSlash<StripPatterns<Path>>>>;
|
|
89
|
+
type ConvertSchema<Schema extends Record<string, any>, Client extends ClientInstance> = UnionToIntersection<{
|
|
90
|
+
[Path in keyof Schema & string]: [ExtractHttpMethodKeys<ExpandAllMethod<Schema[Path]>>] extends [never] ? never : ConvertRoutes<ExpandAllMethod<Schema[Path]>, ExpandPath<Path>, Client>;
|
|
91
|
+
}[keyof Schema & string]>;
|
|
47
92
|
/**
|
|
48
93
|
* Converts the type of a Hono app into a Hyper Fetch SDK schema for `createSdk`.
|
|
49
94
|
*
|
|
@@ -51,9 +96,18 @@ type NestFlatRecord<R extends Record<string, any>> = Prettify<UnionToIntersectio
|
|
|
51
96
|
* from the Hono route definition and its validators. Path segments become nested properties,
|
|
52
97
|
* `:param` segments become `$param` properties, and kebab-case segments become camelCase.
|
|
53
98
|
*
|
|
99
|
+
* Routes below an optional `:param?` segment appear both without and with the param, and regex
|
|
100
|
+
* params like `:id{[0-9]+}` become plain `$id` properties. The endpoint is the one `createSdk`
|
|
101
|
+
* sends the request to, so a trailing slash and the regex patterns of the Hono path are dropped.
|
|
102
|
+
* An `.all()` route gets every standard method, like in Hono's own client.
|
|
103
|
+
*
|
|
54
104
|
* Pass the type of the app returned by your route chain, including apps mounted with
|
|
55
105
|
* `app.route()` and `basePath()`. Anything that is not a Hono app resolves to `{}`.
|
|
56
106
|
*
|
|
107
|
+
* Routes handled by `upgradeWebSocket()` are not requests, so they are left out - connect to them with a
|
|
108
|
+
* `Socket` and check the address with {@link HonoWebSocketPath}. Server-Sent Events routes (`streamSSE()`)
|
|
109
|
+
* stay in the schema as a `$get` with an untyped response: Hono types them like any other raw `Response`.
|
|
110
|
+
*
|
|
57
111
|
* @typeParam App - The Hono app type, usually `typeof app`.
|
|
58
112
|
* @typeParam Client - The client the requests belong to. `createSdk` injects its own client, so
|
|
59
113
|
* this is only needed when the schema is used outside of `createSdk`.
|
|
@@ -69,6 +123,47 @@ type NestFlatRecord<R extends Record<string, any>> = Prettify<UnionToIntersectio
|
|
|
69
123
|
* const { data } = await sdk.users.$userId.$get.send({ params: { userId: "1" } });
|
|
70
124
|
* ```
|
|
71
125
|
*/
|
|
72
|
-
export type HonoAppToHyperFetch<App, Client extends ClientInstance = ClientInstance> = App extends HonoBase<any, infer S, any, any> ? Prettify<
|
|
126
|
+
export type HonoAppToHyperFetch<App, Client extends ClientInstance = ClientInstance> = App extends HonoBase<any, infer S, any, any> ? Prettify<ConvertSchema<MergeUnionToRecord<S>, Client>> : {};
|
|
127
|
+
type ExtractAppSchema<App> = App extends HonoBase<any, infer S, any, any> ? MergeUnionToRecord<S> : never;
|
|
128
|
+
/** `path -> endpoint` of the routes that upgrade a GET to a WebSocket - the routes `hc` exposes as `$ws`. */
|
|
129
|
+
type ExtractWebSocketEndpoints<Schema> = {
|
|
130
|
+
[Path in keyof Schema & string as Schema[Path] extends {
|
|
131
|
+
$get: infer Endpoint;
|
|
132
|
+
} ? IsWebSocketEndpoint<Endpoint> extends true ? Path : never : never]: Schema[Path] extends {
|
|
133
|
+
$get: infer Endpoint;
|
|
134
|
+
} ? Endpoint : never;
|
|
135
|
+
};
|
|
136
|
+
type WebSocketEndpoints<App> = ExtractWebSocketEndpoints<ExtractAppSchema<App>>;
|
|
137
|
+
type ToSegmentPattern<Segment extends string> = Segment extends `:${string}` ? string : Segment;
|
|
138
|
+
/** Replaces every `:param` segment with `string`, so a path with the params filled in matches it. */
|
|
139
|
+
type ToRoutePattern<Segments extends string[]> = `/${JoinSegments<{
|
|
140
|
+
[I in keyof Segments]: ToSegmentPattern<Segments[I]>;
|
|
141
|
+
}>}`;
|
|
142
|
+
/** Every address of a Hono path, with optional params and regex patterns handled like in the HTTP schema */
|
|
143
|
+
type ToPathPattern<Path extends string> = ExpandPath<Path> extends infer Routes extends string[] ? Routes extends string[] ? ToRoutePattern<Routes> : never : never;
|
|
144
|
+
/**
|
|
145
|
+
* Union of the paths a `Socket` can connect to on a Hono app: every route handled by `upgradeWebSocket()`,
|
|
146
|
+
* including the `basePath()` and `app.route()` prefixes. A `:param` segment accepts any value, so
|
|
147
|
+
* `/chat/:roomId` becomes `` `/chat/${string}` `` and `/chat/42` matches it.
|
|
148
|
+
*
|
|
149
|
+
* Hono has no realtime protocol of its own, so this checks the address only. Describe the messages the
|
|
150
|
+
* route sends and receives with `ListenerModel` and `EmitterModel` in a `createSocketSdk` schema.
|
|
151
|
+
*
|
|
152
|
+
* @typeParam App - The Hono app type, usually `typeof app`. Anything that is not a Hono app resolves to `never`.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* // backend
|
|
157
|
+
* const app = new Hono().get("/chat/:roomId", upgradeWebSocket((c) => ({ onMessage() {} })));
|
|
158
|
+
* export type AppType = typeof app;
|
|
159
|
+
*
|
|
160
|
+
* // frontend
|
|
161
|
+
* const path: HonoWebSocketPath<AppType> = `/chat/${roomId}`;
|
|
162
|
+
* const socket = new Socket({ url: `wss://api.example.com${path}` });
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
export type HonoWebSocketPath<App> = {
|
|
166
|
+
[Path in keyof WebSocketEndpoints<App> & string]: ToPathPattern<Path>;
|
|
167
|
+
}[keyof WebSocketEndpoints<App> & string];
|
|
73
168
|
export {};
|
|
74
169
|
//# sourceMappingURL=hono.types.d.ts.map
|
package/dist/hono.types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hono.types.d.ts","sourceRoot":"","sources":["../src/hono.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"hono.types.d.ts","sourceRoot":"","sources":["../src/hono.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAE3C,KAAK,QAAQ,CAAC,CAAC,IAAI;KAChB,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACrB,GAAG,EAAE,CAAC;AAEP,KAAK,mBAAmB,CAAC,CAAC,IAAI,CAAC,CAAC,SAAS,GAAG,GAAG,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,GAAG,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,IAAI,GAAG,CAAC,GAAG,KAAK,CAAC;AAMpH;;;GAGG;AACH,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mBAAmB,CAAC,QAAQ,CAAC,SAAS,IAAI,GACxC,IAAI,GACJ,KAAK,GACP,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS;QAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;KAAE,GAAG,QAAQ,GAAG,KAAK;CAC7E,CAAC;AAEF,KAAK,kBAAkB,CAAC,GAAG,IAAI,yBAAyB,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC;AAEhF,KAAK,gBAAgB,CAAC,OAAO,SAAS,MAAM,IAAI,OAAO,SAAS,IAAI,MAAM,EAAE,GAAG,MAAM,GAAG,OAAO,CAAC;AAEhG,qGAAqG;AACrG,KAAK,cAAc,CAAC,QAAQ,SAAS,MAAM,EAAE,IAAI,IAAI,YAAY,CAAC;KAC/D,CAAC,IAAI,MAAM,QAAQ,GAAG,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;CACrD,CAAC,EAAE,CAAC;AAEL,4GAA4G;AAC5G,KAAK,aAAa,CAAC,IAAI,SAAS,MAAM,IACpC,UAAU,CAAC,IAAI,CAAC,SAAS,MAAM,MAAM,SAAS,MAAM,EAAE,GAClD,MAAM,SAAS,MAAM,EAAE,GACrB,cAAc,CAAC,MAAM,CAAC,GACtB,KAAK,GACP,KAAK,CAAC;AAEZ;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,iBAAiB,CAAC,GAAG,IAAI;KAClC,IAAI,IAAI,MAAM,kBAAkB,CAAC,GAAG,CAAC,GAAG,MAAM,GAAG,aAAa,CAAC,IAAI,CAAC;CACtE,CAAC,MAAM,kBAAkB,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export type { HonoAppToHyperFetch } from './hono.types';
|
|
1
|
+
export type { HonoAppToHyperFetch, HonoWebSocketPath } from './hono.types';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/hono",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.0.0",
|
|
4
4
|
"description": "Turn a Hono app type into a fully typed Hyper Fetch SDK schema",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"@hyper-fetch/core": "*",
|
|
47
|
+
"@hyper-fetch/sockets": "*",
|
|
47
48
|
"hono": ">=4"
|
|
48
49
|
},
|
|
49
50
|
"lint-staged": {
|
|
@@ -60,6 +61,9 @@
|
|
|
60
61
|
],
|
|
61
62
|
"devDependencies": {
|
|
62
63
|
"@hono/node-server": "^2.1.9",
|
|
63
|
-
"hono": "^
|
|
64
|
+
"@hono/node-ws": "^1.3.1",
|
|
65
|
+
"eventsource": "^3.0.2",
|
|
66
|
+
"hono": "^4.13.19",
|
|
67
|
+
"ws": "^8.18.0"
|
|
64
68
|
}
|
|
65
69
|
}
|
package/src/hono.types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ClientInstance, Request } from "@hyper-fetch/core";
|
|
2
2
|
import type { HonoBase } from "hono/hono-base";
|
|
3
|
+
import type { METHODS } from "hono/router";
|
|
3
4
|
|
|
4
5
|
type Prettify<T> = {
|
|
5
6
|
[K in keyof T]: T[K];
|
|
@@ -8,7 +9,7 @@ type Prettify<T> = {
|
|
|
8
9
|
type UnionToIntersection<U> = (U extends any ? (arg: U) => void : never) extends (arg: infer I) => void ? I : never;
|
|
9
10
|
|
|
10
11
|
/* -------------------------------------------------------------------------------------------------
|
|
11
|
-
* Hono schema -> flat record of
|
|
12
|
+
* Hono schema -> flat record of methods
|
|
12
13
|
* -----------------------------------------------------------------------------------------------*/
|
|
13
14
|
|
|
14
15
|
/**
|
|
@@ -29,37 +30,99 @@ type ExtractQuery<T> = "query" extends keyof T ? T["query"] : undefined;
|
|
|
29
30
|
/** Hono encodes methods as `$get`, `$post`, ... on every path entry of the schema. */
|
|
30
31
|
type ExtractMethodKeys<T> = Extract<keyof T, `$${string}`>;
|
|
31
32
|
|
|
33
|
+
type StandardMethodKey = `$${(typeof METHODS)[number]}`;
|
|
34
|
+
|
|
35
|
+
/** Hono marks the routes handled by `upgradeWebSocket()` with the `ws` output format. */
|
|
36
|
+
type IsWebSocketEndpoint<T> = T extends { outputFormat: "ws" } ? true : false;
|
|
37
|
+
|
|
38
|
+
/** Method keys of a path entry that answer over HTTP - WebSocket upgrades are left out. */
|
|
39
|
+
type ExtractHttpMethodKeys<T> = {
|
|
40
|
+
[K in ExtractMethodKeys<T>]: IsWebSocketEndpoint<T[K]> extends true ? never : K;
|
|
41
|
+
}[ExtractMethodKeys<T>];
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Hono stores `.all()` routes under `$all`, and `ALL` is not a method a client can send. Like Hono's
|
|
45
|
+
* own client, the route gets every standard method instead. A method registered on the same path
|
|
46
|
+
* keeps its own handler.
|
|
47
|
+
*/
|
|
48
|
+
type ExpandAllMethod<Methods> = "$all" extends keyof Methods
|
|
49
|
+
? Omit<Methods, "$all"> & { [M in Exclude<StandardMethodKey, keyof Methods>]: Methods["$all"] }
|
|
50
|
+
: Methods;
|
|
51
|
+
|
|
32
52
|
/**
|
|
33
|
-
* Turns
|
|
34
|
-
*
|
|
53
|
+
* Turns the `{ $method: endpoint }` entry of a Hono path into Hyper Fetch `Request`s. The endpoint
|
|
54
|
+
* is the one `createSdk` builds from the accessors, so `:param` segments type `setParams()`.
|
|
55
|
+
* WebSocket upgrades are not requests, so they are skipped.
|
|
35
56
|
*/
|
|
36
|
-
type
|
|
37
|
-
[
|
|
38
|
-
[K
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
Path extends string ? Path : never,
|
|
46
|
-
Client
|
|
47
|
-
>;
|
|
48
|
-
};
|
|
57
|
+
type ConvertMethods<Methods, Endpoint extends string, Client extends ClientInstance> = {
|
|
58
|
+
[K in ExtractHttpMethodKeys<Methods> as K extends `$${infer M extends string}` ? `$${Lowercase<M>}` : never]: Request<
|
|
59
|
+
ExtractOutput<Methods[K]>,
|
|
60
|
+
ExtractJson<ExtractInput<Methods[K]>>,
|
|
61
|
+
ExtractQuery<ExtractInput<Methods[K]>>,
|
|
62
|
+
undefined,
|
|
63
|
+
Endpoint,
|
|
64
|
+
Client
|
|
65
|
+
>;
|
|
49
66
|
};
|
|
50
67
|
|
|
51
68
|
/* -------------------------------------------------------------------------------------------------
|
|
52
|
-
*
|
|
69
|
+
* Paths -> nested SDK schema
|
|
53
70
|
* -----------------------------------------------------------------------------------------------*/
|
|
54
71
|
|
|
55
72
|
type TrimStartSlash<S extends string> = S extends `/${infer R}` ? R : S;
|
|
56
73
|
|
|
74
|
+
/** Skips a regex pattern up to its closing brace, counting the braces nested inside it. */
|
|
75
|
+
type SkipPattern<S extends string, Depth extends unknown[] = []> = S extends `${infer Char}${infer Rest}`
|
|
76
|
+
? Char extends "}"
|
|
77
|
+
? Depth extends [unknown, ...infer Outer]
|
|
78
|
+
? SkipPattern<Rest, Outer>
|
|
79
|
+
: Rest
|
|
80
|
+
: Char extends "{"
|
|
81
|
+
? SkipPattern<Rest, [...Depth, unknown]>
|
|
82
|
+
: SkipPattern<Rest, Depth>
|
|
83
|
+
: S;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Hono restricts a param with a regex, as in `/posts/:id{[0-9]+}`. The client fills the param by its
|
|
87
|
+
* name only, so the pattern is dropped from the path, even when it holds a slash or braces.
|
|
88
|
+
*/
|
|
89
|
+
type StripPatterns<S extends string> = S extends `${infer Head}{${infer Rest}`
|
|
90
|
+
? `${Head}${StripPatterns<SkipPattern<Rest>>}`
|
|
91
|
+
: S;
|
|
92
|
+
|
|
57
93
|
type SplitPath<S extends string> = S extends ""
|
|
58
94
|
? []
|
|
59
95
|
: S extends `${infer Head}/${infer Tail}`
|
|
60
96
|
? [Head, ...SplitPath<Tail>]
|
|
61
97
|
: [S];
|
|
62
98
|
|
|
99
|
+
type JoinSegments<Segments extends string[]> = Segments extends [infer Head extends string]
|
|
100
|
+
? Head
|
|
101
|
+
: Segments extends [infer Head extends string, ...infer Rest extends string[]]
|
|
102
|
+
? `${Head}/${JoinSegments<Rest>}`
|
|
103
|
+
: "";
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* `createSdk` joins the accessors with `/` behind a leading slash, so a Hono path without a leading
|
|
107
|
+
* slash, or with a trailing one, gets the endpoint the request is really sent to.
|
|
108
|
+
*/
|
|
109
|
+
type JoinPath<Segments extends string[]> = `/${JoinSegments<Segments>}`;
|
|
110
|
+
|
|
111
|
+
type StripOptionalMarkers<Segments extends string[]> = {
|
|
112
|
+
[I in keyof Segments]: Segments[I] extends `:${infer Param}?` ? `:${Param}` : Segments[I];
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Hono serves `/files/:id?` both at `/files` and at `/files/:id`, and a chain of trailing optional
|
|
117
|
+
* params at every prefix. Hyper Fetch params are always required, so each prefix is its own route.
|
|
118
|
+
*/
|
|
119
|
+
type ExpandOptionalParams<Segments extends string[]> = Segments extends [
|
|
120
|
+
...infer Init extends string[],
|
|
121
|
+
`:${infer Param}?`,
|
|
122
|
+
]
|
|
123
|
+
? ExpandOptionalParams<Init> | [...StripOptionalMarkers<Init>, `:${Param}`]
|
|
124
|
+
: Segments;
|
|
125
|
+
|
|
63
126
|
type CamelCase<S extends string> = S extends `${infer H}-${infer T}` ? `${H}${Capitalize<CamelCase<T>>}` : S;
|
|
64
127
|
|
|
65
128
|
/**
|
|
@@ -75,12 +138,21 @@ type BuildNestedObject<Segments extends readonly string[], Value> = Segments ext
|
|
|
75
138
|
? { [K in NormalizeSegment<Head>]: BuildNestedObject<Rest, Value> }
|
|
76
139
|
: Value;
|
|
77
140
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
141
|
+
/** Distributes over the routes one Hono path expands to. */
|
|
142
|
+
type ConvertRoutes<Methods, Routes extends string[], Client extends ClientInstance> = Routes extends string[]
|
|
143
|
+
? BuildNestedObject<Routes, Prettify<ConvertMethods<Methods, JoinPath<Routes>, Client>>>
|
|
144
|
+
: never;
|
|
145
|
+
|
|
146
|
+
/** The routes one Hono path expands to - optional params give every prefix, regex patterns are dropped */
|
|
147
|
+
type ExpandPath<Path extends string> = ExpandOptionalParams<SplitPath<TrimStartSlash<StripPatterns<Path>>>>;
|
|
148
|
+
|
|
149
|
+
type ConvertSchema<Schema extends Record<string, any>, Client extends ClientInstance> = UnionToIntersection<
|
|
150
|
+
{
|
|
151
|
+
// A path served only by a WebSocket upgrade has no request to send
|
|
152
|
+
[Path in keyof Schema & string]: [ExtractHttpMethodKeys<ExpandAllMethod<Schema[Path]>>] extends [never]
|
|
153
|
+
? never
|
|
154
|
+
: ConvertRoutes<ExpandAllMethod<Schema[Path]>, ExpandPath<Path>, Client>;
|
|
155
|
+
}[keyof Schema & string]
|
|
84
156
|
>;
|
|
85
157
|
|
|
86
158
|
/**
|
|
@@ -90,9 +162,18 @@ type NestFlatRecord<R extends Record<string, any>> = Prettify<
|
|
|
90
162
|
* from the Hono route definition and its validators. Path segments become nested properties,
|
|
91
163
|
* `:param` segments become `$param` properties, and kebab-case segments become camelCase.
|
|
92
164
|
*
|
|
165
|
+
* Routes below an optional `:param?` segment appear both without and with the param, and regex
|
|
166
|
+
* params like `:id{[0-9]+}` become plain `$id` properties. The endpoint is the one `createSdk`
|
|
167
|
+
* sends the request to, so a trailing slash and the regex patterns of the Hono path are dropped.
|
|
168
|
+
* An `.all()` route gets every standard method, like in Hono's own client.
|
|
169
|
+
*
|
|
93
170
|
* Pass the type of the app returned by your route chain, including apps mounted with
|
|
94
171
|
* `app.route()` and `basePath()`. Anything that is not a Hono app resolves to `{}`.
|
|
95
172
|
*
|
|
173
|
+
* Routes handled by `upgradeWebSocket()` are not requests, so they are left out - connect to them with a
|
|
174
|
+
* `Socket` and check the address with {@link HonoWebSocketPath}. Server-Sent Events routes (`streamSSE()`)
|
|
175
|
+
* stay in the schema as a `$get` with an untyped response: Hono types them like any other raw `Response`.
|
|
176
|
+
*
|
|
96
177
|
* @typeParam App - The Hono app type, usually `typeof app`.
|
|
97
178
|
* @typeParam Client - The client the requests belong to. `createSdk` injects its own client, so
|
|
98
179
|
* this is only needed when the schema is used outside of `createSdk`.
|
|
@@ -111,6 +192,61 @@ type NestFlatRecord<R extends Record<string, any>> = Prettify<
|
|
|
111
192
|
export type HonoAppToHyperFetch<App, Client extends ClientInstance = ClientInstance> =
|
|
112
193
|
// Match `HonoBase` with all four generics: `.route()` returns `HonoBase<E, S, BasePath, CurrentPath>`,
|
|
113
194
|
// so matching against `Hono<E, S, BasePath>` fails to infer the schema and resolves to `{}`.
|
|
114
|
-
App extends HonoBase<any, infer S, any, any>
|
|
115
|
-
|
|
116
|
-
|
|
195
|
+
App extends HonoBase<any, infer S, any, any> ? Prettify<ConvertSchema<MergeUnionToRecord<S>, Client>> : {};
|
|
196
|
+
|
|
197
|
+
/* -------------------------------------------------------------------------------------------------
|
|
198
|
+
* WebSocket routes
|
|
199
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
200
|
+
|
|
201
|
+
type ExtractAppSchema<App> = App extends HonoBase<any, infer S, any, any> ? MergeUnionToRecord<S> : never;
|
|
202
|
+
|
|
203
|
+
/** `path -> endpoint` of the routes that upgrade a GET to a WebSocket - the routes `hc` exposes as `$ws`. */
|
|
204
|
+
type ExtractWebSocketEndpoints<Schema> = {
|
|
205
|
+
[Path in keyof Schema & string as Schema[Path] extends { $get: infer Endpoint }
|
|
206
|
+
? IsWebSocketEndpoint<Endpoint> extends true
|
|
207
|
+
? Path
|
|
208
|
+
: never
|
|
209
|
+
: never]: Schema[Path] extends { $get: infer Endpoint } ? Endpoint : never;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
type WebSocketEndpoints<App> = ExtractWebSocketEndpoints<ExtractAppSchema<App>>;
|
|
213
|
+
|
|
214
|
+
type ToSegmentPattern<Segment extends string> = Segment extends `:${string}` ? string : Segment;
|
|
215
|
+
|
|
216
|
+
/** Replaces every `:param` segment with `string`, so a path with the params filled in matches it. */
|
|
217
|
+
type ToRoutePattern<Segments extends string[]> = `/${JoinSegments<{
|
|
218
|
+
[I in keyof Segments]: ToSegmentPattern<Segments[I]>;
|
|
219
|
+
}>}`;
|
|
220
|
+
|
|
221
|
+
/** Every address of a Hono path, with optional params and regex patterns handled like in the HTTP schema */
|
|
222
|
+
type ToPathPattern<Path extends string> =
|
|
223
|
+
ExpandPath<Path> extends infer Routes extends string[]
|
|
224
|
+
? Routes extends string[]
|
|
225
|
+
? ToRoutePattern<Routes>
|
|
226
|
+
: never
|
|
227
|
+
: never;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Union of the paths a `Socket` can connect to on a Hono app: every route handled by `upgradeWebSocket()`,
|
|
231
|
+
* including the `basePath()` and `app.route()` prefixes. A `:param` segment accepts any value, so
|
|
232
|
+
* `/chat/:roomId` becomes `` `/chat/${string}` `` and `/chat/42` matches it.
|
|
233
|
+
*
|
|
234
|
+
* Hono has no realtime protocol of its own, so this checks the address only. Describe the messages the
|
|
235
|
+
* route sends and receives with `ListenerModel` and `EmitterModel` in a `createSocketSdk` schema.
|
|
236
|
+
*
|
|
237
|
+
* @typeParam App - The Hono app type, usually `typeof app`. Anything that is not a Hono app resolves to `never`.
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* // backend
|
|
242
|
+
* const app = new Hono().get("/chat/:roomId", upgradeWebSocket((c) => ({ onMessage() {} })));
|
|
243
|
+
* export type AppType = typeof app;
|
|
244
|
+
*
|
|
245
|
+
* // frontend
|
|
246
|
+
* const path: HonoWebSocketPath<AppType> = `/chat/${roomId}`;
|
|
247
|
+
* const socket = new Socket({ url: `wss://api.example.com${path}` });
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
export type HonoWebSocketPath<App> = {
|
|
251
|
+
[Path in keyof WebSocketEndpoints<App> & string]: ToPathPattern<Path>;
|
|
252
|
+
}[keyof WebSocketEndpoints<App> & string];
|
package/src/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export type { HonoAppToHyperFetch } from "./hono.types";
|
|
1
|
+
export type { HonoAppToHyperFetch, HonoWebSocketPath } from "./hono.types";
|
package/vite.config.ts
CHANGED
|
@@ -23,6 +23,13 @@ export default defineConfig({
|
|
|
23
23
|
test: {
|
|
24
24
|
alias: {
|
|
25
25
|
"@hyper-fetch/core": path.resolve(__dirname, "../core/src/index.ts"),
|
|
26
|
+
"@hyper-fetch/react": path.resolve(__dirname, "../react/src/index.ts"),
|
|
27
|
+
"@hyper-fetch/sockets": path.resolve(__dirname, "../sockets/src/index.ts"),
|
|
28
|
+
},
|
|
29
|
+
coverage: {
|
|
30
|
+
include: ["src/**/*.ts"],
|
|
31
|
+
provider: "v8",
|
|
32
|
+
thresholds: { 100: true },
|
|
26
33
|
},
|
|
27
34
|
environment: "node",
|
|
28
35
|
globals: true,
|