Skip to content

Commit

Permalink
split fetch/fetch.server to avoid breaking (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes authored Sep 1, 2023
1 parent 85aaba8 commit f3d16fe
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 39 deletions.
2 changes: 1 addition & 1 deletion commerce/butterCMS/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Account } from "$live/blocks/account.ts";
import { fetchAPI } from "../../utils/fetch.ts";
import { fetchAPI } from "../../utils/fetch.server.ts";
import { CategoriesData, Page, PostData, PostsData } from "./types.ts";

export interface Locale {
Expand Down
2 changes: 1 addition & 1 deletion commerce/nuvemShop/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Account } from "./types.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";
import {
ProductBaseNuvemShop,
ProductSearchParams,
Expand Down
2 changes: 1 addition & 1 deletion commerce/occ/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Account } from "$live/blocks/account.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";
import {
CategorySearchResult,
OccProductPage,
Expand Down
2 changes: 1 addition & 1 deletion commerce/shopify/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Account } from "$live/blocks/account.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";
import { Product } from "./types.ts";

const gql = (x: TemplateStringsArray) => x.toString().trim();
Expand Down
2 changes: 1 addition & 1 deletion commerce/yourViews/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";
import { Ratings, Reviews } from "./types.ts";

export type ClientYourViews = ReturnType<typeof createClient>;
Expand Down
2 changes: 1 addition & 1 deletion packs/font/loaders/font.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchSafe } from "deco-sites/std/utils/fetch.ts";
import { fetchSafe } from "deco-sites/std/utils/fetch.server.ts";

const ALLOWED_ORIGINS = new Set(["https://fonts.gstatic.com"]);

Expand Down
2 changes: 1 addition & 1 deletion packs/linxImpulse/loaders/autocompletes/popular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "deco-sites/std/packs/linxImpulse/utils/transform.ts";
import { paths } from "deco-sites/std/packs/linxImpulse/utils/path.ts";
import type { Context } from "deco-sites/std/packs/linxImpulse/accounts/linxImpulse.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";

