-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathfqdn.ts
68 lines (64 loc) · 2.5 KB
/
fqdn.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {isSpinEnvironment, spinFqdn} from './spin.js'
import {AbortError} from '../error.js'
import {serviceEnvironment} from '../../../private/node/context/service.js'
export const CouldntObtainPartnersSpinFQDNError = new AbortError(
"Couldn't obtain the Spin FQDN for Partners when the CLI is not running from a Spin environment.",
)
export const CouldntObtainIdentitySpinFQDNError = new AbortError(
"Couldn't obtain the Spin FQDN for Identity when the CLI is not running from a Spin environment.",
)
export const CouldntObtainShopifySpinFQDNError = new AbortError(
"Couldn't obtain the Spin FQDN for Shopify when the CLI is not running from a Spin environment.",
)
export const NotProvidedStoreFQDNError = new AbortError(
"Couldn't obtain the Shopify FQDN because the store FQDN was not provided.",
)
/**
* It returns the Partners' API service we should interact with.
*
* @returns Fully-qualified domain of the partners service we should interact with.
*/
export async function partnersFqdn(): Promise<string> {
const environment = serviceEnvironment()
const productionFqdn = 'partners.shopify.com'
switch (environment) {
case 'local':
return 'partners.myshopify.io'
case 'spin':
return `partners.${await spinFqdn()}`
default:
return productionFqdn
}
}
/**
* It returns the Identity service we should interact with.
*
* @returns Fully-qualified domain of the Identity service we should interact with.
*/
export async function identityFqdn(): Promise<string> {
const environment = serviceEnvironment()
const productionFqdn = 'accounts.shopify.com'
switch (environment) {
case 'local':
return 'identity.myshopify.io'
case 'spin':
return `identity.${await spinFqdn()}`
default:
return productionFqdn
}
}
/**
* Normalize the store name to be used in the CLI.
* It will add the .myshopify.com domain if it's not present.
* It will add the spin domain if it's not present and we're in a Spin environment.
*
* @param store - Store name.
* @returns Normalized store name.
*/
export async function normalizeStoreFqdn(store: string): Promise<string> {
const storeFqdn = store.replace(/^https?:\/\//, '').replace(/\/$/, '')
const addDomain = async (storeFqdn: string) =>
isSpinEnvironment() ? `${storeFqdn}.shopify.${await spinFqdn()}` : `${storeFqdn}.myshopify.com`
const containDomain = (storeFqdn: string) => storeFqdn.includes('.myshopify.com') || storeFqdn.includes('spin.dev')
return containDomain(storeFqdn) ? storeFqdn : addDomain(storeFqdn)
}