-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
if shopId is passed, use the core url
- Loading branch information
1 parent
d633e49
commit 96cf026
Showing
11 changed files
with
1,186 additions
and
450 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
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
103 changes: 103 additions & 0 deletions
103
packages/hydrogen/src/customer/customer-account-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,103 @@ | ||
import {describe, it, expect} from 'vitest'; | ||
import {createCustomerAccountHelper, URL_TYPE} from './customer-account-helper'; | ||
|
||
const shopId = '1'; | ||
const customerAccountUrl = `https://shopify.com/${shopId}`; | ||
|
||
describe('return correct urls', () => { | ||
describe('when shopId is provided', () => { | ||
const getAccountUrl = createCustomerAccountHelper( | ||
'2024-10', | ||
customerAccountUrl, | ||
shopId, | ||
); | ||
|
||
it('returns customer account base url', () => { | ||
expect(getAccountUrl(URL_TYPE.CA_BASE_URL)).toBe(customerAccountUrl); | ||
}); | ||
|
||
it('returns customer account auth url', () => { | ||
expect(getAccountUrl(URL_TYPE.CA_BASE_AUTH_URL)).toBe( | ||
`https://shopify.com/authentication/${shopId}`, | ||
); | ||
}); | ||
|
||
it('returns customer account graphql url', () => { | ||
expect(getAccountUrl(URL_TYPE.GRAPHQL)).toBe( | ||
`${customerAccountUrl}/account/customer/api/2024-10/graphql`, | ||
); | ||
}); | ||
|
||
it('returns customer account authorize url', () => { | ||
expect(getAccountUrl(URL_TYPE.AUTH)).toBe( | ||
`https://shopify.com/authentication/${shopId}/oauth/authorize`, | ||
); | ||
}); | ||
|
||
it('returns customer account login scope', () => { | ||
expect(getAccountUrl(URL_TYPE.LOGIN_SCOPE)).toBe( | ||
'openid email customer-account-api:full', | ||
); | ||
}); | ||
|
||
it('returns customer account token exchange url', () => { | ||
expect(getAccountUrl(URL_TYPE.TOKEN_EXCHANGE)).toBe( | ||
`https://shopify.com/authentication/${shopId}/oauth/token`, | ||
); | ||
}); | ||
|
||
it('returns customer account logout url', () => { | ||
expect(getAccountUrl(URL_TYPE.LOGOUT)).toBe( | ||
`https://shopify.com/authentication/${shopId}/logout`, | ||
); | ||
}); | ||
}); | ||
|
||
describe('when shopId is not provided', () => { | ||
const getAccountUrl = createCustomerAccountHelper( | ||
'2024-10', | ||
customerAccountUrl, | ||
undefined, | ||
); | ||
|
||
it('returns customer account base url', () => { | ||
expect(getAccountUrl(URL_TYPE.CA_BASE_URL)).toBe(customerAccountUrl); | ||
}); | ||
|
||
it('returns customer account auth url', () => { | ||
expect(getAccountUrl(URL_TYPE.CA_BASE_AUTH_URL)).toBe( | ||
`${customerAccountUrl}/auth`, | ||
); | ||
}); | ||
|
||
it('returns customer account graphql url', () => { | ||
expect(getAccountUrl(URL_TYPE.GRAPHQL)).toBe( | ||
`https://shopify.com/${shopId}/account/customer/api/2024-10/graphql`, | ||
); | ||
}); | ||
|
||
it('returns customer account authorize url', () => { | ||
expect(getAccountUrl(URL_TYPE.AUTH)).toBe( | ||
`${customerAccountUrl}/auth/oauth/authorize`, | ||
); | ||
}); | ||
|
||
it('returns customer account login scope', () => { | ||
expect(getAccountUrl(URL_TYPE.LOGIN_SCOPE)).toBe( | ||
'openid email https://api.customers.com/auth/customer.graphql', | ||
); | ||
}); | ||
|
||
it('returns customer account token exchange url', () => { | ||
expect(getAccountUrl(URL_TYPE.TOKEN_EXCHANGE)).toBe( | ||
`${customerAccountUrl}/auth/oauth/token`, | ||
); | ||
}); | ||
|
||
it('returns customer account logout url', () => { | ||
expect(getAccountUrl(URL_TYPE.LOGOUT)).toBe( | ||
`${customerAccountUrl}/auth/logout`, | ||
); | ||
}); | ||
}); | ||
}); |
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,45 @@ | ||
export enum URL_TYPE { | ||
CA_BASE_URL = 'CA_BASE_URL', | ||
CA_BASE_AUTH_URL = 'CA_BASE_AUTH_URL', | ||
GRAPHQL = 'GRAPHQL', | ||
AUTH = 'AUTH', | ||
LOGIN_SCOPE = 'LOGIN_SCOPE', | ||
TOKEN_EXCHANGE = 'TOKEN_EXCHANGE', | ||
LOGOUT = 'LOGOUT', | ||
} | ||
|
||
export function createCustomerAccountHelper( | ||
customerApiVersion: string, | ||
deprecatedCustomerAccountUrl?: string, | ||
shopId?: string, | ||
) { | ||
const customerAccountUrl = shopId | ||
? `https://shopify.com/${shopId}` | ||
: deprecatedCustomerAccountUrl; | ||
|
||
const customerAccountAuthUrl = shopId | ||
? `https://shopify.com/authentication/${shopId}` | ||
: `${deprecatedCustomerAccountUrl}/auth`; | ||
|
||
return function getCustomerAccountUrl(urlType: URL_TYPE): string { | ||
switch (urlType) { | ||
case URL_TYPE.CA_BASE_URL: | ||
// @ts-expect-error | ||
return customerAccountUrl; | ||
case URL_TYPE.CA_BASE_AUTH_URL: | ||
return customerAccountAuthUrl; | ||
case URL_TYPE.GRAPHQL: | ||
return `${customerAccountUrl}/account/customer/api/${customerApiVersion}/graphql`; | ||
case URL_TYPE.AUTH: | ||
return `${customerAccountAuthUrl}/oauth/authorize`; | ||
case URL_TYPE.LOGIN_SCOPE: | ||
return shopId | ||
? 'openid email customer-account-api:full' | ||
: 'openid email https://api.customers.com/auth/customer.graphql'; | ||
case URL_TYPE.TOKEN_EXCHANGE: | ||
return `${customerAccountAuthUrl}/oauth/token`; | ||
case URL_TYPE.LOGOUT: | ||
return `${customerAccountAuthUrl}/logout`; | ||
} | ||
}; | ||
} |
Oops, something went wrong.