(method, _url, data, headers = {})
| 8 | |
| 9 | export default { |
| 10 | _request(method, _url, data, headers = {}) { |
| 11 | const options = { |
| 12 | credentials: Project.cookieAuthEnabled ? 'include' : undefined, |
| 13 | headers: { |
| 14 | 'Accept': 'application/json', |
| 15 | ...headers, |
| 16 | }, |
| 17 | method, |
| 18 | timeout: 60000, |
| 19 | } |
| 20 | const isExternal = !_url.startsWith(Project.api) |
| 21 | |
| 22 | if (method !== 'get') |
| 23 | options.headers['Content-Type'] = 'application/json; charset=utf-8' |
| 24 | |
| 25 | if ( |
| 26 | (this.token && !isExternal && !Project.cookieAuthEnabled) || |
| 27 | (this.token && isExternal && method !== 'get') |
| 28 | ) { |
| 29 | // add auth tokens to headers of all requests |
| 30 | options.headers.AUTHORIZATION = `Token ${this.token}` |
| 31 | } |
| 32 | |
| 33 | let url = _url |
| 34 | const parts = _url.split(/(https?:\/\/)?(.*?:.*?)@/) |
| 35 | if (parts.length === 4) { |
| 36 | url = (parts[1] || 'http://') + parts[parts.length - 1] |
| 37 | options.headers.AUTHORIZATION = `Basic ${btoa(parts[parts.length - 2])}` |
| 38 | } |
| 39 | |
| 40 | if (data) { |
| 41 | if (method === 'get') { |
| 42 | const qs = getQueryString(data) |
| 43 | url += url.indexOf('?') !== -1 ? `&${qs}` : `?${qs}` |
| 44 | } else { |
| 45 | options.body = JSON.stringify(data) |
| 46 | } |
| 47 | } else if (method === 'post' || method === 'put') { |
| 48 | options.body = '{}' |
| 49 | } |
| 50 | |
| 51 | return fetch(url, options) |
| 52 | .then((response) => this.status(response, isExternal)) |
| 53 | .then((response) => { |
| 54 | // always return json |
| 55 | let contentType = response.headers.get('content-type') |
| 56 | if (!contentType) { |
| 57 | contentType = response.headers.get('Content-Type') |
| 58 | } |
| 59 | if (contentType && contentType.indexOf('application/json') !== -1) { |
| 60 | return response.json() |
| 61 | } |
| 62 | return {} |
| 63 | }) |
| 64 | .then((response) => { |
| 65 | return response |
| 66 | }) |
| 67 | }, |
nothing calls this directly
no test coverage detected