This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1093 from Shopify/support-unified-admin-url
Support unified admin url when using `sanitizeShop`
- Loading branch information
Showing
6 changed files
with
221 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@shopify/shopify-api": minor | ||
--- | ||
|
||
Add helpers to convert between shop admin URLs and legacy URLs. `sanitizeShop` utility method can now support shop admin URLs. |
79 changes: 79 additions & 0 deletions
79
packages/shopify-api/lib/utils/__tests__/shop-admin-url-helper.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {shopifyApi} from '../..'; | ||
import {testConfig} from '../../__tests__/test-config'; | ||
|
||
const VALID_URLS = [ | ||
{ | ||
adminUrl: 'admin.shopify.com/store/my-shop', | ||
legacyAdminUrl: 'my-shop.myshopify.com', | ||
}, | ||
{ | ||
adminUrl: 'admin.web.abc.def-gh.ij.spin.dev/store/my-shop', | ||
legacyAdminUrl: 'my-shop.shopify.abc.def-gh.ij.spin.dev', | ||
}, | ||
]; | ||
|
||
const INVALID_ADMIN_URLS = [ | ||
'not-admin.shopify.com/store/my-shop', | ||
'adminisnotthis.shopify.com/store/my-shop', | ||
'adminisnot.web.abc.def-gh.ij.spin.dev/store/my-shop', | ||
'admin.what.abc.def-gh.ij.spin.dev/store/my-shop', | ||
]; | ||
|
||
const INVALID_LEGACY_URLS = [ | ||
'notshopify.com', | ||
'my-shop.myshopify.com.nope', | ||
'my-shop.myshopify.com/admin', | ||
]; | ||
|
||
describe.each(VALID_URLS)( | ||
'For valid shop URL: %s', | ||
({adminUrl, legacyAdminUrl}) => { | ||
it('can convert from shop admin URL to legacy URL', () => { | ||
const shopify = shopifyApi(testConfig()); | ||
const actual = shopify.utils.shopAdminUrlToLegacyUrl(adminUrl); | ||
expect(actual).toEqual(legacyAdminUrl); | ||
}); | ||
|
||
it('can convert from legacy URL to shop admin URL', () => { | ||
const shopify = shopifyApi(testConfig()); | ||
const actual = shopify.utils.legacyUrlToShopAdminUrl(legacyAdminUrl); | ||
expect(actual).toEqual(adminUrl); | ||
}); | ||
|
||
it('can strip protocol before converting from shop admin URL to legacy URL', () => { | ||
const shopify = shopifyApi(testConfig()); | ||
const urlWithProtocol = `https://${adminUrl}`; | ||
const actual = shopify.utils.shopAdminUrlToLegacyUrl(urlWithProtocol); | ||
expect(actual).toEqual(legacyAdminUrl); | ||
}); | ||
|
||
it('can strip protocol before converting from legacy URL to shop admin URL', () => { | ||
const shopify = shopifyApi(testConfig()); | ||
const urlWithProtocol = `https://${legacyAdminUrl}`; | ||
const actual = shopify.utils.legacyUrlToShopAdminUrl(urlWithProtocol); | ||
expect(actual).toEqual(adminUrl); | ||
}); | ||
}, | ||
); | ||
|
||
describe.each(INVALID_ADMIN_URLS)( | ||
'For invalid shop admin URL: %s', | ||
(invalidUrl) => { | ||
it('returns null when trying to convert from shop admin url to legacy url', () => { | ||
const shopify = shopifyApi(testConfig()); | ||
|
||
expect(shopify.utils.shopAdminUrlToLegacyUrl(invalidUrl)).toBe(null); | ||
}); | ||
}, | ||
); | ||
|
||
describe.each(INVALID_LEGACY_URLS)( | ||
'For invalid legacy shop URL: %s', | ||
(invalidUrl) => { | ||
it('returns null when trying to convert from legacy url to shop admin url', () => { | ||
const shopify = shopifyApi(testConfig()); | ||
|
||
expect(shopify.utils.legacyUrlToShopAdminUrl(invalidUrl)).toBe(null); | ||
}); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Converts admin.shopify.com/store/my-shop to my-shop.myshopify.com | ||
export function shopAdminUrlToLegacyUrl(shopAdminUrl: string): string | null { | ||
const shopUrl = removeProtocol(shopAdminUrl); | ||
|
||
const isShopAdminUrl = shopUrl.split('.')[0] === 'admin'; | ||
|
||
if (!isShopAdminUrl) { | ||
return null; | ||
} | ||
|
||
const regex = new RegExp(`admin\\..+/store/([^/]+)`); | ||
const matches = shopUrl.match(regex); | ||
|
||
if (matches && matches.length === 2) { | ||
const shopName = matches[1]; | ||
const isSpinUrl = shopUrl.includes('spin.dev/store/'); | ||
|
||
if (isSpinUrl) { | ||
return spinAdminUrlToLegacyUrl(shopUrl); | ||
} else { | ||
return `${shopName}.myshopify.com`; | ||
} | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
// Converts my-shop.myshopify.com to admin.shopify.com/store/my-shop | ||
export function legacyUrlToShopAdminUrl(legacyAdminUrl: string): string | null { | ||
const shopUrl = removeProtocol(legacyAdminUrl); | ||
const regex = new RegExp(`(.+)\\.myshopify\\.com$`); | ||
const matches = shopUrl.match(regex); | ||
|
||
if (matches && matches.length === 2) { | ||
const shopName = matches[1]; | ||
return `admin.shopify.com/store/${shopName}`; | ||
} else { | ||
const isSpinUrl = shopUrl.endsWith('spin.dev'); | ||
|
||
if (isSpinUrl) { | ||
return spinLegacyUrlToAdminUrl(shopUrl); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
function spinAdminUrlToLegacyUrl(shopAdminUrl: string) { | ||
const spinRegex = new RegExp(`admin\\.web\\.(.+\\.spin\\.dev)/store/(.+)`); | ||
const spinMatches = shopAdminUrl.match(spinRegex); | ||
|
||
if (spinMatches && spinMatches.length === 3) { | ||
const spinUrl = spinMatches[1]; | ||
const shopName = spinMatches[2]; | ||
return `${shopName}.shopify.${spinUrl}`; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
function spinLegacyUrlToAdminUrl(legacyAdminUrl: string) { | ||
const spinRegex = new RegExp(`(.+)\\.shopify\\.(.+\\.spin\\.dev)`); | ||
const spinMatches = legacyAdminUrl.match(spinRegex); | ||
|
||
if (spinMatches && spinMatches.length === 3) { | ||
const shopName = spinMatches[1]; | ||
const spinUrl = spinMatches[2]; | ||
return `admin.web.${spinUrl}/store/${shopName}`; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
function removeProtocol(url: string): string { | ||
return url.replace(/^https?:\/\//, '').replace(/\/$/, ''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters