@descope/core-js-sdk
Advanced tools
Comparing version 2.17.3 to 2.17.4
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("jwt-decode"),t={exchange:"/v1/auth/accesskey/exchange"},n={verify:"/v1/auth/otp/verify",signIn:"/v1/auth/otp/signin",signUp:"/v1/auth/otp/signup",update:{email:"/v1/auth/otp/update/email",phone:"/v1/auth/otp/update/phone"},signUpOrIn:"/v1/auth/otp/signup-in"},s={verify:"/v1/auth/magiclink/verify",signIn:"/v1/auth/magiclink/signin",signUp:"/v1/auth/magiclink/signup",update:{email:"/v1/auth/magiclink/update/email",phone:"/v1/auth/magiclink/update/phone"},signUpOrIn:"/v1/auth/magiclink/signup-in"},o={verify:"/v1/auth/enchantedlink/verify",signIn:"/v1/auth/enchantedlink/signin",signUp:"/v1/auth/enchantedlink/signup",session:"/v1/auth/enchantedlink/pending-session",update:{email:"/v1/auth/enchantedlink/update/email"},signUpOrIn:"/v1/auth/enchantedlink/signup-in"},i={start:"/v1/auth/oauth/authorize",exchange:"/v1/auth/oauth/exchange",startNative:"v1/auth/oauth/native/start",finishNative:"v1/auth/oauth/native/finish"},a={start:"/v1/auth/saml/authorize",exchange:"/v1/auth/saml/exchange"},r={verify:"/v1/auth/totp/verify",signUp:"/v1/auth/totp/signup",update:"/v1/auth/totp/update"},p={signIn:"/v1/auth/notp/whatsapp/signin",signUp:"/v1/auth/notp/whatsapp/signup",signUpOrIn:"/v1/auth/notp/whatsapp/signup-in",session:"/v1/auth/notp/pending-session"},l={signUp:{start:"/v1/auth/webauthn/signup/start",finish:"/v1/auth/webauthn/signup/finish"},signIn:{start:"/v1/auth/webauthn/signin/start",finish:"/v1/auth/webauthn/signin/finish"},signUpOrIn:{start:"/v1/auth/webauthn/signup-in/start"},update:{start:"v1/auth/webauthn/update/start",finish:"/v1/auth/webauthn/update/finish"}},d={signUp:"/v1/auth/password/signup",signIn:"/v1/auth/password/signin",sendReset:"/v1/auth/password/reset",update:"/v1/auth/password/update",replace:"/v1/auth/password/replace",policy:"/v1/auth/password/policy"},u="/v1/auth/refresh",c="/v1/auth/tenant/select",g="/v1/auth/logout",h="/v1/auth/logoutall",v="/v1/auth/me",f="/v1/auth/me/history",m={start:"/v1/flow/start",next:"/v1/flow/next"};const I="<region>",b=`https://api.${I}descope.com`,k=1e3,y=6e5,O=()=>{const e={};return{headers(t){const n="function"==typeof t.entries?Object.fromEntries(t.entries()):t;return e.Headers=JSON.stringify(n),this},body(t){return e.Body=t,this},url(t){return e.Url=t.toString(),this},method(t){return e.Method=t,this},title(t){return e.Title=t,this},status(t){return e.Status=t,this},build:()=>Object.keys(e).flatMap((t=>e[t]?[`${"Title"!==t?`${t}: `:""}${e[t]}`]:[])).join("\n")}},w=(e,t)=>{const n=(e=>async(...t)=>{const n=await e(...t),s=await n.text();return n.text=()=>Promise.resolve(s),n.json=()=>Promise.resolve(JSON.parse(s)),n.clone=()=>n,n})(t||fetch);return n||null==e||e.warn("Fetch is not defined, you will not be able to send http requests, if you are running in a test, make sure fetch is defined globally"),e?async(...t)=>{if(!n)throw Error("Cannot send http request, fetch is not defined, if you are running in a test, make sure fetch is defined globally");e.log((e=>O().title("Request").url(e[0]).method(e[1].method).headers(e[1].headers).body(e[1].body).build())(t));const s=await n(...t);return e[s.ok?"log":"error"](await(async e=>{const t=await e.text();return O().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var j;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT"}(j||(j={}));const U=(...e)=>new Headers(e.reduce(((e,t)=>{const n=(e=>Array.isArray(e)?e:e instanceof Headers?Array.from(e.entries()):e?Object.entries(e):[])(t);return n.reduce(((t,[n,s])=>(e[n]=s,e)),e),e}),{})),R=e=>void 0===e?void 0:JSON.stringify(e),x=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},P=({baseUrl:e,projectId:t,baseConfig:n,logger:s,hooks:o,cookiePolicy:i,fetch:a})=>{const r=w(s,a),p=async s=>{var a;const p=(null==o?void 0:o.beforeRequest)?o.beforeRequest(s):s,{path:l,body:d,headers:u,queryParams:c,method:g,token:h}=p,v={headers:U(x(t,h),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"2.17.3"},(null==n?void 0:n.baseHeaders)||{},u),method:g,body:R(d)};null!==i&&(v.credentials=i||"include");const f=await r((({path:e,baseUrl:t,queryParams:n,projectId:s})=>{const o=s.slice(1,-27);t=t.replace(I,o?o+".":"");let i=e?`${t.replace(/\/$/,"")}/${null==e?void 0:e.replace(/^\//,"")}`:t;if(n){i=`${i}?`;const e=Object.keys(n);e.forEach(((t,s)=>{i=`${i}${t}=${n[t]}${s===e.length-1?"":"&"}`}))}return i})({path:l,baseUrl:e,queryParams:c,projectId:t}),v);if((null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==f?void 0:f.clone()),null==o?void 0:o.transformResponse){const e=await f.json(),t=((null===(a=f.headers)||void 0===a?void 0:a.get("set-cookie"))||"").split(";").reduce(((e,t)=>{const[n,s]=t.split("=");return Object.assign(Object.assign({},e),{[n.trim()]:s})}),{}),n=Object.assign(Object.assign({},f),{json:()=>Promise.resolve(e),cookies:t});return n.clone=()=>n,o.transformResponse(n)}return f};return{get:(e,{headers:t,queryParams:n,token:s}={})=>p({path:e,headers:t,queryParams:n,body:void 0,method:j.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>p({path:e,headers:n,queryParams:s,body:t,method:j.post,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>p({path:e,headers:n,queryParams:s,body:t,method:j.put,token:o}),delete:(e,{headers:t,queryParams:n,token:s}={})=>p({path:e,headers:t,queryParams:n,body:void 0,method:j.delete,token:s}),hooks:o}};var $={TOO_MANY_REQUESTS:429};function q(e,t,n){var s;let o=E(e);t&&(o=null===(s=null==o?void 0:o.tenants)||void 0===s?void 0:s[t]);const i=null==o?void 0:o[n];return Array.isArray(i)?i:[]}function E(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function S(e){const{exp:t}=E(e);return(new Date).getTime()/1e3>t}function T(e){let t=E(e);const n=Object.keys(null==t?void 0:t.tenants);return Array.isArray(n)?n:[]}function A(e,t){return q(e,t,"permissions")}function M(e,t){return q(e,t,"roles")}const N=(...e)=>e.join("/").replace(/\/{2,}/g,"/");async function C(e,t){var n;const s=await e,o={code:s.status,ok:s.ok,response:s},i=await s.clone().json();return s.ok?o.data=t?t(i):i:(o.error=i,s.status===$.TOO_MANY_REQUESTS&&Object.assign(o.error,{retryAfter:Number.parseInt(null===(n=s.headers)||void 0===n?void 0:n.get("retry-after"))||0})),o}const z=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),H=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),_=e=>t=>e.test(t),J=_(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),L=_(/^\+[1-9]{1}[0-9]{3,14}$/),D=(e,t)=>n=>H(...t).validate(((e,t,n)=>{const s=(Array.isArray(t)?t.join("."):String(t)).replace(/\[\\?("|')?(\w|d)+\\?("|')?\]/g,((e,t,n)=>"."+n)).split("."),o=s.length;let i=0,a=e===Object(e)?e:void 0;for(;null!=a&&i<o;)a=a[s[i++]];return i&&i===o&&void 0!==a?a:n})(n,e)),F=z(J,'"{val}" is not a valid email'),Z=z(L,'"{val}" is not a valid phone number'),B=z((K=1,e=>e.length>=K),"Minimum length is 1");var K;const Q=z((e=>"string"==typeof e),"Input is not a string"),V=z((e=>void 0===e),"Input is defined"),W=(Y=[Q(),V()],G="Input is not a string or undefined",(e=G)=>t=>{const n=Y.filter((e=>e(t)));return!(n.length<Y.length)&&(e?e.replace("{val}",t):n.join(" OR "))});var Y,G;const X=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>H(...e).validate(n[t]))),t(...n)),ee=e=>[Q(`"${e}" must be a string`)],te=e=>[Q(`"${e}" must be a string`),B(`"${e}" must not be empty`)],ne=e=>[Q(`"${e}" must be a string`),F()],se=e=>[Q(`"${e}" must be a string`),Z()],oe=X(te("accessKey")),ie=e=>({exchange:oe(((n,s)=>C(e.post(t.exchange,{loginOptions:s},{token:n}))))}),ae=({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||k,k),timeoutMs:Math.min(t||y,y)});var re,pe;!function(e){e.sms="sms",e.voice="voice",e.whatsapp="whatsapp"}(re||(re={})),function(e){e.email="email"}(pe||(pe={}));const le=Object.assign(Object.assign({},re),pe);var de;!function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(de||(de={}));const ue=te("loginId"),ce=X(te("token")),ge=X(ue),he=X(te("pendingRef")),ve=X(ue,ne("email")),fe=e=>({verify:ce((t=>C(e.post(o.verify,{token:t})))),signIn:ge(((t,n,s,i)=>C(e.post(N(o.signIn,le.email),{loginId:t,URI:n,loginOptions:s},{token:i})))),signUpOrIn:ge(((t,n,s)=>C(e.post(N(o.signUpOrIn,le.email),{loginId:t,URI:n,loginOptions:s})))),signUp:ge(((t,n,s,i)=>C(e.post(N(o.signUp,le.email),{loginId:t,URI:n,user:s,loginOptions:i})))),waitForSession:he(((t,n)=>new Promise((s=>{const{pollingIntervalMs:i,timeoutMs:a}=ae(n);let r;const p=setInterval((async()=>{const n=await e.post(o.session,{pendingRef:t});n.ok&&(clearInterval(p),r&&clearTimeout(r),s(C(Promise.resolve(n))))}),i);r=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${a}ms`,errorCode:"0"},ok:!1}),clearInterval(p)}),a)})))),update:{email:ve(((t,n,s,i,a)=>C(e.post(o.update.email,Object.assign({loginId:t,email:n,URI:s},a),{token:i}))))}}),me=X(te("flowId")),Ie=X(te("executionId"),te("stepId"),te("interactionId")),be=e=>({start:me(((t,n,s,o,i,a,r)=>C(e.post(m.start,{flowId:t,options:n,conditionInteractionId:s,interactionId:o,version:i,componentsVersion:a,input:r})))),next:Ie(((t,n,s,o,i,a)=>C(e.post(m.next,{executionId:t,stepId:n,interactionId:s,version:o,componentsVersion:i,input:a}))))}),ke=te("loginId"),ye=X(te("token")),Oe=X(ke),we=X(ke,se("phone")),je=X(ke,ne("email")),Ue=Object.keys(le).filter((e=>e!==re.voice)),Re=e=>({verify:ye((t=>C(e.post(s.verify,{token:t})))),signIn:Ue.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Oe(((t,o,i,a)=>C(e.post(N(s.signIn,n),{loginId:t,URI:o,loginOptions:i},{token:a}))))})),{}),signUp:Ue.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Oe(((t,o,i,a)=>C(e.post(N(s.signUp,n),{loginId:t,URI:o,user:i,loginOptions:a}))))})),{}),signUpOrIn:Ue.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Oe(((t,o,i)=>C(e.post(N(s.signUpOrIn,n),{loginId:t,URI:o,loginOptions:i}))))})),{}),update:{email:je(((t,n,o,i,a)=>C(e.post(s.update.email,Object.assign({loginId:t,email:n,URI:o},a),{token:i})))),phone:Object.keys(re).filter((e=>e!==re.voice)).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:we(((t,o,i,a,r)=>C(e.post(N(s.update.phone,n),Object.assign({loginId:t,phone:o,URI:i},r),{token:a}))))})),{})}});var xe;!function(e){e.facebook="facebook",e.github="github",e.google="google",e.microsoft="microsoft",e.gitlab="gitlab",e.apple="apple",e.discord="discord",e.linkedin="linkedin",e.slack="slack"}(xe||(xe={}));const Pe=X(te("code")),$e=e=>({start:Object.assign(((t,n,s,o)=>C(e.post(i.start,s||{},{queryParams:Object.assign({provider:t},n&&{redirectURL:n}),token:o}))),Object.keys(xe).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:(t,s,o)=>C(e.post(i.start,s||{},{queryParams:Object.assign({provider:n},t&&{redirectURL:t}),token:o}))})),{})),exchange:Pe((t=>C(e.post(i.exchange,{code:t})))),startNative:(t,n)=>C(e.post(i.startNative,{provider:t,loginOptions:n})),finishNative:(t,n,s,o,a)=>C(e.post(i.finishNative,{provider:t,stateId:n,user:s,code:o,idToken:a}))}),qe=te("loginId"),Ee=X(qe,te("code")),Se=X(qe),Te=X(qe,se("phone")),Ae=X(qe,ne("email")),Me=e=>({verify:Object.keys(le).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ee(((t,o)=>C(e.post(N(n.verify,s),{code:o,loginId:t}))))})),{}),signIn:Object.keys(le).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Se(((t,o,i)=>C(e.post(N(n.signIn,s),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(le).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Se(((t,o,i)=>C(e.post(N(n.signUp,s),{loginId:t,user:o,loginOptions:i}))))})),{}),signUpOrIn:Object.keys(le).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Se(((t,o)=>C(e.post(N(n.signUpOrIn,s),{loginId:t,loginOptions:o}))))})),{}),update:{email:Ae(((t,s,o,i)=>C(e.post(n.update.email,Object.assign({loginId:t,email:s},i),{token:o})))),phone:Object.keys(re).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Te(((t,o,i,a)=>C(e.post(N(n.update.phone,s),Object.assign({loginId:t,phone:o},a),{token:i}))))})),{})}}),Ne=X(te("tenant")),Ce=X(te("code")),ze=e=>({start:Ne(((t,n,s,o)=>C(e.post(a.start,s||{},{queryParams:{tenant:t,redirectURL:n},token:o})))),exchange:Ce((t=>C(e.post(a.exchange,{code:t}))))}),He=te("loginId"),_e=X(He,te("code")),Je=X(He),Le=X(He),De=e=>({signUp:Je(((t,n)=>C(e.post(r.signUp,{loginId:t,user:n})))),verify:_e(((t,n,s,o)=>C(e.post(r.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:Le(((t,n)=>C(e.post(r.update,{loginId:t},{token:n}))))}),Fe=te("loginId"),Ze=te("newPassword"),Be=X(Fe,te("password")),Ke=X(Fe),Qe=X(Fe,Ze),Ve=X(Fe,te("oldPassword"),Ze),We=e=>({signUp:Be(((t,n,s,o)=>C(e.post(d.signUp,{loginId:t,password:n,user:s,loginOptions:o})))),signIn:Be(((t,n,s)=>C(e.post(d.signIn,{loginId:t,password:n,loginOptions:s})))),sendReset:Ke(((t,n,s)=>C(e.post(d.sendReset,{loginId:t,redirectUrl:n,templateOptions:s})))),update:Qe(((t,n,s)=>C(e.post(d.update,{loginId:t,newPassword:n},{token:s})))),replace:Ve(((t,n,s)=>C(e.post(d.replace,{loginId:t,oldPassword:n,newPassword:s})))),policy:()=>C(e.get(d.policy))}),Ye=ee("loginId"),Ge=te("loginId"),Xe=te("origin"),et=X(Ge,Xe,te("name")),tt=X(Ge,Xe),nt=X(Ye,Xe),st=X(Ge,Xe,te("token")),ot=X(te("transactionId"),te("response")),it=e=>({signUp:{start:et(((t,n,s)=>C(e.post(l.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:ot(((t,n)=>C(e.post(l.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:nt(((t,n,s,o)=>C(e.post(l.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:ot(((t,n)=>C(e.post(l.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:tt(((t,n)=>C(e.post(l.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:st(((t,n,s)=>C(e.post(l.update.start,{loginId:t,origin:n},{token:s})))),finish:ot(((t,n)=>C(e.post(l.update.finish,{transactionId:t,response:n}))))}}),at=ee("loginId"),rt=X(at),pt=X(te("pendingRef")),lt=e=>({signUpOrIn:rt(((t,n)=>C(e.post(p.signUpOrIn,{loginId:t,loginOptions:n})))),signUp:rt(((t,n,s)=>C(e.post(p.signUp,{loginId:t,user:n,loginOptions:s})))),signIn:rt(((t,n,s)=>C(e.post(p.signIn,{loginId:t,loginOptions:n},{token:s})))),waitForSession:pt(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:i}=ae(n);let a;const r=setInterval((async()=>{const n=await e.post(p.session,{pendingRef:t});n.ok&&(clearInterval(r),a&&clearTimeout(a),s(C(Promise.resolve(n))))}),o);a=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${i}ms`,errorCode:"0"},ok:!1}),clearInterval(r)}),i)}))))}),dt=X(te("token")),ut=X([W(`"${"token"}" must be string or undefined`)]);var ct,gt;var ht=X([(ct="projectId",gt=te("projectId"),z(D(ct,gt))())])((e=>t=>{var n;return e(Object.assign(Object.assign({},t),{hooks:{beforeRequest:e=>{var n;const s=[].concat((null===(n=t.hooks)||void 0===n?void 0:n.beforeRequest)||[]);return null==s?void 0:s.reduce(((e,t)=>t(e)),e)},afterRequest:async(e,n)=>{var s;const o=[].concat((null===(s=t.hooks)||void 0===s?void 0:s.afterRequest)||[]);(await Promise.allSettled(null==o?void 0:o.map((t=>t(e,null==n?void 0:n.clone()))))).forEach((e=>{var n;return"rejected"===e.status&&(null===(n=t.logger)||void 0===n?void 0:n.error(e.reason))}))},transformResponse:null===(n=t.hooks)||void 0===n?void 0:n.transformResponse}}))})((({projectId:e,logger:t,baseUrl:n,hooks:s,cookiePolicy:o,baseHeaders:i={},fetch:a})=>{return r=P({baseUrl:n||b,projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:ie(r),otp:Me(r),magicLink:Re(r),enchantedLink:fe(r),oauth:$e(r),saml:ze(r),totp:De(r),notp:lt(r),webauthn:it(r),password:We(r),flow:be(r),refresh:ut((e=>C(r.post(u,{},{token:e})))),selectTenant:X([Q("tenantId")],[W('"token" must be string or undefined')])(((e,t)=>C(r.post(c,{tenant:e},{token:t})))),logout:ut((e=>C(r.post(g,{},{token:e})))),logoutAll:ut((e=>C(r.post(h,{},{token:e})))),me:ut((e=>C(r.get(v,{token:e})))),history:ut((e=>C(r.get(f,{token:e})))),isJwtExpired:dt(S),getTenants:dt(T),getJwtPermissions:dt(A),getJwtRoles:dt(M),httpClient:r};var r}))),vt=Object.assign(ht,{DeliveryMethods:le});exports.HttpStatusCodes=$,exports.default=vt,exports.normalizeWaitForSessionConfig=ae,exports.transformResponse=C,exports.wrapWith=(e,t,n)=>(t.forEach((t=>{const s=t.split(".");let o=s.shift(),i=e;for(;s.length>0;){if(i=i[o],!o||!i)throw Error(`Invalid path "${t}", "${o}" is missing or has no value`);o=s.shift()}if("function"!=typeof i[o])throw Error(`"${t}" is not a function`);const a=i[o];i[o]=n(a)})),e); | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("jwt-decode"),t={exchange:"/v1/auth/accesskey/exchange"},n={verify:"/v1/auth/otp/verify",signIn:"/v1/auth/otp/signin",signUp:"/v1/auth/otp/signup",update:{email:"/v1/auth/otp/update/email",phone:"/v1/auth/otp/update/phone"},signUpOrIn:"/v1/auth/otp/signup-in"},s={verify:"/v1/auth/magiclink/verify",signIn:"/v1/auth/magiclink/signin",signUp:"/v1/auth/magiclink/signup",update:{email:"/v1/auth/magiclink/update/email",phone:"/v1/auth/magiclink/update/phone"},signUpOrIn:"/v1/auth/magiclink/signup-in"},o={verify:"/v1/auth/enchantedlink/verify",signIn:"/v1/auth/enchantedlink/signin",signUp:"/v1/auth/enchantedlink/signup",session:"/v1/auth/enchantedlink/pending-session",update:{email:"/v1/auth/enchantedlink/update/email"},signUpOrIn:"/v1/auth/enchantedlink/signup-in"},i={start:"/v1/auth/oauth/authorize",exchange:"/v1/auth/oauth/exchange",startNative:"v1/auth/oauth/native/start",finishNative:"v1/auth/oauth/native/finish"},a={start:"/v1/auth/saml/authorize",exchange:"/v1/auth/saml/exchange"},r={verify:"/v1/auth/totp/verify",signUp:"/v1/auth/totp/signup",update:"/v1/auth/totp/update"},p={signIn:"/v1/auth/notp/whatsapp/signin",signUp:"/v1/auth/notp/whatsapp/signup",signUpOrIn:"/v1/auth/notp/whatsapp/signup-in",session:"/v1/auth/notp/pending-session"},l={signUp:{start:"/v1/auth/webauthn/signup/start",finish:"/v1/auth/webauthn/signup/finish"},signIn:{start:"/v1/auth/webauthn/signin/start",finish:"/v1/auth/webauthn/signin/finish"},signUpOrIn:{start:"/v1/auth/webauthn/signup-in/start"},update:{start:"v1/auth/webauthn/update/start",finish:"/v1/auth/webauthn/update/finish"}},d={signUp:"/v1/auth/password/signup",signIn:"/v1/auth/password/signin",sendReset:"/v1/auth/password/reset",update:"/v1/auth/password/update",replace:"/v1/auth/password/replace",policy:"/v1/auth/password/policy"},u="/v1/auth/refresh",c="/v1/auth/tenant/select",g="/v1/auth/logout",h="/v1/auth/logoutall",v="/v1/auth/me",f="/v1/auth/me/history",m={start:"/v1/flow/start",next:"/v1/flow/next"};const I="<region>",b=`https://api.${I}descope.com`,k=1e3,y=6e5,O=()=>{const e={};return{headers(t){const n="function"==typeof t.entries?Object.fromEntries(t.entries()):t;return e.Headers=JSON.stringify(n),this},body(t){return e.Body=t,this},url(t){return e.Url=t.toString(),this},method(t){return e.Method=t,this},title(t){return e.Title=t,this},status(t){return e.Status=t,this},build:()=>Object.keys(e).flatMap((t=>e[t]?[`${"Title"!==t?`${t}: `:""}${e[t]}`]:[])).join("\n")}},w=(e,t)=>{const n=(e=>async(...t)=>{const n=await e(...t),s=await n.text();return n.text=()=>Promise.resolve(s),n.json=()=>Promise.resolve(JSON.parse(s)),n.clone=()=>n,n})(t||fetch);return n||null==e||e.warn("Fetch is not defined, you will not be able to send http requests, if you are running in a test, make sure fetch is defined globally"),e?async(...t)=>{if(!n)throw Error("Cannot send http request, fetch is not defined, if you are running in a test, make sure fetch is defined globally");e.log((e=>O().title("Request").url(e[0]).method(e[1].method).headers(e[1].headers).body(e[1].body).build())(t));const s=await n(...t);return e[s.ok?"log":"error"](await(async e=>{const t=await e.text();return O().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var j;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT"}(j||(j={}));const U=(...e)=>new Headers(e.reduce(((e,t)=>{const n=(e=>Array.isArray(e)?e:e instanceof Headers?Array.from(e.entries()):e?Object.entries(e):[])(t);return n.reduce(((t,[n,s])=>(e[n]=s,e)),e),e}),{})),R=e=>void 0===e?void 0:JSON.stringify(e),x=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},P=({baseUrl:e,projectId:t,baseConfig:n,logger:s,hooks:o,cookiePolicy:i,fetch:a})=>{const r=w(s,a),p=async s=>{var a;const p=(null==o?void 0:o.beforeRequest)?o.beforeRequest(s):s,{path:l,body:d,headers:u,queryParams:c,method:g,token:h}=p,v={headers:U(x(t,h),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"2.17.4"},(null==n?void 0:n.baseHeaders)||{},u),method:g,body:R(d)};null!==i&&(v.credentials=i||"include");const f=await r((({path:e,baseUrl:t,queryParams:n,projectId:s})=>{const o=s.slice(1,-27);t=t.replace(I,o?o+".":"");let i=e?`${t.replace(/\/$/,"")}/${null==e?void 0:e.replace(/^\//,"")}`:t;if(n){i=`${i}?`;const e=Object.keys(n);e.forEach(((t,s)=>{i=`${i}${t}=${n[t]}${s===e.length-1?"":"&"}`}))}return i})({path:l,baseUrl:e,queryParams:c,projectId:t}),v);if((null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==f?void 0:f.clone()),null==o?void 0:o.transformResponse){const e=await f.json(),t=((null===(a=f.headers)||void 0===a?void 0:a.get("set-cookie"))||"").split(";").reduce(((e,t)=>{const[n,s]=t.split("=");return Object.assign(Object.assign({},e),{[n.trim()]:s})}),{}),n=Object.assign(Object.assign({},f),{json:()=>Promise.resolve(e),cookies:t});return n.clone=()=>n,o.transformResponse(n)}return f};return{get:(e,{headers:t,queryParams:n,token:s}={})=>p({path:e,headers:t,queryParams:n,body:void 0,method:j.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>p({path:e,headers:n,queryParams:s,body:t,method:j.post,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>p({path:e,headers:n,queryParams:s,body:t,method:j.put,token:o}),delete:(e,{headers:t,queryParams:n,token:s}={})=>p({path:e,headers:t,queryParams:n,body:void 0,method:j.delete,token:s}),hooks:o}};var $={TOO_MANY_REQUESTS:429};function q(e,t,n){var s;let o=E(e);t&&(o=null===(s=null==o?void 0:o.tenants)||void 0===s?void 0:s[t]);const i=null==o?void 0:o[n];return Array.isArray(i)?i:[]}function E(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function S(e){const{exp:t}=E(e);return(new Date).getTime()/1e3>t}function T(e){let t=E(e);const n=Object.keys(null==t?void 0:t.tenants);return Array.isArray(n)?n:[]}function A(e,t){return q(e,t,"permissions")}function M(e,t){return q(e,t,"roles")}const N=(...e)=>e.join("/").replace(/\/{2,}/g,"/");async function C(e,t){var n;const s=await e,o={code:s.status,ok:s.ok,response:s},i=await s.clone().json();return s.ok?o.data=t?t(i):i:(o.error=i,s.status===$.TOO_MANY_REQUESTS&&Object.assign(o.error,{retryAfter:Number.parseInt(null===(n=s.headers)||void 0===n?void 0:n.get("retry-after"))||0})),o}const z=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),H=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),_=e=>t=>e.test(t),J=_(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),L=_(/^\+[1-9]{1}[0-9]{3,14}$/),D=(e,t)=>n=>H(...t).validate(((e,t,n)=>{const s=(Array.isArray(t)?t.join("."):String(t)).replace(/\[\\?("|')?(\w|d)+\\?("|')?\]/g,((e,t,n)=>"."+n)).split("."),o=s.length;let i=0,a=e===Object(e)?e:void 0;for(;null!=a&&i<o;)a=a[s[i++]];return i&&i===o&&void 0!==a?a:n})(n,e)),F=z(J,'"{val}" is not a valid email'),Z=z(L,'"{val}" is not a valid phone number'),B=z((K=1,e=>e.length>=K),"Minimum length is 1");var K;const Q=z((e=>"string"==typeof e),"Input is not a string"),V=z((e=>void 0===e),"Input is defined"),W=(Y=[Q(),V()],G="Input is not a string or undefined",(e=G)=>t=>{const n=Y.filter((e=>e(t)));return!(n.length<Y.length)&&(e?e.replace("{val}",t):n.join(" OR "))});var Y,G;const X=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>H(...e).validate(n[t]))),t(...n)),ee=e=>[Q(`"${e}" must be a string`)],te=e=>[Q(`"${e}" must be a string`),B(`"${e}" must not be empty`)],ne=e=>[Q(`"${e}" must be a string`),F()],se=e=>[Q(`"${e}" must be a string`),Z()],oe=X(te("accessKey")),ie=e=>({exchange:oe(((n,s)=>C(e.post(t.exchange,{loginOptions:s},{token:n}))))}),ae=({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||k,k),timeoutMs:Math.min(t||y,y)});var re,pe;!function(e){e.sms="sms",e.voice="voice",e.whatsapp="whatsapp"}(re||(re={})),function(e){e.email="email"}(pe||(pe={}));const le=Object.assign(Object.assign({},re),pe);var de;!function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(de||(de={}));const ue=te("loginId"),ce=X(te("token")),ge=X(ue),he=X(te("pendingRef")),ve=X(ue,ne("email")),fe=e=>({verify:ce((t=>C(e.post(o.verify,{token:t})))),signIn:ge(((t,n,s,i)=>C(e.post(N(o.signIn,le.email),{loginId:t,URI:n,loginOptions:s},{token:i})))),signUpOrIn:ge(((t,n,s)=>C(e.post(N(o.signUpOrIn,le.email),{loginId:t,URI:n,loginOptions:s})))),signUp:ge(((t,n,s,i)=>C(e.post(N(o.signUp,le.email),{loginId:t,URI:n,user:s,loginOptions:i})))),waitForSession:he(((t,n)=>new Promise((s=>{const{pollingIntervalMs:i,timeoutMs:a}=ae(n);let r;const p=setInterval((async()=>{const n=await e.post(o.session,{pendingRef:t});n.ok&&(clearInterval(p),r&&clearTimeout(r),s(C(Promise.resolve(n))))}),i);r=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${a}ms`,errorCode:"0"},ok:!1}),clearInterval(p)}),a)})))),update:{email:ve(((t,n,s,i,a)=>C(e.post(o.update.email,Object.assign({loginId:t,email:n,URI:s},a),{token:i}))))}}),me=X(te("flowId")),Ie=X(te("executionId"),te("stepId"),te("interactionId")),be=e=>({start:me(((t,n,s,o,i,a,r)=>C(e.post(m.start,{flowId:t,options:n,conditionInteractionId:s,interactionId:o,version:i,componentsVersion:a,input:r})))),next:Ie(((t,n,s,o,i,a)=>C(e.post(m.next,{executionId:t,stepId:n,interactionId:s,version:o,componentsVersion:i,input:a}))))}),ke=te("loginId"),ye=X(te("token")),Oe=X(ke),we=X(ke,se("phone")),je=X(ke,ne("email")),Ue=Object.keys(le).filter((e=>e!==re.voice)),Re=e=>({verify:ye((t=>C(e.post(s.verify,{token:t})))),signIn:Ue.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Oe(((t,o,i,a)=>C(e.post(N(s.signIn,n),{loginId:t,URI:o,loginOptions:i},{token:a}))))})),{}),signUp:Ue.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Oe(((t,o,i,a)=>C(e.post(N(s.signUp,n),{loginId:t,URI:o,user:i,loginOptions:a}))))})),{}),signUpOrIn:Ue.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Oe(((t,o,i)=>C(e.post(N(s.signUpOrIn,n),{loginId:t,URI:o,loginOptions:i}))))})),{}),update:{email:je(((t,n,o,i,a)=>C(e.post(s.update.email,Object.assign({loginId:t,email:n,URI:o},a),{token:i})))),phone:Object.keys(re).filter((e=>e!==re.voice)).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:we(((t,o,i,a,r)=>C(e.post(N(s.update.phone,n),Object.assign({loginId:t,phone:o,URI:i},r),{token:a}))))})),{})}});var xe;!function(e){e.facebook="facebook",e.github="github",e.google="google",e.microsoft="microsoft",e.gitlab="gitlab",e.apple="apple",e.discord="discord",e.linkedin="linkedin",e.slack="slack"}(xe||(xe={}));const Pe=X(te("code")),$e=e=>({start:Object.assign(((t,n,s,o)=>C(e.post(i.start,s||{},{queryParams:Object.assign({provider:t},n&&{redirectURL:n}),token:o}))),Object.keys(xe).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:(t,s,o)=>C(e.post(i.start,s||{},{queryParams:Object.assign({provider:n},t&&{redirectURL:t}),token:o}))})),{})),exchange:Pe((t=>C(e.post(i.exchange,{code:t})))),startNative:(t,n)=>C(e.post(i.startNative,{provider:t,loginOptions:n})),finishNative:(t,n,s,o,a)=>C(e.post(i.finishNative,{provider:t,stateId:n,user:s,code:o,idToken:a}))}),qe=te("loginId"),Ee=X(qe,te("code")),Se=X(qe),Te=X(qe,se("phone")),Ae=X(qe,ne("email")),Me=e=>({verify:Object.keys(le).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ee(((t,o)=>C(e.post(N(n.verify,s),{code:o,loginId:t}))))})),{}),signIn:Object.keys(le).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Se(((t,o,i)=>C(e.post(N(n.signIn,s),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(le).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Se(((t,o,i)=>C(e.post(N(n.signUp,s),{loginId:t,user:o,loginOptions:i}))))})),{}),signUpOrIn:Object.keys(le).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Se(((t,o)=>C(e.post(N(n.signUpOrIn,s),{loginId:t,loginOptions:o}))))})),{}),update:{email:Ae(((t,s,o,i)=>C(e.post(n.update.email,Object.assign({loginId:t,email:s},i),{token:o})))),phone:Object.keys(re).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Te(((t,o,i,a)=>C(e.post(N(n.update.phone,s),Object.assign({loginId:t,phone:o},a),{token:i}))))})),{})}}),Ne=X(te("tenant")),Ce=X(te("code")),ze=e=>({start:Ne(((t,n,s,o)=>C(e.post(a.start,s||{},{queryParams:{tenant:t,redirectURL:n},token:o})))),exchange:Ce((t=>C(e.post(a.exchange,{code:t}))))}),He=te("loginId"),_e=X(He,te("code")),Je=X(He),Le=X(He),De=e=>({signUp:Je(((t,n)=>C(e.post(r.signUp,{loginId:t,user:n})))),verify:_e(((t,n,s,o)=>C(e.post(r.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:Le(((t,n)=>C(e.post(r.update,{loginId:t},{token:n}))))}),Fe=te("loginId"),Ze=te("newPassword"),Be=X(Fe,te("password")),Ke=X(Fe),Qe=X(Fe,Ze),Ve=X(Fe,te("oldPassword"),Ze),We=e=>({signUp:Be(((t,n,s,o)=>C(e.post(d.signUp,{loginId:t,password:n,user:s,loginOptions:o})))),signIn:Be(((t,n,s)=>C(e.post(d.signIn,{loginId:t,password:n,loginOptions:s})))),sendReset:Ke(((t,n,s)=>C(e.post(d.sendReset,{loginId:t,redirectUrl:n,templateOptions:s})))),update:Qe(((t,n,s)=>C(e.post(d.update,{loginId:t,newPassword:n},{token:s})))),replace:Ve(((t,n,s)=>C(e.post(d.replace,{loginId:t,oldPassword:n,newPassword:s})))),policy:()=>C(e.get(d.policy))}),Ye=ee("loginId"),Ge=te("loginId"),Xe=te("origin"),et=X(Ge,Xe,te("name")),tt=X(Ge,Xe),nt=X(Ye,Xe),st=X(Ge,Xe,te("token")),ot=X(te("transactionId"),te("response")),it=e=>({signUp:{start:et(((t,n,s)=>C(e.post(l.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:ot(((t,n)=>C(e.post(l.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:nt(((t,n,s,o)=>C(e.post(l.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:ot(((t,n)=>C(e.post(l.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:tt(((t,n)=>C(e.post(l.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:st(((t,n,s)=>C(e.post(l.update.start,{loginId:t,origin:n},{token:s})))),finish:ot(((t,n)=>C(e.post(l.update.finish,{transactionId:t,response:n}))))}}),at=ee("loginId"),rt=X(at),pt=X(te("pendingRef")),lt=e=>({signUpOrIn:rt(((t,n)=>C(e.post(p.signUpOrIn,{loginId:t,loginOptions:n})))),signUp:rt(((t,n,s)=>C(e.post(p.signUp,{loginId:t,user:n,loginOptions:s})))),signIn:rt(((t,n,s)=>C(e.post(p.signIn,{loginId:t,loginOptions:n},{token:s})))),waitForSession:pt(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:i}=ae(n);let a;const r=setInterval((async()=>{const n=await e.post(p.session,{pendingRef:t});n.ok&&(clearInterval(r),a&&clearTimeout(a),s(C(Promise.resolve(n))))}),o);a=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${i}ms`,errorCode:"0"},ok:!1}),clearInterval(r)}),i)}))))}),dt=X(te("token")),ut=X([W(`"${"token"}" must be string or undefined`)]);var ct,gt;var ht=X([(ct="projectId",gt=te("projectId"),z(D(ct,gt))())])((e=>t=>{var n;return e(Object.assign(Object.assign({},t),{hooks:{beforeRequest:e=>{var n;const s=[].concat((null===(n=t.hooks)||void 0===n?void 0:n.beforeRequest)||[]);return null==s?void 0:s.reduce(((e,t)=>t(e)),e)},afterRequest:async(e,n)=>{var s;const o=[].concat((null===(s=t.hooks)||void 0===s?void 0:s.afterRequest)||[]);(await Promise.allSettled(null==o?void 0:o.map((t=>t(e,null==n?void 0:n.clone()))))).forEach((e=>{var n;return"rejected"===e.status&&(null===(n=t.logger)||void 0===n?void 0:n.error(e.reason))}))},transformResponse:null===(n=t.hooks)||void 0===n?void 0:n.transformResponse}}))})((({projectId:e,logger:t,baseUrl:n,hooks:s,cookiePolicy:o,baseHeaders:i={},fetch:a})=>{return r=P({baseUrl:n||b,projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:ie(r),otp:Me(r),magicLink:Re(r),enchantedLink:fe(r),oauth:$e(r),saml:ze(r),totp:De(r),notp:lt(r),webauthn:it(r),password:We(r),flow:be(r),refresh:ut((e=>C(r.post(u,{},{token:e})))),selectTenant:X([Q("tenantId")],[W('"token" must be string or undefined')])(((e,t)=>C(r.post(c,{tenant:e},{token:t})))),logout:ut((e=>C(r.post(g,{},{token:e})))),logoutAll:ut((e=>C(r.post(h,{},{token:e})))),me:ut((e=>C(r.get(v,{token:e})))),history:ut((e=>C(r.get(f,{token:e})))),isJwtExpired:dt(S),getTenants:dt(T),getJwtPermissions:dt(A),getJwtRoles:dt(M),httpClient:r};var r}))),vt=Object.assign(ht,{DeliveryMethods:le});exports.HttpStatusCodes=$,exports.default=vt,exports.normalizeWaitForSessionConfig=ae,exports.transformResponse=C,exports.wrapWith=(e,t,n)=>(t.forEach((t=>{const s=t.split(".");let o=s.shift(),i=e;for(;s.length>0;){if(i=i[o],!o||!i)throw Error(`Invalid path "${t}", "${o}" is missing or has no value`);o=s.shift()}if("function"!=typeof i[o])throw Error(`"${t}" is not a function`);const a=i[o];i[o]=n(a)})),e); | ||
//# sourceMappingURL=index.cjs.js.map |
@@ -1,2 +0,2 @@ | ||
import e from"jwt-decode";var t={exchange:"/v1/auth/accesskey/exchange"},n={verify:"/v1/auth/otp/verify",signIn:"/v1/auth/otp/signin",signUp:"/v1/auth/otp/signup",update:{email:"/v1/auth/otp/update/email",phone:"/v1/auth/otp/update/phone"},signUpOrIn:"/v1/auth/otp/signup-in"},s={verify:"/v1/auth/magiclink/verify",signIn:"/v1/auth/magiclink/signin",signUp:"/v1/auth/magiclink/signup",update:{email:"/v1/auth/magiclink/update/email",phone:"/v1/auth/magiclink/update/phone"},signUpOrIn:"/v1/auth/magiclink/signup-in"},o={verify:"/v1/auth/enchantedlink/verify",signIn:"/v1/auth/enchantedlink/signin",signUp:"/v1/auth/enchantedlink/signup",session:"/v1/auth/enchantedlink/pending-session",update:{email:"/v1/auth/enchantedlink/update/email"},signUpOrIn:"/v1/auth/enchantedlink/signup-in"},i={start:"/v1/auth/oauth/authorize",exchange:"/v1/auth/oauth/exchange",startNative:"v1/auth/oauth/native/start",finishNative:"v1/auth/oauth/native/finish"},a={start:"/v1/auth/saml/authorize",exchange:"/v1/auth/saml/exchange"},r={verify:"/v1/auth/totp/verify",signUp:"/v1/auth/totp/signup",update:"/v1/auth/totp/update"},l={signIn:"/v1/auth/notp/whatsapp/signin",signUp:"/v1/auth/notp/whatsapp/signup",signUpOrIn:"/v1/auth/notp/whatsapp/signup-in",session:"/v1/auth/notp/pending-session"},p={signUp:{start:"/v1/auth/webauthn/signup/start",finish:"/v1/auth/webauthn/signup/finish"},signIn:{start:"/v1/auth/webauthn/signin/start",finish:"/v1/auth/webauthn/signin/finish"},signUpOrIn:{start:"/v1/auth/webauthn/signup-in/start"},update:{start:"v1/auth/webauthn/update/start",finish:"/v1/auth/webauthn/update/finish"}},d={signUp:"/v1/auth/password/signup",signIn:"/v1/auth/password/signin",sendReset:"/v1/auth/password/reset",update:"/v1/auth/password/update",replace:"/v1/auth/password/replace",policy:"/v1/auth/password/policy"},u="/v1/auth/refresh",c="/v1/auth/tenant/select",g="/v1/auth/logout",h="/v1/auth/logoutall",v="/v1/auth/me",f="/v1/auth/me/history",m={start:"/v1/flow/start",next:"/v1/flow/next"};const I="<region>",b=`https://api.${I}descope.com`,k=1e3,y=6e5,O=()=>{const e={};return{headers(t){const n="function"==typeof t.entries?Object.fromEntries(t.entries()):t;return e.Headers=JSON.stringify(n),this},body(t){return e.Body=t,this},url(t){return e.Url=t.toString(),this},method(t){return e.Method=t,this},title(t){return e.Title=t,this},status(t){return e.Status=t,this},build:()=>Object.keys(e).flatMap((t=>e[t]?[`${"Title"!==t?`${t}: `:""}${e[t]}`]:[])).join("\n")}},w=(e,t)=>{const n=(e=>async(...t)=>{const n=await e(...t),s=await n.text();return n.text=()=>Promise.resolve(s),n.json=()=>Promise.resolve(JSON.parse(s)),n.clone=()=>n,n})(t||fetch);return n||null==e||e.warn("Fetch is not defined, you will not be able to send http requests, if you are running in a test, make sure fetch is defined globally"),e?async(...t)=>{if(!n)throw Error("Cannot send http request, fetch is not defined, if you are running in a test, make sure fetch is defined globally");e.log((e=>O().title("Request").url(e[0]).method(e[1].method).headers(e[1].headers).body(e[1].body).build())(t));const s=await n(...t);return e[s.ok?"log":"error"](await(async e=>{const t=await e.text();return O().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var j;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT"}(j||(j={}));const U=(...e)=>new Headers(e.reduce(((e,t)=>{const n=(e=>Array.isArray(e)?e:e instanceof Headers?Array.from(e.entries()):e?Object.entries(e):[])(t);return n.reduce(((t,[n,s])=>(e[n]=s,e)),e),e}),{})),R=e=>void 0===e?void 0:JSON.stringify(e),P=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},x=({baseUrl:e,projectId:t,baseConfig:n,logger:s,hooks:o,cookiePolicy:i,fetch:a})=>{const r=w(s,a),l=async s=>{var a;const l=(null==o?void 0:o.beforeRequest)?o.beforeRequest(s):s,{path:p,body:d,headers:u,queryParams:c,method:g,token:h}=l,v={headers:U(P(t,h),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"2.17.3"},(null==n?void 0:n.baseHeaders)||{},u),method:g,body:R(d)};null!==i&&(v.credentials=i||"include");const f=await r((({path:e,baseUrl:t,queryParams:n,projectId:s})=>{const o=s.slice(1,-27);t=t.replace(I,o?o+".":"");let i=e?`${t.replace(/\/$/,"")}/${null==e?void 0:e.replace(/^\//,"")}`:t;if(n){i=`${i}?`;const e=Object.keys(n);e.forEach(((t,s)=>{i=`${i}${t}=${n[t]}${s===e.length-1?"":"&"}`}))}return i})({path:p,baseUrl:e,queryParams:c,projectId:t}),v);if((null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==f?void 0:f.clone()),null==o?void 0:o.transformResponse){const e=await f.json(),t=((null===(a=f.headers)||void 0===a?void 0:a.get("set-cookie"))||"").split(";").reduce(((e,t)=>{const[n,s]=t.split("=");return Object.assign(Object.assign({},e),{[n.trim()]:s})}),{}),n=Object.assign(Object.assign({},f),{json:()=>Promise.resolve(e),cookies:t});return n.clone=()=>n,o.transformResponse(n)}return f};return{get:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:j.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:j.post,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:j.put,token:o}),delete:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:j.delete,token:s}),hooks:o}};var $={TOO_MANY_REQUESTS:429};function q(e,t,n){var s;let o=E(e);t&&(o=null===(s=null==o?void 0:o.tenants)||void 0===s?void 0:s[t]);const i=null==o?void 0:o[n];return Array.isArray(i)?i:[]}function E(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function T(e){const{exp:t}=E(e);return(new Date).getTime()/1e3>t}function A(e){let t=E(e);const n=Object.keys(null==t?void 0:t.tenants);return Array.isArray(n)?n:[]}function S(e,t){return q(e,t,"permissions")}function M(e,t){return q(e,t,"roles")}const N=(...e)=>e.join("/").replace(/\/{2,}/g,"/");async function z(e,t){var n;const s=await e,o={code:s.status,ok:s.ok,response:s},i=await s.clone().json();return s.ok?o.data=t?t(i):i:(o.error=i,s.status===$.TOO_MANY_REQUESTS&&Object.assign(o.error,{retryAfter:Number.parseInt(null===(n=s.headers)||void 0===n?void 0:n.get("retry-after"))||0})),o}const C=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),H=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),J=e=>t=>e.test(t),L=J(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),D=J(/^\+[1-9]{1}[0-9]{3,14}$/),_=(e,t)=>n=>H(...t).validate(((e,t,n)=>{const s=(Array.isArray(t)?t.join("."):String(t)).replace(/\[\\?("|')?(\w|d)+\\?("|')?\]/g,((e,t,n)=>"."+n)).split("."),o=s.length;let i=0,a=e===Object(e)?e:void 0;for(;null!=a&&i<o;)a=a[s[i++]];return i&&i===o&&void 0!==a?a:n})(n,e)),F=C(L,'"{val}" is not a valid email'),Z=C(D,'"{val}" is not a valid phone number'),B=C((K=1,e=>e.length>=K),"Minimum length is 1");var K;const Q=C((e=>"string"==typeof e),"Input is not a string"),V=C((e=>void 0===e),"Input is defined"),Y=(G=[Q(),V()],W="Input is not a string or undefined",(e=W)=>t=>{const n=G.filter((e=>e(t)));return!(n.length<G.length)&&(e?e.replace("{val}",t):n.join(" OR "))});var G,W;const X=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>H(...e).validate(n[t]))),t(...n)),ee=e=>[Q(`"${e}" must be a string`)],te=e=>[Q(`"${e}" must be a string`),B(`"${e}" must not be empty`)],ne=e=>[Q(`"${e}" must be a string`),F()],se=e=>[Q(`"${e}" must be a string`),Z()],oe=X(te("accessKey")),ie=e=>({exchange:oe(((n,s)=>z(e.post(t.exchange,{loginOptions:s},{token:n}))))}),ae=(e,t,n)=>(t.forEach((t=>{const s=t.split(".");let o=s.shift(),i=e;for(;s.length>0;){if(i=i[o],!o||!i)throw Error(`Invalid path "${t}", "${o}" is missing or has no value`);o=s.shift()}if("function"!=typeof i[o])throw Error(`"${t}" is not a function`);const a=i[o];i[o]=n(a)})),e),re=({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||k,k),timeoutMs:Math.min(t||y,y)});var le,pe;!function(e){e.sms="sms",e.voice="voice",e.whatsapp="whatsapp"}(le||(le={})),function(e){e.email="email"}(pe||(pe={}));const de=Object.assign(Object.assign({},le),pe);var ue;!function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(ue||(ue={}));const ce=te("loginId"),ge=X(te("token")),he=X(ce),ve=X(te("pendingRef")),fe=X(ce,ne("email")),me=e=>({verify:ge((t=>z(e.post(o.verify,{token:t})))),signIn:he(((t,n,s,i)=>z(e.post(N(o.signIn,de.email),{loginId:t,URI:n,loginOptions:s},{token:i})))),signUpOrIn:he(((t,n,s)=>z(e.post(N(o.signUpOrIn,de.email),{loginId:t,URI:n,loginOptions:s})))),signUp:he(((t,n,s,i)=>z(e.post(N(o.signUp,de.email),{loginId:t,URI:n,user:s,loginOptions:i})))),waitForSession:ve(((t,n)=>new Promise((s=>{const{pollingIntervalMs:i,timeoutMs:a}=re(n);let r;const l=setInterval((async()=>{const n=await e.post(o.session,{pendingRef:t});n.ok&&(clearInterval(l),r&&clearTimeout(r),s(z(Promise.resolve(n))))}),i);r=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${a}ms`,errorCode:"0"},ok:!1}),clearInterval(l)}),a)})))),update:{email:fe(((t,n,s,i,a)=>z(e.post(o.update.email,Object.assign({loginId:t,email:n,URI:s},a),{token:i}))))}}),Ie=X(te("flowId")),be=X(te("executionId"),te("stepId"),te("interactionId")),ke=e=>({start:Ie(((t,n,s,o,i,a,r)=>z(e.post(m.start,{flowId:t,options:n,conditionInteractionId:s,interactionId:o,version:i,componentsVersion:a,input:r})))),next:be(((t,n,s,o,i,a)=>z(e.post(m.next,{executionId:t,stepId:n,interactionId:s,version:o,componentsVersion:i,input:a}))))}),ye=te("loginId"),Oe=X(te("token")),we=X(ye),je=X(ye,se("phone")),Ue=X(ye,ne("email")),Re=Object.keys(de).filter((e=>e!==le.voice)),Pe=e=>({verify:Oe((t=>z(e.post(s.verify,{token:t})))),signIn:Re.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:we(((t,o,i,a)=>z(e.post(N(s.signIn,n),{loginId:t,URI:o,loginOptions:i},{token:a}))))})),{}),signUp:Re.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:we(((t,o,i,a)=>z(e.post(N(s.signUp,n),{loginId:t,URI:o,user:i,loginOptions:a}))))})),{}),signUpOrIn:Re.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:we(((t,o,i)=>z(e.post(N(s.signUpOrIn,n),{loginId:t,URI:o,loginOptions:i}))))})),{}),update:{email:Ue(((t,n,o,i,a)=>z(e.post(s.update.email,Object.assign({loginId:t,email:n,URI:o},a),{token:i})))),phone:Object.keys(le).filter((e=>e!==le.voice)).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:je(((t,o,i,a,r)=>z(e.post(N(s.update.phone,n),Object.assign({loginId:t,phone:o,URI:i},r),{token:a}))))})),{})}});var xe;!function(e){e.facebook="facebook",e.github="github",e.google="google",e.microsoft="microsoft",e.gitlab="gitlab",e.apple="apple",e.discord="discord",e.linkedin="linkedin",e.slack="slack"}(xe||(xe={}));const $e=X(te("code")),qe=e=>({start:Object.assign(((t,n,s,o)=>z(e.post(i.start,s||{},{queryParams:Object.assign({provider:t},n&&{redirectURL:n}),token:o}))),Object.keys(xe).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:(t,s,o)=>z(e.post(i.start,s||{},{queryParams:Object.assign({provider:n},t&&{redirectURL:t}),token:o}))})),{})),exchange:$e((t=>z(e.post(i.exchange,{code:t})))),startNative:(t,n)=>z(e.post(i.startNative,{provider:t,loginOptions:n})),finishNative:(t,n,s,o,a)=>z(e.post(i.finishNative,{provider:t,stateId:n,user:s,code:o,idToken:a}))}),Ee=te("loginId"),Te=X(Ee,te("code")),Ae=X(Ee),Se=X(Ee,se("phone")),Me=X(Ee,ne("email")),Ne=e=>({verify:Object.keys(de).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Te(((t,o)=>z(e.post(N(n.verify,s),{code:o,loginId:t}))))})),{}),signIn:Object.keys(de).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ae(((t,o,i)=>z(e.post(N(n.signIn,s),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(de).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ae(((t,o,i)=>z(e.post(N(n.signUp,s),{loginId:t,user:o,loginOptions:i}))))})),{}),signUpOrIn:Object.keys(de).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ae(((t,o)=>z(e.post(N(n.signUpOrIn,s),{loginId:t,loginOptions:o}))))})),{}),update:{email:Me(((t,s,o,i)=>z(e.post(n.update.email,Object.assign({loginId:t,email:s},i),{token:o})))),phone:Object.keys(le).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Se(((t,o,i,a)=>z(e.post(N(n.update.phone,s),Object.assign({loginId:t,phone:o},a),{token:i}))))})),{})}}),ze=X(te("tenant")),Ce=X(te("code")),He=e=>({start:ze(((t,n,s,o)=>z(e.post(a.start,s||{},{queryParams:{tenant:t,redirectURL:n},token:o})))),exchange:Ce((t=>z(e.post(a.exchange,{code:t}))))}),Je=te("loginId"),Le=X(Je,te("code")),De=X(Je),_e=X(Je),Fe=e=>({signUp:De(((t,n)=>z(e.post(r.signUp,{loginId:t,user:n})))),verify:Le(((t,n,s,o)=>z(e.post(r.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:_e(((t,n)=>z(e.post(r.update,{loginId:t},{token:n}))))}),Ze=te("loginId"),Be=te("newPassword"),Ke=X(Ze,te("password")),Qe=X(Ze),Ve=X(Ze,Be),Ye=X(Ze,te("oldPassword"),Be),Ge=e=>({signUp:Ke(((t,n,s,o)=>z(e.post(d.signUp,{loginId:t,password:n,user:s,loginOptions:o})))),signIn:Ke(((t,n,s)=>z(e.post(d.signIn,{loginId:t,password:n,loginOptions:s})))),sendReset:Qe(((t,n,s)=>z(e.post(d.sendReset,{loginId:t,redirectUrl:n,templateOptions:s})))),update:Ve(((t,n,s)=>z(e.post(d.update,{loginId:t,newPassword:n},{token:s})))),replace:Ye(((t,n,s)=>z(e.post(d.replace,{loginId:t,oldPassword:n,newPassword:s})))),policy:()=>z(e.get(d.policy))}),We=ee("loginId"),Xe=te("loginId"),et=te("origin"),tt=X(Xe,et,te("name")),nt=X(Xe,et),st=X(We,et),ot=X(Xe,et,te("token")),it=X(te("transactionId"),te("response")),at=e=>({signUp:{start:tt(((t,n,s)=>z(e.post(p.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:it(((t,n)=>z(e.post(p.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:st(((t,n,s,o)=>z(e.post(p.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:it(((t,n)=>z(e.post(p.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:nt(((t,n)=>z(e.post(p.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:ot(((t,n,s)=>z(e.post(p.update.start,{loginId:t,origin:n},{token:s})))),finish:it(((t,n)=>z(e.post(p.update.finish,{transactionId:t,response:n}))))}}),rt=ee("loginId"),lt=X(rt),pt=X(te("pendingRef")),dt=e=>({signUpOrIn:lt(((t,n)=>z(e.post(l.signUpOrIn,{loginId:t,loginOptions:n})))),signUp:lt(((t,n,s)=>z(e.post(l.signUp,{loginId:t,user:n,loginOptions:s})))),signIn:lt(((t,n,s)=>z(e.post(l.signIn,{loginId:t,loginOptions:n},{token:s})))),waitForSession:pt(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:i}=re(n);let a;const r=setInterval((async()=>{const n=await e.post(l.session,{pendingRef:t});n.ok&&(clearInterval(r),a&&clearTimeout(a),s(z(Promise.resolve(n))))}),o);a=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${i}ms`,errorCode:"0"},ok:!1}),clearInterval(r)}),i)}))))}),ut=X(te("token")),ct=X([Y(`"${"token"}" must be string or undefined`)]);var gt,ht;var vt=X([(gt="projectId",ht=te("projectId"),C(_(gt,ht))())])((e=>t=>{var n;return e(Object.assign(Object.assign({},t),{hooks:{beforeRequest:e=>{var n;const s=[].concat((null===(n=t.hooks)||void 0===n?void 0:n.beforeRequest)||[]);return null==s?void 0:s.reduce(((e,t)=>t(e)),e)},afterRequest:async(e,n)=>{var s;const o=[].concat((null===(s=t.hooks)||void 0===s?void 0:s.afterRequest)||[]);(await Promise.allSettled(null==o?void 0:o.map((t=>t(e,null==n?void 0:n.clone()))))).forEach((e=>{var n;return"rejected"===e.status&&(null===(n=t.logger)||void 0===n?void 0:n.error(e.reason))}))},transformResponse:null===(n=t.hooks)||void 0===n?void 0:n.transformResponse}}))})((({projectId:e,logger:t,baseUrl:n,hooks:s,cookiePolicy:o,baseHeaders:i={},fetch:a})=>{return r=x({baseUrl:n||b,projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:ie(r),otp:Ne(r),magicLink:Pe(r),enchantedLink:me(r),oauth:qe(r),saml:He(r),totp:Fe(r),notp:dt(r),webauthn:at(r),password:Ge(r),flow:ke(r),refresh:ct((e=>z(r.post(u,{},{token:e})))),selectTenant:X([Q("tenantId")],[Y('"token" must be string or undefined')])(((e,t)=>z(r.post(c,{tenant:e},{token:t})))),logout:ct((e=>z(r.post(g,{},{token:e})))),logoutAll:ct((e=>z(r.post(h,{},{token:e})))),me:ct((e=>z(r.get(v,{token:e})))),history:ct((e=>z(r.get(f,{token:e})))),isJwtExpired:ut(T),getTenants:ut(A),getJwtPermissions:ut(S),getJwtRoles:ut(M),httpClient:r};var r}))),ft=Object.assign(vt,{DeliveryMethods:de});export{$ as HttpStatusCodes,ft as default,re as normalizeWaitForSessionConfig,z as transformResponse,ae as wrapWith}; | ||
import e from"jwt-decode";var t={exchange:"/v1/auth/accesskey/exchange"},n={verify:"/v1/auth/otp/verify",signIn:"/v1/auth/otp/signin",signUp:"/v1/auth/otp/signup",update:{email:"/v1/auth/otp/update/email",phone:"/v1/auth/otp/update/phone"},signUpOrIn:"/v1/auth/otp/signup-in"},s={verify:"/v1/auth/magiclink/verify",signIn:"/v1/auth/magiclink/signin",signUp:"/v1/auth/magiclink/signup",update:{email:"/v1/auth/magiclink/update/email",phone:"/v1/auth/magiclink/update/phone"},signUpOrIn:"/v1/auth/magiclink/signup-in"},o={verify:"/v1/auth/enchantedlink/verify",signIn:"/v1/auth/enchantedlink/signin",signUp:"/v1/auth/enchantedlink/signup",session:"/v1/auth/enchantedlink/pending-session",update:{email:"/v1/auth/enchantedlink/update/email"},signUpOrIn:"/v1/auth/enchantedlink/signup-in"},i={start:"/v1/auth/oauth/authorize",exchange:"/v1/auth/oauth/exchange",startNative:"v1/auth/oauth/native/start",finishNative:"v1/auth/oauth/native/finish"},a={start:"/v1/auth/saml/authorize",exchange:"/v1/auth/saml/exchange"},r={verify:"/v1/auth/totp/verify",signUp:"/v1/auth/totp/signup",update:"/v1/auth/totp/update"},l={signIn:"/v1/auth/notp/whatsapp/signin",signUp:"/v1/auth/notp/whatsapp/signup",signUpOrIn:"/v1/auth/notp/whatsapp/signup-in",session:"/v1/auth/notp/pending-session"},p={signUp:{start:"/v1/auth/webauthn/signup/start",finish:"/v1/auth/webauthn/signup/finish"},signIn:{start:"/v1/auth/webauthn/signin/start",finish:"/v1/auth/webauthn/signin/finish"},signUpOrIn:{start:"/v1/auth/webauthn/signup-in/start"},update:{start:"v1/auth/webauthn/update/start",finish:"/v1/auth/webauthn/update/finish"}},d={signUp:"/v1/auth/password/signup",signIn:"/v1/auth/password/signin",sendReset:"/v1/auth/password/reset",update:"/v1/auth/password/update",replace:"/v1/auth/password/replace",policy:"/v1/auth/password/policy"},u="/v1/auth/refresh",c="/v1/auth/tenant/select",g="/v1/auth/logout",h="/v1/auth/logoutall",v="/v1/auth/me",f="/v1/auth/me/history",m={start:"/v1/flow/start",next:"/v1/flow/next"};const I="<region>",b=`https://api.${I}descope.com`,k=1e3,y=6e5,O=()=>{const e={};return{headers(t){const n="function"==typeof t.entries?Object.fromEntries(t.entries()):t;return e.Headers=JSON.stringify(n),this},body(t){return e.Body=t,this},url(t){return e.Url=t.toString(),this},method(t){return e.Method=t,this},title(t){return e.Title=t,this},status(t){return e.Status=t,this},build:()=>Object.keys(e).flatMap((t=>e[t]?[`${"Title"!==t?`${t}: `:""}${e[t]}`]:[])).join("\n")}},w=(e,t)=>{const n=(e=>async(...t)=>{const n=await e(...t),s=await n.text();return n.text=()=>Promise.resolve(s),n.json=()=>Promise.resolve(JSON.parse(s)),n.clone=()=>n,n})(t||fetch);return n||null==e||e.warn("Fetch is not defined, you will not be able to send http requests, if you are running in a test, make sure fetch is defined globally"),e?async(...t)=>{if(!n)throw Error("Cannot send http request, fetch is not defined, if you are running in a test, make sure fetch is defined globally");e.log((e=>O().title("Request").url(e[0]).method(e[1].method).headers(e[1].headers).body(e[1].body).build())(t));const s=await n(...t);return e[s.ok?"log":"error"](await(async e=>{const t=await e.text();return O().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var j;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT"}(j||(j={}));const U=(...e)=>new Headers(e.reduce(((e,t)=>{const n=(e=>Array.isArray(e)?e:e instanceof Headers?Array.from(e.entries()):e?Object.entries(e):[])(t);return n.reduce(((t,[n,s])=>(e[n]=s,e)),e),e}),{})),R=e=>void 0===e?void 0:JSON.stringify(e),P=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},x=({baseUrl:e,projectId:t,baseConfig:n,logger:s,hooks:o,cookiePolicy:i,fetch:a})=>{const r=w(s,a),l=async s=>{var a;const l=(null==o?void 0:o.beforeRequest)?o.beforeRequest(s):s,{path:p,body:d,headers:u,queryParams:c,method:g,token:h}=l,v={headers:U(P(t,h),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"2.17.4"},(null==n?void 0:n.baseHeaders)||{},u),method:g,body:R(d)};null!==i&&(v.credentials=i||"include");const f=await r((({path:e,baseUrl:t,queryParams:n,projectId:s})=>{const o=s.slice(1,-27);t=t.replace(I,o?o+".":"");let i=e?`${t.replace(/\/$/,"")}/${null==e?void 0:e.replace(/^\//,"")}`:t;if(n){i=`${i}?`;const e=Object.keys(n);e.forEach(((t,s)=>{i=`${i}${t}=${n[t]}${s===e.length-1?"":"&"}`}))}return i})({path:p,baseUrl:e,queryParams:c,projectId:t}),v);if((null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==f?void 0:f.clone()),null==o?void 0:o.transformResponse){const e=await f.json(),t=((null===(a=f.headers)||void 0===a?void 0:a.get("set-cookie"))||"").split(";").reduce(((e,t)=>{const[n,s]=t.split("=");return Object.assign(Object.assign({},e),{[n.trim()]:s})}),{}),n=Object.assign(Object.assign({},f),{json:()=>Promise.resolve(e),cookies:t});return n.clone=()=>n,o.transformResponse(n)}return f};return{get:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:j.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:j.post,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:j.put,token:o}),delete:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:j.delete,token:s}),hooks:o}};var $={TOO_MANY_REQUESTS:429};function q(e,t,n){var s;let o=E(e);t&&(o=null===(s=null==o?void 0:o.tenants)||void 0===s?void 0:s[t]);const i=null==o?void 0:o[n];return Array.isArray(i)?i:[]}function E(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function T(e){const{exp:t}=E(e);return(new Date).getTime()/1e3>t}function A(e){let t=E(e);const n=Object.keys(null==t?void 0:t.tenants);return Array.isArray(n)?n:[]}function S(e,t){return q(e,t,"permissions")}function M(e,t){return q(e,t,"roles")}const N=(...e)=>e.join("/").replace(/\/{2,}/g,"/");async function z(e,t){var n;const s=await e,o={code:s.status,ok:s.ok,response:s},i=await s.clone().json();return s.ok?o.data=t?t(i):i:(o.error=i,s.status===$.TOO_MANY_REQUESTS&&Object.assign(o.error,{retryAfter:Number.parseInt(null===(n=s.headers)||void 0===n?void 0:n.get("retry-after"))||0})),o}const C=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),H=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),J=e=>t=>e.test(t),L=J(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),D=J(/^\+[1-9]{1}[0-9]{3,14}$/),_=(e,t)=>n=>H(...t).validate(((e,t,n)=>{const s=(Array.isArray(t)?t.join("."):String(t)).replace(/\[\\?("|')?(\w|d)+\\?("|')?\]/g,((e,t,n)=>"."+n)).split("."),o=s.length;let i=0,a=e===Object(e)?e:void 0;for(;null!=a&&i<o;)a=a[s[i++]];return i&&i===o&&void 0!==a?a:n})(n,e)),F=C(L,'"{val}" is not a valid email'),Z=C(D,'"{val}" is not a valid phone number'),B=C((K=1,e=>e.length>=K),"Minimum length is 1");var K;const Q=C((e=>"string"==typeof e),"Input is not a string"),V=C((e=>void 0===e),"Input is defined"),Y=(G=[Q(),V()],W="Input is not a string or undefined",(e=W)=>t=>{const n=G.filter((e=>e(t)));return!(n.length<G.length)&&(e?e.replace("{val}",t):n.join(" OR "))});var G,W;const X=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>H(...e).validate(n[t]))),t(...n)),ee=e=>[Q(`"${e}" must be a string`)],te=e=>[Q(`"${e}" must be a string`),B(`"${e}" must not be empty`)],ne=e=>[Q(`"${e}" must be a string`),F()],se=e=>[Q(`"${e}" must be a string`),Z()],oe=X(te("accessKey")),ie=e=>({exchange:oe(((n,s)=>z(e.post(t.exchange,{loginOptions:s},{token:n}))))}),ae=(e,t,n)=>(t.forEach((t=>{const s=t.split(".");let o=s.shift(),i=e;for(;s.length>0;){if(i=i[o],!o||!i)throw Error(`Invalid path "${t}", "${o}" is missing or has no value`);o=s.shift()}if("function"!=typeof i[o])throw Error(`"${t}" is not a function`);const a=i[o];i[o]=n(a)})),e),re=({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||k,k),timeoutMs:Math.min(t||y,y)});var le,pe;!function(e){e.sms="sms",e.voice="voice",e.whatsapp="whatsapp"}(le||(le={})),function(e){e.email="email"}(pe||(pe={}));const de=Object.assign(Object.assign({},le),pe);var ue;!function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(ue||(ue={}));const ce=te("loginId"),ge=X(te("token")),he=X(ce),ve=X(te("pendingRef")),fe=X(ce,ne("email")),me=e=>({verify:ge((t=>z(e.post(o.verify,{token:t})))),signIn:he(((t,n,s,i)=>z(e.post(N(o.signIn,de.email),{loginId:t,URI:n,loginOptions:s},{token:i})))),signUpOrIn:he(((t,n,s)=>z(e.post(N(o.signUpOrIn,de.email),{loginId:t,URI:n,loginOptions:s})))),signUp:he(((t,n,s,i)=>z(e.post(N(o.signUp,de.email),{loginId:t,URI:n,user:s,loginOptions:i})))),waitForSession:ve(((t,n)=>new Promise((s=>{const{pollingIntervalMs:i,timeoutMs:a}=re(n);let r;const l=setInterval((async()=>{const n=await e.post(o.session,{pendingRef:t});n.ok&&(clearInterval(l),r&&clearTimeout(r),s(z(Promise.resolve(n))))}),i);r=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${a}ms`,errorCode:"0"},ok:!1}),clearInterval(l)}),a)})))),update:{email:fe(((t,n,s,i,a)=>z(e.post(o.update.email,Object.assign({loginId:t,email:n,URI:s},a),{token:i}))))}}),Ie=X(te("flowId")),be=X(te("executionId"),te("stepId"),te("interactionId")),ke=e=>({start:Ie(((t,n,s,o,i,a,r)=>z(e.post(m.start,{flowId:t,options:n,conditionInteractionId:s,interactionId:o,version:i,componentsVersion:a,input:r})))),next:be(((t,n,s,o,i,a)=>z(e.post(m.next,{executionId:t,stepId:n,interactionId:s,version:o,componentsVersion:i,input:a}))))}),ye=te("loginId"),Oe=X(te("token")),we=X(ye),je=X(ye,se("phone")),Ue=X(ye,ne("email")),Re=Object.keys(de).filter((e=>e!==le.voice)),Pe=e=>({verify:Oe((t=>z(e.post(s.verify,{token:t})))),signIn:Re.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:we(((t,o,i,a)=>z(e.post(N(s.signIn,n),{loginId:t,URI:o,loginOptions:i},{token:a}))))})),{}),signUp:Re.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:we(((t,o,i,a)=>z(e.post(N(s.signUp,n),{loginId:t,URI:o,user:i,loginOptions:a}))))})),{}),signUpOrIn:Re.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:we(((t,o,i)=>z(e.post(N(s.signUpOrIn,n),{loginId:t,URI:o,loginOptions:i}))))})),{}),update:{email:Ue(((t,n,o,i,a)=>z(e.post(s.update.email,Object.assign({loginId:t,email:n,URI:o},a),{token:i})))),phone:Object.keys(le).filter((e=>e!==le.voice)).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:je(((t,o,i,a,r)=>z(e.post(N(s.update.phone,n),Object.assign({loginId:t,phone:o,URI:i},r),{token:a}))))})),{})}});var xe;!function(e){e.facebook="facebook",e.github="github",e.google="google",e.microsoft="microsoft",e.gitlab="gitlab",e.apple="apple",e.discord="discord",e.linkedin="linkedin",e.slack="slack"}(xe||(xe={}));const $e=X(te("code")),qe=e=>({start:Object.assign(((t,n,s,o)=>z(e.post(i.start,s||{},{queryParams:Object.assign({provider:t},n&&{redirectURL:n}),token:o}))),Object.keys(xe).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:(t,s,o)=>z(e.post(i.start,s||{},{queryParams:Object.assign({provider:n},t&&{redirectURL:t}),token:o}))})),{})),exchange:$e((t=>z(e.post(i.exchange,{code:t})))),startNative:(t,n)=>z(e.post(i.startNative,{provider:t,loginOptions:n})),finishNative:(t,n,s,o,a)=>z(e.post(i.finishNative,{provider:t,stateId:n,user:s,code:o,idToken:a}))}),Ee=te("loginId"),Te=X(Ee,te("code")),Ae=X(Ee),Se=X(Ee,se("phone")),Me=X(Ee,ne("email")),Ne=e=>({verify:Object.keys(de).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Te(((t,o)=>z(e.post(N(n.verify,s),{code:o,loginId:t}))))})),{}),signIn:Object.keys(de).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ae(((t,o,i)=>z(e.post(N(n.signIn,s),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(de).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ae(((t,o,i)=>z(e.post(N(n.signUp,s),{loginId:t,user:o,loginOptions:i}))))})),{}),signUpOrIn:Object.keys(de).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ae(((t,o)=>z(e.post(N(n.signUpOrIn,s),{loginId:t,loginOptions:o}))))})),{}),update:{email:Me(((t,s,o,i)=>z(e.post(n.update.email,Object.assign({loginId:t,email:s},i),{token:o})))),phone:Object.keys(le).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Se(((t,o,i,a)=>z(e.post(N(n.update.phone,s),Object.assign({loginId:t,phone:o},a),{token:i}))))})),{})}}),ze=X(te("tenant")),Ce=X(te("code")),He=e=>({start:ze(((t,n,s,o)=>z(e.post(a.start,s||{},{queryParams:{tenant:t,redirectURL:n},token:o})))),exchange:Ce((t=>z(e.post(a.exchange,{code:t}))))}),Je=te("loginId"),Le=X(Je,te("code")),De=X(Je),_e=X(Je),Fe=e=>({signUp:De(((t,n)=>z(e.post(r.signUp,{loginId:t,user:n})))),verify:Le(((t,n,s,o)=>z(e.post(r.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:_e(((t,n)=>z(e.post(r.update,{loginId:t},{token:n}))))}),Ze=te("loginId"),Be=te("newPassword"),Ke=X(Ze,te("password")),Qe=X(Ze),Ve=X(Ze,Be),Ye=X(Ze,te("oldPassword"),Be),Ge=e=>({signUp:Ke(((t,n,s,o)=>z(e.post(d.signUp,{loginId:t,password:n,user:s,loginOptions:o})))),signIn:Ke(((t,n,s)=>z(e.post(d.signIn,{loginId:t,password:n,loginOptions:s})))),sendReset:Qe(((t,n,s)=>z(e.post(d.sendReset,{loginId:t,redirectUrl:n,templateOptions:s})))),update:Ve(((t,n,s)=>z(e.post(d.update,{loginId:t,newPassword:n},{token:s})))),replace:Ye(((t,n,s)=>z(e.post(d.replace,{loginId:t,oldPassword:n,newPassword:s})))),policy:()=>z(e.get(d.policy))}),We=ee("loginId"),Xe=te("loginId"),et=te("origin"),tt=X(Xe,et,te("name")),nt=X(Xe,et),st=X(We,et),ot=X(Xe,et,te("token")),it=X(te("transactionId"),te("response")),at=e=>({signUp:{start:tt(((t,n,s)=>z(e.post(p.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:it(((t,n)=>z(e.post(p.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:st(((t,n,s,o)=>z(e.post(p.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:it(((t,n)=>z(e.post(p.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:nt(((t,n)=>z(e.post(p.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:ot(((t,n,s)=>z(e.post(p.update.start,{loginId:t,origin:n},{token:s})))),finish:it(((t,n)=>z(e.post(p.update.finish,{transactionId:t,response:n}))))}}),rt=ee("loginId"),lt=X(rt),pt=X(te("pendingRef")),dt=e=>({signUpOrIn:lt(((t,n)=>z(e.post(l.signUpOrIn,{loginId:t,loginOptions:n})))),signUp:lt(((t,n,s)=>z(e.post(l.signUp,{loginId:t,user:n,loginOptions:s})))),signIn:lt(((t,n,s)=>z(e.post(l.signIn,{loginId:t,loginOptions:n},{token:s})))),waitForSession:pt(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:i}=re(n);let a;const r=setInterval((async()=>{const n=await e.post(l.session,{pendingRef:t});n.ok&&(clearInterval(r),a&&clearTimeout(a),s(z(Promise.resolve(n))))}),o);a=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${i}ms`,errorCode:"0"},ok:!1}),clearInterval(r)}),i)}))))}),ut=X(te("token")),ct=X([Y(`"${"token"}" must be string or undefined`)]);var gt,ht;var vt=X([(gt="projectId",ht=te("projectId"),C(_(gt,ht))())])((e=>t=>{var n;return e(Object.assign(Object.assign({},t),{hooks:{beforeRequest:e=>{var n;const s=[].concat((null===(n=t.hooks)||void 0===n?void 0:n.beforeRequest)||[]);return null==s?void 0:s.reduce(((e,t)=>t(e)),e)},afterRequest:async(e,n)=>{var s;const o=[].concat((null===(s=t.hooks)||void 0===s?void 0:s.afterRequest)||[]);(await Promise.allSettled(null==o?void 0:o.map((t=>t(e,null==n?void 0:n.clone()))))).forEach((e=>{var n;return"rejected"===e.status&&(null===(n=t.logger)||void 0===n?void 0:n.error(e.reason))}))},transformResponse:null===(n=t.hooks)||void 0===n?void 0:n.transformResponse}}))})((({projectId:e,logger:t,baseUrl:n,hooks:s,cookiePolicy:o,baseHeaders:i={},fetch:a})=>{return r=x({baseUrl:n||b,projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:ie(r),otp:Ne(r),magicLink:Pe(r),enchantedLink:me(r),oauth:qe(r),saml:He(r),totp:Fe(r),notp:dt(r),webauthn:at(r),password:Ge(r),flow:ke(r),refresh:ct((e=>z(r.post(u,{},{token:e})))),selectTenant:X([Q("tenantId")],[Y('"token" must be string or undefined')])(((e,t)=>z(r.post(c,{tenant:e},{token:t})))),logout:ct((e=>z(r.post(g,{},{token:e})))),logoutAll:ct((e=>z(r.post(h,{},{token:e})))),me:ct((e=>z(r.get(v,{token:e})))),history:ct((e=>z(r.get(f,{token:e})))),isJwtExpired:ut(T),getTenants:ut(A),getJwtPermissions:ut(S),getJwtRoles:ut(M),httpClient:r};var r}))),ft=Object.assign(vt,{DeliveryMethods:de});export{$ as HttpStatusCodes,ft as default,re as normalizeWaitForSessionConfig,z as transformResponse,ae as wrapWith}; | ||
//# sourceMappingURL=index.esm.js.map |
{ | ||
"name": "@descope/core-js-sdk", | ||
"version": "2.17.3", | ||
"version": "2.17.4", | ||
"author": "Descope Team <info@descope.com>", | ||
@@ -56,3 +56,3 @@ "homepage": "https://github.com/descope/core-js-sdk", | ||
"eslint-plugin-jest-formatting": "3.1.0", | ||
"eslint-plugin-n": "17.4.0", | ||
"eslint-plugin-n": "17.5.1", | ||
"eslint-plugin-no-only-tests": "3.1.0", | ||
@@ -59,0 +59,0 @@ "eslint-plugin-prefer-arrow": "1.2.3", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package