Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Replaced local utils with graphql-client common client utils
Browse files Browse the repository at this point in the history
  • Loading branch information
melissaluu committed Nov 3, 2023
1 parent 7efc764 commit 745963d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 81 deletions.
17 changes: 0 additions & 17 deletions packages/storefront-api-client/.eslintrc.cjs

This file was deleted.

2 changes: 1 addition & 1 deletion packages/storefront-api-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"!node_modules"
],
"dependencies": {
"@shopify/graphql-client": "^0.4.0"
"@shopify/graphql-client": "^0.5.0"
},
"devDependencies": {
"@babel/core": "^7.21.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/storefront-api-client/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export const SDK_VARIANT_HEADER = "X-SDK-Variant";
export const SDK_VERSION_HEADER = "X-SDK-Version";
export const SDK_VARIANT_SOURCE_HEADER = "X-SDK-Variant-Source";

export const ERROR_PREFIX = "Storefront API Client:";
export const CLIENT = "Storefront API Client";
20 changes: 10 additions & 10 deletions packages/storefront-api-client/src/storefront-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import {
CustomFetchAPI,
RequestParams as GQLClientRequestParams,
getCurrentSupportedAPIVersions,
validateRequiredStoreDomain,
validateDomainAndGetStoreUrl,
validateApiVersion,
getDomain,
} from "@shopify/graphql-client";

import {
Expand All @@ -22,7 +21,7 @@ import {
DEFAULT_CONTENT_TYPE,
PUBLIC_ACCESS_TOKEN_HEADER,
PRIVATE_ACCESS_TOKEN_HEADER,
ERROR_PREFIX,
CLIENT,
} from "./constants";
import {
validateRequiredAccessTokens,
Expand Down Expand Up @@ -53,33 +52,34 @@ export function createStorefrontAPIClient({
)): StorefrontAPIClient {
const currentSupportedApiVersions = getCurrentSupportedAPIVersions();

validateRequiredStoreDomain(ERROR_PREFIX, storeDomain);
const storeUrl = validateDomainAndGetStoreUrl({
client: CLIENT,
storeDomain,
});
validateApiVersion({
errorPrefix: ERROR_PREFIX,
client: CLIENT,
currentSupportedApiVersions,
apiVersion,
});
validateRequiredAccessTokens(publicAccessToken, privateAccessToken);
validatePrivateAccessTokenUsage(privateAccessToken);

const cleanedStoreDomain = getDomain(storeDomain);

const generateApiUrl = (version?: string) => {
if (version) {
validateApiVersion({
errorPrefix: ERROR_PREFIX,
client: CLIENT,
currentSupportedApiVersions,
apiVersion: version,
});
}

const urlApiVersion = (version ?? apiVersion).trim();

return `https://${cleanedStoreDomain}/api/${urlApiVersion}/graphql.json`;
return `${storeUrl}/api/${urlApiVersion}/graphql.json`;
};

const config: SFAPIClientConfig = {
storeDomain: cleanedStoreDomain,
storeDomain: storeUrl,
apiVersion,
publicAccessToken: publicAccessToken ?? null,
privateAccessToken: privateAccessToken ?? null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createGraphQLClient, GraphQLClient } from "@shopify/graphql-client";
import { StorefrontAPIClient } from "types";

import { createStorefrontAPIClient } from "../storefront-api-client";
import { StorefrontAPIClient } from "../types";
import {
SDK_VARIANT_HEADER,
DEFAULT_SDK_VARIANT,
Expand Down Expand Up @@ -107,12 +107,11 @@ describe("Storefront API Client", () => {
expect(() =>
createStorefrontAPIClient({
...config,
// @ts-ignore
storeDomain: undefined,
storeDomain: undefined as any,
})
).toThrow(
new Error(
"Storefront API Client: a valid store domain must be provided"
'Storefront API Client: a valid store domain ("undefined") must be provided'
)
);
});
Expand All @@ -121,12 +120,11 @@ describe("Storefront API Client", () => {
expect(() =>
createStorefrontAPIClient({
...config,
// @ts-ignore
storeDomain: " ",
})
).toThrow(
new Error(
"Storefront API Client: a valid store domain must be provided"
'Storefront API Client: a valid store domain (" ") must be provided'
)
);
});
Expand All @@ -135,12 +133,11 @@ describe("Storefront API Client", () => {
expect(() =>
createStorefrontAPIClient({
...config,
// @ts-ignore
storeDomain: 123,
storeDomain: 123 as any,
})
).toThrow(
new Error(
"Storefront API Client: a valid store domain must be provided"
'Storefront API Client: a valid store domain ("123") must be provided'
)
);
});
Expand All @@ -153,7 +150,7 @@ describe("Storefront API Client", () => {
})
).toThrow(
new Error(
`Storefront API Client: the provided \`apiVersion\` (\`undefined\`) is invalid. Current supported API versions: ${mockApiVersions.join(
`Storefront API Client: the provided apiVersion ("undefined") is invalid. Current supported API versions: ${mockApiVersions.join(
", "
)}`
)
Expand All @@ -164,12 +161,11 @@ describe("Storefront API Client", () => {
expect(() =>
createStorefrontAPIClient({
...config,
// @ts-ignore
apiVersion: { year: 2022, month: 1 },
apiVersion: { year: 2022, month: 1 } as any,
})
).toThrow(
new Error(
`Storefront API Client: the provided \`apiVersion\` (\`[object Object]\`) is invalid. Current supported API versions: ${mockApiVersions.join(
`Storefront API Client: the provided apiVersion ("[object Object]") is invalid. Current supported API versions: ${mockApiVersions.join(
", "
)}`
)
Expand All @@ -187,18 +183,17 @@ describe("Storefront API Client", () => {
});

expect(consoleWarnSpy).toHaveBeenCalledWith(
`Storefront API Client: the provided \`apiVersion\` (\`2022-07\`) is deprecated or not supported. Current supported API versions: ${mockApiVersions.join(
`Storefront API Client: the provided apiVersion ("2022-07") is deprecated or not supported. Current supported API versions: ${mockApiVersions.join(
", "
)}`
);
});

it("throws an error when neither public and private access tokens are provided", () => {
expect(() =>
// @ts-ignore
createStorefrontAPIClient({
...config,
publicAccessToken: undefined,
publicAccessToken: undefined as any,
})
).toThrow(
new Error(
Expand All @@ -209,10 +204,9 @@ describe("Storefront API Client", () => {

it("throws an error when a private access token is provided in a browser environment (window is defined)", () => {
expect(() =>
// @ts-ignore
createStorefrontAPIClient({
...config,
publicAccessToken: undefined,
publicAccessToken: undefined as any,
privateAccessToken: "private-access-token",
})
).toThrow(
Expand All @@ -228,11 +222,10 @@ describe("Storefront API Client", () => {
.mockImplementation(() => undefined as any);

expect(() =>
// @ts-ignore
createStorefrontAPIClient({
...config,
privateAccessToken: "private-token",
})
} as any)
).toThrow(
new Error(
`Storefront API Client: only provide either a public or private access token`
Expand All @@ -247,7 +240,7 @@ describe("Storefront API Client", () => {
describe("client config", () => {
it("returns a config object that includes the provided store domain", () => {
const client = createStorefrontAPIClient(config);
expect(client.config.storeDomain).toBe(domain);
expect(client.config.storeDomain).toBe(`https://${domain}`);
});

it("returns a config object that includes the provided public access token and a null private access token", () => {
Expand Down Expand Up @@ -301,22 +294,22 @@ describe("Storefront API Client", () => {
expect(client.config.apiUrl).toBe(expectedAPIUrl);
});

// it("returns a config object that includes the secure API url constructed with the provided API version and a store domain that does not include a protocol", () => {
// const client = createStorefrontAPIClient({
// ...config,
// storeDomain: cleanedStoreDomain,
// });
// expect(client.config.apiUrl).toBe(expectedAPIUrl);
// });

// it("returns a config object that includes a valid API url constructed with the provided spaced out API version and a store domain", () => {
// const client = createStorefrontAPIClient({
// ...config,
// storeDomain: ` ${cleanedStoreDomain} `,
// apiVersion: ` ${config.apiVersion} `,
// });
// expect(client.config.apiUrl).toBe(expectedAPIUrl);
// });
it("returns a config object that includes the secure API url constructed with the provided API version and a store domain that does not include a protocol", () => {
const client = createStorefrontAPIClient({
...config,
storeDomain: cleanedStoreDomain,
});
expect(client.config.apiUrl).toBe(expectedAPIUrl);
});

it("returns a config object that includes a valid API url constructed with the provided spaced out API version and a store domain", () => {
const client = createStorefrontAPIClient({
...config,
storeDomain: ` ${cleanedStoreDomain} `,
apiVersion: ` ${config.apiVersion} `,
});
expect(client.config.apiUrl).toBe(expectedAPIUrl);
});
});

describe("config headers", () => {
Expand Down Expand Up @@ -434,12 +427,9 @@ describe("Storefront API Client", () => {

it("throws an error when the api version is not a string", () => {
const version = 123;
expect(() =>
// @ts-ignore
client.getApiUrl(version)
).toThrow(
expect(() => client.getApiUrl(version as any)).toThrow(
new Error(
`Storefront API Client: the provided \`apiVersion\` (\`123\`) is invalid. Current supported API versions: ${mockApiVersions.join(
`Storefront API Client: the provided apiVersion ("123") is invalid. Current supported API versions: ${mockApiVersions.join(
", "
)}`
)
Expand All @@ -455,7 +445,7 @@ describe("Storefront API Client", () => {
client.getApiUrl(version);

expect(consoleWarnSpy).toHaveBeenCalledWith(
`Storefront API Client: the provided \`apiVersion\` (\`2021-01\`) is deprecated or not supported. Current supported API versions: ${mockApiVersions.join(
`Storefront API Client: the provided apiVersion ("2021-01") is deprecated or not supported. Current supported API versions: ${mockApiVersions.join(
", "
)}`
);
Expand Down
8 changes: 4 additions & 4 deletions packages/storefront-api-client/src/validations.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ERROR_PREFIX } from "./constants";
import { CLIENT } from "./constants";

export function validatePrivateAccessTokenUsage(
privateAccessToken: string | undefined
) {
if (privateAccessToken && window) {
throw new Error(
`${ERROR_PREFIX} private access tokens and headers should only be used in a server-to-server implementation. Use the API public access token in nonserver environments.`
`${CLIENT}: private access tokens and headers should only be used in a server-to-server implementation. Use the API public access token in nonserver environments.`
);
}
}
Expand All @@ -16,13 +16,13 @@ export function validateRequiredAccessTokens(
) {
if (!publicAccessToken && !privateAccessToken) {
throw new Error(
`${ERROR_PREFIX} a public or private access token must be provided`
`${CLIENT}: a public or private access token must be provided`
);
}

if (publicAccessToken && privateAccessToken) {
throw new Error(
`${ERROR_PREFIX} only provide either a public or private access token`
`${CLIENT}: only provide either a public or private access token`
);
}
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2084,11 +2084,6 @@
pkg-dir "^5.0.0"
pluralize "^8.0.0"

"@shopify/graphql-client@^0.4.0":
version "0.4.0"
resolved "https://registry.npmjs.org/@shopify/graphql-client/-/graphql-client-0.4.0.tgz#7892dc6131971e2bc6c79c8400472aed0b476791"
integrity sha512-Pr/IcwuI/Vt9Z7bGt8ufqYnnuyVJYKLwbrI+bvJJs9ooC18kjTu3cZ203/KewnZ3m5fdQGXJmvPl8A/2RuEoTw==

"@shopify/network@^3.2.1":
version "3.2.1"
resolved "https://registry.yarnpkg.com/@shopify/network/-/network-3.2.1.tgz#c51590bb9ccd177bb7a35c2bad482233198d215b"
Expand Down

0 comments on commit 745963d

Please sign in to comment.