* Issues a request. * @public * @template T * @param {string | Options} url * @param {Options} [config] * @param {any} [_method] * @param {any} [_data] * @returns {Promise >}
(url, config, _method, _data)
| 149 | * @returns {Promise<Response<T>>} |
| 150 | */ |
| 151 | function redaxios(url, config, _method, _data) { |
| 152 | if (typeof url !== 'string') { |
| 153 | config = url; |
| 154 | url = config.url; |
| 155 | } |
| 156 | |
| 157 | const response = /** @type {Response<any>} */ ({ config }); |
| 158 | |
| 159 | /** @type {Options} */ |
| 160 | const options = deepMerge(defaults, config); |
| 161 | |
| 162 | /** @type {Headers} */ |
| 163 | const customHeaders = {}; |
| 164 | |
| 165 | let data = _data || options.data; |
| 166 | |
| 167 | (options.transformRequest || []).map((f) => { |
| 168 | data = f(data, options.headers) || data; |
| 169 | }); |
| 170 | |
| 171 | if (data && typeof data === 'object' && typeof data.append !== 'function') { |
| 172 | data = JSON.stringify(data); |
| 173 | customHeaders['content-type'] = 'application/json'; |
| 174 | } |
| 175 | |
| 176 | const m = |
| 177 | typeof document !== 'undefined' && document.cookie.match(RegExp('(^|; )' + options.xsrfCookieName + '=([^;]*)')); |
| 178 | if (m) customHeaders[options.xsrfHeaderName] = decodeURIComponent(m[2]); |
| 179 | |
| 180 | if (options.auth) { |
| 181 | customHeaders.authorization = options.auth; |
| 182 | } |
| 183 | |
| 184 | if (options.baseURL) { |
| 185 | url = url.replace(/^(?!.*\/\/)\/?(.*)$/, options.baseURL + '/$1'); |
| 186 | } |
| 187 | |
| 188 | if (options.params) { |
| 189 | const divider = ~url.indexOf('?') ? '&' : '?'; |
| 190 | const query = options.paramsSerializer |
| 191 | ? options.paramsSerializer(options.params) |
| 192 | : new URLSearchParams(options.params); |
| 193 | url += divider + query; |
| 194 | } |
| 195 | |
| 196 | const fetchFunc = options.fetch || fetch; |
| 197 | |
| 198 | return fetchFunc(url, { |
| 199 | method: _method || options.method, |
| 200 | body: data, |
| 201 | headers: deepMerge(options.headers, customHeaders, true), |
| 202 | credentials: options.withCredentials ? 'include' : 'same-origin' |
| 203 | }).then((res) => { |
| 204 | for (const i in res) { |
| 205 | if (typeof res[i] != 'function') response[i] = res[i]; |
| 206 | } |
| 207 | |
| 208 | const ok = options.validateStatus ? options.validateStatus(res.status) : res.ok; |
no test coverage detected