From 745963d006427a244cceb32f73678a2b1d9466f1 Mon Sep 17 00:00:00 2001 From: Melissa Luu Date: Wed, 1 Nov 2023 11:11:09 -0400 Subject: [PATCH] Replaced local utils with graphql-client common client utils --- packages/storefront-api-client/.eslintrc.cjs | 17 ----- packages/storefront-api-client/package.json | 2 +- .../storefront-api-client/src/constants.ts | 2 +- .../src/storefront-api-client.ts | 20 ++--- .../src/tests/storefront-api-client.test.ts | 76 ++++++++----------- .../storefront-api-client/src/validations.ts | 8 +- yarn.lock | 5 -- 7 files changed, 49 insertions(+), 81 deletions(-) delete mode 100644 packages/storefront-api-client/.eslintrc.cjs diff --git a/packages/storefront-api-client/.eslintrc.cjs b/packages/storefront-api-client/.eslintrc.cjs deleted file mode 100644 index e290079e1..000000000 --- a/packages/storefront-api-client/.eslintrc.cjs +++ /dev/null @@ -1,17 +0,0 @@ -module.exports = { - extends: "../../.eslintrc.cjs", - overrides: [ - { - files: ["**/*.cjs"], - env: { - node: true, - }, - }, - { - files: ["**/*.test.ts"], - rules: { - "@typescript-eslint/ban-ts-comment": 0, - }, - }, - ], -}; diff --git a/packages/storefront-api-client/package.json b/packages/storefront-api-client/package.json index cdcdc6717..9613c286f 100644 --- a/packages/storefront-api-client/package.json +++ b/packages/storefront-api-client/package.json @@ -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", diff --git a/packages/storefront-api-client/src/constants.ts b/packages/storefront-api-client/src/constants.ts index 351cd8be2..7b9efca15 100644 --- a/packages/storefront-api-client/src/constants.ts +++ b/packages/storefront-api-client/src/constants.ts @@ -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"; diff --git a/packages/storefront-api-client/src/storefront-api-client.ts b/packages/storefront-api-client/src/storefront-api-client.ts index f4f52781b..9fb00c68c 100644 --- a/packages/storefront-api-client/src/storefront-api-client.ts +++ b/packages/storefront-api-client/src/storefront-api-client.ts @@ -3,9 +3,8 @@ import { CustomFetchAPI, RequestParams as GQLClientRequestParams, getCurrentSupportedAPIVersions, - validateRequiredStoreDomain, + validateDomainAndGetStoreUrl, validateApiVersion, - getDomain, } from "@shopify/graphql-client"; import { @@ -22,7 +21,7 @@ import { DEFAULT_CONTENT_TYPE, PUBLIC_ACCESS_TOKEN_HEADER, PRIVATE_ACCESS_TOKEN_HEADER, - ERROR_PREFIX, + CLIENT, } from "./constants"; import { validateRequiredAccessTokens, @@ -53,21 +52,22 @@ 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, }); @@ -75,11 +75,11 @@ export function createStorefrontAPIClient({ 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, diff --git a/packages/storefront-api-client/src/tests/storefront-api-client.test.ts b/packages/storefront-api-client/src/tests/storefront-api-client.test.ts index 2d9bdbdae..97cba0e65 100644 --- a/packages/storefront-api-client/src/tests/storefront-api-client.test.ts +++ b/packages/storefront-api-client/src/tests/storefront-api-client.test.ts @@ -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, @@ -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' ) ); }); @@ -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' ) ); }); @@ -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' ) ); }); @@ -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( ", " )}` ) @@ -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( ", " )}` ) @@ -187,7 +183,7 @@ 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( ", " )}` ); @@ -195,10 +191,9 @@ describe("Storefront API Client", () => { 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( @@ -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( @@ -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` @@ -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", () => { @@ -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", () => { @@ -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( ", " )}` ) @@ -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( ", " )}` ); diff --git a/packages/storefront-api-client/src/validations.ts b/packages/storefront-api-client/src/validations.ts index 33afd14fd..a1e3d255e 100644 --- a/packages/storefront-api-client/src/validations.ts +++ b/packages/storefront-api-client/src/validations.ts @@ -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.` ); } } @@ -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` ); } } diff --git a/yarn.lock b/yarn.lock index 30bd99394..ce0793505 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"