-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
259 lines (251 loc) Β· 7.63 KB
/
index.js
File metadata and controls
259 lines (251 loc) Β· 7.63 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* Ira Fetch - Vanilla JS Fetch API wrapper with goodies π
* @author Denny Portillo<[email protected]>
* @license MIT
* @see https://github.com/d3portillo/ira
* @version 0.0.6
*/
function f(forkConfig = {}) {
const IRA_CONFIG = {
headers: {},
debug: false,
parseBlob: true,
baseURL: undefined,
}
const IRA_METHODS = {
get: "GET",
put: "PUT",
post: "POST",
head: "HEAD",
delete: "DELETE",
connect: "CONNECT",
options: "OPTIONS",
trace: "TRACE",
}
const IRA_LOG = "IraFetch >>>"
const deepify = (obj = {}) => {
const json = {}
Object.keys(obj).forEach((prop) => {
const V = obj[prop]
json[prop] = typeof V != "string" ? JSON.stringify(V) : V
})
return json
}
/**
* Ira.get Method
* @param { String } url
* @param { IRA_CONFIG } extra
*/
function ira(url = "/", extra) {
return makeIraFetch(IRA_METHODS.get, {
acceptsBody: false,
})(url, extra)
}
function makeIraFetch(method = "POST", opt = { acceptsBody: true }) {
/**
* Ira Response Object
* @typedef { Object } IraResponse
* @property {{ json: Object, text: String, blob: ?Blob }} data - Posible parsed response body
* @property { Boolean } ok - Response status <= 300
* @property { Number } status
* @property { String } statusText
* @property { Number } statusCode
* @property { ?Error } error - Null if nothing wrong
*/
/**
* @param { String } url - URL To fetch from
* @param {{ headers: {}, body: ?String, params: {}, debug: Boolean, parseBlob: Boolean }} extra - Your normal fetch opts
* @return { Promise<IraResponse> }
*/
const fetchPromise = (url = "/", extra = {}) => {
const config = {
...IRA_CONFIG,
...extra,
...forkConfig,
...ira._config,
}
let { headers = {}, body = "", params = {} } = extra
headers = deepify({ ...config.headers, ...headers })
const cType = headers["Content-Type"] || headers["Content-type"] || ""
if (cType.includes("json")) body = deepify(body)
try {
let { baseURL, parseBlob } = config
url = baseURL ? `${baseURL}${url}` : url
const paramsChain = new URLSearchParams(params).toString()
if (paramsChain.length) {
const lastChar = url.charAt(url.length - 1)
if (lastChar != "/") url = `${url}/`
url = `${url}?${paramsChain}`
}
const { fetch } = window
if (!fetch) throw new Error("Not inside a browser")
if (!url) throw new Error("URL not provided")
return new Promise((send) => {
ira.__exec_on.request({
url,
statusCode: 200,
method,
headers: new Headers(headers),
config,
})
if (config.debug) console.info(`${IRA_LOG} URL='${url}' >>> SENT β‘`)
fetch(url, {
...extra,
method,
headers,
...(opt.acceptsBody ? { body } : {}),
})
.then((response) => {
const { ok, status, statusText } = response
const clone = response.clone()
Promise.all([
response.text(),
parseBlob ? clone.blob() : null,
]).then(([t, b]) => {
if (typeof b == "string") {
;[t, b] = [b, t]
}
let json = {}
try {
json = JSON.parse(t)
if (typeof json == "string") json = {}
} catch (_) {}
if (parseBlob) {
const BIN_TYPE = /video|image|audio|ogg|pdf|rar|zip|font|7z|flash|x-/g
// Posible blob types to binaries - this will be too ugly parsed
// to utf-8 string from resposne.text
const isBinary = BIN_TYPE.test(b.type)
const isUTF8 = /utf8|utf-8/gi.test(b.type)
if (isBinary && !isUTF8) t = ""
}
const data = { json, text: t, blob: b }
const statusCode = status
send({
data,
ok,
status,
statusText,
statusCode,
error: null,
})
ira.__exec_on.response({
url,
statusCode,
method,
headers: response.headers,
config,
})
if (config.debug) {
console.info(`π ${IRA_LOG} URL='${url}' >>> RESPONSE: `, {
config,
responseData: { data },
})
}
})
})
.catch((error) => {
console.error(
`β ${IRA_LOG} - Got error on request, URL='${url}' >>> ${error}`,
{ config }
)
send({
data: { json: {}, text: "", blob: null },
ok: false,
status: 500,
statusText: error,
statusCode: 500,
error,
})
})
})
} catch (e) {
console.error(`${IRA_LOG} >>> ${e}`)
}
}
return fetchPromise
}
ira.get = makeIraFetch(IRA_METHODS.get, {
acceptsBody: false,
})
ira.head = makeIraFetch(IRA_METHODS.head, {
acceptsBody: false,
})
ira.delete = makeIraFetch(IRA_METHODS.delete, {
acceptsBody: false,
})
ira.post = makeIraFetch()
ira.put = makeIraFetch(IRA_METHODS.put)
ira.connect = makeIraFetch(IRA_METHODS.connect)
ira.options = makeIraFetch(IRA_METHODS.options)
ira.trace = makeIraFetch(IRA_METHODS.trace)
/**
* Acces your current Ira config
* @type { IRA_CONFIG }
*/
ira._config = {}
/**
* Sets persisten config headers or body for future requests
* @param { IRA_CONFIG } config
*/
ira.config = (config = {}) => {
if (!Object.entries(config).length) {
// We reset config if *empty { } , is provided
config = { ...IRA_CONFIG }
}
ira._config = { ...config }
}
/**
* Returns a base64 String from a blob
* @param { Blob } blob
* @returns { Promise<String> }
*/
ira.blobToBase64 = (blob) => {
return new Promise((cb) => {
try {
const rdr = new FileReader()
rdr.onload = () => cb(rdr.result)
rdr.readAsDataURL(blob)
} catch (e) {
console.error(`${IRA_LOG} - Got error on Blob parse >>> ${e}`)
cb("")
}
})
}
ira.__exec_on = {
request: () => null,
response: () => null,
}
/**
* @callback cbFunction
* @param {{ url: String, statusCode: ?Number, method: String, headers: {}, config: {} }} callbackObject
*/
/**
* Add your custom callbacks on response and request events
* @param {("request" | "response")} event
* @param { cbFunction } callback - Request or Response Objects Callback
*/
ira.on = (event, callback) => {
if (typeof callback == "function" && ira.__exec_on[event]) {
ira.__exec_on[event] = callback
}
}
/**
* Sets config and returns a custom Ira Fork
* @param { IRA_CONFIG } config
* @returns { ira }
*/
ira.extend = (conf = {}) => {
conf.headers = conf.headers ? conf.headers : {}
const iH = ira._config.headers
conf.headers = iH ? { ...iH, ...conf.headers } : conf.headers
const __ira = f(conf)
__ira.config({ ...ira._config, ...conf })
return __ira
}
return ira
}
"object" == typeof exports && "object" == typeof module
? (module.exports = f())
: "object" == typeof exports
? (exports.ira = f())
: (this.ira = f())