@commercelayer/organization-config 2.5.0 → 2.7.0

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 CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var f=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var P=(l,t)=>{for(var e in t)f(l,e,{get:t[e],enumerable:!0})},x=(l,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of D(t))!L.call(l,n)&&n!==e&&f(l,n,{get:()=>t[n],enumerable:!(i=O(t,n))||i.enumerable});return l};var J=l=>x(f({},"__esModule",{value:!0}),l);var S={};P(S,{getMfeConfig:()=>T});module.exports=J(S);var C=require("merge-anything");function T({jsonConfig:l,market:t,params:e}){var u,g,o;if((l==null?void 0:l.mfe)==null)return null;let i=(g=(u=l==null?void 0:l.mfe)==null?void 0:u.default)!=null?g:{},n=t!=null?(o=l==null?void 0:l.mfe[t])!=null?o:{}:{};function b(I){var r,c,d,y,N,k,M;let s=JSON.stringify(I).replace(/:lang/g,(r=e==null?void 0:e.lang)!=null?r:":lang").replace(/:slug/g,(c=e==null?void 0:e.slug)!=null?c:":slug").replace(/:token/g,(d=e==null?void 0:e.token)!=null?d:":token").replace(/:access_token/g,(y=e==null?void 0:e.accessToken)!=null?y:":access_token").replace(/:order_id/g,(N=e==null?void 0:e.orderId)!=null?N:":order_id").replace(/:sku_list_id/g,(k=e==null?void 0:e.skuListId)!=null?k:":sku_list_id").replace(/:sku_id/g,(M=e==null?void 0:e.skuId)!=null?M:":sku_id");return JSON.parse(s)}let _=(0,C.merge)(JSON.parse(JSON.stringify(i)),n);return b(_)}0&&(module.exports={getMfeConfig});
1
+ "use strict";var f=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var $=Object.prototype.hasOwnProperty;var P=(t,n)=>{for(var e in n)f(t,e,{get:n[e],enumerable:!0})},L=(t,n,e,i)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of O(n))!$.call(t,o)&&o!==e&&f(t,o,{get:()=>n[o],enumerable:!(i=h(n,o))||i.enumerable});return t};var x=t=>L(f({},"__esModule",{value:!0}),t);var p={};P(p,{getMfeConfig:()=>b});module.exports=x(p);var a=require("@commercelayer/js-auth"),u=require("merge-anything");function b({jsonConfig:t,market:n,params:e}){var g,r,s,d;let i=(0,u.merge)(z({params:e}),(r=(g=t==null?void 0:t.mfe)==null?void 0:g.default)!=null?r:{}),o=n!=null?(d=(s=t==null?void 0:t.mfe)==null?void 0:s[n])!=null?d:{}:{};function l(D){var k,y,C,_,T,N,M;let I=JSON.stringify(D).replace(/:lang/g,(k=e==null?void 0:e.lang)!=null?k:":lang").replace(/:slug/g,(y=e==null?void 0:e.slug)!=null?y:":slug").replace(/:token/g,(C=e==null?void 0:e.token)!=null?C:":token").replace(/:access_token/g,(_=e==null?void 0:e.accessToken)!=null?_:":access_token").replace(/:order_id/g,(T=e==null?void 0:e.orderId)!=null?T:":order_id").replace(/:sku_list_id/g,(N=e==null?void 0:e.skuListId)!=null?N:":sku_list_id").replace(/:sku_id/g,(M=e==null?void 0:e.skuId)!=null?M:":sku_id");return JSON.parse(I)}let c=(0,u.merge)(JSON.parse(JSON.stringify(i)),o);return Object.keys(c).length===0?null:l(c)}function z({params:t}){var n;if((t==null?void 0:t.accessToken)==null)return{};try{let e=(0,a.jwtDecode)(t.accessToken),i=(n=t.slug)!=null?n:"organization"in e.payload?e.payload.organization.slug:null;if(i==null)return{};let o=e.payload.iss.endsWith("commercelayer.co")?".stg":"",l=`https://${i}${o}.commercelayer.app`;return{links:{cart:`${l}/cart/:order_id?accessToken=:access_token`,checkout:`${l}/checkout/:order_id?accessToken=:access_token`,my_account:`${l}/my-account?accessToken=:access_token`,identity:`${l}/identity`,microstore:t.skuListId!=null?`${l}/microstore/list/:sku_list_id?accessToken=:access_token`:`${l}/microstore/sku/:sku_id?accessToken=:access_token`}}}catch(e){return console.log(e),{}}}0&&(module.exports={getMfeConfig});
2
2
  //# sourceMappingURL=index.cjs.map
