Skip to content

Commit

Permalink
feat: customize mfe links
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Oct 22, 2024
1 parent 669511e commit 8f69238
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/drop-in/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"dependencies": {
"@commercelayer/js-auth": "^6.3.1",
"@commercelayer/organization-config": "^1.4.9",
"@commercelayer/sdk": "^6.14.1",
"@types/lodash": "^4.17.7",
"iframe-resizer": "4.4.2",
Expand Down
12 changes: 9 additions & 3 deletions packages/drop-in/src/apis/commercelayer/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAccessToken } from '#apis/commercelayer/client'
import { getConfig } from '#apis/commercelayer/config'
import { getConfig, getOrganizationConfig } from '#apis/commercelayer/config'
import { getClosestLocationHref } from '#utils/url'

export async function getMyAccountUrl(): Promise<string | undefined> {
Expand All @@ -10,7 +10,10 @@ export async function getMyAccountUrl(): Promise<string | undefined> {
return undefined
}

return `${config.appEndpoint}/my-account?accessToken=${accessToken}`
const organizationConfig = await getOrganizationConfig()
const hostedUrl = `${config.appEndpoint}/my-account`

return `${organizationConfig?.links?.my_account ?? hostedUrl}?accessToken=${accessToken}`
}

export async function getIdentityUrl(
Expand All @@ -22,7 +25,10 @@ export async function getIdentityUrl(
return '#'
}

return `${config.appEndpoint}/identity/${type}?clientId=${
const organizationConfig = await getOrganizationConfig()
const hostedUrl = `${config.appEndpoint}/identity`

return `${organizationConfig?.links?.identity ?? hostedUrl}/${type}?clientId=${
config.clientId
}&scope=${config.scope}&returnUrl=${getClosestLocationHref()}`
}
16 changes: 9 additions & 7 deletions packages/drop-in/src/apis/commercelayer/cart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createClient, getAccessToken } from '#apis/commercelayer/client'
import { getConfig } from '#apis/commercelayer/config'
import { getConfig, getOrganizationConfig } from '#apis/commercelayer/config'
import { fireEvent } from '#apis/event'
import { getKeyForCart } from '#apis/storage'
import type {
Expand Down Expand Up @@ -86,9 +86,10 @@ export async function getCartUrl(
cartId = cart.id
}

return `${config.appEndpoint}/cart/${
cartId ?? 'null'
}?accessToken=${accessToken}`
const organizationConfig = await getOrganizationConfig()
const hostedUrl = `${config.appEndpoint}/cart/${cartId ?? 'null'}`

return `${organizationConfig?.links?.cart ?? hostedUrl}?accessToken=${accessToken}`
}

export async function getCheckoutUrl(): Promise<string | undefined> {
Expand All @@ -100,9 +101,10 @@ export async function getCheckoutUrl(): Promise<string | undefined> {
return undefined
}

return `${config.appEndpoint}/checkout/${
cart.id ?? 'null'
}?accessToken=${accessToken}`
const organizationConfig = await getOrganizationConfig()
const hostedUrl = `${config.appEndpoint}/checkout/${cart.id ?? 'null'}`

return `${organizationConfig?.links?.checkout ?? hostedUrl}?accessToken=${accessToken}`
}

export async function _getCart(): Promise<Order | null> {
Expand Down
42 changes: 42 additions & 0 deletions packages/drop-in/src/apis/commercelayer/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { jwtDecode, jwtIsSalesChannel } from '@commercelayer/js-auth'
import { createClient, getAccessToken } from './client'
import { getConfig as mergeConfig } from '@commercelayer/organization-config'
import { getCart } from './cart'
import { memoize } from '#utils/utils'

export interface CommerceLayerConfig {
/**
* Client ID is the application unique identifier. You can find it in your dashboard.
Expand Down Expand Up @@ -107,3 +113,39 @@ export function getConfig(): Config {
appEndpoint
}
}

const getOrganization = memoize(async () => {
const config = getConfig()
const client = await createClient(config)
return await client.organization.retrieve({
fields: {
// @ts-expect-error This is the resource name
organizations: ['config']
}
})
})

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export async function getOrganizationConfig() {
const config = getConfig()
const { accessToken } = await getAccessToken(config)
const jwt = jwtDecode(accessToken)
const cart = await getCart()

const organization = await getOrganization()

const mergeConfigOptions: Parameters<typeof mergeConfig>[0] = {
jsonConfig: organization.config ?? {},
market:
jwtIsSalesChannel(jwt.payload) && jwt.payload.market?.id[0] != null
? `market:id:${jwt.payload.market.id[0]}`
: undefined,
params: {
accessToken,
orderId: cart?.id,
lang: 'en'
}
}

return mergeConfig(mergeConfigOptions)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as cart from '#apis/commercelayer/cart'
import * as config from '#apis/commercelayer/config'
import * as client from '#apis/commercelayer/client'
import type { CommerceLayerClient } from '@commercelayer/sdk'
import { newSpecPage } from '@stencil/core/testing'
Expand All @@ -17,6 +18,8 @@ describe('cl-cart-link.spec', () => {
scope: 'market:code:usa'
})

jest.spyOn(config, 'getOrganizationConfig').mockResolvedValue({})

const { root, waitForChanges } = await newSpecPage({
components: [CLCartLink],
html: '<cl-cart-link>Cart</cl-cart-link>'
Expand Down Expand Up @@ -45,6 +48,7 @@ describe('cl-cart-link.spec', () => {
accessToken: 'token-123',
scope: 'market:code:usa'
})
jest.spyOn(config, 'getOrganizationConfig').mockResolvedValue({})
jest.spyOn(cart, 'getCart').mockResolvedValue({
type: 'orders',
id: 'order-123',
Expand Down Expand Up @@ -86,6 +90,7 @@ describe('cl-cart-link.spec', () => {
accessToken: 'token-123',
scope: 'market:code:usa'
})
jest.spyOn(config, 'getOrganizationConfig').mockResolvedValue({})
jest.spyOn(cart, 'getCart').mockResolvedValue(null)
jest
.spyOn(client, 'createClient')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as client from '#apis/commercelayer/client'
import * as config from '#apis/commercelayer/config'
import { newSpecPage } from '@stencil/core/testing'
import { ClIdentityLink } from './cl-identity-link'
import * as logger from '#utils/logger'
Expand All @@ -14,6 +15,8 @@ beforeEach(() => {
scope: 'market:code:usa'
})

jest.spyOn(config, 'getOrganizationConfig').mockResolvedValue({})

log = jest.spyOn(logger, 'log')
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as client from '#apis/commercelayer/client'
import * as config from '#apis/commercelayer/config'
import { fireEvent } from '#apis/event'
import { newSpecPage } from '@stencil/core/testing'
import { ClMyAccountLink } from './cl-my-account-link'
Expand All @@ -15,6 +16,8 @@ describe('cl-my-account-link.spec', () => {
scope: 'market:code:usa'
})

jest.spyOn(config, 'getOrganizationConfig').mockResolvedValue({})

const { root, waitForChanges } = await newSpecPage({
components: [ClMyAccountLink],
html: '<cl-my-account-link>My Account</cl-my-account-link>'
Expand All @@ -38,6 +41,8 @@ describe('cl-my-account-link.spec', () => {
scope: 'market:code:usa'
})

jest.spyOn(config, 'getOrganizationConfig').mockResolvedValue({})

const { root, waitForChanges } = await newSpecPage({
components: [ClMyAccountLink],
html: '<cl-my-account-link target="_blank">My Account</cl-my-account-link>'
Expand All @@ -62,6 +67,8 @@ describe('cl-my-account-link.spec', () => {
scope: 'market:code:usa'
})

jest.spyOn(config, 'getOrganizationConfig').mockResolvedValue({})

const { root, waitForChanges } = await newSpecPage({
components: [ClMyAccountLink],
html: '<cl-my-account-link target="_blank">My Account</cl-my-account-link>'
Expand All @@ -86,6 +93,8 @@ describe('cl-my-account-link.spec', () => {
scope: 'market:code:usa'
})

jest.spyOn(config, 'getOrganizationConfig').mockResolvedValue({})

const { root, waitForChanges } = await newSpecPage({
components: [ClMyAccountLink],
html: '<cl-my-account-link target="_blank">My Account</cl-my-account-link>'
Expand Down
30 changes: 30 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8f69238

Please sign in to comment.