Skip to content

Commit

Permalink
Merge pull request #1000 from goodeggs/renovate/goodeggs-toolkit-7.x
Browse files Browse the repository at this point in the history
Update dependency @goodeggs/toolkit to v7
  • Loading branch information
serhalp authored Oct 10, 2022
2 parents f2e7188 + 8b7f049 commit ceaddd2
Show file tree
Hide file tree
Showing 4 changed files with 845 additions and 1,970 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@babel/preset-env": "^7.19.3",
"@babel/preset-typescript": "^7.18.6",
"@babel/register": "^7.18.9",
"@goodeggs/toolkit": "^4.2.0",
"@goodeggs/toolkit": "^7.0.0",
"@goodeggs/tsconfig": "^1.0.0",
"@types/isomorphic-fetch": "^0.0.36",
"@types/lodash": "^4.14.186",
Expand Down Expand Up @@ -71,7 +71,7 @@
"postversion": "git push --follow-tags",
"tdd": "yarn run test:mocha --watch",
"test": "yarn run todos && yarn run lint && yarn run typecheck && yarn run test:mocha:coverage",
"test:mocha": "yarn run test:mocha:glob '**/{,*.}test.ts'",
"test:mocha": "yarn run test:mocha:glob 'src/**/{,*.}test.ts'",
"test:mocha:coverage": "yarn run nyc --report-dir=coverage --temp-directory=coverage/.nyc_output --reporter=lcov --reporter=text-summary yarn run test:mocha",
"test:mocha:glob": "NODE_ENV=test yarn run mocha --require @babel/polyfill --require .register.js --extension ts",
"todos": "yarn run todos:glob '**/*.{js,jsx,ts,tsx}'",
Expand Down
6 changes: 4 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'isomorphic-fetch';

import {describe, it, beforeEach, afterEach} from 'mocha';
import {expect, useSinonSandbox} from 'goodeggs-test-helpers';
import fake from 'fake-eggs';
import nock from 'nock';

import jsonFetch, {retriers} from '.';
Expand Down Expand Up @@ -406,7 +408,7 @@ describe('jsonFetch', function () {
});

it('rejects all other inputs', async function () {
expect(retriers.is5xx(new Error())).to.equal(false);
expect(retriers.is5xx(new Error(fake.sentence()))).to.equal(false);
expect(
retriers.is5xx(
new Response('', {
Expand Down Expand Up @@ -498,7 +500,7 @@ describe('jsonFetch', function () {

describe('.isNetworkError', function () {
it('accepts any errors', async function () {
expect(retriers.isNetworkError(new Error())).to.equal(true);
expect(retriers.isNetworkError(new Error(fake.sentence()))).to.equal(true);
});

it('rejects any non errors', async function () {
Expand Down
18 changes: 13 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface JsonFetchOptions extends Omit<RequestInit, 'body'> {
shouldRetry?: (responseOrError: Response | Error) => boolean;
retry?: Parameters<typeof promiseRetry>[0];
timeout?: number;
expectedStatuses?: Array<number>;
expectedStatuses?: number[];
onRequestStart?: (opts: OnRequestOptions) => void;
onRequestEnd?: (opts: OnRequestEndOptions) => void;
}
Expand Down Expand Up @@ -126,7 +126,12 @@ async function createJsonFetchResponse(response: Response): Promise<JsonFetchRes
};
}

function createErrorResponse(response: Response, responseText: string) {
interface ErrorResponse {
status: number;
statusText: string;
text: string;
}
function createErrorResponse(response: Response, responseText: string): ErrorResponse {
// do not include headers as they potentially contain sensitive information
return {
status: response.status,
Expand All @@ -148,14 +153,14 @@ function getResponseBody(response: Response, responseText: string): JSON | null

function isApplicationJson(headers: Headers): boolean {
const responseContentType = headers.get('Content-Type') ?? '';
return /application\/json/.test(responseContentType);
return responseContentType.includes('application/json');
}

function assertExpectedStatus<
T extends {
readonly status: number;
},
>(expectedStatuses: Array<number> | null | undefined, jsonFetchResponse: T): void {
>(expectedStatuses: number[] | null | undefined, jsonFetchResponse: T): void {
if (Array.isArray(expectedStatuses) && !expectedStatuses.includes(jsonFetchResponse.status)) {
const err = new FetchUnexpectedStatusError(
`Unexpected fetch response status ${jsonFetchResponse.status}`,
Expand All @@ -168,13 +173,16 @@ function assertExpectedStatus<
}
}

interface ErrorRequestData extends Omit<JsonFetchOptions, 'headers'> {
url: string;
}
function getErrorRequestData({
requestUrl,
requestOptions,
}: {
requestUrl: string;
requestOptions: JsonFetchOptions;
}) {
}): ErrorRequestData {
const data = {...requestOptions, url: requestUrl};
// do not include headers as they potentially contain sensitive information
delete data.headers;
Expand Down
Loading

0 comments on commit ceaddd2

Please sign in to comment.