@descope/core-js-sdk
Advanced tools
Comparing version 2.23.5 to 2.24.0
@@ -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"},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`,y=1e3,k=6e5,O="dct",j=()=>{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=>j().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 j().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var U;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT",e.patch="PATCH"}(U||(U={}));const P=({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},R=(...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}),{})),x={"Content-Type":"application/json"},q=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},$=e=>{try{e=JSON.parse(e)}catch(e){return!1}return"object"==typeof e&&null!==e},T=({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=(e=>void 0===e?void 0:JSON.stringify(e))(d),f={headers:R(q(t,h),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"2.23.5"},(null==n?void 0:n.baseHeaders)||{},$(v)?x:{},u),method:g,body:v};null!==i&&(f.credentials=i||"include");const m=await r(P({path:p,baseUrl:e,queryParams:c,projectId:t}),f);if((null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==m?void 0:m.clone()),null==o?void 0:o.transformResponse){const e=await m.json(),t=((null===(a=m.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({},m),{json:()=>Promise.resolve(e),cookies:t});return n.clone=()=>n,o.transformResponse(n)}return m};return{get:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:U.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.post,token:o}),patch:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.patch,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.put,token:o}),delete:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:U.delete,token:s}),hooks:o,buildUrl:(n,s)=>P({projectId:t,baseUrl:e,path:n,queryParams:s})}};var E={TOO_MANY_REQUESTS:429};function S(e,t,n){var s;let o=A(e);if(t){if(!(null==o?void 0:o.tenants)&&(null==o?void 0:o[O])===t)return(null==o?void 0:o[n])||[];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 A(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function M(e){const{exp:t}=A(e);return(new Date).getTime()/1e3>t}function N(e){let t=A(e);const n=Object.keys(null==t?void 0:t.tenants);return Array.isArray(n)?n:[]}function C(e,t){return S(e,t,"permissions")}function H(e,t){return S(e,t,"roles")}const z=(...e)=>e.join("/").replace(/\/{2,}/g,"/");async function J(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===E.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 _=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),L=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),D=e=>t=>e.test(t),F=D(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),Z=D(/^\+[1-9]{1}[0-9]{3,14}$/),B=(e,t)=>n=>L(...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)),K=_(F,'"{val}" is not a valid email'),Q=_(Z,'"{val}" is not a valid phone number'),V=_((W=1,e=>e.length>=W),"Minimum length is 1");var W;const Y=_((e=>"string"==typeof e),"Input is not a string"),G=_((e=>void 0===e),"Input is defined"),X=(ee=[Y(),G()],te="Input is not a string or undefined",(e=te)=>t=>{const n=ee.filter((e=>e(t)));return!(n.length<ee.length)&&(e?e.replace("{val}",t):n.join(" OR "))});var ee,te;const ne=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>L(...e).validate(n[t]))),t(...n)),se=e=>[Y(`"${e}" must be a string`)],oe=e=>[Y(`"${e}" must be a string`),V(`"${e}" must not be empty`)],ie=e=>[Y(`"${e}" must be a string`),K()],ae=e=>[Y(`"${e}" must be a string`),Q()],re=ne(oe("accessKey")),le=e=>({exchange:re(((n,s)=>J(e.post(t.exchange,{loginOptions:s},{token:n}))))}),pe=({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||y,y),timeoutMs:Math.min(t||k,k)});var de,ue;!function(e){e.sms="sms",e.voice="voice",e.whatsapp="whatsapp"}(de||(de={})),function(e){e.email="email"}(ue||(ue={}));const ce=Object.assign(Object.assign({},de),ue);var ge;!function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(ge||(ge={}));const he=oe("loginId"),ve=ne(oe("token")),fe=ne(he),me=ne(oe("pendingRef")),Ie=ne(he,ie("email")),be=e=>({verify:ve((t=>J(e.post(o.verify,{token:t})))),signIn:fe(((t,n,s,i)=>J(e.post(z(o.signIn,ce.email),{loginId:t,URI:n,loginOptions:s},{token:i})))),signUpOrIn:fe(((t,n,s)=>J(e.post(z(o.signUpOrIn,ce.email),{loginId:t,URI:n,loginOptions:s})))),signUp:fe(((t,n,s,i)=>J(e.post(z(o.signUp,ce.email),{loginId:t,URI:n,user:s,loginOptions:i})))),waitForSession:me(((t,n)=>new Promise((s=>{const{pollingIntervalMs:i,timeoutMs:a}=pe(n);let r;const l=setInterval((async()=>{const n=await e.post(o.session,{pendingRef:t});n.ok&&(clearInterval(l),r&&clearTimeout(r),s(J(Promise.resolve(n))))}),i);r=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${a}ms`,errorCode:"0"},ok:!1}),clearInterval(l)}),a)})))),update:{email:Ie(((t,n,s,i,a)=>J(e.post(o.update.email,Object.assign({loginId:t,email:n,URI:s},a),{token:i}))))}}),ye=ne(oe("flowId")),ke=ne(oe("executionId"),oe("stepId"),oe("interactionId")),Oe=e=>({start:ye(((t,n,s,o,i,a,r)=>J(e.post(m.start,{flowId:t,options:n,conditionInteractionId:s,interactionId:o,version:i,componentsVersion:a,input:r})))),next:ke(((t,n,s,o,i,a)=>J(e.post(m.next,{executionId:t,stepId:n,interactionId:s,version:o,componentsVersion:i,input:a}))))}),je=oe("loginId"),we=ne(oe("token")),Ue=ne(je),Pe=ne(je,ae("phone")),Re=ne(je,ie("email")),xe=Object.keys(ce).filter((e=>e!==de.voice)),qe=e=>({verify:we((t=>J(e.post(s.verify,{token:t})))),signIn:xe.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Ue(((t,o,i,a)=>J(e.post(z(s.signIn,n),{loginId:t,URI:o,loginOptions:i},{token:a}))))})),{}),signUp:xe.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Ue(((t,o,i,a)=>J(e.post(z(s.signUp,n),{loginId:t,URI:o,user:i,loginOptions:a}))))})),{}),signUpOrIn:xe.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Ue(((t,o,i)=>J(e.post(z(s.signUpOrIn,n),{loginId:t,URI:o,loginOptions:i}))))})),{}),update:{email:Re(((t,n,o,i,a)=>J(e.post(s.update.email,Object.assign({loginId:t,email:n,URI:o},a),{token:i})))),phone:Object.keys(de).filter((e=>e!==de.voice)).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Pe(((t,o,i,a,r)=>J(e.post(z(s.update.phone,n),Object.assign({loginId:t,phone:o,URI:i},r),{token:a}))))})),{})}});var $e;!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"}($e||($e={}));const Te=ne(oe("code")),Ee=e=>({start:Object.assign(((t,n,s,o)=>J(e.post(i.start,s||{},{queryParams:Object.assign({provider:t},n&&{redirectURL:n}),token:o}))),Object.keys($e).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:(t,s,o)=>J(e.post(i.start,s||{},{queryParams:Object.assign({provider:n},t&&{redirectURL:t}),token:o}))})),{})),exchange:Te((t=>J(e.post(i.exchange,{code:t})))),startNative:(t,n,s)=>J(e.post(i.startNative,{provider:t,loginOptions:n,implicit:s})),finishNative:(t,n,s,o,a)=>J(e.post(i.finishNative,{provider:t,stateId:n,user:s,code:o,idToken:a}))}),Se=oe("loginId"),Ae=ne(Se,oe("code")),Me=ne(Se),Ne=ne(Se,ae("phone")),Ce=ne(Se,ie("email")),He=e=>({verify:Object.keys(ce).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ae(((t,o)=>J(e.post(z(n.verify,s),{code:o,loginId:t}))))})),{}),signIn:Object.keys(ce).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Me(((t,o,i)=>J(e.post(z(n.signIn,s),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(ce).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Me(((t,o,i)=>J(e.post(z(n.signUp,s),{loginId:t,user:o,loginOptions:i}))))})),{}),signUpOrIn:Object.keys(ce).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Me(((t,o)=>J(e.post(z(n.signUpOrIn,s),{loginId:t,loginOptions:o}))))})),{}),update:{email:Ce(((t,s,o,i)=>J(e.post(n.update.email,Object.assign({loginId:t,email:s},i),{token:o})))),phone:Object.keys(de).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ne(((t,o,i,a)=>J(e.post(z(n.update.phone,s),Object.assign({loginId:t,phone:o},a),{token:i}))))})),{})}}),ze=ne(oe("tenant")),Je=ne(oe("code")),_e=e=>({start:ze(((t,n,s,o)=>J(e.post(a.start,s||{},{queryParams:{tenant:t,redirectURL:n},token:o})))),exchange:Je((t=>J(e.post(a.exchange,{code:t}))))}),Le=oe("loginId"),De=ne(Le,oe("code")),Fe=ne(Le),Ze=ne(Le),Be=e=>({signUp:Fe(((t,n)=>J(e.post(r.signUp,{loginId:t,user:n})))),verify:De(((t,n,s,o)=>J(e.post(r.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:Ze(((t,n)=>J(e.post(r.update,{loginId:t},{token:n}))))}),Ke=oe("loginId"),Qe=oe("newPassword"),Ve=ne(Ke,oe("password")),We=ne(Ke),Ye=ne(Ke,Qe),Ge=ne(Ke,oe("oldPassword"),Qe),Xe=e=>({signUp:Ve(((t,n,s,o)=>J(e.post(d.signUp,{loginId:t,password:n,user:s,loginOptions:o})))),signIn:Ve(((t,n,s)=>J(e.post(d.signIn,{loginId:t,password:n,loginOptions:s})))),sendReset:We(((t,n,s)=>J(e.post(d.sendReset,{loginId:t,redirectUrl:n,templateOptions:s})))),update:Ye(((t,n,s)=>J(e.post(d.update,{loginId:t,newPassword:n},{token:s})))),replace:Ge(((t,n,s)=>J(e.post(d.replace,{loginId:t,oldPassword:n,newPassword:s})))),policy:()=>J(e.get(d.policy))}),et=se("loginId"),tt=oe("loginId"),nt=oe("origin"),st=ne(tt,nt,oe("name")),ot=ne(tt,nt),it=ne(et,nt),at=ne(tt,nt,oe("token")),rt=ne(oe("transactionId"),oe("response")),lt=e=>({signUp:{start:st(((t,n,s)=>J(e.post(p.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:rt(((t,n)=>J(e.post(p.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:it(((t,n,s,o)=>J(e.post(p.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:rt(((t,n)=>J(e.post(p.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:ot(((t,n)=>J(e.post(p.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:at(((t,n,s)=>J(e.post(p.update.start,{loginId:t,origin:n},{token:s})))),finish:rt(((t,n)=>J(e.post(p.update.finish,{transactionId:t,response:n}))))}}),pt=se("loginId"),dt=ne(pt),ut=ne(oe("pendingRef")),ct=e=>({signUpOrIn:dt(((t,n)=>J(e.post(l.signUpOrIn,{loginId:t,loginOptions:n})))),signUp:dt(((t,n,s)=>J(e.post(l.signUp,{loginId:t,user:n,loginOptions:s})))),signIn:dt(((t,n,s)=>J(e.post(l.signIn,{loginId:t,loginOptions:n},{token:s})))),waitForSession:ut(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:i}=pe(n);let a;const r=setInterval((async()=>{const n=await e.post(l.session,{pendingRef:t});n.ok&&(clearInterval(r),a&&clearTimeout(a),s(J(Promise.resolve(n))))}),o);a=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${i}ms`,errorCode:"0"},ok:!1}),clearInterval(r)}),i)}))))}),gt=ne(oe("token")),ht=ne([X(`"${"token"}" must be string or undefined`)]);var vt,ft;var mt=ne([(vt="projectId",ft=oe("projectId"),_(B(vt,ft))())])((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)||[]);if(0==o.length)return;(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=T({baseUrl:n||b,projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:le(r),otp:He(r),magicLink:qe(r),enchantedLink:be(r),oauth:Ee(r),saml:_e(r),totp:Be(r),notp:ct(r),webauthn:lt(r),password:Xe(r),flow:Oe(r),refresh:ht((e=>J(r.post(u,{},{token:e})))),selectTenant:ne([Y("tenantId")],[X('"token" must be string or undefined')])(((e,t)=>J(r.post(c,{tenant:e},{token:t})))),logout:ht((e=>J(r.post(g,{},{token:e})))),logoutAll:ht((e=>J(r.post(h,{},{token:e})))),me:ht((e=>J(r.get(v,{token:e})))),history:ht((e=>J(r.get(f,{token:e})))),isJwtExpired:gt(M),getTenants:gt(N),getJwtPermissions:gt(C),getJwtRoles:gt(H),httpClient:r};var r}))),It=Object.assign(mt,{DeliveryMethods:ce});exports.HttpStatusCodes=E,exports.default=It,exports.normalizeWaitForSessionConfig=pe,exports.transformResponse=J,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"},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`,y=1e3,k=6e5,O="dct",j=()=>{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=>j().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 j().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var U;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT",e.patch="PATCH"}(U||(U={}));const P=({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},R=(...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}),{})),x={"Content-Type":"application/json"},q=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},$=e=>{try{e=JSON.parse(e)}catch(e){return!1}return"object"==typeof e&&null!==e},T=({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=(e=>void 0===e?void 0:JSON.stringify(e))(d),f={headers:R(q(t,h),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"2.24.0"},(null==n?void 0:n.baseHeaders)||{},$(v)?x:{},u),method:g,body:v};null!==i&&(f.credentials=i||"include");const m=await r(P({path:p,baseUrl:e,queryParams:c,projectId:t}),f);if((null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==m?void 0:m.clone()),null==o?void 0:o.transformResponse){const e=await m.json(),t=((null===(a=m.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({},m),{json:()=>Promise.resolve(e),cookies:t});return n.clone=()=>n,o.transformResponse(n)}return m};return{get:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:U.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.post,token:o}),patch:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.patch,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.put,token:o}),delete:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:U.delete,token:s}),hooks:o,buildUrl:(n,s)=>P({projectId:t,baseUrl:e,path:n,queryParams:s})}};var E={TOO_MANY_REQUESTS:429};function S(e,t,n){var s;let o=A(e);if(t){if(!(null==o?void 0:o.tenants)&&(null==o?void 0:o[O])===t)return(null==o?void 0:o[n])||[];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 A(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function M(e){const{exp:t}=A(e);return(new Date).getTime()/1e3>t}function N(e){let t=A(e);const n=Object.keys(null==t?void 0:t.tenants);return Array.isArray(n)?n:[]}function C(e,t){return S(e,t,"permissions")}function H(e,t){return S(e,t,"roles")}const z=(...e)=>e.join("/").replace(/\/{2,}/g,"/");async function J(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===E.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 _=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),L=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),D=e=>t=>e.test(t),F=D(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),Z=D(/^\+[1-9]{1}[0-9]{3,14}$/),B=(e,t)=>n=>L(...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)),K=_(F,'"{val}" is not a valid email'),Q=_(Z,'"{val}" is not a valid phone number'),V=_((W=1,e=>e.length>=W),"Minimum length is 1");var W;const Y=_((e=>"string"==typeof e),"Input is not a string"),G=_((e=>void 0===e),"Input is defined"),X=(ee=[Y(),G()],te="Input is not a string or undefined",(e=te)=>t=>{const n=ee.filter((e=>e(t)));return!(n.length<ee.length)&&(e?e.replace("{val}",t):n.join(" OR "))});var ee,te;const ne=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>L(...e).validate(n[t]))),t(...n)),se=e=>[Y(`"${e}" must be a string`)],oe=e=>[Y(`"${e}" must be a string`),V(`"${e}" must not be empty`)],ie=e=>[Y(`"${e}" must be a string`),K()],ae=e=>[Y(`"${e}" must be a string`),Q()],re=ne(oe("accessKey")),le=e=>({exchange:re(((n,s)=>J(e.post(t.exchange,{loginOptions:s},{token:n}))))}),pe=({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||y,y),timeoutMs:Math.min(t||k,k)});var de,ue;!function(e){e.sms="sms",e.voice="voice",e.whatsapp="whatsapp"}(de||(de={})),function(e){e.email="email"}(ue||(ue={}));const ce=Object.assign(Object.assign({},de),ue);var ge;!function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(ge||(ge={}));const he=oe("loginId"),ve=ne(oe("token")),fe=ne(he),me=ne(oe("pendingRef")),Ie=ne(he,ie("email")),be=e=>({verify:ve((t=>J(e.post(o.verify,{token:t})))),signIn:fe(((t,n,s,i)=>J(e.post(z(o.signIn,ce.email),{loginId:t,URI:n,loginOptions:s},{token:i})))),signUpOrIn:fe(((t,n,s)=>J(e.post(z(o.signUpOrIn,ce.email),{loginId:t,URI:n,loginOptions:s})))),signUp:fe(((t,n,s,i)=>J(e.post(z(o.signUp,ce.email),{loginId:t,URI:n,user:s,loginOptions:i})))),waitForSession:me(((t,n)=>new Promise((s=>{const{pollingIntervalMs:i,timeoutMs:a}=pe(n);let r;const l=setInterval((async()=>{const n=await e.post(o.session,{pendingRef:t});n.ok&&(clearInterval(l),r&&clearTimeout(r),s(J(Promise.resolve(n))))}),i);r=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${a}ms`,errorCode:"0"},ok:!1}),clearInterval(l)}),a)})))),update:{email:Ie(((t,n,s,i,a)=>J(e.post(o.update.email,Object.assign({loginId:t,email:n,URI:s},a),{token:i}))))}}),ye=ne(oe("flowId")),ke=ne(oe("executionId"),oe("stepId"),oe("interactionId")),Oe=e=>({start:ye(((t,n,s,o,i,a,r)=>J(e.post(m.start,{flowId:t,options:n,conditionInteractionId:s,interactionId:o,version:i,componentsVersion:a,input:r})))),next:ke(((t,n,s,o,i,a)=>J(e.post(m.next,{executionId:t,stepId:n,interactionId:s,version:o,componentsVersion:i,input:a}))))}),je=oe("loginId"),we=ne(oe("token")),Ue=ne(je),Pe=ne(je,ae("phone")),Re=ne(je,ie("email")),xe=Object.keys(ce).filter((e=>e!==de.voice)),qe=e=>({verify:we((t=>J(e.post(s.verify,{token:t})))),signIn:xe.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Ue(((t,o,i,a)=>J(e.post(z(s.signIn,n),{loginId:t,URI:o,loginOptions:i},{token:a}))))})),{}),signUp:xe.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Ue(((t,o,i,a)=>J(e.post(z(s.signUp,n),{loginId:t,URI:o,user:i,loginOptions:a}))))})),{}),signUpOrIn:xe.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Ue(((t,o,i)=>J(e.post(z(s.signUpOrIn,n),{loginId:t,URI:o,loginOptions:i}))))})),{}),update:{email:Re(((t,n,o,i,a)=>J(e.post(s.update.email,Object.assign({loginId:t,email:n,URI:o},a),{token:i})))),phone:Object.keys(de).filter((e=>e!==de.voice)).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Pe(((t,o,i,a,r)=>J(e.post(z(s.update.phone,n),Object.assign({loginId:t,phone:o,URI:i},r),{token:a}))))})),{})}});var $e;!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"}($e||($e={}));const Te=ne(oe("code")),Ee=e=>({start:Object.assign(((t,n,s,o)=>J(e.post(i.start,s||{},{queryParams:Object.assign({provider:t},n&&{redirectURL:n}),token:o}))),Object.keys($e).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:(t,s,o)=>J(e.post(i.start,s||{},{queryParams:Object.assign({provider:n},t&&{redirectURL:t}),token:o}))})),{})),exchange:Te((t=>J(e.post(i.exchange,{code:t})))),startNative:(t,n,s)=>J(e.post(i.startNative,{provider:t,loginOptions:n,implicit:s})),finishNative:(t,n,s,o,a)=>J(e.post(i.finishNative,{provider:t,stateId:n,user:s,code:o,idToken:a}))}),Se=oe("loginId"),Ae=ne(Se,oe("code")),Me=ne(Se),Ne=ne(Se,ae("phone")),Ce=ne(Se,ie("email")),He=e=>({verify:Object.keys(ce).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ae(((t,o)=>J(e.post(z(n.verify,s),{code:o,loginId:t}))))})),{}),signIn:Object.keys(ce).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Me(((t,o,i)=>J(e.post(z(n.signIn,s),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(ce).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Me(((t,o,i)=>J(e.post(z(n.signUp,s),{loginId:t,user:o,loginOptions:i}))))})),{}),signUpOrIn:Object.keys(ce).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Me(((t,o)=>J(e.post(z(n.signUpOrIn,s),{loginId:t,loginOptions:o}))))})),{}),update:{email:Ce(((t,s,o,i)=>J(e.post(n.update.email,Object.assign({loginId:t,email:s},i),{token:o})))),phone:Object.keys(de).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ne(((t,o,i,a)=>J(e.post(z(n.update.phone,s),Object.assign({loginId:t,phone:o},a),{token:i}))))})),{})}}),ze=ne(oe("tenant")),Je=ne(oe("code")),_e=e=>({start:ze(((t,n,s,o)=>J(e.post(a.start,s||{},{queryParams:{tenant:t,redirectURL:n},token:o})))),exchange:Je((t=>J(e.post(a.exchange,{code:t}))))}),Le=oe("loginId"),De=ne(Le,oe("code")),Fe=ne(Le),Ze=ne(Le),Be=e=>({signUp:Fe(((t,n)=>J(e.post(r.signUp,{loginId:t,user:n})))),verify:De(((t,n,s,o)=>J(e.post(r.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:Ze(((t,n)=>J(e.post(r.update,{loginId:t},{token:n}))))}),Ke=oe("loginId"),Qe=oe("newPassword"),Ve=ne(Ke,oe("password")),We=ne(Ke),Ye=ne(Ke,Qe),Ge=ne(Ke,oe("oldPassword"),Qe),Xe=e=>({signUp:Ve(((t,n,s,o)=>J(e.post(d.signUp,{loginId:t,password:n,user:s,loginOptions:o})))),signIn:Ve(((t,n,s)=>J(e.post(d.signIn,{loginId:t,password:n,loginOptions:s})))),sendReset:We(((t,n,s)=>J(e.post(d.sendReset,{loginId:t,redirectUrl:n,templateOptions:s})))),update:Ye(((t,n,s)=>J(e.post(d.update,{loginId:t,newPassword:n},{token:s})))),replace:Ge(((t,n,s)=>J(e.post(d.replace,{loginId:t,oldPassword:n,newPassword:s})))),policy:()=>J(e.get(d.policy))}),et=se("loginId"),tt=oe("loginId"),nt=oe("origin"),st=ne(tt,nt,oe("name")),ot=ne(tt,nt),it=ne(et,nt),at=ne(tt,nt,oe("token")),rt=ne(oe("transactionId"),oe("response")),lt=e=>({signUp:{start:st(((t,n,s)=>J(e.post(p.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:rt(((t,n)=>J(e.post(p.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:it(((t,n,s,o)=>J(e.post(p.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:rt(((t,n)=>J(e.post(p.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:ot(((t,n)=>J(e.post(p.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:at(((t,n,s)=>J(e.post(p.update.start,{loginId:t,origin:n},{token:s})))),finish:rt(((t,n)=>J(e.post(p.update.finish,{transactionId:t,response:n}))))}}),pt=se("loginId"),dt=ne(pt),ut=ne(oe("pendingRef")),ct=e=>({signUpOrIn:dt(((t,n)=>J(e.post(l.signUpOrIn,{loginId:t,loginOptions:n})))),signUp:dt(((t,n,s)=>J(e.post(l.signUp,{loginId:t,user:n,loginOptions:s})))),signIn:dt(((t,n,s)=>J(e.post(l.signIn,{loginId:t,loginOptions:n},{token:s})))),waitForSession:ut(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:i}=pe(n);let a;const r=setInterval((async()=>{const n=await e.post(l.session,{pendingRef:t});n.ok&&(clearInterval(r),a&&clearTimeout(a),s(J(Promise.resolve(n))))}),o);a=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${i}ms`,errorCode:"0"},ok:!1}),clearInterval(r)}),i)}))))}),gt=ne(oe("token")),ht=ne([X(`"${"token"}" must be string or undefined`)]);var vt,ft;var mt=ne([(vt="projectId",ft=oe("projectId"),_(B(vt,ft))())])((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)||[]);if(0==o.length)return;(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=T({baseUrl:n||b,projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:le(r),otp:He(r),magicLink:qe(r),enchantedLink:be(r),oauth:Ee(r),saml:_e(r),totp:Be(r),notp:ct(r),webauthn:lt(r),password:Xe(r),flow:Oe(r),refresh:ht((e=>J(r.post(u,{},{token:e})))),selectTenant:ne([Y("tenantId")],[X('"token" must be string or undefined')])(((e,t)=>J(r.post(c,{tenant:e},{token:t})))),logout:ht((e=>J(r.post(g,{},{token:e})))),logoutAll:ht((e=>J(r.post(h,{},{token:e})))),me:ht((e=>J(r.get(v,{token:e})))),history:ht((e=>J(r.get(f,{token:e})))),isJwtExpired:gt(M),getTenants:gt(N),getJwtPermissions:gt(C),getJwtRoles:gt(H),httpClient:r};var r}))),It=Object.assign(mt,{DeliveryMethods:ce});exports.HttpStatusCodes=E,exports.default=It,exports.normalizeWaitForSessionConfig=pe,exports.transformResponse=J,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 |
@@ -238,2 +238,3 @@ type JSONSerializable = string | number | boolean | null | Array<JSONSerializable>; | ||
oidcPrompt?: string; | ||
oidcErrorRedirectUri?: string; | ||
}; | ||
@@ -240,0 +241,0 @@ type ResponseData = Record<string, any>; |
@@ -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`,y=1e3,k=6e5,O="dct",j=()=>{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=>j().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 j().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var U;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT",e.patch="PATCH"}(U||(U={}));const P=({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},R=(...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}),{})),x={"Content-Type":"application/json"},$=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},q=e=>{try{e=JSON.parse(e)}catch(e){return!1}return"object"==typeof e&&null!==e},T=({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=(e=>void 0===e?void 0:JSON.stringify(e))(d),f={headers:R($(t,h),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"2.23.5"},(null==n?void 0:n.baseHeaders)||{},q(v)?x:{},u),method:g,body:v};null!==i&&(f.credentials=i||"include");const m=await r(P({path:p,baseUrl:e,queryParams:c,projectId:t}),f);if((null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==m?void 0:m.clone()),null==o?void 0:o.transformResponse){const e=await m.json(),t=((null===(a=m.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({},m),{json:()=>Promise.resolve(e),cookies:t});return n.clone=()=>n,o.transformResponse(n)}return m};return{get:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:U.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.post,token:o}),patch:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.patch,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.put,token:o}),delete:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:U.delete,token:s}),hooks:o,buildUrl:(n,s)=>P({projectId:t,baseUrl:e,path:n,queryParams:s})}};var E={TOO_MANY_REQUESTS:429};function A(e,t,n){var s;let o=S(e);if(t){if(!(null==o?void 0:o.tenants)&&(null==o?void 0:o[O])===t)return(null==o?void 0:o[n])||[];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 S(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function M(e){const{exp:t}=S(e);return(new Date).getTime()/1e3>t}function N(e){let t=S(e);const n=Object.keys(null==t?void 0:t.tenants);return Array.isArray(n)?n:[]}function C(e,t){return A(e,t,"permissions")}function H(e,t){return A(e,t,"roles")}const J=(...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===E.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 L=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),D=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),_=e=>t=>e.test(t),F=_(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),Z=_(/^\+[1-9]{1}[0-9]{3,14}$/),B=(e,t)=>n=>D(...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)),K=L(F,'"{val}" is not a valid email'),Q=L(Z,'"{val}" is not a valid phone number'),V=L((Y=1,e=>e.length>=Y),"Minimum length is 1");var Y;const G=L((e=>"string"==typeof e),"Input is not a string"),W=L((e=>void 0===e),"Input is defined"),X=(ee=[G(),W()],te="Input is not a string or undefined",(e=te)=>t=>{const n=ee.filter((e=>e(t)));return!(n.length<ee.length)&&(e?e.replace("{val}",t):n.join(" OR "))});var ee,te;const ne=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>D(...e).validate(n[t]))),t(...n)),se=e=>[G(`"${e}" must be a string`)],oe=e=>[G(`"${e}" must be a string`),V(`"${e}" must not be empty`)],ie=e=>[G(`"${e}" must be a string`),K()],ae=e=>[G(`"${e}" must be a string`),Q()],re=ne(oe("accessKey")),le=e=>({exchange:re(((n,s)=>z(e.post(t.exchange,{loginOptions:s},{token:n}))))}),pe=(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),de=({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||y,y),timeoutMs:Math.min(t||k,k)});var ue,ce;!function(e){e.sms="sms",e.voice="voice",e.whatsapp="whatsapp"}(ue||(ue={})),function(e){e.email="email"}(ce||(ce={}));const ge=Object.assign(Object.assign({},ue),ce);var he;!function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(he||(he={}));const ve=oe("loginId"),fe=ne(oe("token")),me=ne(ve),Ie=ne(oe("pendingRef")),be=ne(ve,ie("email")),ye=e=>({verify:fe((t=>z(e.post(o.verify,{token:t})))),signIn:me(((t,n,s,i)=>z(e.post(J(o.signIn,ge.email),{loginId:t,URI:n,loginOptions:s},{token:i})))),signUpOrIn:me(((t,n,s)=>z(e.post(J(o.signUpOrIn,ge.email),{loginId:t,URI:n,loginOptions:s})))),signUp:me(((t,n,s,i)=>z(e.post(J(o.signUp,ge.email),{loginId:t,URI:n,user:s,loginOptions:i})))),waitForSession:Ie(((t,n)=>new Promise((s=>{const{pollingIntervalMs:i,timeoutMs:a}=de(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:be(((t,n,s,i,a)=>z(e.post(o.update.email,Object.assign({loginId:t,email:n,URI:s},a),{token:i}))))}}),ke=ne(oe("flowId")),Oe=ne(oe("executionId"),oe("stepId"),oe("interactionId")),je=e=>({start:ke(((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:Oe(((t,n,s,o,i,a)=>z(e.post(m.next,{executionId:t,stepId:n,interactionId:s,version:o,componentsVersion:i,input:a}))))}),we=oe("loginId"),Ue=ne(oe("token")),Pe=ne(we),Re=ne(we,ae("phone")),xe=ne(we,ie("email")),$e=Object.keys(ge).filter((e=>e!==ue.voice)),qe=e=>({verify:Ue((t=>z(e.post(s.verify,{token:t})))),signIn:$e.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Pe(((t,o,i,a)=>z(e.post(J(s.signIn,n),{loginId:t,URI:o,loginOptions:i},{token:a}))))})),{}),signUp:$e.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Pe(((t,o,i,a)=>z(e.post(J(s.signUp,n),{loginId:t,URI:o,user:i,loginOptions:a}))))})),{}),signUpOrIn:$e.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Pe(((t,o,i)=>z(e.post(J(s.signUpOrIn,n),{loginId:t,URI:o,loginOptions:i}))))})),{}),update:{email:xe(((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(ue).filter((e=>e!==ue.voice)).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Re(((t,o,i,a,r)=>z(e.post(J(s.update.phone,n),Object.assign({loginId:t,phone:o,URI:i},r),{token:a}))))})),{})}});var Te;!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"}(Te||(Te={}));const Ee=ne(oe("code")),Ae=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(Te).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:Ee((t=>z(e.post(i.exchange,{code:t})))),startNative:(t,n,s)=>z(e.post(i.startNative,{provider:t,loginOptions:n,implicit:s})),finishNative:(t,n,s,o,a)=>z(e.post(i.finishNative,{provider:t,stateId:n,user:s,code:o,idToken:a}))}),Se=oe("loginId"),Me=ne(Se,oe("code")),Ne=ne(Se),Ce=ne(Se,ae("phone")),He=ne(Se,ie("email")),Je=e=>({verify:Object.keys(ge).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Me(((t,o)=>z(e.post(J(n.verify,s),{code:o,loginId:t}))))})),{}),signIn:Object.keys(ge).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ne(((t,o,i)=>z(e.post(J(n.signIn,s),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(ge).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ne(((t,o,i)=>z(e.post(J(n.signUp,s),{loginId:t,user:o,loginOptions:i}))))})),{}),signUpOrIn:Object.keys(ge).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ne(((t,o)=>z(e.post(J(n.signUpOrIn,s),{loginId:t,loginOptions:o}))))})),{}),update:{email:He(((t,s,o,i)=>z(e.post(n.update.email,Object.assign({loginId:t,email:s},i),{token:o})))),phone:Object.keys(ue).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ce(((t,o,i,a)=>z(e.post(J(n.update.phone,s),Object.assign({loginId:t,phone:o},a),{token:i}))))})),{})}}),ze=ne(oe("tenant")),Le=ne(oe("code")),De=e=>({start:ze(((t,n,s,o)=>z(e.post(a.start,s||{},{queryParams:{tenant:t,redirectURL:n},token:o})))),exchange:Le((t=>z(e.post(a.exchange,{code:t}))))}),_e=oe("loginId"),Fe=ne(_e,oe("code")),Ze=ne(_e),Be=ne(_e),Ke=e=>({signUp:Ze(((t,n)=>z(e.post(r.signUp,{loginId:t,user:n})))),verify:Fe(((t,n,s,o)=>z(e.post(r.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:Be(((t,n)=>z(e.post(r.update,{loginId:t},{token:n}))))}),Qe=oe("loginId"),Ve=oe("newPassword"),Ye=ne(Qe,oe("password")),Ge=ne(Qe),We=ne(Qe,Ve),Xe=ne(Qe,oe("oldPassword"),Ve),et=e=>({signUp:Ye(((t,n,s,o)=>z(e.post(d.signUp,{loginId:t,password:n,user:s,loginOptions:o})))),signIn:Ye(((t,n,s)=>z(e.post(d.signIn,{loginId:t,password:n,loginOptions:s})))),sendReset:Ge(((t,n,s)=>z(e.post(d.sendReset,{loginId:t,redirectUrl:n,templateOptions:s})))),update:We(((t,n,s)=>z(e.post(d.update,{loginId:t,newPassword:n},{token:s})))),replace:Xe(((t,n,s)=>z(e.post(d.replace,{loginId:t,oldPassword:n,newPassword:s})))),policy:()=>z(e.get(d.policy))}),tt=se("loginId"),nt=oe("loginId"),st=oe("origin"),ot=ne(nt,st,oe("name")),it=ne(nt,st),at=ne(tt,st),rt=ne(nt,st,oe("token")),lt=ne(oe("transactionId"),oe("response")),pt=e=>({signUp:{start:ot(((t,n,s)=>z(e.post(p.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:lt(((t,n)=>z(e.post(p.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:at(((t,n,s,o)=>z(e.post(p.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:lt(((t,n)=>z(e.post(p.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:it(((t,n)=>z(e.post(p.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:rt(((t,n,s)=>z(e.post(p.update.start,{loginId:t,origin:n},{token:s})))),finish:lt(((t,n)=>z(e.post(p.update.finish,{transactionId:t,response:n}))))}}),dt=se("loginId"),ut=ne(dt),ct=ne(oe("pendingRef")),gt=e=>({signUpOrIn:ut(((t,n)=>z(e.post(l.signUpOrIn,{loginId:t,loginOptions:n})))),signUp:ut(((t,n,s)=>z(e.post(l.signUp,{loginId:t,user:n,loginOptions:s})))),signIn:ut(((t,n,s)=>z(e.post(l.signIn,{loginId:t,loginOptions:n},{token:s})))),waitForSession:ct(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:i}=de(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)}))))}),ht=ne(oe("token")),vt=ne([X(`"${"token"}" must be string or undefined`)]);var ft,mt;var It=ne([(ft="projectId",mt=oe("projectId"),L(B(ft,mt))())])((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)||[]);if(0==o.length)return;(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=T({baseUrl:n||b,projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:le(r),otp:Je(r),magicLink:qe(r),enchantedLink:ye(r),oauth:Ae(r),saml:De(r),totp:Ke(r),notp:gt(r),webauthn:pt(r),password:et(r),flow:je(r),refresh:vt((e=>z(r.post(u,{},{token:e})))),selectTenant:ne([G("tenantId")],[X('"token" must be string or undefined')])(((e,t)=>z(r.post(c,{tenant:e},{token:t})))),logout:vt((e=>z(r.post(g,{},{token:e})))),logoutAll:vt((e=>z(r.post(h,{},{token:e})))),me:vt((e=>z(r.get(v,{token:e})))),history:vt((e=>z(r.get(f,{token:e})))),isJwtExpired:ht(M),getTenants:ht(N),getJwtPermissions:ht(C),getJwtRoles:ht(H),httpClient:r};var r}))),bt=Object.assign(It,{DeliveryMethods:ge});export{E as HttpStatusCodes,bt as default,de as normalizeWaitForSessionConfig,z as transformResponse,pe 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`,y=1e3,k=6e5,O="dct",j=()=>{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=>j().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 j().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var U;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT",e.patch="PATCH"}(U||(U={}));const P=({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},R=(...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}),{})),x={"Content-Type":"application/json"},$=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},q=e=>{try{e=JSON.parse(e)}catch(e){return!1}return"object"==typeof e&&null!==e},T=({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=(e=>void 0===e?void 0:JSON.stringify(e))(d),f={headers:R($(t,h),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"2.24.0"},(null==n?void 0:n.baseHeaders)||{},q(v)?x:{},u),method:g,body:v};null!==i&&(f.credentials=i||"include");const m=await r(P({path:p,baseUrl:e,queryParams:c,projectId:t}),f);if((null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==m?void 0:m.clone()),null==o?void 0:o.transformResponse){const e=await m.json(),t=((null===(a=m.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({},m),{json:()=>Promise.resolve(e),cookies:t});return n.clone=()=>n,o.transformResponse(n)}return m};return{get:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:U.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.post,token:o}),patch:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.patch,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>l({path:e,headers:n,queryParams:s,body:t,method:U.put,token:o}),delete:(e,{headers:t,queryParams:n,token:s}={})=>l({path:e,headers:t,queryParams:n,body:void 0,method:U.delete,token:s}),hooks:o,buildUrl:(n,s)=>P({projectId:t,baseUrl:e,path:n,queryParams:s})}};var E={TOO_MANY_REQUESTS:429};function A(e,t,n){var s;let o=S(e);if(t){if(!(null==o?void 0:o.tenants)&&(null==o?void 0:o[O])===t)return(null==o?void 0:o[n])||[];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 S(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function M(e){const{exp:t}=S(e);return(new Date).getTime()/1e3>t}function N(e){let t=S(e);const n=Object.keys(null==t?void 0:t.tenants);return Array.isArray(n)?n:[]}function C(e,t){return A(e,t,"permissions")}function H(e,t){return A(e,t,"roles")}const J=(...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===E.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 L=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),D=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),_=e=>t=>e.test(t),F=_(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),Z=_(/^\+[1-9]{1}[0-9]{3,14}$/),B=(e,t)=>n=>D(...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)),K=L(F,'"{val}" is not a valid email'),Q=L(Z,'"{val}" is not a valid phone number'),V=L((Y=1,e=>e.length>=Y),"Minimum length is 1");var Y;const G=L((e=>"string"==typeof e),"Input is not a string"),W=L((e=>void 0===e),"Input is defined"),X=(ee=[G(),W()],te="Input is not a string or undefined",(e=te)=>t=>{const n=ee.filter((e=>e(t)));return!(n.length<ee.length)&&(e?e.replace("{val}",t):n.join(" OR "))});var ee,te;const ne=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>D(...e).validate(n[t]))),t(...n)),se=e=>[G(`"${e}" must be a string`)],oe=e=>[G(`"${e}" must be a string`),V(`"${e}" must not be empty`)],ie=e=>[G(`"${e}" must be a string`),K()],ae=e=>[G(`"${e}" must be a string`),Q()],re=ne(oe("accessKey")),le=e=>({exchange:re(((n,s)=>z(e.post(t.exchange,{loginOptions:s},{token:n}))))}),pe=(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),de=({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||y,y),timeoutMs:Math.min(t||k,k)});var ue,ce;!function(e){e.sms="sms",e.voice="voice",e.whatsapp="whatsapp"}(ue||(ue={})),function(e){e.email="email"}(ce||(ce={}));const ge=Object.assign(Object.assign({},ue),ce);var he;!function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(he||(he={}));const ve=oe("loginId"),fe=ne(oe("token")),me=ne(ve),Ie=ne(oe("pendingRef")),be=ne(ve,ie("email")),ye=e=>({verify:fe((t=>z(e.post(o.verify,{token:t})))),signIn:me(((t,n,s,i)=>z(e.post(J(o.signIn,ge.email),{loginId:t,URI:n,loginOptions:s},{token:i})))),signUpOrIn:me(((t,n,s)=>z(e.post(J(o.signUpOrIn,ge.email),{loginId:t,URI:n,loginOptions:s})))),signUp:me(((t,n,s,i)=>z(e.post(J(o.signUp,ge.email),{loginId:t,URI:n,user:s,loginOptions:i})))),waitForSession:Ie(((t,n)=>new Promise((s=>{const{pollingIntervalMs:i,timeoutMs:a}=de(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:be(((t,n,s,i,a)=>z(e.post(o.update.email,Object.assign({loginId:t,email:n,URI:s},a),{token:i}))))}}),ke=ne(oe("flowId")),Oe=ne(oe("executionId"),oe("stepId"),oe("interactionId")),je=e=>({start:ke(((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:Oe(((t,n,s,o,i,a)=>z(e.post(m.next,{executionId:t,stepId:n,interactionId:s,version:o,componentsVersion:i,input:a}))))}),we=oe("loginId"),Ue=ne(oe("token")),Pe=ne(we),Re=ne(we,ae("phone")),xe=ne(we,ie("email")),$e=Object.keys(ge).filter((e=>e!==ue.voice)),qe=e=>({verify:Ue((t=>z(e.post(s.verify,{token:t})))),signIn:$e.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Pe(((t,o,i,a)=>z(e.post(J(s.signIn,n),{loginId:t,URI:o,loginOptions:i},{token:a}))))})),{}),signUp:$e.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Pe(((t,o,i,a)=>z(e.post(J(s.signUp,n),{loginId:t,URI:o,user:i,loginOptions:a}))))})),{}),signUpOrIn:$e.reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Pe(((t,o,i)=>z(e.post(J(s.signUpOrIn,n),{loginId:t,URI:o,loginOptions:i}))))})),{}),update:{email:xe(((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(ue).filter((e=>e!==ue.voice)).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Re(((t,o,i,a,r)=>z(e.post(J(s.update.phone,n),Object.assign({loginId:t,phone:o,URI:i},r),{token:a}))))})),{})}});var Te;!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"}(Te||(Te={}));const Ee=ne(oe("code")),Ae=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(Te).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:Ee((t=>z(e.post(i.exchange,{code:t})))),startNative:(t,n,s)=>z(e.post(i.startNative,{provider:t,loginOptions:n,implicit:s})),finishNative:(t,n,s,o,a)=>z(e.post(i.finishNative,{provider:t,stateId:n,user:s,code:o,idToken:a}))}),Se=oe("loginId"),Me=ne(Se,oe("code")),Ne=ne(Se),Ce=ne(Se,ae("phone")),He=ne(Se,ie("email")),Je=e=>({verify:Object.keys(ge).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Me(((t,o)=>z(e.post(J(n.verify,s),{code:o,loginId:t}))))})),{}),signIn:Object.keys(ge).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ne(((t,o,i)=>z(e.post(J(n.signIn,s),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(ge).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ne(((t,o,i)=>z(e.post(J(n.signUp,s),{loginId:t,user:o,loginOptions:i}))))})),{}),signUpOrIn:Object.keys(ge).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ne(((t,o)=>z(e.post(J(n.signUpOrIn,s),{loginId:t,loginOptions:o}))))})),{}),update:{email:He(((t,s,o,i)=>z(e.post(n.update.email,Object.assign({loginId:t,email:s},i),{token:o})))),phone:Object.keys(ue).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Ce(((t,o,i,a)=>z(e.post(J(n.update.phone,s),Object.assign({loginId:t,phone:o},a),{token:i}))))})),{})}}),ze=ne(oe("tenant")),Le=ne(oe("code")),De=e=>({start:ze(((t,n,s,o)=>z(e.post(a.start,s||{},{queryParams:{tenant:t,redirectURL:n},token:o})))),exchange:Le((t=>z(e.post(a.exchange,{code:t}))))}),_e=oe("loginId"),Fe=ne(_e,oe("code")),Ze=ne(_e),Be=ne(_e),Ke=e=>({signUp:Ze(((t,n)=>z(e.post(r.signUp,{loginId:t,user:n})))),verify:Fe(((t,n,s,o)=>z(e.post(r.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:Be(((t,n)=>z(e.post(r.update,{loginId:t},{token:n}))))}),Qe=oe("loginId"),Ve=oe("newPassword"),Ye=ne(Qe,oe("password")),Ge=ne(Qe),We=ne(Qe,Ve),Xe=ne(Qe,oe("oldPassword"),Ve),et=e=>({signUp:Ye(((t,n,s,o)=>z(e.post(d.signUp,{loginId:t,password:n,user:s,loginOptions:o})))),signIn:Ye(((t,n,s)=>z(e.post(d.signIn,{loginId:t,password:n,loginOptions:s})))),sendReset:Ge(((t,n,s)=>z(e.post(d.sendReset,{loginId:t,redirectUrl:n,templateOptions:s})))),update:We(((t,n,s)=>z(e.post(d.update,{loginId:t,newPassword:n},{token:s})))),replace:Xe(((t,n,s)=>z(e.post(d.replace,{loginId:t,oldPassword:n,newPassword:s})))),policy:()=>z(e.get(d.policy))}),tt=se("loginId"),nt=oe("loginId"),st=oe("origin"),ot=ne(nt,st,oe("name")),it=ne(nt,st),at=ne(tt,st),rt=ne(nt,st,oe("token")),lt=ne(oe("transactionId"),oe("response")),pt=e=>({signUp:{start:ot(((t,n,s)=>z(e.post(p.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:lt(((t,n)=>z(e.post(p.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:at(((t,n,s,o)=>z(e.post(p.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:lt(((t,n)=>z(e.post(p.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:it(((t,n)=>z(e.post(p.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:rt(((t,n,s)=>z(e.post(p.update.start,{loginId:t,origin:n},{token:s})))),finish:lt(((t,n)=>z(e.post(p.update.finish,{transactionId:t,response:n}))))}}),dt=se("loginId"),ut=ne(dt),ct=ne(oe("pendingRef")),gt=e=>({signUpOrIn:ut(((t,n)=>z(e.post(l.signUpOrIn,{loginId:t,loginOptions:n})))),signUp:ut(((t,n,s)=>z(e.post(l.signUp,{loginId:t,user:n,loginOptions:s})))),signIn:ut(((t,n,s)=>z(e.post(l.signIn,{loginId:t,loginOptions:n},{token:s})))),waitForSession:ct(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:i}=de(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)}))))}),ht=ne(oe("token")),vt=ne([X(`"${"token"}" must be string or undefined`)]);var ft,mt;var It=ne([(ft="projectId",mt=oe("projectId"),L(B(ft,mt))())])((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)||[]);if(0==o.length)return;(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=T({baseUrl:n||b,projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:le(r),otp:Je(r),magicLink:qe(r),enchantedLink:ye(r),oauth:Ae(r),saml:De(r),totp:Ke(r),notp:gt(r),webauthn:pt(r),password:et(r),flow:je(r),refresh:vt((e=>z(r.post(u,{},{token:e})))),selectTenant:ne([G("tenantId")],[X('"token" must be string or undefined')])(((e,t)=>z(r.post(c,{tenant:e},{token:t})))),logout:vt((e=>z(r.post(g,{},{token:e})))),logoutAll:vt((e=>z(r.post(h,{},{token:e})))),me:vt((e=>z(r.get(v,{token:e})))),history:vt((e=>z(r.get(f,{token:e})))),isJwtExpired:ht(M),getTenants:ht(N),getJwtPermissions:ht(C),getJwtRoles:ht(H),httpClient:r};var r}))),bt=Object.assign(It,{DeliveryMethods:ge});export{E as HttpStatusCodes,bt as default,de as normalizeWaitForSessionConfig,z as transformResponse,pe as wrapWith}; | ||
//# sourceMappingURL=index.esm.js.map |
{ | ||
"name": "@descope/core-js-sdk", | ||
"version": "2.23.5", | ||
"version": "2.24.0", | ||
"author": "Descope Team <info@descope.com>", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/descope/descope-js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
268088
715