-
-
Notifications
You must be signed in to change notification settings - Fork 447
/
types.ts
136 lines (121 loc) · 3.71 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import type { UpgradedWebSocketResponseInputJSONType } from '../helper/websocket/index.ts'
import type { Hono } from '../hono.ts'
import type { Schema } from '../types.ts'
import type { HasRequiredKeys } from '../utils/types.ts'
type HonoRequest = (typeof Hono.prototype)['request']
export type ClientRequestOptions<T = unknown> = keyof T extends never
? {
headers?:
| Record<string, string>
| (() => Record<string, string> | Promise<Record<string, string>>)
fetch?: typeof fetch | HonoRequest
}
: {
headers: T | (() => T | Promise<T>)
fetch?: typeof fetch | HonoRequest
}
export type ClientRequest<S extends Schema> = {
[M in keyof S]: S[M] extends { input: infer R; output: infer O }
? R extends object
? HasRequiredKeys<R> extends true
? (args: R, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
: (args?: R, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
: never
: never
} & {
$url: (
arg?: S[keyof S] extends { input: infer R }
? R extends { param: infer P }
? { param: P }
: {}
: {}
) => URL
} & {
// WebSocket
$ws: S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } }
? S['$get'] extends { input: infer I }
? (args?: Omit<I, 'json'>) => WebSocket
: never
: never
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type BlankRecordToNever<T> = T extends any
? T extends null
? null
: keyof T extends never
? never
: T
: never
export interface ClientResponse<T> {
readonly body: ReadableStream | null
readonly bodyUsed: boolean
ok: boolean
status: number
statusText: string
headers: Headers
url: string
redirect(url: string, status: number): Response
clone(): Response
json(): Promise<BlankRecordToNever<T>>
text(): Promise<string>
blob(): Promise<Blob>
formData(): Promise<FormData>
arrayBuffer(): Promise<ArrayBuffer>
}
export interface Response extends ClientResponse<unknown> {}
export type Fetch<T> = (
args?: InferRequestType<T>,
opt?: ClientRequestOptions
) => Promise<ClientResponse<InferResponseType<T>>>
export type InferResponseType<T> = T extends (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args: any | undefined,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options: any | undefined
) => Promise<ClientResponse<infer O>>
? O
: never
export type InferRequestType<T> = T extends (
args: infer R,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options: any | undefined
) => Promise<ClientResponse<unknown>>
? NonNullable<R>
: never
export type InferRequestOptionsType<T> = T extends (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args: any,
options: infer R
) => Promise<ClientResponse<unknown>>
? NonNullable<R>
: never
type PathToChain<
Path extends string,
E extends Schema,
Original extends string = ''
> = Path extends `/${infer P}`
? PathToChain<P, E, Path>
: Path extends `${infer P}/${infer R}`
? { [K in P]: PathToChain<R, E, Original> }
: {
[K in Path extends '' ? 'index' : Path]: ClientRequest<
E extends Record<string, unknown> ? E[Original] : never
>
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Client<T> = T extends Hono<any, infer S, any>
? S extends Record<infer K, Schema>
? K extends string
? PathToChain<K, S>
: never
: never
: never
export type Callback = (opts: CallbackOptions) => unknown
interface CallbackOptions {
path: string[]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args: any[]
}
export type ObjectType<T = unknown> = {
[key: string]: T
}