diff --git a/packages/storefront-api-client/README.md b/packages/storefront-api-client/README.md index e8c7dad95..abd3c7986 100644 --- a/packages/storefront-api-client/README.md +++ b/packages/storefront-api-client/README.md @@ -18,7 +18,7 @@ const client = createStorefrontApiClient({ ### Create a server enabled client using a private access token and a custom Fetch API -> **Warning**
+> [!WARNING] > Private Storefront API delegate access tokens should only be used in server-to-server implementations and not within a browser environment. In order to use the client within a server, a server enabled JS Fetch API will need to be provided to the client at initialization. diff --git a/packages/storefront-api-client/package.json b/packages/storefront-api-client/package.json index d04e16324..a0c2d9cc3 100644 --- a/packages/storefront-api-client/package.json +++ b/packages/storefront-api-client/package.json @@ -59,12 +59,7 @@ "Storefront API" ], "files": [ - "**/*.d.ts", - "**/*.d.ts.map", - "**/*.js", - "**/*.js.map", - "**/*.mjs", - "**/*.mjs.map", + "dist/**/*.*", "!node_modules" ], "dependencies": { diff --git a/packages/storefront-api-client/src/storefront-api-client.ts b/packages/storefront-api-client/src/storefront-api-client.ts index 769e4a491..df90b179d 100644 --- a/packages/storefront-api-client/src/storefront-api-client.ts +++ b/packages/storefront-api-client/src/storefront-api-client.ts @@ -1,19 +1,17 @@ import { createGraphQLClient, - CustomFetchApi, RequestParams as GQLClientRequestParams, getCurrentSupportedApiVersions, validateDomainAndGetStoreUrl, validateApiVersion, - ApiClientLogger, ApiClientRequestParams, ApiClientRequestOptions, } from "@shopify/graphql-client"; import { + StorefrontApiClientOptions, StorefrontApiClient, StorefrontApiClientConfig, - StorefrontApiClientLogContentTypes, } from "./types"; import { DEFAULT_SDK_VARIANT, @@ -40,23 +38,7 @@ export function createStorefrontApiClient({ retries = 0, customFetchApi: clientFetchApi, logger, -}: { - storeDomain: string; - apiVersion: string; - clientName?: string; - retries?: number; - customFetchApi?: CustomFetchApi; - logger?: ApiClientLogger; -} & ( - | { - publicAccessToken?: never; - privateAccessToken: string; - } - | { - publicAccessToken: string; - privateAccessToken?: never; - } -)): StorefrontApiClient { +}: StorefrontApiClientOptions): StorefrontApiClient { const currentSupportedApiVersions = getCurrentSupportedApiVersions(); const storeUrl = validateDomainAndGetStoreUrl({ @@ -83,8 +65,11 @@ export function createStorefrontApiClient({ const config: StorefrontApiClientConfig = { storeDomain: storeUrl, apiVersion, - publicAccessToken: publicAccessToken ?? null, - privateAccessToken: privateAccessToken ?? null, + ...(publicAccessToken + ? { publicAccessToken } + : { + privateAccessToken: privateAccessToken!, + }), headers: { "Content-Type": DEFAULT_CONTENT_TYPE, Accept: DEFAULT_CONTENT_TYPE, @@ -162,9 +147,7 @@ function generateGetHeader( config: StorefrontApiClientConfig ): StorefrontApiClient["getHeaders"] { return (customHeaders) => { - return customHeaders - ? { ...customHeaders, ...config.headers } - : config.headers; + return { ...(customHeaders ?? {}), ...config.headers }; }; } 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 3e24c0a0a..2caec6bc4 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 @@ -279,13 +279,13 @@ describe("Storefront API Client", () => { 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", () => { + it("returns a config object that includes the provided public access token and not a private access token", () => { const client = createStorefrontApiClient(config); expect(client.config.publicAccessToken).toBe(config.publicAccessToken); - expect(client.config.privateAccessToken).toBeNull(); + expect(client.config.privateAccessToken).toBeUndefined(); }); - it("returns a config object that includes the provided private access token and a null public access token when in a server environment (window is undefined)", () => { + it("returns a config object that includes the provided private access token and not a public access token when in a server environment (window is undefined)", () => { const windowSpy = jest .spyOn(window, "window", "get") .mockImplementation(() => undefined as any); @@ -298,7 +298,7 @@ describe("Storefront API Client", () => { privateAccessToken, }); expect(client.config.privateAccessToken).toBe(privateAccessToken); - expect(client.config.publicAccessToken).toBeNull(); + expect(client.config.publicAccessToken).toBeUndefined(); windowSpy.mockRestore(); }); @@ -429,7 +429,7 @@ describe("Storefront API Client", () => { it("returns the client's default headers if no custom headers are provided", () => { const headers = client.getHeaders(); - expect(headers).toBe(client.config.headers); + expect(headers).toEqual(client.config.headers); }); it("returns a headers object that contains both the client default headers and the provided custom headers", () => { diff --git a/packages/storefront-api-client/src/types.ts b/packages/storefront-api-client/src/types.ts index 064b8ab21..ec06c9980 100644 --- a/packages/storefront-api-client/src/types.ts +++ b/packages/storefront-api-client/src/types.ts @@ -1,20 +1,39 @@ import { - Headers, ApiClient, + Headers, + CustomFetchApi, + ApiClientLogger, ApiClientLogContentTypes, } from "@shopify/graphql-client"; export type StorefrontApiClientLogContentTypes = ApiClientLogContentTypes; -export interface StorefrontApiClientConfig { - readonly storeDomain: string; - readonly apiVersion: string; - readonly publicAccessToken: string | null; - readonly privateAccessToken: string | null; - readonly headers: Headers; - readonly apiUrl: string; - readonly clientName?: string; - readonly retries?: number; -} +export type StorefrontApiClientConfig = { + storeDomain: string; + apiVersion: string; + headers: Headers; + apiUrl: string; + retries?: number; + clientName?: string; +} & ( + | { + publicAccessToken?: never; + privateAccessToken: string; + } + | { + publicAccessToken: string; + privateAccessToken?: never; + } +); + +export type StorefrontApiClientOptions = Omit< + StorefrontApiClientConfig, + "headers" | "apiUrl" +> & { + customFetchApi?: CustomFetchApi; + logger?: ApiClientLogger; +}; -export type StorefrontApiClient = ApiClient; +export type StorefrontApiClient = ApiClient< + Readonly +>;