-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
77 lines (76 loc) · 2.59 KB
/
index.js
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
class CreateRequest {
constructor(endpoint, options) {
this.request = (body, baseUrl) =>
new Promise((resolve, reject) => {
const endpoint = baseUrl ? `${baseUrl}${this.endpoint}` : this.endpoint
const b = body ? { body: JSON.stringify(body) } : {}
try {
fetch(endpoint, Object.assign(Object.assign({}, this.options), b))
.then(async (res) => {
var _a
if (res.status === 200 && res.ok) {
try {
let parsedResponse
switch ((_a = this.options) === null || _a === void 0 ? void 0 : _a.responseType) {
case 'json':
parsedResponse = await res.json()
break
case 'text':
parsedResponse = await res.text()
break
default:
parsedResponse = await res.json()
break
}
resolve(parsedResponse)
} catch (parseError) {
console.log({ parseError })
reject(parseError)
}
} else {
reject(res)
}
})
.catch((err) => {
reject(err)
})
} catch (err) {
reject(err)
}
})
this.endpoint = endpoint
this.options = options
}
}
const APIConstructor = (reqs, options) => {
return (key, ...params) => {
const request = reqs[key]
if (!request) {
throw new Error(
`Request name ${key} is not available. Available request names are ${Object.keys(reqs).join(', ')}`,
)
}
return new Promise(async (resolve, reject) => {
var _a, _b, _c
try {
;(_a = options === null || options === void 0 ? void 0 : options.onRequestStart) === null || _a === void 0
? void 0
: _a.call(options, { requestName: key, params })
const res = await request.request(params, options === null || options === void 0 ? void 0 : options.baseUrl)
;(_b = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _b === void 0
? void 0
: _b.call(options, {
requestName: key,
response: res,
})
resolve(res)
} catch (err) {
;(_c = options === null || options === void 0 ? void 0 : options.onError) === null || _c === void 0
? void 0
: _c.call(options, err)
reject(err)
}
})
}
}
export { APIConstructor, CreateRequest }