Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: drop fetchResponse in favor of fetchSafe #64

Merged
merged 1 commit into from
May 14, 2023
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
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;
};