Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/organization-config/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"linter": {
"enabled": true,
"rules": {
"recommended": true
"recommended": true,
"style": {
"useBlockStatements": "warn"
}
}
},
"javascript": {
Expand Down
1 change: 1 addition & 0 deletions packages/organization-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"vitest": "^4.0.10"
},
"dependencies": {
"@commercelayer/js-auth": "^7.2.0",
"merge-anything": "^5.1.7"
}
}
98 changes: 98 additions & 0 deletions packages/organization-config/src/getMfeConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ describe("getMfeConfig function", () => {
params: {},
}),
).toBeNull()

expect(
getMfeConfig({
jsonConfig: null,
market: "market:id:ZKcv13rT",
params: {},
}),
).toBeNull()

expect(
getMfeConfig({
jsonConfig: undefined,
market: "market:id:ZKcv13rT",
params: {},
}),
).toBeNull()
})

it("should return the default config if there is no override", () => {
Expand Down Expand Up @@ -294,4 +310,86 @@ describe("getMfeConfig function", () => {
})
expect(config?.language).toBe("it-IT")
})

it("should use default links set to our official build-in apps (microstore as list)", () => {
const config = getMfeConfig({
jsonConfig: null,
params: {
accessToken: ioAccessToken,
orderId: "order123",
skuListId: "skuList123",
},
market: "market:id:ZKcv13rT",
})
expect(config).toMatchObject({
links: {
cart: `https://demo-store.commercelayer.app/cart/order123?accessToken=${ioAccessToken}`,
checkout: `https://demo-store.commercelayer.app/checkout/order123?accessToken=${ioAccessToken}`,
identity: `https://demo-store.commercelayer.app/identity`,
microstore: `https://demo-store.commercelayer.app/microstore/list/skuList123?accessToken=${ioAccessToken}`,
my_account: `https://demo-store.commercelayer.app/my-account?accessToken=${ioAccessToken}`,
},
})
})

it("should use default links set to our official build-in apps (microstore as sku)", () => {
const config = getMfeConfig({
jsonConfig: null,
params: {
accessToken: ioAccessToken,
orderId: "order123",
skuId: "sku123",
},
market: "market:id:ZKcv13rT",
})
expect(config).toMatchObject({
links: {
cart: `https://demo-store.commercelayer.app/cart/order123?accessToken=${ioAccessToken}`,
checkout: `https://demo-store.commercelayer.app/checkout/order123?accessToken=${ioAccessToken}`,
identity: `https://demo-store.commercelayer.app/identity`,
microstore: `https://demo-store.commercelayer.app/microstore/sku/sku123?accessToken=${ioAccessToken}`,
my_account: `https://demo-store.commercelayer.app/my-account?accessToken=${ioAccessToken}`,
},
})
})

it("should use default links set to our official build-in apps with the ability to extend", () => {
const config = getMfeConfig({
jsonConfig: {
mfe: {
default: {
links: {
cart: "https://custom-cart.example.com/:order_id?accessToken=:access_token",
checkout:
"https://custom-checkout.example.com/:order_id?accessToken=:access_token",
},
},
"market:id:ZKcv13rT": {
links: {
cart: "https://market-cart.example.com/:order_id?accessToken=:access_token",
identity: "https://custom-identity.example.com",
},
},
},
},
params: {
accessToken: ioAccessToken,
orderId: "order123",
skuListId: "skuList123",
},
market: "market:id:ZKcv13rT",
})
expect(config).toMatchObject({
links: {
cart: `https://market-cart.example.com/order123?accessToken=${ioAccessToken}`,
checkout: `https://custom-checkout.example.com/order123?accessToken=${ioAccessToken}`,
identity: `https://custom-identity.example.com`,
microstore: `https://demo-store.commercelayer.app/microstore/list/skuList123?accessToken=${ioAccessToken}`,
my_account: `https://demo-store.commercelayer.app/my-account?accessToken=${ioAccessToken}`,
},
})
})
})

