@hyper-fetch/hono 9.8.0 → 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 +1 -0
- 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/responses/responses.sdk.spec.ts +149 -0
- package/__tests__/features/responses/responses.types.spec.ts +107 -0
- package/__tests__/utils/inputs.app.ts +57 -0
- package/__tests__/utils/paths.app.ts +31 -0
- package/__tests__/utils/responses.app.ts +54 -0
- package/__tests__/utils/server.ts +18 -0
- package/dist/hono.types.d.ts +49 -10
- package/dist/hono.types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/hono.types.ts +90 -26
- package/vite.config.ts +5 -0
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,90 @@ 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
|
+
|
|
32
35
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
36
|
+
* Hono stores `.all()` routes under `$all`, and `ALL` is not a method a client can send. Like Hono's
|
|
37
|
+
* own client, the route gets every standard method instead. A method registered on the same path
|
|
38
|
+
* keeps its own handler.
|
|
35
39
|
*/
|
|
36
|
-
type
|
|
37
|
-
[
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
40
|
+
type ExpandAllMethod<Methods> = "$all" extends keyof Methods
|
|
41
|
+
? Omit<Methods, "$all"> & { [M in Exclude<StandardMethodKey, keyof Methods>]: Methods["$all"] }
|
|
42
|
+
: Methods;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Turns the `{ $method: endpoint }` entry of a Hono path into Hyper Fetch `Request`s. The endpoint
|
|
46
|
+
* is the one `createSdk` builds from the accessors, so `:param` segments type `setParams()`.
|
|
47
|
+
*/
|
|
48
|
+
type ConvertMethods<Methods, Endpoint extends string, Client extends ClientInstance> = {
|
|
49
|
+
[K in ExtractMethodKeys<Methods> as K extends `$${infer M extends string}` ? `$${Lowercase<M>}` : never]: Request<
|
|
50
|
+
ExtractOutput<Methods[K]>,
|
|
51
|
+
ExtractJson<ExtractInput<Methods[K]>>,
|
|
52
|
+
ExtractQuery<ExtractInput<Methods[K]>>,
|
|
53
|
+
undefined,
|
|
54
|
+
Endpoint,
|
|
55
|
+
Client
|
|
56
|
+
>;
|
|
49
57
|
};
|
|
50
58
|
|
|
51
59
|
/* -------------------------------------------------------------------------------------------------
|
|
52
|
-
*
|
|
60
|
+
* Paths -> nested SDK schema
|
|
53
61
|
* -----------------------------------------------------------------------------------------------*/
|
|
54
62
|
|
|
55
63
|
type TrimStartSlash<S extends string> = S extends `/${infer R}` ? R : S;
|
|
56
64
|
|
|
65
|
+
/** Skips a regex pattern up to its closing brace, counting the braces nested inside it. */
|
|
66
|
+
type SkipPattern<S extends string, Depth extends unknown[] = []> = S extends `${infer Char}${infer Rest}`
|
|
67
|
+
? Char extends "}"
|
|
68
|
+
? Depth extends [unknown, ...infer Outer]
|
|
69
|
+
? SkipPattern<Rest, Outer>
|
|
70
|
+
: Rest
|
|
71
|
+
: Char extends "{"
|
|
72
|
+
? SkipPattern<Rest, [...Depth, unknown]>
|
|
73
|
+
: SkipPattern<Rest, Depth>
|
|
74
|
+
: S;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Hono restricts a param with a regex, as in `/posts/:id{[0-9]+}`. The client fills the param by its
|
|
78
|
+
* name only, so the pattern is dropped from the path, even when it holds a slash or braces.
|
|
79
|
+
*/
|
|
80
|
+
type StripPatterns<S extends string> = S extends `${infer Head}{${infer Rest}`
|
|
81
|
+
? `${Head}${StripPatterns<SkipPattern<Rest>>}`
|
|
82
|
+
: S;
|
|
83
|
+
|
|
57
84
|
type SplitPath<S extends string> = S extends ""
|
|
58
85
|
? []
|
|
59
86
|
: S extends `${infer Head}/${infer Tail}`
|
|
60
87
|
? [Head, ...SplitPath<Tail>]
|
|
61
88
|
: [S];
|
|
62
89
|
|
|
90
|
+
type JoinSegments<Segments extends string[]> = Segments extends [infer Head extends string]
|
|
91
|
+
? Head
|
|
92
|
+
: Segments extends [infer Head extends string, ...infer Rest extends string[]]
|
|
93
|
+
? `${Head}/${JoinSegments<Rest>}`
|
|
94
|
+
: "";
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* `createSdk` joins the accessors with `/` behind a leading slash, so a Hono path without a leading
|
|
98
|
+
* slash, or with a trailing one, gets the endpoint the request is really sent to.
|
|
99
|
+
*/
|
|
100
|
+
type JoinPath<Segments extends string[]> = `/${JoinSegments<Segments>}`;
|
|
101
|
+
|
|
102
|
+
type StripOptionalMarkers<Segments extends string[]> = {
|
|
103
|
+
[I in keyof Segments]: Segments[I] extends `:${infer Param}?` ? `:${Param}` : Segments[I];
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Hono serves `/files/:id?` both at `/files` and at `/files/:id`, and a chain of trailing optional
|
|
108
|
+
* params at every prefix. Hyper Fetch params are always required, so each prefix is its own route.
|
|
109
|
+
*/
|
|
110
|
+
type ExpandOptionalParams<Segments extends string[]> = Segments extends [
|
|
111
|
+
...infer Init extends string[],
|
|
112
|
+
`:${infer Param}?`,
|
|
113
|
+
]
|
|
114
|
+
? ExpandOptionalParams<Init> | [...StripOptionalMarkers<Init>, `:${Param}`]
|
|
115
|
+
: Segments;
|
|
116
|
+
|
|
63
117
|
type CamelCase<S extends string> = S extends `${infer H}-${infer T}` ? `${H}${Capitalize<CamelCase<T>>}` : S;
|
|
64
118
|
|
|
65
119
|
/**
|
|
@@ -75,12 +129,19 @@ type BuildNestedObject<Segments extends readonly string[], Value> = Segments ext
|
|
|
75
129
|
? { [K in NormalizeSegment<Head>]: BuildNestedObject<Rest, Value> }
|
|
76
130
|
: Value;
|
|
77
131
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
132
|
+
/** Distributes over the routes one Hono path expands to. */
|
|
133
|
+
type ConvertRoutes<Methods, Routes extends string[], Client extends ClientInstance> = Routes extends string[]
|
|
134
|
+
? BuildNestedObject<Routes, Prettify<ConvertMethods<Methods, JoinPath<Routes>, Client>>>
|
|
135
|
+
: never;
|
|
136
|
+
|
|
137
|
+
type ConvertSchema<Schema extends Record<string, any>, Client extends ClientInstance> = UnionToIntersection<
|
|
138
|
+
{
|
|
139
|
+
[Path in keyof Schema & string]: ConvertRoutes<
|
|
140
|
+
ExpandAllMethod<Schema[Path]>,
|
|
141
|
+
ExpandOptionalParams<SplitPath<TrimStartSlash<StripPatterns<Path>>>>,
|
|
142
|
+
Client
|
|
143
|
+
>;
|
|
144
|
+
}[keyof Schema & string]
|
|
84
145
|
>;
|
|
85
146
|
|
|
86
147
|
/**
|
|
@@ -90,6 +151,11 @@ type NestFlatRecord<R extends Record<string, any>> = Prettify<
|
|
|
90
151
|
* from the Hono route definition and its validators. Path segments become nested properties,
|
|
91
152
|
* `:param` segments become `$param` properties, and kebab-case segments become camelCase.
|
|
92
153
|
*
|
|
154
|
+
* Routes below an optional `:param?` segment appear both without and with the param, and regex
|
|
155
|
+
* params like `:id{[0-9]+}` become plain `$id` properties. The endpoint is the one `createSdk`
|
|
156
|
+
* sends the request to, so a trailing slash and the regex patterns of the Hono path are dropped.
|
|
157
|
+
* An `.all()` route gets every standard method, like in Hono's own client.
|
|
158
|
+
*
|
|
93
159
|
* Pass the type of the app returned by your route chain, including apps mounted with
|
|
94
160
|
* `app.route()` and `basePath()`. Anything that is not a Hono app resolves to `{}`.
|
|
95
161
|
*
|
|
@@ -111,6 +177,4 @@ type NestFlatRecord<R extends Record<string, any>> = Prettify<
|
|
|
111
177
|
export type HonoAppToHyperFetch<App, Client extends ClientInstance = ClientInstance> =
|
|
112
178
|
// Match `HonoBase` with all four generics: `.route()` returns `HonoBase<E, S, BasePath, CurrentPath>`,
|
|
113
179
|
// 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
|
-
? Prettify<NestFlatRecord<ConvertToRequest<MergeUnionToRecord<S>, Client>>>
|
|
116
|
-
: {};
|
|
180
|
+
App extends HonoBase<any, infer S, any, any> ? Prettify<ConvertSchema<MergeUnionToRecord<S>, Client>> : {};
|
package/vite.config.ts
CHANGED
|
@@ -24,6 +24,11 @@ export default defineConfig({
|
|
|
24
24
|
alias: {
|
|
25
25
|
"@hyper-fetch/core": path.resolve(__dirname, "../core/src/index.ts"),
|
|
26
26
|
},
|
|
27
|
+
coverage: {
|
|
28
|
+
include: ["src/**/*.ts"],
|
|
29
|
+
provider: "v8",
|
|
30
|
+
thresholds: { 100: true },
|
|
31
|
+
},
|
|
27
32
|
environment: "node",
|
|
28
33
|
globals: true,
|
|
29
34
|
include: ["__tests__/**/*.spec.{ts,tsx}", "src/**/*.spec.{ts,tsx}"],
|