This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
972: Refactor requests to allow for non Normandy requests r=jaredkerim a=rehandalal Blocks #943 r? Co-authored-by: Rehan Dalal <rehandalal@gmail.com>
- Loading branch information
Showing
7 changed files
with
139 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,8 @@ | ||
import APIClient, { APIError } from 'console/utils/api'; | ||
import APIClient from 'console/utils/api'; | ||
|
||
describe('APIClient util', () => { | ||
it('should work', () => { | ||
const wrapper = () => new APIClient(); | ||
expect(wrapper).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('APIError', () => { | ||
it('should identify as an APIError', () => { | ||
const errMessage = 'Danger, Will Robinson! Danger!'; | ||
const errData = { danger: true }; | ||
let didThrow = false; | ||
|
||
try { | ||
throw new APIError(errMessage, errData); | ||
} catch (e) { | ||
didThrow = true; | ||
expect(e.name).toBe('APIError'); | ||
expect(e.message).toBe(errMessage); | ||
expect(e.data).toBe(errData); | ||
} | ||
|
||
expect(didThrow).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
export async function request(url, options = {}, extraHeaders = {}) { | ||
let queryString = ''; | ||
|
||
const headers = new Headers(); | ||
headers.append('Accept', 'application/json'); | ||
if (!(options.body && options.body instanceof FormData)) { | ||
headers.append('Content-Type', 'application/json'); | ||
} | ||
|
||
for (let headerName in extraHeaders) { | ||
headers.append(headerName, extraHeaders[headerName]); | ||
} | ||
|
||
const settings = { | ||
headers, | ||
credentials: 'same-origin', | ||
method: 'GET', | ||
...options, | ||
}; | ||
|
||
// Convert `data` to `body` or querystring if necessary. | ||
if ('data' in settings) { | ||
if ('body' in settings) { | ||
throw new Error('Only pass one of `settings.data` and `settings.body`.'); | ||
} | ||
|
||
if (['GET', 'HEAD'].includes(settings.method.toUpperCase())) { | ||
const queryStringData = Object.keys(settings.data).map( | ||
key => `${key}=${encodeURIComponent(settings.data[key])}`, | ||
); | ||
queryString = `?${queryStringData.join('&')}`; | ||
} else { | ||
settings.body = JSON.stringify(settings.data); | ||
} | ||
|
||
delete settings.data; | ||
} | ||
|
||
const response = await fetch(`${url}${queryString}`, settings); | ||
|
||
if (!response.ok) { | ||
let message; | ||
let data = {}; | ||
let err; | ||
|
||
try { | ||
data = await response.json(); | ||
message = data.detail || response.statusText; | ||
} catch (error) { | ||
message = error.message; | ||
err = error; | ||
} | ||
|
||
data = { ...data, status: response.status }; | ||
|
||
throw new RequestError(message, data, err); | ||
} | ||
|
||
if (response.status !== 204) { | ||
return response.json(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export function RequestError(message, data = {}) { | ||
this.data = data; | ||
this.message = message; | ||
this.stack = Error().stack; | ||
} | ||
RequestError.prototype = Object.create(Error.prototype); | ||
RequestError.prototype.name = 'RequestError'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters