Skip to content

Commit

Permalink
drop fetchResponse in favor of fetchSafe (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes authored May 14, 2023
1 parent e89c57a commit 1d74a97
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 26 deletions.
2 changes: 1 addition & 1 deletion import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
"@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
"std/": "https://deno.land/[email protected]/"
}
}
}
4 changes: 2 additions & 2 deletions packs/vtex/loaders/legacy/productListingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
legacyFacetToFilter,
toProduct,
} from "deco-sites/std/packs/vtex/utils/transform.ts";
import { fetchAPI, fetchResponse } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI, fetchSafe } from "deco-sites/std/utils/fetch.ts";
import type { LegacyFacets, LegacyProduct } from "../../types.ts";

const MAX_ALLOWED_PAGES = 500;
Expand Down Expand Up @@ -148,7 +148,7 @@ const loader = async (
fmap && fParams.set("map", fmap);

const [vtexProductsResponse, vtexFacets] = await Promise.all([
fetchResponse<LegacyProduct[]>(
fetchSafe(
`${search.products.search.term(getTerm(term, map))}?${pParams}`,
{ withProxyCache: true, headers: withSegmentCookie(segment) },
),
Expand Down
2 changes: 1 addition & 1 deletion schemas.gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -10208,4 +10208,4 @@
}
}
}
}
}
29 changes: 7 additions & 22 deletions utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ const toProxyCache = async (
return proxyUrl;
};

export const fetchSafe: typeof fetch = async (input, init) => {
const response = await fetch(input, init);
export const fetchSafe = async (
input: string | Request | URL,
init?: RequestInit & { withProxyCache?: boolean },
) => {
const url = init?.withProxyCache ? await toProxyCache(input, init) : input;
const response = await fetch(url, init);

if (response.ok) {
return response;
Expand All @@ -51,29 +55,10 @@ export const fetchAPI = async <T>(

headers.set("accept", "application/json");

const url = init?.withProxyCache ? await toProxyCache(input, init) : input;
const response = await fetchSafe(url, {
const response = await fetchSafe(input, {
...init,
headers,
});

return response.json();
};

// TODO: Shortcuted, refactor this into fetchAPI =D
export const fetchResponse = async <T>(
input: string | Request | URL,
init?: RequestInit & { withProxyCache?: boolean },
): Promise<Response> => {
const headers = new Headers(init?.headers);

headers.set("accept", "application/json");

const url = init?.withProxyCache ? await toProxyCache(input, init) : input;
const response = await fetchSafe(url, {
...init,
headers,
});

return response;
};

0 comments on commit 1d74a97

Please sign in to comment.