Skip to content

Commit

Permalink
Merge pull request #31 from concordnow/raw
Browse files Browse the repository at this point in the history
Add `raw` support
  • Loading branch information
rstudner authored Sep 29, 2021
2 parents 798aee8 + 9d88f82 commit 0341a43
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
26 changes: 21 additions & 5 deletions addon/mixins/fetch-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ export default Mixin.create({
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',

/**
* Make a fetch request
* Make a fetch request, returning the raw fetch response
*
* Unlike ember-ajax this method returns the raw fetch response not an xhr
* @method request
* @param {string} url The url for the request
* @param {object} options The options hash for the request
* @return {Promise<*>}
* @return {object} containing {response, requestOptions, builtURL}
*/
async request(url, options = {}) {
async raw(url, options = {}) {
const hash = this.options(url, options);
const method = hash.method || hash.type || 'GET';
const requestOptions = {
Expand Down Expand Up @@ -114,15 +116,29 @@ export default Mixin.create({
if (timeout) {
clearTimeout(timeout);
}
response = await parseJSON(response);

return this._handleResponse(response, requestOptions, builtURL);
return {response, requestOptions, builtURL};
} catch(error) {
// TODO: do we want to just throw here or should some errors be okay?
throw error;
}
},

/**
* Make a fetch request, ignoring the raw fetch response and dealing only with
* the response content
* @method request
* @param {string} url The url for the request
* @param {object} options The options hash for the request
* @return {Promise<*>}
*/
async request(url, options = {}) {
let {response, requestOptions, builtURL} = await this.raw(url, options);
response = await parseJSON(response);

return this._handleResponse(response, requestOptions, builtURL);
},

/**
* Determine whether the headers should be added for this request
*
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/mixins/fetch-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,39 @@ module('Unit | Mixin | fetch-request', function(hooks) {
assert.equal(options.contentType, defaultContentType);
});

test('raw() response.post === options.data.post', function(assert) {
const service = FetchRequest.create();
const url = '/posts';
const title = 'Title';
const description = 'Some description.';
const contentType = 'application/json';
const customHeader = 'My custom header';
const options = {
data: {
post: { title, description }
}
};
const serverResponse = [
200,
{ 'Content-Type': contentType,
'Custom-Header': customHeader },
JSON.stringify(options.data)
];

this.server.get(url, () => serverResponse);

const rawPromise = service.raw(url, options);

return rawPromise.then(function({ response }) {
assert.equal(response.status, 200);
assert.equal(response.headers.get('Custom-Header'), customHeader);
assert.equal(response.headers.get('Content-Type'), contentType);
return response.json();
}).then((json) => {
assert.deepEqual(json.post, options.data.post);
});
});

test('post() response.post === options.data.post', function(assert) {
const service = FetchRequest.create();
const url = '/posts';
Expand Down

0 comments on commit 0341a43

Please sign in to comment.