Skip to content

Commit

Permalink
Add protocol automatically to storeDomain (#985)
Browse files Browse the repository at this point in the history
* Auto add protocol if missing

* Stop adding protocol manually to storeDomain parameter

* Changesets

* Refactor
  • Loading branch information
frandiox authored Jun 8, 2023
1 parent 5124d61 commit 7b4afea
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .changeset/clean-experts-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@shopify/hydrogen-react': patch
'@shopify/hydrogen': patch
'demo-store': patch
---

A default `https://` protocol is now added automatically to `storeDomain` if missing.
2 changes: 1 addition & 1 deletion examples/express/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function getContext(req) {
i18n: {language: 'EN', country: 'US'},
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storeDomain: env.PUBLIC_STORE_DOMAIN,
storefrontId: env.PUBLIC_STOREFRONT_ID,
storefrontHeaders: {
requestGroupId: req.get('request-id'),
Expand Down
19 changes: 18 additions & 1 deletion packages/hydrogen-react/src/storefront-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ describe(`createStorefrontClient`, () => {
}),
).toBe(`https://newdomain.myshopify.com`);
});

it(`automatically adds protocol if missing`, () => {
const client = createStorefrontClient(
generateConfig({storeDomain: 'newdomain.myshopify.com'}),
);

expect(client.getShopifyDomain()).toBe(`https://newdomain.myshopify.com`);
});
});

describe(`getStorefrontApiUrl`, () => {
Expand Down Expand Up @@ -97,6 +105,16 @@ describe(`createStorefrontClient`, () => {
`https://testing.myshopify.com/api/${SFAPI_VERSION}/graphql.json`,
);
});

it(`automatically adds protocol if missing`, () => {
const client = createStorefrontClient(
generateConfig({storeDomain: 'newdomain.myshopify.com'}),
);

expect(client.getStorefrontApiUrl()).toBe(
`https://newdomain.myshopify.com/api/${SFAPI_VERSION}/graphql.json`,
);
});
});

describe(`getPrivateTokenHeaders`, () => {
Expand Down Expand Up @@ -178,7 +196,6 @@ function generateConfig(
props?: Partial<StorefrontClientProps>,
): StorefrontClientProps {
return {
storefrontApiVersion: SFAPI_VERSION,
storeDomain: 'https://testing.myshopify.com',
...props,
};
Expand Down
17 changes: 12 additions & 5 deletions packages/hydrogen-react/src/storefront-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,20 @@ export function createStorefrontClient(
);
}

const getShopifyDomain: StorefrontClientReturn['getShopifyDomain'] = (
overrideProps,
) => {
const domain = overrideProps?.storeDomain ?? storeDomain;
return domain.includes('://') ? domain : `https://${domain}`;
};

return {
getShopifyDomain(overrideProps): string {
return overrideProps?.storeDomain ?? storeDomain;
},
getShopifyDomain,
getStorefrontApiUrl(overrideProps): string {
const finalDomainUrl = overrideProps?.storeDomain ?? storeDomain;
return `${finalDomainUrl}${finalDomainUrl.endsWith('/') ? '' : '/'}api/${
const domain = getShopifyDomain(overrideProps);
const apiUrl = domain + (domain.endsWith('/') ? 'api' : '/api');

return `${apiUrl}/${
overrideProps?.storefrontApiVersion ?? storefrontApiVersion
}/graphql.json`;
},
Expand Down
2 changes: 1 addition & 1 deletion packages/hydrogen/docs/staticPages/authenticate.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {storefront} = createStorefrontClient({
// `env` provides access to runtime data, including environment variables
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storeDomain: env.PUBLIC_STORE_DOMAIN,
storefrontId: env.PUBLIC_STOREFRONT_ID,
storefrontHeaders: getStorefrontHeaders(request),
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default {
waitUntil: (p) => executionContext.waitUntil(p),
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storeDomain: env.PUBLIC_STORE_DOMAIN,
storefrontHeaders: getStorefrontHeaders(request),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default {
waitUntil: (p: Promise<unknown>) => executionContext.waitUntil(p),
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storeDomain: env.PUBLIC_STORE_DOMAIN,
storefrontHeaders: getStorefrontHeaders(request),
});

Expand Down
4 changes: 2 additions & 2 deletions packages/hydrogen/src/storefrontClient.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export default {
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
/* Public Storefront API token for your store */
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
/* Your store domain: "https://{shop}.myshopify.com" */
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
/* Your store domain: "{shop}.myshopify.com" */
storeDomain: env.PUBLIC_STORE_DOMAIN,
/**
* Storefront API headers containing:
* - buyerIp: The IP address of the customer.
Expand Down
4 changes: 2 additions & 2 deletions packages/hydrogen/src/storefrontClient.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default {
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
/* Public Storefront API token for your store */
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
/* Your store domain: "https://{shop}.myshopify.com" */
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
/* Your store domain: "{shop}.myshopify.com" */
storeDomain: env.PUBLIC_STORE_DOMAIN,
/**
* Storefront API headers containing:
* - buyerIp: The IP address of the customer.
Expand Down
2 changes: 1 addition & 1 deletion templates/demo-store/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
i18n: getLocaleFromRequest(request),
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storeDomain: env.PUBLIC_STORE_DOMAIN,
storefrontId: env.PUBLIC_STOREFRONT_ID,
storefrontHeaders: getStorefrontHeaders(request),
});
Expand Down
2 changes: 1 addition & 1 deletion templates/hello-world/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
i18n: {language: 'EN', country: 'US'},
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storeDomain: env.PUBLIC_STORE_DOMAIN,
storefrontId: env.PUBLIC_STOREFRONT_ID,
storefrontHeaders: getStorefrontHeaders(request),
});
Expand Down
2 changes: 1 addition & 1 deletion templates/skeleton/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
i18n: {language: 'EN', country: 'US'},
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storeDomain: env.PUBLIC_STORE_DOMAIN,
storefrontId: env.PUBLIC_STOREFRONT_ID,
storefrontHeaders: getStorefrontHeaders(request),
});
Expand Down

0 comments on commit 7b4afea

Please sign in to comment.