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

Add storefront private token header #962

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/beige-kids-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/shopify-api': patch
---

Fix storefront client incorrectly setting private token as access token
2 changes: 0 additions & 2 deletions .changeset/tough-schools-know.md

This file was deleted.

5 changes: 2 additions & 3 deletions lib/clients/graphql/__tests__/storefront_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ describe('Storefront GraphQL client', () => {
}).toMatchMadeHttpRequest();
});

it('can return response from config private app setting', async () => {
shopify.config.isCustomStoreApp = true;
it('can return response from private acess token in config setting', async () => {
shopify.config.privateAppStorefrontAccessToken = 'private_token';

const client = new shopify.clients.Storefront({
Expand All @@ -96,7 +95,7 @@ describe('Storefront GraphQL client', () => {
path: `/api/${shopify.config.apiVersion}/graphql.json`,
data: QUERY,
headers: {
[ShopifyHeader.StorefrontAccessToken]: 'private_token',
[ShopifyHeader.StorefrontPrivateToken]: 'private_token',
},
}).toMatchMadeHttpRequest();
});
Expand Down
21 changes: 16 additions & 5 deletions lib/clients/graphql/storefront_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,24 @@ export class StorefrontClient extends GraphqlClient {

protected getApiHeaders(): HeaderParams {
const sdkVariant = LIBRARY_NAME.toLowerCase().split(' ').join('-');
const privateToken =
this.storefrontClass().config.privateAppStorefrontAccessToken;

if (this.storefrontAccessToken && privateToken) {
logger(this.storefrontClass().config).warning(
'You have both private and public storefront access tokens. The private token will be used.',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging here instead of earlier at validateConfig because the config params doesn't include the public access token at that point. I could do a blanket log.info at the earlier stage but I feel like that's a little bit spammy

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree - this shouldn't be a super common case but it's nice to nudge folks to fix it!

);
}

const tokenHeaderParam =
privateToken === undefined
? {[ShopifyHeader.StorefrontAccessToken]: this.storefrontAccessToken}
: ({
[ShopifyHeader.StorefrontPrivateToken]: privateToken,
} as HeaderParams);

return {
[ShopifyHeader.StorefrontAccessToken]: this.storefrontClass().config
.isCustomStoreApp
? this.storefrontClass().config.privateAppStorefrontAccessToken ||
this.storefrontAccessToken
: this.storefrontAccessToken,
...tokenHeaderParam,
[ShopifyHeader.StorefrontSDKVariant]: sdkVariant,
[ShopifyHeader.StorefrontSDKVersion]: SHOPIFY_API_LIBRARY_VERSION,
};
Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum ShopifyHeader {
Topic = 'X-Shopify-Topic',
WebhookId = 'X-Shopify-Webhook-Id',
StorefrontAccessToken = 'X-Shopify-Storefront-Access-Token',
StorefrontPrivateToken = 'Shopify-Storefront-Private-Token',
StorefrontSDKVariant = 'X-SDK-Variant',
StorefrontSDKVersion = 'X-SDK-Version',
}
Expand Down