@commercelayer/organization-config 2.7.0 → 2.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +31 -1
- package/dist/index.d.ts +31 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var r=Object.defineProperty;var U=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var V=(l,t)=>{for(var e in t)r(l,e,{get:t[e],enumerable:!0})},J=(l,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of x(t))!z.call(l,n)&&n!==e&&r(l,n,{get:()=>t[n],enumerable:!(o=U(t,n))||o.enumerable});return l};var W=l=>J(r({},"__esModule",{value:!0}),l);var G={};V(G,{getMfeConfig:()=>S,hasValidLinks:()=>w});module.exports=W(G);var P=require("@commercelayer/js-auth"),f=require("merge-anything");function S({jsonConfig:l,market:t,params:e}){var c,g,s,d;let o=(0,f.merge)(F({params:e}),(g=(c=l==null?void 0:l.mfe)==null?void 0:c.default)!=null?g:{}),n=t!=null?(d=(s=l==null?void 0:l.mfe)==null?void 0:s[t])!=null?d:{}:{};function i(O){var y,k,_,C,T,b,N,M,p,h,I,D,L;let $=JSON.stringify(O).replace(/:lang/g,(y=e==null?void 0:e.lang)!=null?y:":lang").replace(/:slug/g,(k=e==null?void 0:e.slug)!=null?k:":slug").replace(/:token/g,(_=e==null?void 0:e.token)!=null?_:":token").replace(/:access_token/g,(C=e==null?void 0:e.accessToken)!=null?C:":access_token").replace(/:order_id/g,(T=e==null?void 0:e.orderId)!=null?T:":order_id").replace(/:sku_list_id/g,(b=e==null?void 0:e.skuListId)!=null?b:":sku_list_id").replace(/:sku_id/g,(N=e==null?void 0:e.skuId)!=null?N:":sku_id").replace(/:identity_type/g,(M=e==null?void 0:e.identityType)!=null?M:":identity_type").replace(/:client_id/g,(p=e==null?void 0:e.clientId)!=null?p:":client_id").replace(/:scope/g,(h=e==null?void 0:e.scope)!=null?h:":scope").replace(/:public_scope/g,(I=e==null?void 0:e.publicScope)!=null?I:":public_scope").replace(/:return_url/g,(D=e==null?void 0:e.returnUrl)!=null?D:":return_url").replace(/:reset_password_url/g,(L=e==null?void 0:e.resetPasswordUrl)!=null?L:":reset_password_url");return JSON.parse($)}let u=(0,f.merge)(JSON.parse(JSON.stringify(o)),n);return Object.keys(u).length===0?null:i(u)}function F({params:l}){var t;if((l==null?void 0:l.accessToken)==null)return{};try{let e=(0,P.jwtDecode)(l.accessToken),o=(t=l.slug)!=null?t:"organization"in e.payload?e.payload.organization.slug:null;if(o==null)return{};let n=e.payload.iss.endsWith("commercelayer.co")?".stg":"",i=`https://${o}${n}.commercelayer.app`;return{links:{cart:`${i}/cart/:order_id?accessToken=:access_token`,checkout:`${i}/checkout/:order_id?accessToken=:access_token`,my_account:`${i}/my-account?accessToken=:access_token`,identity:`${i}/identity/:identity_type?${["clientId=:client_id","scope=:scope",l.publicScope!=null?"publicScope=:public_scope":null,l.lang!=null?"lang=:lang":null,l.returnUrl!=null?"returnUrl=:return_url":null,l.resetPasswordUrl!=null?"resetPasswordUrl=:reset_password_url":null].filter(c=>c!=null).join("&")}`,microstore:l.skuListId!=null?`${i}/microstore/list/:sku_list_id?accessToken=:access_token`:`${i}/microstore/sku/:sku_id?accessToken=:access_token`}}}catch(e){return console.log(e),{}}}function w(l){return l!=null&&l.links!=null&&l.links.cart!=null&&l.links.checkout!=null&&l.links.identity!=null&&l.links.my_account!=null}0&&(module.exports={getMfeConfig,hasValidLinks});
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/getMfeConfig.ts"],"sourcesContent":["export {\n type DefaultMfeConfig,\n getMfeConfig,\n type MfeConfigs,\n} from \"./getMfeConfig\"\n","import { jwtDecode } from \"@commercelayer/js-auth\"\nimport { merge } from \"merge-anything\"\nimport type { ValidConfigForOrganizationsInCommerceLayer } from \"./schema/types\"\n\n/**\n * Represents a type that can be null or undefined, making it optional in use.\n * @template T The type that is being made nullable.\n */\ntype NullableType<T> = T | null | undefined\n\n/**\n * Params used by the getConfig function\n */\ninterface ConfigParams {\n /**\n * Language code (e.g., 'en', 'fr') used to dynamically replace the `:lang` placeholder in URLs.\n */\n lang?: NullableType<string>\n /**\n * Organization slug used to replace the `:slug` placeholder in URLs.\n */\n slug?: NullableType<string>\n /**\n * Access token string used to replace the `:access_token` placeholder in URLs.\n */\n accessToken?: NullableType<string>\n /**\n * Unique identifier for an order used to replace the `:order_id` placeholder in URLs.\n */\n orderId?: NullableType<string>\n /**\n * Order token string used to replace the `:token` placeholder in URLs.\n */\n token?: NullableType<string>\n /**\n * Unique identifier for an SKU list used to replace the `:sku_list_id` placeholder in URLs.\n */\n skuListId?: NullableType<string>\n /**\n * Unique identifier for an SKU used to replace the `:sku_id` placeholder in URLs.\n */\n skuId?: NullableType<string>\n}\n\ninterface GetMfeConfigProps {\n /**\n * `config` attribute of the organization\n */\n jsonConfig?: { mfe?: MfeConfigs } | null\n /**\n * Market identifier for fetching specific configuration overrides. (`market:id:hashid`)\n */\n market?: string\n /**\n * Parameters for replacing URL placeholders.\n */\n params?: ConfigParams\n}\n\nexport type MfeConfigs = NonNullable<\n ValidConfigForOrganizationsInCommerceLayer[\"mfe\"]\n>\n\nexport type DefaultMfeConfig = NonNullable<\n NonNullable<ValidConfigForOrganizationsInCommerceLayer[\"mfe\"]>[\"default\"]\n>\n\n/**\n * Retrieves and merges the default organization configuration with market-specific overrides based on the provided market identifier.\n * Placeholder values in the configuration URLs can be replaced with actual values from the `params`.\n *\n * @param jsonConfig The complete configuration object of the organization.\n * @param market The market identifier used to get market-specific configuration overrides.\n * @param params The object containing replacement values for URL placeholders, such as language and access token.\n * @returns The merged configuration object for the specified market, or null if no configuration is found.\n */\nexport function getMfeConfig({\n jsonConfig,\n market,\n params,\n}: GetMfeConfigProps): DefaultMfeConfig | null {\n const defaultConfig = merge(\n getDefaults({ params }),\n jsonConfig?.mfe?.default ?? {},\n )\n\n const overrideConfig = market != null ? (jsonConfig?.mfe?.[market] ?? {}) : {}\n\n // Replace placeholders in all string values within the object\n function replacePlaceholders(config: DefaultMfeConfig): DefaultMfeConfig {\n const replacedConfig = JSON.stringify(config)\n .replace(/:lang/g, params?.lang ?? \":lang\")\n .replace(/:slug/g, params?.slug ?? \":slug\")\n .replace(/:token/g, params?.token ?? \":token\")\n .replace(/:access_token/g, params?.accessToken ?? \":access_token\")\n .replace(/:order_id/g, params?.orderId ?? \":order_id\")\n .replace(/:sku_list_id/g, params?.skuListId ?? \":sku_list_id\")\n .replace(/:sku_id/g, params?.skuId ?? \":sku_id\")\n\n return JSON.parse(replacedConfig)\n }\n\n const mergedConfig: DefaultMfeConfig = merge(\n JSON.parse(JSON.stringify(defaultConfig)),\n overrideConfig,\n )\n\n if (Object.keys(mergedConfig).length === 0) {\n return null\n }\n\n return replacePlaceholders(mergedConfig)\n}\n\n/**\n * Provides default MFE configuration.\n */\nfunction getDefaults({ params }: GetMfeConfigProps): DefaultMfeConfig {\n if (params?.accessToken == null) {\n return {}\n }\n\n try {\n const jwt = jwtDecode(params.accessToken)\n const slug =\n params.slug ??\n (\"organization\" in jwt.payload ? jwt.payload.organization.slug : null)\n\n if (slug == null) {\n return {}\n }\n\n const domainPrefix = jwt.payload.iss.endsWith(\"commercelayer.co\")\n ? \".stg\"\n : \"\"\n\n const appEndpoint = `https://${slug}${domainPrefix}.commercelayer.app`\n\n const defaultConfig: DefaultMfeConfig = {\n links: {\n cart: `${appEndpoint}/cart/:order_id?accessToken=:access_token`,\n checkout: `${appEndpoint}/checkout/:order_id?accessToken=:access_token`,\n my_account: `${appEndpoint}/my-account?accessToken=:access_token`,\n identity: `${appEndpoint}/identity`,\n microstore:\n params.skuListId != null\n ? `${appEndpoint}/microstore/list/:sku_list_id?accessToken=:access_token`\n : `${appEndpoint}/microstore/sku/:sku_id?accessToken=:access_token`,\n },\n }\n\n return defaultConfig\n } catch (_error) {\n console.log(_error)\n return {}\n }\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAA0B,kCAC1BC,EAAsB,0BA2Ef,SAASC,EAAa,CAC3B,WAAAC,EACA,OAAAC,EACA,OAAAC,CACF,EAA+C,CAhF/C,IAAAC,EAAAC,EAAAC,EAAAC,EAiFE,IAAMC,KAAgB,SACpBC,EAAY,CAAE,OAAAN,CAAO,CAAC,GACtBE,GAAAD,EAAAH,GAAA,YAAAA,EAAY,MAAZ,YAAAG,EAAiB,UAAjB,KAAAC,EAA4B,CAAC,CAC/B,EAEMK,EAAiBR,GAAU,MAAQK,GAAAD,EAAAL,GAAA,YAAAA,EAAY,MAAZ,YAAAK,EAAkBJ,KAAlB,KAAAK,EAA6B,CAAC,EAAK,CAAC,EAG7E,SAASI,EAAoBC,EAA4C,CAzF3E,IAAAR,EAAAC,EAAAC,EAAAC,EAAAM,EAAAC,EAAAC,EA0FI,IAAMC,EAAiB,KAAK,UAAUJ,CAAM,EACzC,QAAQ,UAAUR,EAAAD,GAAA,YAAAA,EAAQ,OAAR,KAAAC,EAAgB,OAAO,EACzC,QAAQ,UAAUC,EAAAF,GAAA,YAAAA,EAAQ,OAAR,KAAAE,EAAgB,OAAO,EACzC,QAAQ,WAAWC,EAAAH,GAAA,YAAAA,EAAQ,QAAR,KAAAG,EAAiB,QAAQ,EAC5C,QAAQ,kBAAkBC,EAAAJ,GAAA,YAAAA,EAAQ,cAAR,KAAAI,EAAuB,eAAe,EAChE,QAAQ,cAAcM,EAAAV,GAAA,YAAAA,EAAQ,UAAR,KAAAU,EAAmB,WAAW,EACpD,QAAQ,iBAAiBC,EAAAX,GAAA,YAAAA,EAAQ,YAAR,KAAAW,EAAqB,cAAc,EAC5D,QAAQ,YAAYC,EAAAZ,GAAA,YAAAA,EAAQ,QAAR,KAAAY,EAAiB,SAAS,EAEjD,OAAO,KAAK,MAAMC,CAAc,CAClC,CAEA,IAAMC,KAAiC,SACrC,KAAK,MAAM,KAAK,UAAUT,CAAa,CAAC,EACxCE,CACF,EAEA,OAAI,OAAO,KAAKO,CAAY,EAAE,SAAW,EAChC,KAGFN,EAAoBM,CAAY,CACzC,CAKA,SAASR,EAAY,CAAE,OAAAN,CAAO,EAAwC,CArHtE,IAAAC,EAsHE,IAAID,GAAA,YAAAA,EAAQ,cAAe,KACzB,MAAO,CAAC,EAGV,GAAI,CACF,IAAMe,KAAM,aAAUf,EAAO,WAAW,EAClCgB,GACJf,EAAAD,EAAO,OAAP,KAAAC,EACC,iBAAkBc,EAAI,QAAUA,EAAI,QAAQ,aAAa,KAAO,KAEnE,GAAIC,GAAQ,KACV,MAAO,CAAC,EAGV,IAAMC,EAAeF,EAAI,QAAQ,IAAI,SAAS,kBAAkB,EAC5D,OACA,GAEEG,EAAc,WAAWF,CAAI,GAAGC,CAAY,qBAelD,MAbwC,CACtC,MAAO,CACL,KAAM,GAAGC,CAAW,4CACpB,SAAU,GAAGA,CAAW,gDACxB,WAAY,GAAGA,CAAW,wCAC1B,SAAU,GAAGA,CAAW,YACxB,WACElB,EAAO,WAAa,KAChB,GAAGkB,CAAW,0DACd,GAAGA,CAAW,mDACtB,CACF,CAGF,OAASC,EAAQ,CACf,eAAQ,IAAIA,CAAM,EACX,CAAC,CACV,CACF","names":["index_exports","__export","getMfeConfig","__toCommonJS","import_js_auth","import_merge_anything","getMfeConfig","jsonConfig","market","params","_a","_b","_c","_d","defaultConfig","getDefaults","overrideConfig","replacePlaceholders","config","_e","_f","_g","replacedConfig","mergedConfig","jwt","slug","domainPrefix","appEndpoint","_error"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/getMfeConfig.ts"],"sourcesContent":["export {\n type DefaultMfeConfig,\n getMfeConfig,\n hasValidLinks,\n type MfeConfigs,\n type MfeConfigWithLinks,\n} from \"./getMfeConfig\"\n","import { jwtDecode } from \"@commercelayer/js-auth\"\nimport { merge } from \"merge-anything\"\nimport type { SetRequired } from \"type-fest\"\nimport type { ValidConfigForOrganizationsInCommerceLayer } from \"./schema/types\"\n\n/**\n * Represents a type that can be null or undefined, making it optional in use.\n * @template T The type that is being made nullable.\n */\ntype NullableType<T> = T | null | undefined\n\n/**\n * Params used by the getConfig function\n */\ninterface ConfigParams {\n /**\n * Language code (e.g., 'en', 'fr') used to dynamically replace the `:lang` placeholder in URLs.\n */\n lang?: NullableType<string>\n /**\n * Organization slug used to replace the `:slug` placeholder in URLs.\n */\n slug?: NullableType<string>\n /**\n * Access token string used to replace the `:access_token` placeholder in URLs.\n */\n accessToken?: NullableType<string>\n /**\n * Unique identifier for an order used to replace the `:order_id` placeholder in URLs.\n */\n orderId?: NullableType<string>\n /**\n * Order token string used to replace the `:token` placeholder in URLs.\n */\n token?: NullableType<string>\n /**\n * Unique identifier for an SKU list used to replace the `:sku_list_id` placeholder in URLs.\n */\n skuListId?: NullableType<string>\n /**\n * Unique identifier for an SKU used to replace the `:sku_id` placeholder in URLs.\n */\n skuId?: NullableType<string>\n /**\n * Type used for the identity, which can be used to replace a `:identity_type` placeholder in URLs.\n */\n identityType?: NullableType<\"login\" | \"signup\">\n /**\n * Client ID used for authentication, which can be used to replace a `:client_id` placeholder in URLs.\n */\n clientId?: NullableType<string>\n /**\n * For the identity, it is the market used for the login process (e.g. a private market).\n */\n scope?: NullableType<string>\n /**\n * For the identity, it is the default scope used by the app to obtain the organization settings needed to customize the UI (name, logo, colors, etc.).\n */\n publicScope?: NullableType<string>\n /**\n * Return URL after certain actions, which can be used to replace a `:return_url` placeholder in URLs.\n */\n returnUrl?: NullableType<string>\n /**\n * URL to the reset password page, which can be used to replace a `:reset_password_url` placeholder in URLs.\n */\n resetPasswordUrl?: NullableType<string>\n}\n\ninterface GetMfeConfigProps {\n /**\n * `config` attribute of the organization\n */\n jsonConfig?: { mfe?: MfeConfigs } | null\n /**\n * Market identifier for fetching specific configuration overrides. (`market:id:hashid`)\n */\n market?: string\n /**\n * Parameters for replacing URL placeholders.\n */\n params?: ConfigParams\n}\n\nexport type MfeConfigs = NonNullable<\n ValidConfigForOrganizationsInCommerceLayer[\"mfe\"]\n>\n\nexport type DefaultMfeConfig = NonNullable<\n NonNullable<ValidConfigForOrganizationsInCommerceLayer[\"mfe\"]>[\"default\"]\n>\n\n/**\n * Retrieves and merges the default organization configuration with market-specific overrides based on the provided market identifier.\n * Placeholder values in the configuration URLs can be replaced with actual values from the `params`.\n *\n * @param jsonConfig The complete configuration object of the organization.\n * @param market The market identifier used to get market-specific configuration overrides.\n * @param params The object containing replacement values for URL placeholders, such as language and access token.\n * @returns The merged configuration object for the specified market, or null if no configuration is found.\n */\nexport function getMfeConfig({\n jsonConfig,\n market,\n params,\n}: GetMfeConfigProps): DefaultMfeConfig | null {\n const defaultConfig = merge(\n getDefaults({ params }),\n jsonConfig?.mfe?.default ?? {},\n )\n\n const overrideConfig = market != null ? (jsonConfig?.mfe?.[market] ?? {}) : {}\n\n // Replace placeholders in all string values within the object\n function replacePlaceholders(config: DefaultMfeConfig): DefaultMfeConfig {\n const replacedConfig = JSON.stringify(config)\n .replace(/:lang/g, params?.lang ?? \":lang\")\n .replace(/:slug/g, params?.slug ?? \":slug\")\n .replace(/:token/g, params?.token ?? \":token\")\n .replace(/:access_token/g, params?.accessToken ?? \":access_token\")\n .replace(/:order_id/g, params?.orderId ?? \":order_id\")\n .replace(/:sku_list_id/g, params?.skuListId ?? \":sku_list_id\")\n .replace(/:sku_id/g, params?.skuId ?? \":sku_id\")\n .replace(/:identity_type/g, params?.identityType ?? \":identity_type\")\n .replace(/:client_id/g, params?.clientId ?? \":client_id\")\n .replace(/:scope/g, params?.scope ?? \":scope\")\n .replace(/:public_scope/g, params?.publicScope ?? \":public_scope\")\n .replace(/:return_url/g, params?.returnUrl ?? \":return_url\")\n .replace(\n /:reset_password_url/g,\n params?.resetPasswordUrl ?? \":reset_password_url\",\n )\n\n return JSON.parse(replacedConfig)\n }\n\n const mergedConfig: DefaultMfeConfig = merge(\n JSON.parse(JSON.stringify(defaultConfig)),\n overrideConfig,\n )\n\n if (Object.keys(mergedConfig).length === 0) {\n return null\n }\n\n return replacePlaceholders(mergedConfig)\n}\n\n/**\n * Provides default MFE configuration.\n */\nfunction getDefaults({ params }: GetMfeConfigProps): DefaultMfeConfig {\n if (params?.accessToken == null) {\n return {}\n }\n\n try {\n const jwt = jwtDecode(params.accessToken)\n const slug =\n params.slug ??\n (\"organization\" in jwt.payload ? jwt.payload.organization.slug : null)\n\n if (slug == null) {\n return {}\n }\n\n const domainPrefix = jwt.payload.iss.endsWith(\"commercelayer.co\")\n ? \".stg\"\n : \"\"\n\n const appEndpoint = `https://${slug}${domainPrefix}.commercelayer.app`\n\n const defaultConfig: DefaultMfeConfig = {\n links: {\n cart: `${appEndpoint}/cart/:order_id?accessToken=:access_token`,\n checkout: `${appEndpoint}/checkout/:order_id?accessToken=:access_token`,\n my_account: `${appEndpoint}/my-account?accessToken=:access_token`,\n identity: `${appEndpoint}/identity/:identity_type?${[\n \"clientId=:client_id\",\n \"scope=:scope\",\n params.publicScope != null ? \"publicScope=:public_scope\" : null,\n params.lang != null ? \"lang=:lang\" : null,\n params.returnUrl != null ? \"returnUrl=:return_url\" : null,\n params.resetPasswordUrl != null\n ? \"resetPasswordUrl=:reset_password_url\"\n : null,\n ]\n .filter((part) => part != null)\n .join(\"&\")}`,\n microstore:\n params.skuListId != null\n ? `${appEndpoint}/microstore/list/:sku_list_id?accessToken=:access_token`\n : `${appEndpoint}/microstore/sku/:sku_id?accessToken=:access_token`,\n },\n }\n\n return defaultConfig\n } catch (_error) {\n console.log(_error)\n return {}\n }\n}\n\nexport function hasValidLinks(\n config: ReturnType<typeof getMfeConfig>,\n): config is MfeConfigWithLinks {\n return (\n config != null &&\n config.links != null &&\n config.links.cart != null &&\n config.links.checkout != null &&\n config.links.identity != null &&\n config.links.my_account != null\n )\n}\n\nexport type MfeConfigWithLinks = Omit<DefaultMfeConfig, \"links\"> & {\n links: SetRequired<\n NonNullable<DefaultMfeConfig[\"links\"]>,\n \"cart\" | \"checkout\" | \"identity\" | \"my_account\"\n >\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,EAAA,kBAAAC,IAAA,eAAAC,EAAAJ,GCAA,IAAAK,EAA0B,kCAC1BC,EAAsB,0BAoGf,SAASC,EAAa,CAC3B,WAAAC,EACA,OAAAC,EACA,OAAAC,CACF,EAA+C,CAzG/C,IAAAC,EAAAC,EAAAC,EAAAC,EA0GE,IAAMC,KAAgB,SACpBC,EAAY,CAAE,OAAAN,CAAO,CAAC,GACtBE,GAAAD,EAAAH,GAAA,YAAAA,EAAY,MAAZ,YAAAG,EAAiB,UAAjB,KAAAC,EAA4B,CAAC,CAC/B,EAEMK,EAAiBR,GAAU,MAAQK,GAAAD,EAAAL,GAAA,YAAAA,EAAY,MAAZ,YAAAK,EAAkBJ,KAAlB,KAAAK,EAA6B,CAAC,EAAK,CAAC,EAG7E,SAASI,EAAoBC,EAA4C,CAlH3E,IAAAR,EAAAC,EAAAC,EAAAC,EAAAM,EAAAC,EAAAC,EAAAC,EAAAC,EAAAC,EAAAC,EAAAC,EAAAC,EAmHI,IAAMC,EAAiB,KAAK,UAAUV,CAAM,EACzC,QAAQ,UAAUR,EAAAD,GAAA,YAAAA,EAAQ,OAAR,KAAAC,EAAgB,OAAO,EACzC,QAAQ,UAAUC,EAAAF,GAAA,YAAAA,EAAQ,OAAR,KAAAE,EAAgB,OAAO,EACzC,QAAQ,WAAWC,EAAAH,GAAA,YAAAA,EAAQ,QAAR,KAAAG,EAAiB,QAAQ,EAC5C,QAAQ,kBAAkBC,EAAAJ,GAAA,YAAAA,EAAQ,cAAR,KAA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EAAI,QAAQ,IAAI,SAAS,kBAAkB,EAC5D,OACA,GAEEG,EAAc,WAAWF,CAAI,GAAGC,CAAY,qBA0BlD,MAxBwC,CACtC,MAAO,CACL,KAAM,GAAGC,CAAW,4CACpB,SAAU,GAAGA,CAAW,gDACxB,WAAY,GAAGA,CAAW,wCAC1B,SAAU,GAAGA,CAAW,4BAA4B,CAClD,sBACA,eACAxB,EAAO,aAAe,KAAO,4BAA8B,KAC3DA,EAAO,MAAQ,KAAO,aAAe,KACrCA,EAAO,WAAa,KAAO,wBAA0B,KACrDA,EAAO,kBAAoB,KACvB,uCACA,IACN,EACG,OAAQyB,GAASA,GAAQ,IAAI,EAC7B,KAAK,GAAG,CAAC,GACZ,WACEzB,EAAO,WAAa,KAChB,GAAGwB,CAAW,0DACd,GAAGA,CAAW,mDACtB,CACF,CAGF,OAASE,EAAQ,CACf,eAAQ,IAAIA,CAAM,EACX,CAAC,CACV,CACF,CAEO,SAASC,EACdlB,EAC8B,CAC9B,OACEA,GAAU,MACVA,EAAO,OAAS,MAChBA,EAAO,MAAM,MAAQ,MACrBA,EAAO,MAAM,UAAY,MACzBA,EAAO,MAAM,UAAY,MACzBA,EAAO,MAAM,YAAc,IAE/B","names":["index_exports","__export","getMfeConfig","hasValidLinks","__toCommonJS","import_js_auth","import_merge_anything","getMfeConfig","jsonConfig","market","params","_a","_b","_c","_d","defaultConfig","getDefaults","overrideConfig","replacePlaceholders","config","_e","_f","_g","_h","_i","_j","_k","_l","_m","replacedConfig","mergedConfig","jwt","slug","domainPrefix","appEndpoint","part","_error","hasValidLinks"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { SetRequired } from 'type-fest';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* This file was automatically generated by json-schema-to-typescript.
|
|
3
5
|
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
|
|
@@ -252,6 +254,30 @@ interface ConfigParams {
|
|
|
252
254
|
* Unique identifier for an SKU used to replace the `:sku_id` placeholder in URLs.
|
|
253
255
|
*/
|
|
254
256
|
skuId?: NullableType<string>;
|
|
257
|
+
/**
|
|
258
|
+
* Type used for the identity, which can be used to replace a `:identity_type` placeholder in URLs.
|
|
259
|
+
*/
|
|
260
|
+
identityType?: NullableType<"login" | "signup">;
|
|
261
|
+
/**
|
|
262
|
+
* Client ID used for authentication, which can be used to replace a `:client_id` placeholder in URLs.
|
|
263
|
+
*/
|
|
264
|
+
clientId?: NullableType<string>;
|
|
265
|
+
/**
|
|
266
|
+
* For the identity, it is the market used for the login process (e.g. a private market).
|
|
267
|
+
*/
|
|
268
|
+
scope?: NullableType<string>;
|
|
269
|
+
/**
|
|
270
|
+
* For the identity, it is the default scope used by the app to obtain the organization settings needed to customize the UI (name, logo, colors, etc.).
|
|
271
|
+
*/
|
|
272
|
+
publicScope?: NullableType<string>;
|
|
273
|
+
/**
|
|
274
|
+
* Return URL after certain actions, which can be used to replace a `:return_url` placeholder in URLs.
|
|
275
|
+
*/
|
|
276
|
+
returnUrl?: NullableType<string>;
|
|
277
|
+
/**
|
|
278
|
+
* URL to the reset password page, which can be used to replace a `:reset_password_url` placeholder in URLs.
|
|
279
|
+
*/
|
|
280
|
+
resetPasswordUrl?: NullableType<string>;
|
|
255
281
|
}
|
|
256
282
|
interface GetMfeConfigProps {
|
|
257
283
|
/**
|
|
@@ -281,5 +307,9 @@ type DefaultMfeConfig = NonNullable<NonNullable<ValidConfigForOrganizationsInCom
|
|
|
281
307
|
* @returns The merged configuration object for the specified market, or null if no configuration is found.
|
|
282
308
|
*/
|
|
283
309
|
declare function getMfeConfig({ jsonConfig, market, params, }: GetMfeConfigProps): DefaultMfeConfig | null;
|
|
310
|
+
declare function hasValidLinks(config: ReturnType<typeof getMfeConfig>): config is MfeConfigWithLinks;
|
|
311
|
+
type MfeConfigWithLinks = Omit<DefaultMfeConfig, "links"> & {
|
|
312
|
+
links: SetRequired<NonNullable<DefaultMfeConfig["links"]>, "cart" | "checkout" | "identity" | "my_account">;
|
|
313
|
+
};
|
|
284
314
|
|
|
285
|
-
export { type DefaultMfeConfig, type MfeConfigs, getMfeConfig };
|
|
315
|
+
export { type DefaultMfeConfig, type MfeConfigWithLinks, type MfeConfigs, getMfeConfig, hasValidLinks };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { SetRequired } from 'type-fest';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* This file was automatically generated by json-schema-to-typescript.
|
|
3
5
|
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
|
|
@@ -252,6 +254,30 @@ interface ConfigParams {
|
|
|
252
254
|
* Unique identifier for an SKU used to replace the `:sku_id` placeholder in URLs.
|
|
253
255
|
*/
|
|
254
256
|
skuId?: NullableType<string>;
|
|
257
|
+
/**
|
|
258
|
+
* Type used for the identity, which can be used to replace a `:identity_type` placeholder in URLs.
|
|
259
|
+
*/
|
|
260
|
+
identityType?: NullableType<"login" | "signup">;
|
|
261
|
+
/**
|
|
262
|
+
* Client ID used for authentication, which can be used to replace a `:client_id` placeholder in URLs.
|
|
263
|
+
*/
|
|
264
|
+
clientId?: NullableType<string>;
|
|
265
|
+
/**
|
|
266
|
+
* For the identity, it is the market used for the login process (e.g. a private market).
|
|
267
|
+
*/
|
|
268
|
+
scope?: NullableType<string>;
|
|
269
|
+
/**
|
|
270
|
+
* For the identity, it is the default scope used by the app to obtain the organization settings needed to customize the UI (name, logo, colors, etc.).
|
|
271
|
+
*/
|
|
272
|
+
publicScope?: NullableType<string>;
|
|
273
|
+
/**
|
|
274
|
+
* Return URL after certain actions, which can be used to replace a `:return_url` placeholder in URLs.
|
|
275
|
+
*/
|
|
276
|
+
returnUrl?: NullableType<string>;
|
|
277
|
+
/**
|
|
278
|
+
* URL to the reset password page, which can be used to replace a `:reset_password_url` placeholder in URLs.
|
|
279
|
+
*/
|
|
280
|
+
resetPasswordUrl?: NullableType<string>;
|
|
255
281
|
}
|
|
256
282
|
interface GetMfeConfigProps {
|
|
257
283
|
/**
|
|
@@ -281,5 +307,9 @@ type DefaultMfeConfig = NonNullable<NonNullable<ValidConfigForOrganizationsInCom
|
|
|
281
307
|
* @returns The merged configuration object for the specified market, or null if no configuration is found.
|
|
282
308
|
*/
|
|
283
309
|
declare function getMfeConfig({ jsonConfig, market, params, }: GetMfeConfigProps): DefaultMfeConfig | null;
|
|
310
|
+
declare function hasValidLinks(config: ReturnType<typeof getMfeConfig>): config is MfeConfigWithLinks;
|
|
311
|
+
type MfeConfigWithLinks = Omit<DefaultMfeConfig, "links"> & {
|
|
312
|
+
links: SetRequired<NonNullable<DefaultMfeConfig["links"]>, "cart" | "checkout" | "identity" | "my_account">;
|
|
313
|
+
};
|
|
284
314
|
|
|
285
|
-
export { type DefaultMfeConfig, type MfeConfigs, getMfeConfig };
|
|
315
|
+
export { type DefaultMfeConfig, type MfeConfigWithLinks, type MfeConfigs, getMfeConfig, hasValidLinks };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{jwtDecode as
|
|
1
|
+
import{jwtDecode as S}from"@commercelayer/js-auth";import{merge as D}from"merge-anything";function w({jsonConfig:l,market:n,params:e}){var o,r,f,g;let i=D(O({params:e}),(r=(o=l==null?void 0:l.mfe)==null?void 0:o.default)!=null?r:{}),c=n!=null?(g=(f=l==null?void 0:l.mfe)==null?void 0:f[n])!=null?g:{}:{};function t(L){var s,d,y,k,_,C,T,b,N,M,p,h,I;let P=JSON.stringify(L).replace(/:lang/g,(s=e==null?void 0:e.lang)!=null?s:":lang").replace(/:slug/g,(d=e==null?void 0:e.slug)!=null?d:":slug").replace(/:token/g,(y=e==null?void 0:e.token)!=null?y:":token").replace(/:access_token/g,(k=e==null?void 0:e.accessToken)!=null?k:":access_token").replace(/:order_id/g,(_=e==null?void 0:e.orderId)!=null?_:":order_id").replace(/:sku_list_id/g,(C=e==null?void 0:e.skuListId)!=null?C:":sku_list_id").replace(/:sku_id/g,(T=e==null?void 0:e.skuId)!=null?T:":sku_id").replace(/:identity_type/g,(b=e==null?void 0:e.identityType)!=null?b:":identity_type").replace(/:client_id/g,(N=e==null?void 0:e.clientId)!=null?N:":client_id").replace(/:scope/g,(M=e==null?void 0:e.scope)!=null?M:":scope").replace(/:public_scope/g,(p=e==null?void 0:e.publicScope)!=null?p:":public_scope").replace(/:return_url/g,(h=e==null?void 0:e.returnUrl)!=null?h:":return_url").replace(/:reset_password_url/g,(I=e==null?void 0:e.resetPasswordUrl)!=null?I:":reset_password_url");return JSON.parse(P)}let u=D(JSON.parse(JSON.stringify(i)),c);return Object.keys(u).length===0?null:t(u)}function O({params:l}){var n;if((l==null?void 0:l.accessToken)==null)return{};try{let e=S(l.accessToken),i=(n=l.slug)!=null?n:"organization"in e.payload?e.payload.organization.slug:null;if(i==null)return{};let c=e.payload.iss.endsWith("commercelayer.co")?".stg":"",t=`https://${i}${c}.commercelayer.app`;return{links:{cart:`${t}/cart/:order_id?accessToken=:access_token`,checkout:`${t}/checkout/:order_id?accessToken=:access_token`,my_account:`${t}/my-account?accessToken=:access_token`,identity:`${t}/identity/:identity_type?${["clientId=:client_id","scope=:scope",l.publicScope!=null?"publicScope=:public_scope":null,l.lang!=null?"lang=:lang":null,l.returnUrl!=null?"returnUrl=:return_url":null,l.resetPasswordUrl!=null?"resetPasswordUrl=:reset_password_url":null].filter(o=>o!=null).join("&")}`,microstore:l.skuListId!=null?`${t}/microstore/list/:sku_list_id?accessToken=:access_token`:`${t}/microstore/sku/:sku_id?accessToken=:access_token`}}}catch(e){return console.log(e),{}}}function $(l){return l!=null&&l.links!=null&&l.links.cart!=null&&l.links.checkout!=null&&l.links.identity!=null&&l.links.my_account!=null}export{w as getMfeConfig,$ as hasValidLinks};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/getMfeConfig.ts"],"sourcesContent":["import { jwtDecode } from \"@commercelayer/js-auth\"\nimport { merge } from \"merge-anything\"\nimport type { ValidConfigForOrganizationsInCommerceLayer } from \"./schema/types\"\n\n/**\n * Represents a type that can be null or undefined, making it optional in use.\n * @template T The type that is being made nullable.\n */\ntype NullableType<T> = T | null | undefined\n\n/**\n * Params used by the getConfig function\n */\ninterface ConfigParams {\n /**\n * Language code (e.g., 'en', 'fr') used to dynamically replace the `:lang` placeholder in URLs.\n */\n lang?: NullableType<string>\n /**\n * Organization slug used to replace the `:slug` placeholder in URLs.\n */\n slug?: NullableType<string>\n /**\n * Access token string used to replace the `:access_token` placeholder in URLs.\n */\n accessToken?: NullableType<string>\n /**\n * Unique identifier for an order used to replace the `:order_id` placeholder in URLs.\n */\n orderId?: NullableType<string>\n /**\n * Order token string used to replace the `:token` placeholder in URLs.\n */\n token?: NullableType<string>\n /**\n * Unique identifier for an SKU list used to replace the `:sku_list_id` placeholder in URLs.\n */\n skuListId?: NullableType<string>\n /**\n * Unique identifier for an SKU used to replace the `:sku_id` placeholder in URLs.\n */\n skuId?: NullableType<string>\n}\n\ninterface GetMfeConfigProps {\n /**\n * `config` attribute of the organization\n */\n jsonConfig?: { mfe?: MfeConfigs } | null\n /**\n * Market identifier for fetching specific configuration overrides. (`market:id:hashid`)\n */\n market?: string\n /**\n * Parameters for replacing URL placeholders.\n */\n params?: ConfigParams\n}\n\nexport type MfeConfigs = NonNullable<\n ValidConfigForOrganizationsInCommerceLayer[\"mfe\"]\n>\n\nexport type DefaultMfeConfig = NonNullable<\n NonNullable<ValidConfigForOrganizationsInCommerceLayer[\"mfe\"]>[\"default\"]\n>\n\n/**\n * Retrieves and merges the default organization configuration with market-specific overrides based on the provided market identifier.\n * Placeholder values in the configuration URLs can be replaced with actual values from the `params`.\n *\n * @param jsonConfig The complete configuration object of the organization.\n * @param market The market identifier used to get market-specific configuration overrides.\n * @param params The object containing replacement values for URL placeholders, such as language and access token.\n * @returns The merged configuration object for the specified market, or null if no configuration is found.\n */\nexport function getMfeConfig({\n jsonConfig,\n market,\n params,\n}: GetMfeConfigProps): DefaultMfeConfig | null {\n const defaultConfig = merge(\n getDefaults({ params }),\n jsonConfig?.mfe?.default ?? {},\n )\n\n const overrideConfig = market != null ? (jsonConfig?.mfe?.[market] ?? {}) : {}\n\n // Replace placeholders in all string values within the object\n function replacePlaceholders(config: DefaultMfeConfig): DefaultMfeConfig {\n const replacedConfig = JSON.stringify(config)\n .replace(/:lang/g, params?.lang ?? \":lang\")\n .replace(/:slug/g, params?.slug ?? \":slug\")\n .replace(/:token/g, params?.token ?? \":token\")\n .replace(/:access_token/g, params?.accessToken ?? \":access_token\")\n .replace(/:order_id/g, params?.orderId ?? \":order_id\")\n .replace(/:sku_list_id/g, params?.skuListId ?? \":sku_list_id\")\n .replace(/:sku_id/g, params?.skuId ?? \":sku_id\")\n\n return JSON.parse(replacedConfig)\n }\n\n const mergedConfig: DefaultMfeConfig = merge(\n JSON.parse(JSON.stringify(defaultConfig)),\n overrideConfig,\n )\n\n if (Object.keys(mergedConfig).length === 0) {\n return null\n }\n\n return replacePlaceholders(mergedConfig)\n}\n\n/**\n * Provides default MFE configuration.\n */\nfunction getDefaults({ params }: GetMfeConfigProps): DefaultMfeConfig {\n if (params?.accessToken == null) {\n return {}\n }\n\n try {\n const jwt = jwtDecode(params.accessToken)\n const slug =\n params.slug ??\n (\"organization\" in jwt.payload ? jwt.payload.organization.slug : null)\n\n if (slug == null) {\n return {}\n }\n\n const domainPrefix = jwt.payload.iss.endsWith(\"commercelayer.co\")\n ? \".stg\"\n : \"\"\n\n const appEndpoint = `https://${slug}${domainPrefix}.commercelayer.app`\n\n const defaultConfig: DefaultMfeConfig = {\n links: {\n cart: `${appEndpoint}/cart/:order_id?accessToken=:access_token`,\n checkout: `${appEndpoint}/checkout/:order_id?accessToken=:access_token`,\n my_account: `${appEndpoint}/my-account?accessToken=:access_token`,\n identity: `${appEndpoint}/identity`,\n microstore:\n params.skuListId != null\n ? `${appEndpoint}/microstore/list/:sku_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iB,SAAS,EAEjD,OAAO,KAAK,MAAMC,CAAc,CAClC,CAEA,IAAMC,EAAiClB,EACrC,KAAK,MAAM,KAAK,UAAUS,CAAa,CAAC,EACxCE,CACF,EAEA,OAAI,OAAO,KAAKO,CAAY,EAAE,SAAW,EAChC,KAGFN,EAAoBM,CAAY,CACzC,CAKA,SAASR,EAAY,CAAE,OAAAN,CAAO,EAAwC,CArHtE,IAAAC,EAsHE,IAAID,GAAA,YAAAA,EAAQ,cAAe,KACzB,MAAO,CAAC,EAGV,GAAI,CACF,IAAMe,EAAMpB,EAAUK,EAAO,WAAW,EAClCgB,GACJf,EAAAD,EAAO,OAAP,KAAAC,EACC,iBAAkBc,EAAI,QAAUA,EAAI,QAAQ,aAAa,KAAO,KAEnE,GAAIC,GAAQ,KACV,MAAO,CAAC,EAGV,IAAMC,EAAeF,EAAI,QAAQ,IAAI,SAAS,kBAAkB,EAC5D,OACA,GAEEG,EAAc,WAAWF,CAAI,GAAGC,CAAY,qBAelD,MAbwC,CACtC,MAAO,CACL,KAAM,GAAGC,CAAW,4CACpB,SAAU,GAAGA,CAAW,gDACxB,WAAY,GAAGA,CAAW,wCAC1B,SAAU,GAAGA,CAAW,YACxB,WACElB,EAAO,WAAa,KAChB,GAAGkB,CAAW,0DACd,GAAGA,CAAW,mDACtB,CACF,CAGF,OAASC,EAAQ,CACf,eAAQ,IAAIA,CAAM,EACX,CAAC,CACV,CACF","names":["jwtDecode","merge","getMfeConfig","jsonConfig","market","params","_a","_b","_c","_d","defaultConfig","getDefaults","overrideConfig","replacePlaceholders","config","_e","_f","_g","replacedConfig","mergedConfig","jwt","slug","domainPrefix","appEndpoint","_error"]}
|
|
1
|
+
{"version":3,"sources":["../src/getMfeConfig.ts"],"sourcesContent":["import { jwtDecode } from \"@commercelayer/js-auth\"\nimport { merge } from \"merge-anything\"\nimport type { SetRequired } from \"type-fest\"\nimport type { ValidConfigForOrganizationsInCommerceLayer } from \"./schema/types\"\n\n/**\n * Represents a type that can be null or undefined, making it optional in use.\n * @template T The type that is being made nullable.\n */\ntype NullableType<T> = T | null | undefined\n\n/**\n * Params used by the getConfig function\n */\ninterface ConfigParams {\n /**\n * Language code (e.g., 'en', 'fr') used to dynamically replace the `:lang` placeholder in URLs.\n */\n lang?: NullableType<string>\n /**\n * Organization slug used to replace the `:slug` placeholder in URLs.\n */\n slug?: NullableType<string>\n /**\n * Access token string used to replace the `:access_token` placeholder in URLs.\n */\n accessToken?: NullableType<string>\n /**\n * Unique identifier for an order used to replace the `:order_id` placeholder in URLs.\n */\n orderId?: NullableType<string>\n /**\n * Order token string used to replace the `:token` placeholder in URLs.\n */\n token?: NullableType<string>\n /**\n * Unique identifier for an SKU list used to replace the `:sku_list_id` placeholder in URLs.\n */\n skuListId?: NullableType<string>\n /**\n * Unique identifier for an SKU used to replace the `:sku_id` placeholder in URLs.\n */\n skuId?: NullableType<string>\n /**\n * Type used for the identity, which can be used to replace a `:identity_type` placeholder in URLs.\n */\n identityType?: NullableType<\"login\" | \"signup\">\n /**\n * Client ID used for authentication, which can be used to replace a `:client_id` placeholder in URLs.\n */\n clientId?: NullableType<string>\n /**\n * For the identity, it is the market used for the login process (e.g. a private market).\n */\n scope?: NullableType<string>\n /**\n * For the identity, it is the default scope used by the app to obtain the organization settings needed to customize the UI (name, logo, colors, etc.).\n */\n publicScope?: NullableType<string>\n /**\n * Return URL after certain actions, which can be used to replace a `:return_url` placeholder in URLs.\n */\n returnUrl?: NullableType<string>\n /**\n * URL to the reset password page, which can be used to replace a `:reset_password_url` placeholder in URLs.\n */\n resetPasswordUrl?: NullableType<string>\n}\n\ninterface GetMfeConfigProps {\n /**\n * `config` attribute of the organization\n */\n jsonConfig?: { mfe?: MfeConfigs } | null\n /**\n * Market identifier for fetching specific configuration overrides. (`market:id:hashid`)\n */\n market?: string\n /**\n * Parameters for replacing URL placeholders.\n */\n params?: ConfigParams\n}\n\nexport type MfeConfigs = NonNullable<\n ValidConfigForOrganizationsInCommerceLayer[\"mfe\"]\n>\n\nexport type DefaultMfeConfig = NonNullable<\n NonNullable<ValidConfigForOrganizationsInCommerceLayer[\"mfe\"]>[\"default\"]\n>\n\n/**\n * Retrieves and merges the default organization configuration with market-specific overrides based on the provided market identifier.\n * Placeholder values in the configuration URLs can be replaced with actual values from the `params`.\n *\n * @param jsonConfig The complete configuration object of the organization.\n * @param market The market identifier used to get market-specific configuration overrides.\n * @param params The object containing replacement values for URL placeholders, such as language and access token.\n * @returns The merged configuration object for the specified market, or null if no configuration is found.\n */\nexport function getMfeConfig({\n jsonConfig,\n market,\n params,\n}: GetMfeConfigProps): DefaultMfeConfig | null {\n const defaultConfig = merge(\n getDefaults({ params }),\n jsonConfig?.mfe?.default ?? {},\n )\n\n const overrideConfig = market != null ? (jsonConfig?.mfe?.[market] ?? {}) : {}\n\n // Replace placeholders in all string values within the object\n function replacePlaceholders(config: DefaultMfeConfig): DefaultMfeConfig {\n const replacedConfig = JSON.stringify(config)\n .replace(/:lang/g, params?.lang ?? \":lang\")\n .replace(/:slug/g, params?.slug ?? \":slug\")\n .replace(/:token/g, params?.token ?? \":token\")\n .replace(/:access_token/g, params?.accessToken ?? \":access_token\")\n .replace(/:order_id/g, params?.orderId ?? \":order_id\")\n .replace(/:sku_list_id/g, params?.skuListId ?? \":sku_list_id\")\n .replace(/:sku_id/g, params?.skuId ?? \":sku_id\")\n .replace(/:identity_type/g, params?.identityType ?? \":identity_type\")\n .replace(/:client_id/g, params?.clientId ?? \":client_id\")\n .replace(/:scope/g, params?.scope ?? \":scope\")\n .replace(/:public_scope/g, params?.publicScope ?? \":public_scope\")\n .replace(/:return_url/g, params?.returnUrl ?? \":return_url\")\n .replace(\n /:reset_password_url/g,\n params?.resetPasswordUrl ?? \":reset_password_url\",\n )\n\n return JSON.parse(replacedConfig)\n }\n\n const mergedConfig: DefaultMfeConfig = merge(\n JSON.parse(JSON.stringify(defaultConfig)),\n overrideConfig,\n )\n\n if (Object.keys(mergedConfig).length === 0) {\n return null\n }\n\n return replacePlaceholders(mergedConfig)\n}\n\n/**\n * Provides default MFE configuration.\n */\nfunction getDefaults({ params }: GetMfeConfigProps): DefaultMfeConfig {\n if (params?.accessToken == null) {\n return {}\n }\n\n try {\n const jwt = jwtDecode(params.accessToken)\n const slug =\n params.slug ??\n (\"organization\" in jwt.payload ? jwt.payload.organization.slug : null)\n\n if (slug == null) {\n return {}\n }\n\n const domainPrefix = jwt.payload.iss.endsWith(\"commercelayer.co\")\n ? \".stg\"\n : \"\"\n\n const appEndpoint = `https://${slug}${domainPrefix}.commercelayer.app`\n\n const defaultConfig: DefaultMfeConfig = {\n links: {\n cart: `${appEndpoint}/cart/:order_id?accessToken=:access_token`,\n checkout: `${appEndpoint}/checkout/:order_id?accessToken=:access_token`,\n my_account: `${appEndpoint}/my-account?accessToken=:access_token`,\n identity: `${appEndpoint}/identity/:identity_type?${[\n \"clientId=:client_id\",\n \"scope=:scope\",\n params.publicScope != null ? \"publicScope=:public_scope\" : null,\n params.lang != null ? \"lang=:lang\" : null,\n params.returnUrl != null ? \"returnUrl=:return_url\" : null,\n params.resetPasswordUrl != null\n ? \"resetPasswordUrl=:reset_password_url\"\n : null,\n ]\n .filter((part) => part != null)\n .join(\"&\")}`,\n microstore:\n params.skuListId != null\n ? `${appEndpoint}/microstore/list/:sku_list_id?accessToken=:access_token`\n : `${appEndpoint}/microstore/sku/:sku_id?accessToken=:access_token`,\n },\n }\n\n return defaultConfig\n } catch (_error) {\n console.log(_error)\n return {}\n }\n}\n\nexport function hasValidLinks(\n config: ReturnType<typeof getMfeConfig>,\n): config is MfeConfigWithLinks {\n return (\n config != null &&\n config.links != null &&\n config.links.cart != null &&\n config.links.checkout != null &&\n config.links.identity != null &&\n config.links.my_account != null\n )\n}\n\nexport type MfeConfigWithLinks = Omit<DefaultMfeConfig, \"links\"> & {\n links: SetRequired<\n NonNullable<DefaultMfeConfig[\"links\"]>,\n \"cart\" | \"checkout\" | \"identity\" | \"my_account\"\n >\n}\n"],"mappings":"AAAA,OAAS,aAA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A0B,KACrDA,EAAO,kBAAoB,KACvB,uCACA,IACN,EACG,OAAQyB,GAASA,GAAQ,IAAI,EAC7B,KAAK,GAAG,CAAC,GACZ,WACEzB,EAAO,WAAa,KAChB,GAAGwB,CAAW,0DACd,GAAGA,CAAW,mDACtB,CACF,CAGF,OAASE,EAAQ,CACf,eAAQ,IAAIA,CAAM,EACX,CAAC,CACV,CACF,CAEO,SAASC,EACdlB,EAC8B,CAC9B,OACEA,GAAU,MACVA,EAAO,OAAS,MAChBA,EAAO,MAAM,MAAQ,MACrBA,EAAO,MAAM,UAAY,MACzBA,EAAO,MAAM,UAAY,MACzBA,EAAO,MAAM,YAAc,IAE/B","names":["jwtDecode","merge","getMfeConfig","jsonConfig","market","params","_a","_b","_c","_d","defaultConfig","getDefaults","overrideConfig","replacePlaceholders","config","_e","_f","_g","_h","_i","_j","_k","_l","_m","replacedConfig","mergedConfig","jwt","slug","domainPrefix","appEndpoint","part","_error","hasValidLinks"]}
|