-
-
Notifications
You must be signed in to change notification settings - Fork 662
/
Copy pathclient.ts
204 lines (182 loc) · 5.65 KB
/
client.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import type { Hono } from '../hono'
import type { FormValue, ValidationTargets } from '../types'
import { serialize } from '../utils/cookie'
import type { UnionToIntersection } from '../utils/types'
import type { Callback, Client, ClientRequestOptions } from './types'
import {
buildSearchParams,
deepMerge,
mergePath,
removeIndexString,
replaceUrlParam,
replaceUrlProtocol,
} from './utils'
const createProxy = (callback: Callback, path: string[]) => {
const proxy: unknown = new Proxy(() => {}, {
get(_obj, key) {
if (typeof key !== 'string' || key === 'then') {
return undefined
}
return createProxy(callback, [...path, key])
},
apply(_1, _2, args) {
return callback({
path,
args,
})
},
})
return proxy
}
class ClientRequestImpl {
private url: string
private method: string
private queryParams: URLSearchParams | undefined = undefined
private pathParams: Record<string, string> = {}
private rBody: BodyInit | undefined
private cType: string | undefined = undefined
constructor(url: string, method: string) {
this.url = url
this.method = method
}
fetch = async (
args?: ValidationTargets<FormValue> & {
param?: Record<string, string>
},
opt?: ClientRequestOptions
) => {
if (args) {
if (args.query) {
this.queryParams = buildSearchParams(args.query)
}
if (args.form) {
const form = new FormData()
for (const [k, v] of Object.entries(args.form)) {
if (Array.isArray(v)) {
for (const v2 of v) {
form.append(k, v2)
}
} else {
form.append(k, v)
}
}
this.rBody = form
}
if (args.json) {
this.rBody = JSON.stringify(args.json)
this.cType = 'application/json'
}
if (args.param) {
this.pathParams = args.param
}
}
let methodUpperCase = this.method.toUpperCase()
const headerValues: Record<string, string> = {
...args?.header,
...(typeof opt?.headers === 'function' ? await opt.headers() : opt?.headers),
}
if (args?.cookie) {
const cookies: string[] = []
for (const [key, value] of Object.entries(args.cookie)) {
cookies.push(serialize(key, value, { path: '/' }))
}
headerValues['Cookie'] = cookies.join(',')
}
if (this.cType) {
headerValues['Content-Type'] = this.cType
}
const headers = new Headers(headerValues ?? undefined)
let url = this.url
url = removeIndexString(url)
url = replaceUrlParam(url, this.pathParams)
if (this.queryParams) {
url = url + '?' + this.queryParams.toString()
}
methodUpperCase = this.method.toUpperCase()
const setBody = !(methodUpperCase === 'GET' || methodUpperCase === 'HEAD')
// Pass URL string to 1st arg for testing with MSW and node-fetch
return (opt?.fetch || fetch)(url, {
body: setBody ? this.rBody : undefined,
method: methodUpperCase,
headers: headers,
...opt?.init,
})
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const hc = <T extends Hono<any, any, any>>(
baseUrl: string,
options?: ClientRequestOptions
) =>
createProxy(function proxyCallback(opts) {
const parts = [...opts.path]
// allow calling .toString() and .valueOf() on the proxy
if (parts.at(-1) === 'toString') {
if (parts.at(-2) === 'name') {
// e.g. hc().somePath.name.toString() -> "somePath"
return parts.at(-3) || ''
}
// e.g. hc().somePath.toString()
return proxyCallback.toString()
}
if (parts.at(-1) === 'valueOf') {
if (parts.at(-2) === 'name') {
// e.g. hc().somePath.name.valueOf() -> "somePath"
return parts.at(-3) || ''
}
// e.g. hc().somePath.valueOf()
return proxyCallback
}
let method = ''
if (/^\$/.test(parts.at(-1) as string)) {
const last = parts.pop()
if (last) {
method = last.replace(/^\$/, '')
}
}
const path = parts.join('/')
const url = mergePath(baseUrl, path)
if (method === 'url') {
let result = url
if (opts.args[0]) {
if (opts.args[0].param) {
result = replaceUrlParam(url, opts.args[0].param)
}
if (opts.args[0].query) {
result = result + '?' + buildSearchParams(opts.args[0].query).toString()
}
}
return new URL(result)
}
if (method === 'ws') {
const webSocketUrl = replaceUrlProtocol(
opts.args[0] && opts.args[0].param ? replaceUrlParam(url, opts.args[0].param) : url,
'ws'
)
const targetUrl = new URL(webSocketUrl)
const queryParams: Record<string, string | string[]> | undefined = opts.args[0]?.query
if (queryParams) {
Object.entries(queryParams).forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach((item) => targetUrl.searchParams.append(key, item))
} else {
targetUrl.searchParams.set(key, value)
}
})
}
const establishWebSocket = (...args: ConstructorParameters<typeof WebSocket>) => {
if (options?.webSocket !== undefined && typeof options.webSocket === 'function') {
return options.webSocket(...args)
}
return new WebSocket(...args)
}
return establishWebSocket(targetUrl.toString())
}
const req = new ClientRequestImpl(url, method)
if (method) {
options ??= {}
const args = deepMerge<ClientRequestOptions>(options, { ...opts.args[1] })
return req.fetch(opts.args[0], args)
}
return req
}, []) as UnionToIntersection<Client<T>>