-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaxios-controller.js
More file actions
158 lines (144 loc) · 3.79 KB
/
axios-controller.js
File metadata and controls
158 lines (144 loc) · 3.79 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
const buildControllerDefaultOpts = {
unwrap: true
};
/**
* Unwrap an axios result object
* @param {Promise<any>} promise
* @returns
*/
function unwrap(promise) {
if (!(promise instanceof Promise)) {
return promise;
}
return promise
.then(res => {
return res ? res.data : null;
})
.catch(err => {
if (err.response) {
return Promise.reject(new Error(err.message, { cause: err.response.data }));
} else {
return Promise.reject(err);
}
});
}
/**
* @param {string} url
* @returns
*/
function removeLeadingSlashes(url) {
if (typeof url !== 'string') return '';
url = url.trimStart();
return url.replace(/^\/+/, '');
}
/**
* @param {string} url
* @returns
*/
function removeTrailingSlashes(url) {
if (typeof url !== 'string') return '';
url = url.trimStart();
return url.replace(/\/+$/, '');
}
/**
* @param {string} url
* @returns
*/
function removeLeadingAndTrailingSlashes(url) {
url = removeLeadingSlashes(url);
url = removeTrailingSlashes(url);
return url;
}
/**
* @param {string} url
* @returns
*/
function appendSlash(url) {
if (typeof url !== 'string') return '/';
url = url.trimEnd();
if (!url.endsWith('/')) return url + '/';
return url;
}
/**
* @param {string} url
* @returns
*/
function toAbsoluteUrl(url) {
const a = document.createElement('a');
a.href = url;
return a.href;
}
function createEmptyProxy() {
return {};
}
/**
*
* @param {AxiosControllerHttp} proxy
* @param {{ unwrap?: boolean }} [opts]
* @returns
*/
function build(proxy, opts) {
opts = {
...buildControllerDefaultOpts,
...opts
};
/** @type {(controller: any, fn?: any) => any} */
return function (controller, fn) {
fn = fn || createEmptyProxy;
const ctrlProxy = {
get(url = '', ...args) {
return proxy.get(controller + '/' + url, ...args);
},
head(url = '', ...args) {
return proxy.head(controller + '/' + url, ...args);
},
post(url = '', ...args) {
if (typeof url !== 'string') {
args = [url];
url = '';
}
return proxy.post(controller + '/' + url, ...args);
},
put(url = '', ...args) {
if (typeof url !== 'string') {
args = [url];
url = '';
}
return proxy.put(controller + '/' + url, ...args);
},
patch(url = '', ...args) {
if (typeof url !== 'string') {
args = [url];
url = '';
}
return proxy.patch(controller + '/' + url, ...args);
},
delete(url = '', ...args) {
return proxy.delete(controller + '/' + url, ...args);
}
};
const ctrl = fn(ctrlProxy);
if (opts.unwrap) {
Object.entries(ctrl).forEach(([key, val]) => (ctrl[key] = (...args) => unwrap(val(...args))));
}
ctrl.getUri = (...args) => {
const url =
removeTrailingSlashes(controller) +
'/' +
args.map(u => removeLeadingAndTrailingSlashes(u)).join('/') +
'';
const baseUrl = removeTrailingSlashes(toAbsoluteUrl(proxy.defaults.baseURL));
return new URL(removeLeadingSlashes(url), appendSlash(baseUrl)).toString();
};
return ctrl;
};
}
module.exports = {
unwrap,
removeLeadingSlashes,
removeTrailingSlashes,
removeLeadingAndTrailingSlashes,
appendSlash,
buildController: build, // legacy
build
};