@@ -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 { 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 }\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 if (jsonConfig?.mfe == null) {\n return null\n }\n\n const defaultConfig = jsonConfig?.mfe?.default ?? {}\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 return replacePlaceholders(mergedConfig)\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAAsB,0BA2Ef,SAASC,EAAa,CAC3B,WAAAC,EACA,OAAAC,EACA,OAAAC,CACF,EAA+C,CA/E/C,IAAAC,EAAAC,EAAAC,EAgFE,IAAIL,GAAA,YAAAA,EAAY,MAAO,KACrB,OAAO,KAGT,IAAMM,GAAgBF,GAAAD,EAAAH,GAAA,YAAAA,EAAY,MAAZ,YAAAG,EAAiB,UAAjB,KAAAC,EAA4B,CAAC,EAC7CG,EAAiBN,GAAU,MAAQI,EAAAL,GAAA,YAAAA,EAAY,IAAIC,KAAhB,KAAAI,EAA2B,CAAC,EAAK,CAAC,EAG3E,SAASG,EAAoBC,EAA4C,CAxF3E,IAAAN,EAAAC,EAAAC,EAAAK,EAAAC,EAAAC,EAAAC,EAyFI,IAAMC,EAAiB,KAAK,UAAUL,CAAM,EACzC,QAAQ,UAAUN,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,kBAAkBK,EAAAR,GAAA,YAAAA,EAAQ,cAAR,KAAAQ,EAAuB,eAAe,EAChE,QAAQ,cAAcC,EAAAT,GAAA,YAAAA,EAAQ,UAAR,KAAAS,EAAmB,WAAW,EACpD,QAAQ,iBAAiBC,EAAAV,GAAA,YAAAA,EAAQ,YAAR,KAAAU,EAAqB,cAAc,EAC5D,QAAQ,YAAYC,EAAAX,GAAA,YAAAA,EAAQ,QAAR,KAAAW,EAAiB,SAAS,EAEjD,OAAO,KAAK,MAAMC,CAAc,CAClC,CAEA,IAAMC,KAAiC,SACrC,KAAK,MAAM,KAAK,UAAUT,CAAa,CAAC,EACxCC,CACF,EACA,OAAOC,EAAoBO,CAAY,CACzC","names":["index_exports","__export","getMfeConfig","__toCommonJS","import_merge_anything","getMfeConfig","jsonConfig","market","params","_a","_b","_c","defaultConfig","overrideConfig","replacePlaceholders","config","_d","_e","_f","_g","replacedConfig","mergedConfig"]}
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 (_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Ac,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"]}
package/dist/index.d.cts CHANGED
@@ -259,7 +259,7 @@ interface GetMfeConfigProps {
259
259
  */
260
260
  jsonConfig?: {
261
261
  mfe?: MfeConfigs;
262
- };
262
+ } | null;
263
263
  /**
264
264
  * Market identifier for fetching specific configuration overrides. (`market:id:hashid`)
265
265
  */
package/dist/index.d.ts CHANGED
@@ -259,7 +259,7 @@ interface GetMfeConfigProps {
259
259
  */
260
260
  jsonConfig?: {
261
261
  mfe?: MfeConfigs;
262
- };
262
+ } | null;
263
263
  /**
264
264
  * Market identifier for fetching specific configuration overrides. (`market:id:hashid`)
265
265
  */
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{merge as _}from"merge-anything";function I({jsonConfig:l,market:t,params:e}){var n,i,f;if((l==null?void 0:l.mfe)==null)return null;let N=(i=(n=l==null?void 0:l.mfe)==null?void 0:n.default)!=null?i:{},k=t!=null?(f=l==null?void 0:l.mfe[t])!=null?f:{}:{};function M(T){var u,g,o,r,c,d,y;let b=JSON.stringify(T).replace(/:lang/g,(u=e==null?void 0:e.lang)!=null?u:":lang").replace(/:slug/g,(g=e==null?void 0:e.slug)!=null?g:":slug").replace(/:token/g,(o=e==null?void 0:e.token)!=null?o:":token").replace(/:access_token/g,(r=e==null?void 0:e.accessToken)!=null?r:":access_token").replace(/:order_id/g,(c=e==null?void 0:e.orderId)!=null?c:":order_id").replace(/:sku_list_id/g,(d=e==null?void 0:e.skuListId)!=null?d:":sku_list_id").replace(/:sku_id/g,(y=e==null?void 0:e.skuId)!=null?y:":sku_id");return JSON.parse(b)}let C=_(JSON.parse(JSON.stringify(N)),k);return M(C)}export{I as getMfeConfig};
1
+ import{jwtDecode as b}from"@commercelayer/js-auth";import{merge as N}from"merge-anything";function D({jsonConfig:t,market:o,params:e}){var f,u,g,r;let l=N(I({params:e}),(u=(f=t==null?void 0:t.mfe)==null?void 0:f.default)!=null?u:{}),i=o!=null?(r=(g=t==null?void 0:t.mfe)==null?void 0:g[o])!=null?r:{}:{};function n(M){var s,d,k,y,C,_,T;let a=JSON.stringify(M).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,(k=e==null?void 0:e.token)!=null?k:":token").replace(/:access_token/g,(y=e==null?void 0:e.accessToken)!=null?y:":access_token").replace(/:order_id/g,(C=e==null?void 0:e.orderId)!=null?C:":order_id").replace(/:sku_list_id/g,(_=e==null?void 0:e.skuListId)!=null?_:":sku_list_id").replace(/:sku_id/g,(T=e==null?void 0:e.skuId)!=null?T:":sku_id");return JSON.parse(a)}let c=N(JSON.parse(JSON.stringify(l)),i);return Object.keys(c).length===0?null:n(c)}function I({params:t}){var o;if((t==null?void 0:t.accessToken)==null)return{};try{let e=b(t.accessToken),l=(o=t.slug)!=null?o:"organization"in e.payload?e.payload.organization.slug:null;if(l==null)return{};let i=e.payload.iss.endsWith("commercelayer.co")?".stg":"",n=`https://${l}${i}.commercelayer.app`;return{links:{cart:`${n}/cart/:order_id?accessToken=:access_token`,checkout:`${n}/checkout/:order_id?accessToken=:access_token`,my_account:`${n}/my-account?accessToken=:access_token`,identity:`${n}/identity`,microstore:t.skuListId!=null?`${n}/microstore/list/:sku_list_id?accessToken=:access_token`:`${n}/microstore/sku/:sku_id?accessToken=:access_token`}}}catch(e){return console.log(e),{}}}export{D as getMfeConfig};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/getMfeConfig.ts"],"sourcesContent":["import { 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 }\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 if (jsonConfig?.mfe == null) {\n return null\n }\n\n const defaultConfig = jsonConfig?.mfe?.default ?? {}\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\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AMC,EAAiCjB,EACrC,KAAK,MAAM,KAAK,UAAUQ,CAAa,CAAC,EACxCC,CACF,EACA,OAAOC,EAAoBO,CAAY,CACzC","names":["merge","getMfeConfig","jsonConfig","market","params","_a","_b","_c","defaultConfig","overrideConfig","replacePlaceholders","config","_d","_e","_f","_g","replacedConfig","mergedConfig"]}
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_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":"AAAA,OAAS,aAAAA,MAAiB,yBAC1B,OAAS,SAAAC,MAAa,iBA2Ef,SAASC,EAAa,CAC3B,WAAAC,EACA,OAAAC,EACA,OAAAC,CACF,EAA+C,CAhF/C,IAAAC,EAAAC,EAAAC,EAAAC,EAiFE,IAAMC,EAAgBT,EACpBU,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,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercelayer/organization-config",
3
- "version": "2.5.0",
3
+ "version": "2.7.0",
4
4
  "description": "Organization config utils for extracting config by market",