interface AutocompletesPopularReponse {
requestId: string;
Expand Down
2 changes: 1 addition & 1 deletion packs/linxImpulse/loaders/autocompletes/suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "deco-sites/std/packs/linxImpulse/utils/transform.ts";
import { paths } from "deco-sites/std/packs/linxImpulse/utils/path.ts";
import type { Context } from "deco-sites/std/packs/linxImpulse/accounts/linxImpulse.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";
import { HttpError } from "deco-sites/std/utils/HttpError.ts";

interface AutocompletesResponse {
Expand Down
2 changes: 1 addition & 1 deletion packs/linxImpulse/loaders/pages/recommendations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "deco-sites/std/packs/linxImpulse/utils/transform.ts";
import { paths } from "deco-sites/std/packs/linxImpulse/utils/path.ts";
import type { Context } from "deco-sites/std/packs/linxImpulse/accounts/linxImpulse.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";

export interface Props {
/**
Expand Down
2 changes: 1 addition & 1 deletion packs/linxImpulse/loaders/products/similarItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "deco-sites/std/packs/linxImpulse/utils/transform.ts";
import { paths } from "deco-sites/std/packs/linxImpulse/utils/path.ts";
import type { Context } from "deco-sites/std/packs/linxImpulse/accounts/linxImpulse.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";
import { HttpError } from "deco-sites/std/utils/HttpError.ts";

export interface Props {
Expand Down
2 changes: 1 addition & 1 deletion packs/linxImpulse/loaders/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "deco-sites/std/packs/linxImpulse/utils/transform.ts";
import { paths } from "deco-sites/std/packs/linxImpulse/utils/path.ts";
import type { Context } from "deco-sites/std/packs/linxImpulse/accounts/linxImpulse.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";
import { HttpError } from "deco-sites/std/utils/HttpError.ts";

const sortOptions = [
Expand Down
2 changes: 1 addition & 1 deletion packs/vnda/actions/cart/addItem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpError } from "deco-sites/std/utils/HttpError.ts";
import { fetchAPI, fetchSafe } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI, fetchSafe } from "deco-sites/std/utils/fetch.server.ts";
import { getSetCookies, setCookie } from "std/http/mod.ts";
import type { Context } from "../../accounts/vnda.ts";
import { DECO_USER_AGENT, USER_AGENT_HEADER } from "../../constants.ts";
Expand Down
2 changes: 1 addition & 1 deletion packs/vnda/actions/cart/setShippingAddress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";
import type { Context } from "../../accounts/vnda.ts";
import type { Cart, Shipping } from "../../types.ts";
import { paths } from "../../utils/paths.ts";
Expand Down
2 changes: 1 addition & 1 deletion packs/vnda/actions/cart/updateCoupon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";
import type { Context } from "../../accounts/vnda.ts";
import type { Cart, Coupon } from "../../types.ts";
import { paths } from "../../utils/paths.ts";
Expand Down
2 changes: 1 addition & 1 deletion packs/vnda/actions/cart/updateItem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";
import type { Context } from "../../accounts/vnda.ts";
import type { Cart, OrderForm } from "../../types.ts";
import { paths } from "../../utils/paths.ts";
Expand Down
2 changes: 1 addition & 1 deletion packs/vnda/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SortOption } from "deco-sites/std/commerce/types.ts";
import { fetchAPI, fetchSafe } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI, fetchSafe } from "deco-sites/std/utils/fetch.server.ts";
import { Account as ConfigVNDA } from "./accounts/vnda.ts";
import {
BASE_URL_PROD,
Expand Down
2 changes: 1 addition & 1 deletion packs/vnda/loaders/cart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpError } from "deco-sites/std/utils/HttpError.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.server.ts";
import type { Context } from "../accounts/vnda.ts";
import type { Cart, OrderForm, RelatedItem } from "../types.ts";
import { paths } from "../utils/paths.ts";
Expand Down
31 changes: 31 additions & 0 deletions utils/fetch.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as fetcher from "https://denopkg.com/deco-cx/[email protected]/utils/fetch.ts";

export interface FetchOptions {
withProxyCache?: boolean;
}

export const fetchSafe = (
input: string | Request | URL,
init?: RequestInit & FetchOptions,
) =>
fetcher.fetchSafe(input, {
...init,
deco: init?.withProxyCache
? {
cache: "stale-while-revalidate",
}
: undefined,
});

export const fetchAPI = <T>(
input: string | Request | URL,
init?: RequestInit & FetchOptions,
): Promise<T> =>
fetcher.fetchAPI(input, {
...init,
deco: init?.withProxyCache
? {
cache: "stale-while-revalidate",
}
: undefined,
});
5 changes: 4 additions & 1 deletion utils/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
assertSpyCalls,
spy,
} from "https://deno.land/[email protected]/testing/mock.ts";
import { CONNECTION_CLOSED_MESSAGE, retryExceptionOr500 } from "./fetch.ts";
import {
CONNECTION_CLOSED_MESSAGE,
retryExceptionOr500,
} from "./fetch.server.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

Deno.test("retry handler", async (t) => {
Expand Down
42 changes: 23 additions & 19 deletions utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import * as fetcher from "https://denopkg.com/deco-cx/[email protected]/utils/fetch.ts";
import { HttpError } from "deco-sites/std/utils/HttpError.ts";

export interface FetchOptions {
withProxyCache?: boolean;
}

export const fetchSafe = (
export const fetchSafe = async (
input: string | Request | URL,
init?: RequestInit & FetchOptions,
) =>
fetcher.fetchSafe(input, {
...init,
deco: init?.withProxyCache
? {
cache: "stale-while-revalidate",
}
: undefined,
});
) => {
const response = await fetch(input, init);

if (response.ok) {
return response;
}

console.error(`${input}\n`, response, `\n`);
throw new HttpError(response.status, `${input}`);
};

export const fetchAPI = <T>(
export const fetchAPI = async <T>(
input: string | Request | URL,
init?: RequestInit & FetchOptions,
): Promise<T> =>
fetcher.fetchAPI(input, {
): Promise<T> => {
const headers = new Headers(init?.headers);

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

const response = await fetchSafe(input, {
...init,
deco: init?.withProxyCache
? {
cache: "stale-while-revalidate",
}
: undefined,
headers,
});

return response.json();
};
4 changes: 2 additions & 2 deletions utils/fetchVTEX.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { FetchOptions } from "./fetch.ts";
import { FetchOptions } from "./fetch.server.ts";
import {
fetchAPI as fetchAPIBase,
fetchSafe as fetchSafeBase,
} from "./fetch.ts";
} from "./fetch.server.ts";

const processFetch = async (
_fetch: (
Expand Down

0 comments on commit f3d16fe

Please sign in to comment.