const ioAccessToken =
"eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCIsImtpZCI6IjliN2JiZmVlMzQzZDVkNDQ5ZGFkODhmMjg0MGEyZTM3YzhkZWFlZTg5NjM4MGQ1ODA2YTc4NWVkMWQ1OTc5ZjAifQ.eyJvcmdhbml6YXRpb24iOnsiaWQiOiJleW9aT0Z2UHBSIiwic2x1ZyI6ImRlbW8tc3RvcmUiLCJlbnRlcnByaXNlI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7eUmhiy_0_XKH71xWQ4O2jl2pdb7dqp9wr_vDTM1NteBhV0-xgkkw6FLUz2jwTxnNsNrbtHRiXCWaeD_AgrTFxy_oeHQerP_vcqfK4GwIMfb6GXRxE_e6CR1awz12LoX7k64NGZwAQUVGHCpLjLfr4GXDdRK97RyChnDVvb8eq1GGq1aa9P525egRG0dQ4C0"
62 changes: 56 additions & 6 deletions packages/organization-config/src/getMfeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { jwtDecode } from "@commercelayer/js-auth"
import { merge } from "merge-anything"
import type { ValidConfigForOrganizationsInCommerceLayer } from "./schema/types"

Expand Down Expand Up @@ -45,7 +46,7 @@ interface GetMfeConfigProps {
/**
* `config` attribute of the organization
*/
jsonConfig?: { mfe?: MfeConfigs }
jsonConfig?: { mfe?: MfeConfigs } | null
/**
* Market identifier for fetching specific configuration overrides. (`market:id:hashid`)
*/
Expand Down Expand Up @@ -78,12 +79,12 @@ export function getMfeConfig({
market,
params,
}: GetMfeConfigProps): DefaultMfeConfig | null {
if (jsonConfig?.mfe == null) {
return null
}
const defaultConfig = merge(
getDefaults({ params }),
jsonConfig?.mfe?.default ?? {},
)

const defaultConfig = jsonConfig?.mfe?.default ?? {}
const overrideConfig = market != null ? (jsonConfig?.mfe[market] ?? {}) : {}
const overrideConfig = market != null ? (jsonConfig?.mfe?.[market] ?? {}) : {}

// Replace placeholders in all string values within the object
function replacePlaceholders(config: DefaultMfeConfig): DefaultMfeConfig {
Expand All @@ -103,5 +104,54 @@ export function getMfeConfig({
JSON.parse(JSON.stringify(defaultConfig)),
overrideConfig,
)

if (Object.keys(mergedConfig).length === 0) {
return null
}

return replacePlaceholders(mergedConfig)
}

/**
* Provides default MFE configuration.
*/
function getDefaults({ params }: GetMfeConfigProps): DefaultMfeConfig {
if (params?.accessToken == null) {
return {}
}

try {
const jwt = jwtDecode(params.accessToken)
const slug =
params.slug ??
("organization" in jwt.payload ? jwt.payload.organization.slug : null)

if (slug == null) {
return {}
}

const domainPrefix = jwt.payload.iss.endsWith("commercelayer.co")
? ".stg"
: ""

const appEndpoint = `https://${slug}${domainPrefix}.commercelayer.app`

const defaultConfig: DefaultMfeConfig = {
links: {
cart: `${appEndpoint}/cart/:order_id?accessToken=:access_token`,
checkout: `${appEndpoint}/checkout/:order_id?accessToken=:access_token`,
my_account: `${appEndpoint}/my-account?accessToken=:access_token`,
identity: `${appEndpoint}/identity`,
microstore:
params.skuListId != null
? `${appEndpoint}/microstore/list/:sku_list_id?accessToken=:access_token`
: `${appEndpoint}/microstore/sku/:sku_id?accessToken=:access_token`,
},
}

return defaultConfig
} catch (_error) {
console.log(_error)
return {}
}
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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