5
5
  "author": {
6
6
  "name": "Matteo Alessani",
@@ -35,19 +35,20 @@
35
35
  "url": "https://github.com/commercelayer/common-utils/issues"
36
36
  },
37
37
  "devDependencies": {
38
- "@biomejs/biome": "^2.3.5",
39
- "@types/lodash": "^4.17.20",
38
+ "@biomejs/biome": "^2.3.9",
39
+ "@types/lodash": "^4.17.21",
40
40
  "@types/node": "^24.10.1",
41
41
  "deepmerge": "^4.3.1",
42
42
  "deepmerge-ts": "^7.1.5",
43
43
  "json-schema-to-typescript": "^15.0.4",
44
44
  "lodash": "^4.17.21",
45
45
  "tsup": "^8.5.1",
46
- "type-fest": "^5.2.0",
46
+ "type-fest": "^5.3.1",
47
47
  "typescript": "^5.9.3",
48
48
  "vitest": "^4.0.10"
49
49
  },
50
50
  "dependencies": {
51
+ "@commercelayer/js-auth": "^7.2.0",
51
52
  "merge-anything": "^5.1.7"
52
53
  },
53
54
  "scripts": {
@@ -57,7 +58,7 @@
57
58
  "test:verbose": "pnpm run lint && vitest run --silent=false",
58
59
  "bench": "vitest bench",
59
60
  "build": "tsup",
60
- "jsr:publish": "npx jsr publish",
61
+ "jsr:publish": "pnpx jsr publish",
61
62
  "generate:types": "node src/schema/generate.js && pnpm run lint:fix"
62
63
  }
63
64
  }