-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
core.ts
143 lines (142 loc) · 4.17 KB
/
core.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
import { mix, extractContentType, isLikelyJsonMime } from "./utils.js"
import { JSON_MIME, CONTENT_TYPE_HEADER, CATCHER_FALLBACK } from "./constants.js"
import { resolver } from "./resolver.js"
import config from "./config.js"
import type { Wretch, ErrorType } from "./types.js"
export const core: Wretch = {
_url: "",
_options: {},
_config: config,
_catchers: new Map(),
_resolvers: [],
_deferred: [],
_middlewares: [],
_addons: [],
addon(addon) {
return { ...this, _addons: [...this._addons, addon], ...addon.wretch }
},
errorType(errorType: ErrorType) {
return {
...this,
_config: {
...this._config,
errorType
}
}
},
polyfills(polyfills, replace = false) {
return {
...this,
_config: {
...this._config,
polyfills: replace ? polyfills : mix(this._config.polyfills, polyfills)
}
}
},
url(_url, replace = false) {
if (replace)
return { ...this, _url }
const split = this._url.split("?")
return {
...this,
_url: split.length > 1 ?
split[0] + _url + "?" + split[1] :
this._url + _url
}
},
options(options, replace = false) {
return { ...this, _options: replace ? options : mix(this._options, options) }
},
headers(headerValues) {
const headers =
!headerValues ? {} :
Array.isArray(headerValues) ? Object.fromEntries(headerValues) :
"entries" in headerValues ? Object.fromEntries((headerValues as Headers).entries()) :
headerValues
return { ...this, _options: mix(this._options, { headers }) }
},
accept(headerValue) {
return this.headers({ Accept: headerValue })
},
content(headerValue) {
return this.headers({ [CONTENT_TYPE_HEADER]: headerValue })
},
auth(headerValue) {
return this.headers({ Authorization: headerValue })
},
catcher(errorId, catcher) {
const newMap = new Map(this._catchers)
newMap.set(errorId, catcher)
return { ...this, _catchers: newMap }
},
catcherFallback(catcher) {
return this.catcher(CATCHER_FALLBACK, catcher)
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
resolve<R = unknown>(resolver, clear: boolean = false) {
return { ...this, _resolvers: clear ? [resolver] : [...this._resolvers, resolver] }
},
defer(callback, clear: boolean = false) {
return {
...this,
_deferred: clear ? [callback] : [...this._deferred, callback]
}
},
middlewares(middlewares, clear = false) {
return {
...this,
_middlewares: clear ? middlewares : [...this._middlewares, ...middlewares]
}
},
fetch(method: string = this._options.method, url = "", body = null) {
let base = this.url(url).options({ method })
// "Jsonify" the body if it is an object and if it is likely that the content type targets json.
const contentType = extractContentType(base._options.headers)
const formDataClass = this._config.polyfill("FormData", false)
const jsonify =
typeof body === "object" &&
!(formDataClass && body instanceof formDataClass) &&
(!base._options.headers || !contentType || isLikelyJsonMime(contentType))
base =
!body ? base :
jsonify ? base.json(body, contentType) :
base.body(body)
return resolver(
base
._deferred
.reduce((acc: Wretch, curr) => curr(acc, acc._url, acc._options), base)
)
},
get(url = "") {
return this.fetch("GET", url)
},
delete(url = "") {
return this.fetch("DELETE", url)
},
put(body, url = "") {
return this.fetch("PUT", url, body)
},
post(body, url = "") {
return this.fetch("POST", url, body)
},
patch(body, url = "") {
return this.fetch("PATCH", url, body)
},
head(url = "") {
return this.fetch("HEAD", url)
},
opts(url = "") {
return this.fetch("OPTIONS", url)
},
body(contents) {
return { ...this, _options: { ...this._options, body: contents } }
},
json(jsObject, contentType) {
const currentContentType = extractContentType(this._options.headers)
return this.content(
contentType ||
isLikelyJsonMime(currentContentType) && currentContentType ||
JSON_MIME
).body(JSON.stringify(jsObject))
}
}