|
|
@ -8,19 +8,17 @@ const safeMethods = new Set(['GET', 'HEAD', 'OPTIONS', 'TRACE']); |
|
|
|
// fetch wrapper, use below method name functions and the `data` option to pass in data
|
|
|
|
// fetch wrapper, use below method name functions and the `data` option to pass in data
|
|
|
|
// which will automatically set an appropriate headers. For json content, only object
|
|
|
|
// which will automatically set an appropriate headers. For json content, only object
|
|
|
|
// and array types are currently supported.
|
|
|
|
// and array types are currently supported.
|
|
|
|
export function request(url, {method = 'GET', headers = {}, data, body, ...other} = {}) { |
|
|
|
export function request(url, {method = 'GET', data, headers = {}, ...other} = {}) { |
|
|
|
let contentType; |
|
|
|
let body, contentType; |
|
|
|
if (!body) { |
|
|
|
if (data instanceof FormData || data instanceof URLSearchParams) { |
|
|
|
if (data instanceof FormData || data instanceof URLSearchParams) { |
|
|
|
body = data; |
|
|
|
body = data; |
|
|
|
} else if (isObject(data) || Array.isArray(data)) { |
|
|
|
} else if (isObject(data) || Array.isArray(data)) { |
|
|
|
contentType = 'application/json'; |
|
|
|
contentType = 'application/json'; |
|
|
|
body = JSON.stringify(data); |
|
|
|
body = JSON.stringify(data); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const headersMerged = new Headers({ |
|
|
|
const headersMerged = new Headers({ |
|
|
|
...(!safeMethods.has(method.toUpperCase()) && {'x-csrf-token': csrfToken}), |
|
|
|
...(!safeMethods.has(method) && {'x-csrf-token': csrfToken}), |
|
|
|
...(contentType && {'content-type': contentType}), |
|
|
|
...(contentType && {'content-type': contentType}), |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -31,8 +29,8 @@ export function request(url, {method = 'GET', headers = {}, data, body, ...other |
|
|
|
return fetch(url, { |
|
|
|
return fetch(url, { |
|
|
|
method, |
|
|
|
method, |
|
|
|
headers: headersMerged, |
|
|
|
headers: headersMerged, |
|
|
|
...(body && {body}), |
|
|
|
|
|
|
|
...other, |
|
|
|
...other, |
|
|
|
|
|
|
|
...(body && {body}), |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|