Skip to content

Commit

Permalink
Remove redundant response.headers check
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Mar 14, 2023
1 parent a1ddb49 commit fb671e3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/exchanges/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('on success', () => {
beforeEach(() => {
fetch.mockResolvedValue({
status: 200,
headers: { get: () => 'application/json' },
text: vi.fn().mockResolvedValue(response),
});
});
Expand Down Expand Up @@ -91,6 +92,7 @@ describe('on error', () => {
beforeEach(() => {
fetch.mockResolvedValue({
status: 400,
headers: { get: () => 'application/json' },
text: vi.fn().mockResolvedValue(JSON.stringify({})),
});
});
Expand Down Expand Up @@ -126,6 +128,7 @@ describe('on error', () => {
it('ignores the error when a result is available', async () => {
fetch.mockResolvedValue({
status: 400,
headers: { get: () => 'application/json' },
text: vi.fn().mockResolvedValue(response),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,9 @@ exports[`on success > uses the mock fetch if given 1`] = `
{
"type": "return",
"value": {
"headers": {
"get": [Function],
},
"status": 200,
"text": [MockFunction spy] {
"calls": [
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/internal/fetchSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('on success', () => {
beforeEach(() => {
fetch.mockResolvedValue({
status: 200,
headers: { get: () => 'application/json' },
text: vi.fn().mockResolvedValue(response),
});
});
Expand All @@ -70,6 +71,7 @@ describe('on success', () => {
const fetchOptions = {};
const fetcher = vi.fn().mockResolvedValue({
status: 200,
headers: { get: () => 'application/json' },
text: vi.fn().mockResolvedValue(response),
});

Expand Down Expand Up @@ -99,6 +101,7 @@ describe('on error', () => {
fetch.mockResolvedValue({
status: 400,
statusText: 'Forbidden',
headers: { get: () => 'application/json' },
text: vi.fn().mockResolvedValue('{}'),
});
});
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/internal/fetchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ async function* fetchOperation(
// if a teardown comes in immediately
await Promise.resolve();
response = await (operation.context.fetch || fetch)(url, fetchOptions);
const contentType =
(response.headers && response.headers.get('Content-Type')) || '';
const contentType = response.headers.get('Content-Type') || '';
if (/text\//i.test(contentType)) {
const text = await response.text();
return yield makeErrorResult(operation, new Error(text), response);
Expand Down Expand Up @@ -87,8 +86,7 @@ async function* fetchOperation(
} catch (_error) {}

if (next.startsWith('--') || (payload && !payload.hasNext)) {
if (!result)
yield (result = makeResult(operation, {}, response));
if (!result) yield (result = makeResult(operation, {}, response));
break chunks;
}
}
Expand All @@ -103,7 +101,8 @@ async function* fetchOperation(

yield makeErrorResult(
operation,
(response!.status < 200 || response!.status >= 300) && response!.statusText
(response!.status < 200 || response!.status >= 300) &&
response!.statusText
? new Error(response!.statusText)
: error,
response!
Expand Down

0 comments on commit fb671e3

Please sign in to comment.