UNPKG

@commercelayer/organization-config

Version:

Organization config utils for extracting config by market

2 lines 3.24 kB
"use strict";var r=Object.defineProperty;var O=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=O(t,n))||o.enumerable});return l};var W=l=>J(r({},"__esModule",{value:!0}),l);var G={};V(G,{getMfeConfig:()=>S,hasValidLinks:()=>U});module.exports=W(G);var P=require("@commercelayer/js-auth"),f=require("merge-anything");function S({jsonConfig:l,market:t,params:e}){var u,g,s,d;let o=(0,f.merge)(F({params:e}),(g=(u=l==null?void 0:l.mfe)==null?void 0:u.default)!=null?g:{}),n=t!=null?(d=(s=l==null?void 0:l.mfe)==null?void 0:s[t])!=null?d:{}:{};function i($){var y,k,_,C,T,b,N,M,p,h,I,D,L;let w=JSON.stringify($).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(w)}let c=(0,f.merge)(JSON.parse(JSON.stringify(o)),n);return Object.keys(c).length===0?null:i(c)}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",l.returnUrl!=null?"returnUrl=:return_url":null].filter(u=>u!=null).join("&")}`,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(u=>u!=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 U(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}); //# sourceMappingURL=index.cjs.map