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

Commit

Permalink
Update types as per review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
melissaluu committed Nov 6, 2023
1 parent a949091 commit a1cef4b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 49 deletions.
2 changes: 1 addition & 1 deletion packages/storefront-api-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const client = createStorefrontApiClient({

### Create a server enabled client using a private access token and a custom Fetch API

> **Warning** <br/>
> [!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.
Expand Down
7 changes: 1 addition & 6 deletions packages/storefront-api-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@
"Storefront API"
],
"files": [
"**/*.d.ts",
"**/*.d.ts.map",
"**/*.js",
"**/*.js.map",
"**/*.mjs",
"**/*.mjs.map",
"dist/**/*.*",
"!node_modules"
],
"dependencies": {
Expand Down
33 changes: 8 additions & 25 deletions packages/storefront-api-client/src/storefront-api-client.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -40,23 +38,7 @@ export function createStorefrontApiClient({
retries = 0,
customFetchApi: clientFetchApi,
logger,
}: {
storeDomain: string;
apiVersion: string;
clientName?: string;
retries?: number;
customFetchApi?: CustomFetchApi;
logger?: ApiClientLogger<StorefrontApiClientLogContentTypes>;
} & (
| {
publicAccessToken?: never;
privateAccessToken: string;
}
| {
publicAccessToken: string;
privateAccessToken?: never;
}
)): StorefrontApiClient {
}: StorefrontApiClientOptions): StorefrontApiClient {
const currentSupportedApiVersions = getCurrentSupportedApiVersions();

const storeUrl = validateDomainAndGetStoreUrl({
Expand All @@ -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,
Expand Down Expand Up @@ -162,9 +147,7 @@ function generateGetHeader(
config: StorefrontApiClientConfig
): StorefrontApiClient["getHeaders"] {
return (customHeaders) => {
return customHeaders
? { ...customHeaders, ...config.headers }
: config.headers;
return { ...(customHeaders ?? {}), ...config.headers };
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
});
Expand Down Expand Up @@ -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", () => {
Expand Down
43 changes: 31 additions & 12 deletions packages/storefront-api-client/src/types.ts
Original file line number Diff line number Diff line change
@@ -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<StorefrontApiClientLogContentTypes>;
};

export type StorefrontApiClient = ApiClient<StorefrontApiClientConfig>;
export type StorefrontApiClient = ApiClient<
Readonly<StorefrontApiClientConfig>
>;

0 comments on commit a1cef4b

Please sign in to comment.