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

Commit

Permalink
Use common API client utils from graphql-client library
Browse files Browse the repository at this point in the history
  • Loading branch information
melissaluu committed Nov 3, 2023
1 parent f74d883 commit 7efc764
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 162 deletions.
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.3.0"
"@shopify/graphql-client": "^0.4.0"
},
"devDependencies": {
"@babel/core": "^7.21.3",
Expand Down
39 changes: 21 additions & 18 deletions packages/storefront-api-client/src/storefront-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import {
createGraphQLClient,
CustomFetchAPI,
RequestParams as GQLClientRequestParams,
getCurrentSupportedAPIVersions,
validateRequiredStoreDomain,
validateApiVersion,
getDomain,
} from "@shopify/graphql-client";

import {
Expand All @@ -18,16 +22,12 @@ import {
DEFAULT_CONTENT_TYPE,
PUBLIC_ACCESS_TOKEN_HEADER,
PRIVATE_ACCESS_TOKEN_HEADER,
ERROR_PREFIX,
} from "./constants";
import {
getCurrentSupportedAPIVersions,
validateRequiredStoreDomain,
validateRequiredAccessTokens,
validatePrivateAccessTokenUsage,
validateApiVersion,
} from "./utilities";

const httpRegEx = new RegExp("^(https?:)?//");
} from "./validations";

export function createStorefrontAPIClient({
storeDomain,
Expand All @@ -51,32 +51,35 @@ export function createStorefrontAPIClient({
privateAccessToken?: never;
}
)): StorefrontAPIClient {
const currentSupportedAPIVersions = getCurrentSupportedAPIVersions();
const currentSupportedApiVersions = getCurrentSupportedAPIVersions();

validateRequiredStoreDomain(storeDomain);
validateApiVersion(currentSupportedAPIVersions, apiVersion);
validateRequiredStoreDomain(ERROR_PREFIX, storeDomain);
validateApiVersion({
errorPrefix: ERROR_PREFIX,
currentSupportedApiVersions,
apiVersion,
});
validateRequiredAccessTokens(publicAccessToken, privateAccessToken);
validatePrivateAccessTokenUsage(privateAccessToken);

const trimmedStoreDomain = storeDomain.trim();
const cleanedStoreDomain = httpRegEx.test(trimmedStoreDomain)
? trimmedStoreDomain.substring(trimmedStoreDomain.indexOf("//") + 2)
: trimmedStoreDomain;
const cleanedStoreDomain = getDomain(storeDomain);

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

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

return `https://${cleanedStoreDomain}${
cleanedStoreDomain.endsWith("/") ? "" : "/"
}api/${urlApiVersion}/graphql.json`;
return `https://${cleanedStoreDomain}/api/${urlApiVersion}/graphql.json`;
};

const config: SFAPIClientConfig = {
storeDomain: trimmedStoreDomain,
storeDomain: cleanedStoreDomain,
apiVersion,
publicAccessToken: publicAccessToken ?? null,
privateAccessToken: privateAccessToken ?? null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,19 @@ const mockApiVersions = [
"unstable",
];

jest.mock("../utilities/api-versions", () => {
return {
...jest.requireActual("../utilities/api-versions"),
getCurrentSupportedAPIVersions: () => mockApiVersions,
};
});

jest.mock("@shopify/graphql-client", () => {
return {
...jest.requireActual("@shopify/graphql-client"),
createGraphQLClient: jest.fn(),
getCurrentSupportedAPIVersions: () => mockApiVersions,
};
});

describe("Storefront API Client", () => {
describe("createStorefrontAPIClient()", () => {
const domain = "test-store.myshopify.io";
const config = {
storeDomain: "https://test-store.myshopify.io",
storeDomain: `https://${domain}`,
apiVersion: "2023-10",
publicAccessToken: "public-token",
};
Expand Down Expand Up @@ -154,12 +149,11 @@ describe("Storefront API Client", () => {
expect(() =>
createStorefrontAPIClient({
...config,
// @ts-ignore
apiVersion: undefined,
apiVersion: undefined as any,
})
).toThrow(
new Error(
`Storefront API Client: the provided \`apiVersion\` 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 @@ -175,7 +169,7 @@ describe("Storefront API Client", () => {
})
).toThrow(
new Error(
`Storefront API Client: the provided \`apiVersion\` 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 Down Expand Up @@ -253,7 +247,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(config.storeDomain);
expect(client.config.storeDomain).toBe(domain);
});

it("returns a config object that includes the provided public access token and a null private access token", () => {
Expand Down Expand Up @@ -307,22 +301,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 @@ -445,7 +439,7 @@ describe("Storefront API Client", () => {
client.getApiUrl(version)
).toThrow(
new Error(
`Storefront API Client: the provided \`apiVersion\` is invalid. Current supported API versions: ${mockApiVersions.join(
`Storefront API Client: the provided \`apiVersion\` (\`123\`) is invalid. Current supported API versions: ${mockApiVersions.join(
", "
)}`
)
Expand Down
46 changes: 0 additions & 46 deletions packages/storefront-api-client/src/utilities/api-versions.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/storefront-api-client/src/utilities/index.ts

This file was deleted.

61 changes: 0 additions & 61 deletions packages/storefront-api-client/src/utilities/validations.ts

This file was deleted.

28 changes: 28 additions & 0 deletions packages/storefront-api-client/src/validations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ERROR_PREFIX } 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.`
);
}
}

export function validateRequiredAccessTokens(
publicAccessToken: string | undefined,
privateAccessToken: string | undefined
) {
if (!publicAccessToken && !privateAccessToken) {
throw new Error(
`${ERROR_PREFIX} 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`
);
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2084,10 +2084,10 @@
pkg-dir "^5.0.0"
pluralize "^8.0.0"

"@shopify/graphql-client@^0.3.0":
version "0.3.0"
resolved "https://registry.npmjs.org/@shopify/graphql-client/-/graphql-client-0.3.0.tgz#1789ceb92f15aee49aa7ff27b1eae6396c4dd1aa"
integrity sha512-xkSx9ahiGW3bf40SrN9wANBVV4IiBhH9RIKGoni8YY/iMXxZh2jkg7TX1Y6Qpqsxg/9te+F1WBKjc0OF8pcA5w==
"@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"
Expand Down

0 comments on commit 7efc764

Please sign in to comment.