Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/apollo-datasource-rest/src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {
protected parseBody(response: Response): Promise<object | string> {
const contentType = response.headers.get('Content-Type');
if (
// As one might expect, a "204 No Content" is empty! This means there
// isn't enough to `JSON.parse`, and trying will result in an error.
response.status !== 204 &&
contentType &&
(contentType.startsWith('application/json') ||
contentType.startsWith('application/hal+json'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,24 @@ describe('RESTDataSource', () => {

expect(data).toEqual('bar');
});

it('returns data as a string when response status code is 204 no content', async () => {
const dataSource = new class extends RESTDataSource {
baseURL = 'https://api.example.com';

getFoo() {
return this.get('');
}
}();

dataSource.httpCache = httpCache;

fetch.mockResponseOnce('', { 'Content-Type': 'application/json' }, 204);

const data = await dataSource.getFoo();

expect(data).toEqual('');
});
});

describe('memoization', () => {
Expand Down