@commercelayer/organization-config
Version:
Organization config utils for extracting config by market
2 lines • 2.72 kB
JavaScript
import{jwtDecode as S}from"@commercelayer/js-auth";import{merge as D}from"merge-anything";function U({jsonConfig:l,market:i,params:e}){var n,r,f,g;let o=D($({params:e}),(r=(n=l==null?void 0:l.mfe)==null?void 0:n.default)!=null?r:{}),u=i!=null?(g=(f=l==null?void 0:l.mfe)==null?void 0:f[i])!=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 c=D(JSON.parse(JSON.stringify(o)),u);return Object.keys(c).length===0?null:t(c)}function $({params:l}){var i;if((l==null?void 0:l.accessToken)==null)return{};try{let e=S(l.accessToken),o=(i=l.slug)!=null?i:"organization"in e.payload?e.payload.organization.slug:null;if(o==null)return{};let u=e.payload.iss.endsWith("commercelayer.co")?".stg":"",t=`https://${o}${u}.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",l.returnUrl!=null?"returnUrl=:return_url":null].filter(n=>n!=null).join("&")}`,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(n=>n!=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 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}export{U as getMfeConfig,w as hasValidLinks};
//# sourceMappingURL=index.js.map