-
Notifications
You must be signed in to change notification settings - Fork 126
/
fetch.ts
170 lines (145 loc) · 5.39 KB
/
fetch.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
import destr from 'destr'
import { withBase, withQuery } from 'ufo'
import type { Fetch, RequestInfo, RequestInit, Response } from './types'
import { createFetchError } from './error'
import { isPayloadMethod, isJSONSerializable, detectResponseType, ResponseType, MappedType } from './utils'
export interface CreateFetchOptions {
// eslint-disable-next-line no-use-before-define
defaults?: FetchOptions
fetch: Fetch
Headers: typeof Headers
}
export type FetchRequest = RequestInfo
export interface FetchResponse<T> extends Response { data?: T }
export interface SearchParams { [key: string]: any }
export interface FetchContext<T = any, R extends ResponseType = ResponseType> {
request: FetchRequest
// eslint-disable-next-line no-use-before-define
options: FetchOptions<R>,
response?: FetchResponse<T>
error?: Error
}
export interface FetchOptions<R extends ResponseType = ResponseType> extends Omit<RequestInit, 'body'> {
baseURL?: string
body?: RequestInit['body'] | Record<string, any>
params?: SearchParams
parseResponse?: (responseText: string) => any
responseType?: R
response?: boolean
retry?: number | false
onRequest?(ctx: FetchContext): Promise<void>
onRequestError?(ctx: FetchContext & { error: Error }): Promise<void>
onResponse?(ctx: FetchContext & { response: FetchResponse<R> }): Promise<void>
onResponseError?(ctx: FetchContext & { response: FetchResponse<R> }): Promise<void>
}
export interface $Fetch {
<T = any, R extends ResponseType = 'json'>(request: FetchRequest, opts?: FetchOptions<R>): Promise<MappedType<R, T>>
raw<T = any, R extends ResponseType = 'json'>(request: FetchRequest, opts?: FetchOptions<R>): Promise<FetchResponse<MappedType<R, T>>>
create(defaults: FetchOptions): $Fetch
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
const retryStatusCodes = new Set([
408, // Request Timeout
409, // Conflict
425, // Too Early
429, // Too Many Requests
500, // Internal Server Error
502, // Bad Gateway
503, // Service Unavailable
504 // Gateway Timeout
])
export function createFetch (globalOptions: CreateFetchOptions): $Fetch {
const { fetch, Headers } = globalOptions
function onError (ctx: FetchContext): Promise<FetchResponse<any>> {
// Retry
if (ctx.options.retry !== false) {
const retries = typeof ctx.options.retry === 'number'
? ctx.options.retry
: (isPayloadMethod(ctx.options.method) ? 0 : 1)
const responseCode = (ctx.response && ctx.response.status) || 500
if (retries > 0 && retryStatusCodes.has(responseCode)) {
return $fetchRaw(ctx.request, {
...ctx.options,
retry: retries - 1
})
}
}
// Throw normalized error
const err = createFetchError(ctx.request, ctx.error, ctx.response)
// Only available on V8 based runtimes (https://v8.dev/docs/stack-trace-api)
if (Error.captureStackTrace) {
Error.captureStackTrace(err, $fetchRaw)
}
throw err
}
const $fetchRaw: $Fetch['raw'] = async function $fetchRaw (_request, _opts = {}) {
const ctx: FetchContext = {
request: _request,
options: { ...globalOptions.defaults, ..._opts },
response: undefined,
error: undefined
}
if (ctx.options.onRequest) {
await ctx.options.onRequest(ctx)
}
if (typeof ctx.request === 'string') {
if (ctx.options.baseURL) {
ctx.request = withBase(ctx.request, ctx.options.baseURL)
}
if (ctx.options.params) {
ctx.request = withQuery(ctx.request, ctx.options.params)
}
if (ctx.options.body && isPayloadMethod(ctx.options.method)) {
if (isJSONSerializable(ctx.options.body)) {
ctx.options.body = JSON.stringify(ctx.options.body)
ctx.options.headers = new Headers(ctx.options.headers)
if (!ctx.options.headers.has('content-type')) {
ctx.options.headers.set('content-type', 'application/json')
}
if (!ctx.options.headers.has('accept')) {
ctx.options.headers.set('accept', 'application/json')
}
}
}
}
ctx.response = await fetch(ctx.request, ctx.options as RequestInit).catch(async (error) => {
ctx.error = error
if (ctx.options.onRequestError) {
await ctx.options.onRequestError(ctx as any)
}
return onError(ctx)
})
const responseType =
(ctx.options.parseResponse ? 'json' : ctx.options.responseType) ||
detectResponseType(ctx.response.headers.get('content-type') || '')
// We override the `.json()` method to parse the body more securely with `destr`
if (responseType === 'json') {
const data = await ctx.response.text()
const parseFn = ctx.options.parseResponse || destr
ctx.response.data = parseFn(data)
} else {
ctx.response.data = await ctx.response[responseType]()
}
if (ctx.options.onResponse) {
await ctx.options.onResponse(ctx as any)
}
if (!ctx.response.ok) {
if (ctx.options.onResponseError) {
await ctx.options.onResponseError(ctx as any)
}
}
return ctx.response.ok ? ctx.response : onError(ctx)
}
const $fetch = function $fetch (request, opts) {
return $fetchRaw(request, opts).then(r => r.data)
} as $Fetch
$fetch.raw = $fetchRaw
$fetch.create = (defaultOptions = {}) => createFetch({
...globalOptions,
defaults: {
...globalOptions.defaults,
...defaultOptions
}
})
return $fetch
}