-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.ts
32 lines (29 loc) · 983 Bytes
/
index.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
import { AspidaClient, AspidaRequest, HttpMethod, headersToObject } from 'aspida'
import ky, { Options } from 'ky'
export default (client = ky, options?: Options): AspidaClient => ({
fetch<T, U>(url: string, method: HttpMethod, request?: AspidaRequest) {
const send = <V>(fn: (res: Response) => Promise<V>) => async () => {
const res = await client(url, {
...options,
method,
body: request?.body,
searchParams: request?.query,
headers: { ...options?.headers, ...request?.headers }
})
return {
status: res.status,
headers: headersToObject<U>(res.headers),
body: res.body,
data: await fn(res)
}
}
return {
send: send(() => Promise.resolve(null)),
json: send(res => res.json()),
text: send(res => res.text()),
arrayBuffer: send(res => res.arrayBuffer()),
blob: send(res => res.blob()),
formData: send(res => res.formData())
}
}
})