-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathky.javascript.txt
60 lines (51 loc) · 3.59 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
┏━━━━━━━━┓
┃ KY ┃
┗━━━━━━━━┛
ALTERNATIVES ==> #See HTTP client summary
KY ==> #Browser|Node.js
#Version 1.7.5
KY('URL'|REQ[, OPTS])->PROMISE_RES#Wrapper around fetch(...)
OPTS.* #Like fetch(...)
OPTS.prefixUrl #STR. Prepended to 'URL'
OPTS.searchParams #STR|SEARCHPARAMS|ITERABLE. Same as new URLSearchParams() argument
OPTS.json #OBJ. Instead of OPTS.body
#Sets Content-Type: application/json [C]
OPTS.parseJson #FUNC(STR)->VAL (def: JSON.parse())
OPTS.stringifyJson #FUNC(VAL)->STR (def: JSON.stringify())
OPTS.throwHttpErrors #BOOL (def: true). Throw HTTPERROR is !RES.ok or if no more retries left
OPTS.timeout #NUM (def: 10000, in ms). Uses ABORT-CONTROLLER when available
#Throws TIMEOUTERROR
OPTS.onDownloadProgress #FUNC(OBJ) with OBJ:
# - percent 0-1 (0 if no Content-Length [S])
# - transferredBytes NUM
# - totalBytes NUM (0 if no Content-Length [S])
OPTS.fetch #FUNC(...). Custom fetch
OPTS.retry #Either:
# - RETRY_OPTS
# - NUM: same as {limit: NUM}
#If 413|429|503 with Retry-After [S] or RateLimit-Reset [S], use it instead
RETRY_OPTS.limit #NUM (def: 2). Number of retries
RETRY_OPTS.methods #STR_ARR (def: ['get', 'put', 'head', 'delete', 'options', 'trace'])
RETRY_OPTS.statusCodes #NUM_ARR (def: [408, 413, 429, 500, 502, 503, 504])
RETRY_OPTS.afterStatusCodes #NUM_ARR (def: [413, 429, 503])
RETRY_OPTS.maxRetryAfter #NUM (in secs). Ignore Retry-After [S] and RateLimit-Reset [S] if above this
RETRY_OPTS.backoffLimit #NUM (in ms, def: undefined). Stop when reaching this amount
RETRY_OPTS.delay #FUNC(NUM)->NUM2 (in ms):
# - timeout between each retry
# - NUM is attempts count, 1-based
# - def: double on each retry, starting at 300ms
OPTS.hooks #HOOKS_OPTS
HOOKS_OPTS.beforeRequest #ARR of FUNC(REQ, OPTS)[->[PROMISE_]_REQ|RES]
#Can return KY.stop to stop retry
HOOKS_OPTS.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
HOOKS_OPTS.afterResponse #ARR of FUNC(REQ, OPTS, RES)->[PROMISE_]RES
HOOKS_OPTS.beforeError #ARR of FUNC(ERROR)->ERROR
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[(OPTS)])->KY #Add to defaults OPTS
TimeoutError #Name 'TimeoutError', request REQ
HTTPError #Name 'HTTPError', request REQ, response RES, options OPTS