Skip to content

Commit

Permalink
fix: Reject API calls that return a HTTP status that is not 2xx
Browse files Browse the repository at this point in the history
This should allow consumers to properly handle any 400 / 500 http errors
  • Loading branch information
joerideg committed Sep 23, 2024
1 parent 43af51d commit 7b261ea
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/features/autosuggest/autosuggest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Configuration } from '../../shared/configuration.type';
import { SUGGEST_ENDPOINT_PROD } from '../../shared/constants';
import { logAPICall } from '../../shared/logger';
import { typedFetch } from '../../shared/typed-fetch';
import { buildApiUrl } from '../../shared/url-builders';
import type { AutosuggestOptions } from './autosuggest-options.type';
import type { SuggestFixedOptions } from './suggest-fixed-options.type';
Expand Down Expand Up @@ -34,6 +35,5 @@ export async function autoSuggest(

logAPICall('autoSuggest', configuration, options, FIXED_OPTIONS, defaults, queryParams, url);

const data = await fetch(url);
return data.json() as Promise<SuggestResponse>;
return typedFetch<SuggestResponse>(url);
}
19 changes: 7 additions & 12 deletions src/features/recs-pathways/widgets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Configuration } from '../../shared/configuration.type';
import { WIDGET_ENDPOINT_PROD } from '../../shared/constants';
import { logAPICall } from '../../shared/logger';
import { typedFetch } from '../../shared/typed-fetch';
import { buildApiUrl } from '../../shared/url-builders';
import type {
CategoryWidgetOptions,
Expand All @@ -27,8 +28,7 @@ export async function getGlobalWidget(

logAPICall('getGlobalWidget', configuration, options, {}, {}, queryParams, url);

const data = await fetch(url);
return data.json() as Promise<WidgetResponse>;
return typedFetch<WidgetResponse>(url);
}

/**
Expand All @@ -46,8 +46,7 @@ export async function getCategoryWidget(

logAPICall(getCategoryWidget.name, configuration, options, {}, {}, queryParams, url);

const data = await fetch(url);
return data.json() as Promise<WidgetResponse>;
return typedFetch<WidgetResponse>(url);
}

/**
Expand All @@ -65,8 +64,7 @@ export async function getKeywordWidget(

logAPICall('getKeywordWidget', configuration, options, {}, {}, queryParams, url);

const data = await fetch(url);
return data.json() as Promise<WidgetResponse>;
return typedFetch<WidgetResponse>(url);
}

/**
Expand All @@ -84,8 +82,7 @@ export async function getItemWidget(

logAPICall('getItemWidget', configuration, options, {}, {}, queryParams, url);

const data = await fetch(url);
return data.json() as Promise<WidgetResponse>;
return typedFetch<WidgetResponse>(url);
}

/**
Expand All @@ -106,8 +103,7 @@ export async function getPersonalizedWidget(

logAPICall('getPersonalizedWidget', configuration, options, {}, {}, queryParams, url);

const data = await fetch(url);
return data.json() as Promise<WidgetResponse>;
return typedFetch<WidgetResponse>(url);
}

/**
Expand All @@ -128,6 +124,5 @@ export async function getRecentlyViewedWidget(

logAPICall('getRecentlyViewedWidget', configuration, options, {}, {}, queryParams, url);

const data = await fetch(url);
return data.json() as Promise<WidgetResponse>;
return typedFetch<WidgetResponse>(url);
}
20 changes: 0 additions & 20 deletions src/features/search/bestseller/bestseller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,6 @@ describe('Bestseller API', () => {
q: 'testQuery',
});

test('fetch call with correct URL', async () => {
const expectedUrl = new URL(config.searchEndpoint);
expectedUrl.searchParams.set('q', 'testQuery');
expectedUrl.searchParams.set('request_type', 'search');
expectedUrl.searchParams.set('search_type', 'bestseller');
expectedUrl.searchParams.set('fl', 'pid');
expectedUrl.searchParams.set('start', '0');

await mockRequest(
bestseller,
[config, searchOptions],
[
http.get(config.searchEndpoint, ({ request }) => {
expect(request.url).toBe(expectedUrl.toString());
return HttpResponse.json(createSearchResponseMock());
}),
],
);
});

test('checks that config and searchOptions are added to the searchParams in the request URL', async () => {
const { account_id, domain_key, _br_uid_2, url, q, fl, start, rows } = {
...config,
Expand Down
4 changes: 2 additions & 2 deletions src/features/search/bestseller/bestseller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Configuration } from '../../../shared/configuration.type';
import { SEARCH_ENDPOINT_PROD } from '../../../shared/constants';
import { logAPICall } from '../../../shared/logger';
import { typedFetch } from '../../../shared/typed-fetch';
import { buildApiUrl } from '../../../shared/url-builders';
import type { SearchRequestParameters } from '../search-request.type';
import type { SearchResponse } from '../search-response.type';
Expand Down Expand Up @@ -40,6 +41,5 @@ export async function bestseller(

logAPICall('bestseller', configuration, options, FIXED_OPTIONS, defaults, queryParams, url);

const data = await fetch(url);
return data.json() as Promise<SearchResponse>;
return typedFetch<SearchResponse>(url);
}
4 changes: 2 additions & 2 deletions src/features/search/category-search/category-search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Configuration } from '../../../shared/configuration.type';
import { SEARCH_ENDPOINT_PROD } from '../../../shared/constants';
import { logAPICall } from '../../../shared/logger';
import { typedFetch } from '../../../shared/typed-fetch';
import { buildApiUrl } from '../../../shared/url-builders';
import type { SearchRequestParameters } from '../search-request.type';
import type { SearchResponse } from '../search-response.type';
Expand Down Expand Up @@ -42,6 +43,5 @@ export async function categorySearch(

logAPICall('categorySearch', configuration, options, FIXED_OPTIONS, defaults, queryParams, url);

const data = await fetch(url);
return data.json() as Promise<SearchResponse>;
return typedFetch<SearchResponse>(url);
}
4 changes: 2 additions & 2 deletions src/features/search/content-search/content-search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Configuration } from '../../../shared/configuration.type';
import { SEARCH_ENDPOINT_PROD } from '../../../shared/constants';
import { logAPICall } from '../../../shared/logger';
import { typedFetch } from '../../../shared/typed-fetch';
import { buildApiUrl } from '../../../shared/url-builders';
import type { ContentSearchRequestParameters } from '../search-request.type';
import type { SearchResponse } from '../search-response.type';
Expand Down Expand Up @@ -48,6 +49,5 @@ export async function contentSearch(

logAPICall('contentSearch', configuration, options, FIXED_OPTIONS, defaults, queryParams, url);

const data = await fetch(url);
return data.json() as Promise<SearchResponse>;
return typedFetch<SearchResponse>(url);
}
4 changes: 2 additions & 2 deletions src/features/search/product-search/product-search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Configuration } from '../../../shared/configuration.type';
import { SEARCH_ENDPOINT_PROD } from '../../../shared/constants';
import { logAPICall } from '../../../shared/logger';
import { typedFetch } from '../../../shared/typed-fetch';
import { buildApiUrl } from '../../../shared/url-builders';
import { SearchRequestParameters } from '../search-request.type';
import type { SearchResponse } from '../search-response.type';
Expand Down Expand Up @@ -42,6 +43,5 @@ export async function productSearch(

logAPICall('productSearch', configuration, options, FIXED_OPTIONS, defaults, queryParams, url);

const data = await fetch(url);
return data.json() as Promise<SearchResponse>;
return typedFetch<SearchResponse>(url);
}
9 changes: 9 additions & 0 deletions src/shared/typed-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export async function typedFetch<T>(url: URL): Promise<T> {
const data = await fetch(url);

if (!data.ok) {
throw new Error(`HTTP error! status: ${data.status}`);
}

return data.json() as T;
}

0 comments on commit 7b261ea

Please sign in to comment.