-
Notifications
You must be signed in to change notification settings - Fork 16
/
ky.javascript.txt
61 lines (53 loc) · 4.96 KB
/
ky.javascript.txt
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
KY
ALTERNATIVES ==> #See HTTP client summary
KY ==> #Browser|Node.js
#Version 1.3.0
KY('URL'|REQ[, OPTS])->PROMISE_RES#Wrapper around fetch(...)
#Additional OPTS (beyond fetch(...) ones):
# - prefixUrl STR: prepended to 'URL'
# - searchParams STR|SEARCHPARAMS|ITERABLE (same as new URLSearchParams() argument)
# - json OBJ: instead of OPTS.body. Sets Content-Type: application/json [C]
# - parseJson FUNC(STR)->VAL (def: JSON.parse())
# - stringifyJson FUNC(VAL)->STR (def: JSON.stringify())
# - throwHttpErrors BOOL (def: true):
# - throw HTTPERROR is !RES.ok or if no more retries left
# - timeout NUM (def: 10000, in ms)
# - uses ABORT-CONTROLLER when available
# - throws TIMEOUTERROR
# - retry:
# - either:
# - OBJ:
# - limit NUM (def: 2): number of retries
# - methods STR_ARR (def: ['get', 'put', 'head', 'delete', 'options', 'trace'])
# - statusCodes NUM_ARR (def: [408, 413, 429, 500, 502, 503, 504])
# - maxRetryAfter NUM (in secs): ignore Retry-After [S] if above this
# - backoffLimit NUM (in ms, def: undefined): stop when reaching this amount
# - delay(NUM)->NUM2 (in ms):
# - timeout between each retry
# - NUM is attempts count, 1-based
# - def: double on each retry, starting at 300ms
# - NUM: same as {limit: NUM}
# - if 413|429|503 with Retry-After [S], use it instead
# - onDownloadProgress(OBJ):
# - OBJ:
# - percent 0-1 (0 if no Content-Length [S])
# - transferredBytes NUM
# - totalBytes NUM (0 if no Content-Length [S])
# - hooks OBJ:
# - beforeRequest ARR of FUNC(REQ, OPTS)[->[PROMISE_]_REQ|RES]
# - can return KY.stop to stop retry
# - beforeRetry ARR of FUNC(OBJ)[->PROMISE]
# - can return KY.stop to stop retry
# - OBJ: request REQ, options OPTS, error ERROR (including ERROR.response RES), retryCount NUM
# - afterResponse ARR of FUNC(REQ, OPTS, RES)->[PROMISE_]RES
# - beforeError ARR of FUNC(ERROR)->ERROR
# - fetch(...): custom fetch
PROMISE.json|text|formData| #Like (await PROMISE).json|...().
arrayBuffer|blob()->VAL #Also sets Accept: application/json|text/*|multipart/form-data|*/*|*/* [C]
KY[.METHOD](...) #Same with OPTS.method set
KY.create(OPTS)->KY #Override defaults OPTS
KY.extend(OPTS)->KY #Add to defaults OPTS
TimeoutError #Name 'TimeoutError', request REQ
HTTPError #Name 'HTTPError', request REQ, response RES, options OPTS