@descope/core-js-sdk
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("jwt-decode"),t=require("lodash.get"),n={exchange:"/v1/auth/accesskey/exchange"},s={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"},o={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"},i={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"},a={start:"/v1/auth/oauth/authorize",exchange:"/v1/auth/oauth/exchange"},r={start:"/v1/auth/saml/authorize",exchange:"/v1/auth/saml/exchange"},u={verify:"/v1/auth/totp/verify",signUp:"/v1/auth/totp/signup",update:"/v1/auth/totp/update"},d={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"}},l="/v1/auth/refresh",c="/v1/auth/logout",p="/v1/auth/logoutall",g="/v1/auth/me",h={start:"/v1/flow/start",next:"/v1/flow/next"};const v=()=>{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")}},f=(e,t)=>{const 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=>v().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.clone?e.clone().text():e.text());return e.text=()=>Promise.resolve(t),e.json=()=>Promise.resolve(JSON.parse(t)),v().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var m;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT"}(m||(m={}));const I=(...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}),{})),b=e=>void 0===e?void 0:JSON.stringify(e),k=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},y=({baseUrl:e,projectId:t,baseConfig:n,logger:s,hooks:o,cookiePolicy:i,fetch:a})=>{const r=f(s,a),u=async s=>{const a=(null==o?void 0:o.beforeRequest)?o.beforeRequest(s):s,{path:u,body:d,headers:l,queryParams:c,method:p,token:g}=a,h=await r((({path:e,baseUrl:t,queryParams:n})=>{const s=new URL(e,t);return n&&(s.search=new URLSearchParams(n).toString()),s})({path:u,baseUrl:e,queryParams:c}),{headers:I(k(t,g),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"1.0.1"},(null==n?void 0:n.baseHeaders)||{},l),method:p,body:b(d),credentials:i||"include"});return(null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==h?void 0:h.clone()),h};return{get:(e,{headers:t,queryParams:n,token:s}={})=>u({path:e,headers:t,queryParams:n,body:void 0,method:m.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.post,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.put,token:o}),delete:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.delete,token:o}),hooks:o}};var O={TOO_MANY_REQUESTS:429};function w(e,t,n){var s;let o=j(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 j(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function U(e){const{exp:t}=j(e);return(new Date).getTime()/1e3>t}function x(e,t){return w(e,t,"permissions")}function R(e,t){return w(e,t,"roles")}const P=(...e)=>e.join("/").replace(/\/{2,}/g,"/");async function q(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===O.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=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),$=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),S=e=>t=>e.test(t),M=S(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),T=S(/^\+[1-9]{1}[0-9]{3,14}$/),A=E(M,'"{val}" is not a valid email'),H=E(T,'"{val}" is not a valid phone number'),C=E((L=1,e=>e.length>=L),"Minimum length is 1");var L;const _=E((e=>"string"==typeof e),"Input is not a string"),z=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>$(...e).validate(n[t]))),t(...n)),J=e=>[_(`"${e}" must be a string`),C(`"${e}" must not be empty`)],N=e=>[_(`"${e}" must be a string`),A()],D=e=>[_(`"${e}" must be a string`),H()],Z=z(J("accessKey")),B=e=>({exchange:Z((t=>q(e.post(n.exchange,{},{token:t}))))});var F,K,Q,Y;!function(e){e.sms="sms",e.whatsapp="whatsapp"}(F||(F={})),function(e){e.email="email",e.sms="sms",e.whatsapp="whatsapp"}(K||(K={})),function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(Q||(Q={})),function(e){e.signUp="signup",e.signIn="signin",e.verify="verify"}(Y||(Y={}));const G=J("loginId"),W=z(J("token")),V=z(G),X=z(J("pendingRef")),ee=z(G,N("email")),te=e=>({verify:W((t=>q(e.post(i.verify,{token:t})))),signIn:V(((t,n,s,o)=>q(e.post(P(i.signIn,K.email),{loginId:t,URI:n,loginOptions:s},{token:o})))),signUpOrIn:V(((t,n)=>q(e.post(P(i.signUpOrIn,K.email),{loginId:t,URI:n})))),signUp:V(((t,n,s)=>q(e.post(P(i.signUp,K.email),{loginId:t,URI:n,user:s})))),waitForSession:X(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:a}=(({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||1e3,1e3),timeoutMs:Math.min(t||6e5,6e5)}))(n);let r;const u=setInterval((async()=>{const n=await e.post(i.session,{pendingRef:t});n.ok&&(clearInterval(u),r&&clearTimeout(r),s(q(Promise.resolve(n))))}),o);r=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${a}ms`,errorCode:"0"},ok:!1}),clearInterval(u)}),a)})))),update:{email:ee(((t,n,s,o)=>q(e.post(i.update.email,{loginId:t,email:n,URI:s},{token:o}))))}}),ne=z(J("flowId")),se=z(J("executionId"),J("stepId"),J("interactionId")),oe=e=>({start:ne(((t,n,s,o,i)=>q(e.post(h.start,{flowId:t,options:n,conditionInteractionId:s,interactionId:o,input:i})))),next:se(((t,n,s,o)=>q(e.post(h.next,{executionId:t,stepId:n,interactionId:s,input:o}))))}),ie=J("loginId"),ae=z(J("token")),re=z(ie),ue=z(ie,D("phone")),de=z(ie,N("email")),le=e=>({verify:ae((t=>q(e.post(o.verify,{token:t})))),signIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s,i,a)=>q(e.post(P(o.signIn,n),{loginId:t,URI:s,loginOptions:i},{token:a}))))})),{}),signUp:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s,i)=>q(e.post(P(o.signUp,n),{loginId:t,URI:s,user:i}))))})),{}),signUpOrIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s)=>q(e.post(P(o.signUpOrIn,n),{loginId:t,URI:s}))))})),{}),update:{email:de(((t,n,s,i)=>q(e.post(o.update.email,{loginId:t,email:n,URI:s},{token:i})))),phone:Object.keys(F).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:ue(((t,s,i,a)=>q(e.post(P(o.update.phone,n),{loginId:t,phone:s,URI:i},{token:a}))))})),{})}});var ce;!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"}(ce||(ce={}));const pe=z(J("code")),ge=e=>({start:Object.keys(ce).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:async(t,{redirect:s=!1}={},o,i)=>{const r=await e.post(a.start,o||{},{queryParams:Object.assign({provider:n},t&&{redirectURL:t}),token:i});if(!s||!r.ok)return q(Promise.resolve(r));const{url:u}=await r.json();window.location.href=u}})),{}),exchange:pe((t=>q(e.post(a.exchange,{code:t}))))});var he;!function(e){e.signUp="signup",e.signIn="signin",e.verify="verify",e.updatePhone="updatePhone"}(he||(he={}));const ve=J("loginId"),fe=z(ve,J("code")),me=z(ve),Ie=z(ve,D("phone")),be=z(ve,N("email")),ke=e=>({verify:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:fe(((t,o)=>q(e.post(P(s.verify,n),{code:o,loginId:t}))))})),{}),signIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me(((t,o,i)=>q(e.post(P(s.signIn,n),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me(((t,o)=>q(e.post(P(s.signUp,n),{loginId:t,user:o}))))})),{}),signUpOrIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me((t=>q(e.post(P(s.signUpOrIn,n),{loginId:t}))))})),{}),update:{email:be(((t,n,o)=>q(e.post(s.update.email,{loginId:t,email:n},{token:o})))),phone:Object.keys(F).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Ie(((t,o,i)=>q(e.post(P(s.update.phone,n),{loginId:t,phone:o},{token:i}))))})),{})}}),ye=z(J("tenant")),Oe=z(J("code")),we=e=>({start:ye((async(t,n,{redirect:s=!1}={},o,i)=>{const a=await e.post(r.start,o||{},{queryParams:{tenant:t,redirectURL:n},token:i});if(!s||!a.ok)return q(Promise.resolve(a));const{url:u}=await a.json();window.location.href=u})),exchange:Oe((t=>q(e.post(r.exchange,{code:t}))))}),je=J("loginId"),Ue=z(je,J("code")),xe=z(je),Re=z(je),Pe=e=>({signUp:xe(((t,n)=>q(e.post(u.signUp,{loginId:t,user:n})))),verify:Ue(((t,n,s,o)=>q(e.post(u.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:Re(((t,n)=>q(e.post(u.update,{loginId:t},{token:n}))))}),qe=[_(`"${"loginId"}" must be a string`)];const Ee=J("loginId"),$e=J("origin"),Se=z(Ee,$e,J("name")),Me=z(Ee,$e),Te=z(qe,$e),Ae=z(Ee,$e,J("token")),He=z(J("transactionId"),J("response")),Ce=e=>({signUp:{start:Se(((t,n,s)=>q(e.post(d.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:He(((t,n)=>q(e.post(d.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:Te(((t,n,s,o)=>q(e.post(d.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:He(((t,n)=>q(e.post(d.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:Me(((t,n)=>q(e.post(d.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:Ae(((t,n,s)=>q(e.post(d.update.start,{loginId:t,origin:n},{token:s})))),finish:He(((t,n)=>q(e.post(d.update.finish,{transactionId:t,response:n}))))}}),Le=z(J("token"));var _e,ze;var Je=z([(_e="projectId",ze=J("projectId"),E(((e,n)=>s=>$(...n).validate(t(s,e)))(_e,ze))())])((e=>t=>{var n,s;const o=[].concat((null===(n=t.hooks)||void 0===n?void 0:n.beforeRequest)||[]),i=[].concat((null===(s=t.hooks)||void 0===s?void 0:s.afterRequest)||[]);return e(Object.assign(Object.assign({},t),{hooks:{beforeRequest:e=>null==o?void 0:o.reduce(((e,t)=>t(e)),e),afterRequest:async(e,n)=>{(await Promise.allSettled(null==i?void 0:i.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))}))}}}))})((({projectId:e,logger:t,baseUrl:n,hooks:s,cookiePolicy:o,baseHeaders:i={},fetch:a})=>{return r=y({baseUrl:n||"https://api.descope.com",projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:B(r),otp:ke(r),magicLink:le(r),enchantedLink:te(r),oauth:ge(r),saml:we(r),totp:Pe(r),webauthn:Ce(r),flow:oe(r),refresh:e=>q(r.post(l,{},{token:e})),logout:e=>q(r.post(c,{},{token:e})),logoutAll:e=>q(r.post(p,{},{token:e})),me:e=>q(r.get(g,{token:e})),isJwtExpired:Le(U),getJwtPermissions:Le(x),getJwtRoles:Le(R),httpClient:r};var r})));var Ne=Object.assign(Je,{DeliveryMethods:K});exports.HttpStatusCodes=O,exports.addHooksToConfig=(e,t)=>{var n;return["beforeRequest","afterRequest"].reduce(((n,s)=>{var o;return n[s]=[].concat((null===(o=e.hooks)||void 0===o?void 0:o[s])||[]).concat((null==t?void 0:t[s])||[]),n}),null!==(n=e.hooks)&&void 0!==n?n:e.hooks={}),e},exports.default=Ne,exports.transformResponse=q,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=require("lodash.get"),n={exchange:"/v1/auth/accesskey/exchange"},s={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"},o={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"},i={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"},a={start:"/v1/auth/oauth/authorize",exchange:"/v1/auth/oauth/exchange"},r={start:"/v1/auth/saml/authorize",exchange:"/v1/auth/saml/exchange"},u={verify:"/v1/auth/totp/verify",signUp:"/v1/auth/totp/signup",update:"/v1/auth/totp/update"},d={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"}},l="/v1/auth/refresh",c="/v1/auth/logout",p="/v1/auth/logoutall",g="/v1/auth/me",h={start:"/v1/flow/start",next:"/v1/flow/next"};const v=()=>{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")}},f=(e,t)=>{const 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=>v().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.clone?e.clone().text():e.text());return e.text=()=>Promise.resolve(t),e.json=()=>Promise.resolve(JSON.parse(t)),v().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var m;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT"}(m||(m={}));const I=(...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}),{})),b=e=>void 0===e?void 0:JSON.stringify(e),k=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},y=({baseUrl:e,projectId:t,baseConfig:n,logger:s,hooks:o,cookiePolicy:i,fetch:a})=>{const r=f(s,a),u=async s=>{const a=(null==o?void 0:o.beforeRequest)?o.beforeRequest(s):s,{path:u,body:d,headers:l,queryParams:c,method:p,token:g}=a,h=await r((({path:e,baseUrl:t,queryParams:n})=>{const s=new URL(e,t);return n&&(s.search=new URLSearchParams(n).toString()),s})({path:u,baseUrl:e,queryParams:c}),{headers:I(k(t,g),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"1.0.2"},(null==n?void 0:n.baseHeaders)||{},l),method:p,body:b(d),credentials:i||"include"});return(null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==h?void 0:h.clone()),h};return{get:(e,{headers:t,queryParams:n,token:s}={})=>u({path:e,headers:t,queryParams:n,body:void 0,method:m.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.post,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.put,token:o}),delete:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.delete,token:o}),hooks:o}};var O={TOO_MANY_REQUESTS:429};function w(e,t,n){var s;let o=j(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 j(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function U(e){const{exp:t}=j(e);return(new Date).getTime()/1e3>t}function x(e,t){return w(e,t,"permissions")}function R(e,t){return w(e,t,"roles")}const P=(...e)=>e.join("/").replace(/\/{2,}/g,"/");async function q(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===O.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=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),$=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),S=e=>t=>e.test(t),M=S(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),T=S(/^\+[1-9]{1}[0-9]{3,14}$/),A=E(M,'"{val}" is not a valid email'),H=E(T,'"{val}" is not a valid phone number'),C=E((L=1,e=>e.length>=L),"Minimum length is 1");var L;const _=E((e=>"string"==typeof e),"Input is not a string"),z=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>$(...e).validate(n[t]))),t(...n)),J=e=>[_(`"${e}" must be a string`),C(`"${e}" must not be empty`)],N=e=>[_(`"${e}" must be a string`),A()],D=e=>[_(`"${e}" must be a string`),H()],Z=z(J("accessKey")),B=e=>({exchange:Z((t=>q(e.post(n.exchange,{},{token:t}))))});var F,K,Q,Y;!function(e){e.sms="sms",e.whatsapp="whatsapp"}(F||(F={})),function(e){e.email="email",e.sms="sms",e.whatsapp="whatsapp"}(K||(K={})),function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(Q||(Q={})),function(e){e.signUp="signup",e.signIn="signin",e.verify="verify"}(Y||(Y={}));const G=J("loginId"),W=z(J("token")),V=z(G),X=z(J("pendingRef")),ee=z(G,N("email")),te=e=>({verify:W((t=>q(e.post(i.verify,{token:t})))),signIn:V(((t,n,s,o)=>q(e.post(P(i.signIn,K.email),{loginId:t,URI:n,loginOptions:s},{token:o})))),signUpOrIn:V(((t,n)=>q(e.post(P(i.signUpOrIn,K.email),{loginId:t,URI:n})))),signUp:V(((t,n,s)=>q(e.post(P(i.signUp,K.email),{loginId:t,URI:n,user:s})))),waitForSession:X(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:a}=(({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||1e3,1e3),timeoutMs:Math.min(t||6e5,6e5)}))(n);let r;const u=setInterval((async()=>{const n=await e.post(i.session,{pendingRef:t});n.ok&&(clearInterval(u),r&&clearTimeout(r),s(q(Promise.resolve(n))))}),o);r=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${a}ms`,errorCode:"0"},ok:!1}),clearInterval(u)}),a)})))),update:{email:ee(((t,n,s,o)=>q(e.post(i.update.email,{loginId:t,email:n,URI:s},{token:o}))))}}),ne=z(J("flowId")),se=z(J("executionId"),J("stepId"),J("interactionId")),oe=e=>({start:ne(((t,n,s,o,i)=>q(e.post(h.start,{flowId:t,options:n,conditionInteractionId:s,interactionId:o,input:i})))),next:se(((t,n,s,o)=>q(e.post(h.next,{executionId:t,stepId:n,interactionId:s,input:o}))))}),ie=J("loginId"),ae=z(J("token")),re=z(ie),ue=z(ie,D("phone")),de=z(ie,N("email")),le=e=>({verify:ae((t=>q(e.post(o.verify,{token:t})))),signIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s,i,a)=>q(e.post(P(o.signIn,n),{loginId:t,URI:s,loginOptions:i},{token:a}))))})),{}),signUp:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s,i)=>q(e.post(P(o.signUp,n),{loginId:t,URI:s,user:i}))))})),{}),signUpOrIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s)=>q(e.post(P(o.signUpOrIn,n),{loginId:t,URI:s}))))})),{}),update:{email:de(((t,n,s,i)=>q(e.post(o.update.email,{loginId:t,email:n,URI:s},{token:i})))),phone:Object.keys(F).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:ue(((t,s,i,a)=>q(e.post(P(o.update.phone,n),{loginId:t,phone:s,URI:i},{token:a}))))})),{})}});var ce;!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"}(ce||(ce={}));const pe=z(J("code")),ge=e=>({start:Object.keys(ce).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:async(t,{redirect:s=!1}={},o,i)=>{const r=await e.post(a.start,o||{},{queryParams:Object.assign({provider:n},t&&{redirectURL:t}),token:i});if(!s||!r.ok)return q(Promise.resolve(r));const{url:u}=await r.json();window.location.href=u}})),{}),exchange:pe((t=>q(e.post(a.exchange,{code:t}))))});var he;!function(e){e.signUp="signup",e.signIn="signin",e.verify="verify",e.updatePhone="updatePhone"}(he||(he={}));const ve=J("loginId"),fe=z(ve,J("code")),me=z(ve),Ie=z(ve,D("phone")),be=z(ve,N("email")),ke=e=>({verify:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:fe(((t,o)=>q(e.post(P(s.verify,n),{code:o,loginId:t}))))})),{}),signIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me(((t,o,i)=>q(e.post(P(s.signIn,n),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me(((t,o)=>q(e.post(P(s.signUp,n),{loginId:t,user:o}))))})),{}),signUpOrIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me((t=>q(e.post(P(s.signUpOrIn,n),{loginId:t}))))})),{}),update:{email:be(((t,n,o)=>q(e.post(s.update.email,{loginId:t,email:n},{token:o})))),phone:Object.keys(F).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Ie(((t,o,i)=>q(e.post(P(s.update.phone,n),{loginId:t,phone:o},{token:i}))))})),{})}}),ye=z(J("tenant")),Oe=z(J("code")),we=e=>({start:ye((async(t,n,{redirect:s=!1}={},o,i)=>{const a=await e.post(r.start,o||{},{queryParams:{tenant:t,redirectURL:n},token:i});if(!s||!a.ok)return q(Promise.resolve(a));const{url:u}=await a.json();window.location.href=u})),exchange:Oe((t=>q(e.post(r.exchange,{code:t}))))}),je=J("loginId"),Ue=z(je,J("code")),xe=z(je),Re=z(je),Pe=e=>({signUp:xe(((t,n)=>q(e.post(u.signUp,{loginId:t,user:n})))),verify:Ue(((t,n,s,o)=>q(e.post(u.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:Re(((t,n)=>q(e.post(u.update,{loginId:t},{token:n}))))}),qe=[_(`"${"loginId"}" must be a string`)];const Ee=J("loginId"),$e=J("origin"),Se=z(Ee,$e,J("name")),Me=z(Ee,$e),Te=z(qe,$e),Ae=z(Ee,$e,J("token")),He=z(J("transactionId"),J("response")),Ce=e=>({signUp:{start:Se(((t,n,s)=>q(e.post(d.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:He(((t,n)=>q(e.post(d.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:Te(((t,n,s,o)=>q(e.post(d.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:He(((t,n)=>q(e.post(d.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:Me(((t,n)=>q(e.post(d.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:Ae(((t,n,s)=>q(e.post(d.update.start,{loginId:t,origin:n},{token:s})))),finish:He(((t,n)=>q(e.post(d.update.finish,{transactionId:t,response:n}))))}}),Le=z(J("token"));var _e,ze;var Je=z([(_e="projectId",ze=J("projectId"),E(((e,n)=>s=>$(...n).validate(t(s,e)))(_e,ze))())])((e=>t=>{var n,s;const o=[].concat((null===(n=t.hooks)||void 0===n?void 0:n.beforeRequest)||[]),i=[].concat((null===(s=t.hooks)||void 0===s?void 0:s.afterRequest)||[]);return e(Object.assign(Object.assign({},t),{hooks:{beforeRequest:e=>null==o?void 0:o.reduce(((e,t)=>t(e)),e),afterRequest:async(e,n)=>{(await Promise.allSettled(null==i?void 0:i.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))}))}}}))})((({projectId:e,logger:t,baseUrl:n,hooks:s,cookiePolicy:o,baseHeaders:i={},fetch:a})=>{return r=y({baseUrl:n||"https://api.descope.com",projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:B(r),otp:ke(r),magicLink:le(r),enchantedLink:te(r),oauth:ge(r),saml:we(r),totp:Pe(r),webauthn:Ce(r),flow:oe(r),refresh:e=>q(r.post(l,{},{token:e})),logout:e=>q(r.post(c,{},{token:e})),logoutAll:e=>q(r.post(p,{},{token:e})),me:e=>q(r.get(g,{token:e})),isJwtExpired:Le(U),getJwtPermissions:Le(x),getJwtRoles:Le(R),httpClient:r};var r})));var Ne=Object.assign(Je,{DeliveryMethods:K});exports.HttpStatusCodes=O,exports.addHooksToConfig=(e,t)=>{var n;return["beforeRequest","afterRequest"].reduce(((n,s)=>{var o;return n[s]=[].concat((null===(o=e.hooks)||void 0===o?void 0:o[s])||[]).concat((null==t?void 0:t[s])||[]),n}),null!==(n=e.hooks)&&void 0!==n?n:e.hooks={}),e},exports.default=Ne,exports.transformResponse=q,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";import t from"lodash.get";var n={exchange:"/v1/auth/accesskey/exchange"},s={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"},o={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"},i={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"},a={start:"/v1/auth/oauth/authorize",exchange:"/v1/auth/oauth/exchange"},r={start:"/v1/auth/saml/authorize",exchange:"/v1/auth/saml/exchange"},u={verify:"/v1/auth/totp/verify",signUp:"/v1/auth/totp/signup",update:"/v1/auth/totp/update"},d={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"}},l="/v1/auth/refresh",c="/v1/auth/logout",p="/v1/auth/logoutall",g="/v1/auth/me",h={start:"/v1/flow/start",next:"/v1/flow/next"};const v=()=>{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")}},f=(e,t)=>{const 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=>v().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.clone?e.clone().text():e.text());return e.text=()=>Promise.resolve(t),e.json=()=>Promise.resolve(JSON.parse(t)),v().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var m;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT"}(m||(m={}));const I=(...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}),{})),b=e=>void 0===e?void 0:JSON.stringify(e),k=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},y=({baseUrl:e,projectId:t,baseConfig:n,logger:s,hooks:o,cookiePolicy:i,fetch:a})=>{const r=f(s,a),u=async s=>{const a=(null==o?void 0:o.beforeRequest)?o.beforeRequest(s):s,{path:u,body:d,headers:l,queryParams:c,method:p,token:g}=a,h=await r((({path:e,baseUrl:t,queryParams:n})=>{const s=new URL(e,t);return n&&(s.search=new URLSearchParams(n).toString()),s})({path:u,baseUrl:e,queryParams:c}),{headers:I(k(t,g),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"1.0.1"},(null==n?void 0:n.baseHeaders)||{},l),method:p,body:b(d),credentials:i||"include"});return(null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==h?void 0:h.clone()),h};return{get:(e,{headers:t,queryParams:n,token:s}={})=>u({path:e,headers:t,queryParams:n,body:void 0,method:m.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.post,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.put,token:o}),delete:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.delete,token:o}),hooks:o}};var O={TOO_MANY_REQUESTS:429};function w(e,t,n){var s;let o=j(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 j(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function U(e){const{exp:t}=j(e);return(new Date).getTime()/1e3>t}function R(e,t){return w(e,t,"permissions")}function x(e,t){return w(e,t,"roles")}const P=(...e)=>e.join("/").replace(/\/{2,}/g,"/");async function q(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===O.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=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),$=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),S=e=>t=>e.test(t),M=S(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),T=S(/^\+[1-9]{1}[0-9]{3,14}$/),A=E(M,'"{val}" is not a valid email'),L=E(T,'"{val}" is not a valid phone number'),z=E((H=1,e=>e.length>=H),"Minimum length is 1");var H;const J=E((e=>"string"==typeof e),"Input is not a string"),N=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>$(...e).validate(n[t]))),t(...n)),C=e=>[J(`"${e}" must be a string`),z(`"${e}" must not be empty`)],_=e=>[J(`"${e}" must be a string`),A()],D=e=>[J(`"${e}" must be a string`),L()],Z=N(C("accessKey")),B=e=>({exchange:Z((t=>q(e.post(n.exchange,{},{token:t}))))});var F,K,Q,Y;!function(e){e.sms="sms",e.whatsapp="whatsapp"}(F||(F={})),function(e){e.email="email",e.sms="sms",e.whatsapp="whatsapp"}(K||(K={})),function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(Q||(Q={})),function(e){e.signUp="signup",e.signIn="signin",e.verify="verify"}(Y||(Y={}));const G=C("loginId"),V=N(C("token")),W=N(G),X=N(C("pendingRef")),ee=N(G,_("email")),te=e=>({verify:V((t=>q(e.post(i.verify,{token:t})))),signIn:W(((t,n,s,o)=>q(e.post(P(i.signIn,K.email),{loginId:t,URI:n,loginOptions:s},{token:o})))),signUpOrIn:W(((t,n)=>q(e.post(P(i.signUpOrIn,K.email),{loginId:t,URI:n})))),signUp:W(((t,n,s)=>q(e.post(P(i.signUp,K.email),{loginId:t,URI:n,user:s})))),waitForSession:X(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:a}=(({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||1e3,1e3),timeoutMs:Math.min(t||6e5,6e5)}))(n);let r;const u=setInterval((async()=>{const n=await e.post(i.session,{pendingRef:t});n.ok&&(clearInterval(u),r&&clearTimeout(r),s(q(Promise.resolve(n))))}),o);r=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${a}ms`,errorCode:"0"},ok:!1}),clearInterval(u)}),a)})))),update:{email:ee(((t,n,s,o)=>q(e.post(i.update.email,{loginId:t,email:n,URI:s},{token:o}))))}}),ne=N(C("flowId")),se=N(C("executionId"),C("stepId"),C("interactionId")),oe=e=>({start:ne(((t,n,s,o,i)=>q(e.post(h.start,{flowId:t,options:n,conditionInteractionId:s,interactionId:o,input:i})))),next:se(((t,n,s,o)=>q(e.post(h.next,{executionId:t,stepId:n,interactionId:s,input:o}))))}),ie=C("loginId"),ae=N(C("token")),re=N(ie),ue=N(ie,D("phone")),de=N(ie,_("email")),le=e=>({verify:ae((t=>q(e.post(o.verify,{token:t})))),signIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s,i,a)=>q(e.post(P(o.signIn,n),{loginId:t,URI:s,loginOptions:i},{token:a}))))})),{}),signUp:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s,i)=>q(e.post(P(o.signUp,n),{loginId:t,URI:s,user:i}))))})),{}),signUpOrIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s)=>q(e.post(P(o.signUpOrIn,n),{loginId:t,URI:s}))))})),{}),update:{email:de(((t,n,s,i)=>q(e.post(o.update.email,{loginId:t,email:n,URI:s},{token:i})))),phone:Object.keys(F).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:ue(((t,s,i,a)=>q(e.post(P(o.update.phone,n),{loginId:t,phone:s,URI:i},{token:a}))))})),{})}});var ce;!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"}(ce||(ce={}));const pe=N(C("code")),ge=e=>({start:Object.keys(ce).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:async(t,{redirect:s=!1}={},o,i)=>{const r=await e.post(a.start,o||{},{queryParams:Object.assign({provider:n},t&&{redirectURL:t}),token:i});if(!s||!r.ok)return q(Promise.resolve(r));const{url:u}=await r.json();window.location.href=u}})),{}),exchange:pe((t=>q(e.post(a.exchange,{code:t}))))});var he;!function(e){e.signUp="signup",e.signIn="signin",e.verify="verify",e.updatePhone="updatePhone"}(he||(he={}));const ve=C("loginId"),fe=N(ve,C("code")),me=N(ve),Ie=N(ve,D("phone")),be=N(ve,_("email")),ke=e=>({verify:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:fe(((t,o)=>q(e.post(P(s.verify,n),{code:o,loginId:t}))))})),{}),signIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me(((t,o,i)=>q(e.post(P(s.signIn,n),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me(((t,o)=>q(e.post(P(s.signUp,n),{loginId:t,user:o}))))})),{}),signUpOrIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me((t=>q(e.post(P(s.signUpOrIn,n),{loginId:t}))))})),{}),update:{email:be(((t,n,o)=>q(e.post(s.update.email,{loginId:t,email:n},{token:o})))),phone:Object.keys(F).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Ie(((t,o,i)=>q(e.post(P(s.update.phone,n),{loginId:t,phone:o},{token:i}))))})),{})}}),ye=N(C("tenant")),Oe=N(C("code")),we=e=>({start:ye((async(t,n,{redirect:s=!1}={},o,i)=>{const a=await e.post(r.start,o||{},{queryParams:{tenant:t,redirectURL:n},token:i});if(!s||!a.ok)return q(Promise.resolve(a));const{url:u}=await a.json();window.location.href=u})),exchange:Oe((t=>q(e.post(r.exchange,{code:t}))))}),je=C("loginId"),Ue=N(je,C("code")),Re=N(je),xe=N(je),Pe=e=>({signUp:Re(((t,n)=>q(e.post(u.signUp,{loginId:t,user:n})))),verify:Ue(((t,n,s,o)=>q(e.post(u.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:xe(((t,n)=>q(e.post(u.update,{loginId:t},{token:n}))))}),qe=[J(`"${"loginId"}" must be a string`)];const Ee=C("loginId"),$e=C("origin"),Se=N(Ee,$e,C("name")),Me=N(Ee,$e),Te=N(qe,$e),Ae=N(Ee,$e,C("token")),Le=N(C("transactionId"),C("response")),ze=e=>({signUp:{start:Se(((t,n,s)=>q(e.post(d.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:Le(((t,n)=>q(e.post(d.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:Te(((t,n,s,o)=>q(e.post(d.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:Le(((t,n)=>q(e.post(d.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:Me(((t,n)=>q(e.post(d.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:Ae(((t,n,s)=>q(e.post(d.update.start,{loginId:t,origin:n},{token:s})))),finish:Le(((t,n)=>q(e.post(d.update.finish,{transactionId:t,response:n}))))}}),He=N(C("token"));var Je,Ne;var Ce=N([(Je="projectId",Ne=C("projectId"),E(((e,n)=>s=>$(...n).validate(t(s,e)))(Je,Ne))())])((e=>t=>{var n,s;const o=[].concat((null===(n=t.hooks)||void 0===n?void 0:n.beforeRequest)||[]),i=[].concat((null===(s=t.hooks)||void 0===s?void 0:s.afterRequest)||[]);return e(Object.assign(Object.assign({},t),{hooks:{beforeRequest:e=>null==o?void 0:o.reduce(((e,t)=>t(e)),e),afterRequest:async(e,n)=>{(await Promise.allSettled(null==i?void 0:i.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))}))}}}))})((({projectId:e,logger:t,baseUrl:n,hooks:s,cookiePolicy:o,baseHeaders:i={},fetch:a})=>{return r=y({baseUrl:n||"https://api.descope.com",projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:B(r),otp:ke(r),magicLink:le(r),enchantedLink:te(r),oauth:ge(r),saml:we(r),totp:Pe(r),webauthn:ze(r),flow:oe(r),refresh:e=>q(r.post(l,{},{token:e})),logout:e=>q(r.post(c,{},{token:e})),logoutAll:e=>q(r.post(p,{},{token:e})),me:e=>q(r.get(g,{token:e})),isJwtExpired:He(U),getJwtPermissions:He(R),getJwtRoles:He(x),httpClient:r};var r})));const _e=(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=(e,t)=>{var n;return["beforeRequest","afterRequest"].reduce(((n,s)=>{var o;return n[s]=[].concat((null===(o=e.hooks)||void 0===o?void 0:o[s])||[]).concat((null==t?void 0:t[s])||[]),n}),null!==(n=e.hooks)&&void 0!==n?n:e.hooks={}),e};var Ze=Object.assign(Ce,{DeliveryMethods:K});export{O as HttpStatusCodes,De as addHooksToConfig,Ze as default,q as transformResponse,_e as wrapWith}; | ||
import e from"jwt-decode";import t from"lodash.get";var n={exchange:"/v1/auth/accesskey/exchange"},s={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"},o={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"},i={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"},a={start:"/v1/auth/oauth/authorize",exchange:"/v1/auth/oauth/exchange"},r={start:"/v1/auth/saml/authorize",exchange:"/v1/auth/saml/exchange"},u={verify:"/v1/auth/totp/verify",signUp:"/v1/auth/totp/signup",update:"/v1/auth/totp/update"},d={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"}},l="/v1/auth/refresh",c="/v1/auth/logout",p="/v1/auth/logoutall",g="/v1/auth/me",h={start:"/v1/flow/start",next:"/v1/flow/next"};const v=()=>{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")}},f=(e,t)=>{const 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=>v().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.clone?e.clone().text():e.text());return e.text=()=>Promise.resolve(t),e.json=()=>Promise.resolve(JSON.parse(t)),v().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(s)),s}:n};var m;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT"}(m||(m={}));const I=(...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}),{})),b=e=>void 0===e?void 0:JSON.stringify(e),k=(e,t="")=>{let n=e;return t&&(n=n+":"+t),{Authorization:`Bearer ${n}`}},y=({baseUrl:e,projectId:t,baseConfig:n,logger:s,hooks:o,cookiePolicy:i,fetch:a})=>{const r=f(s,a),u=async s=>{const a=(null==o?void 0:o.beforeRequest)?o.beforeRequest(s):s,{path:u,body:d,headers:l,queryParams:c,method:p,token:g}=a,h=await r((({path:e,baseUrl:t,queryParams:n})=>{const s=new URL(e,t);return n&&(s.search=new URLSearchParams(n).toString()),s})({path:u,baseUrl:e,queryParams:c}),{headers:I(k(t,g),{"x-descope-sdk-name":"core-js","x-descope-sdk-version":"1.0.2"},(null==n?void 0:n.baseHeaders)||{},l),method:p,body:b(d),credentials:i||"include"});return(null==o?void 0:o.afterRequest)&&await o.afterRequest(s,null==h?void 0:h.clone()),h};return{get:(e,{headers:t,queryParams:n,token:s}={})=>u({path:e,headers:t,queryParams:n,body:void 0,method:m.get,token:s}),post:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.post,token:o}),put:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.put,token:o}),delete:(e,t,{headers:n,queryParams:s,token:o}={})=>u({path:e,headers:n,queryParams:s,body:t,method:m.delete,token:o}),hooks:o}};var O={TOO_MANY_REQUESTS:429};function w(e,t,n){var s;let o=j(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 j(t){if("string"!=typeof t||!t)throw new Error("Invalid token provided");return e(t)}function U(e){const{exp:t}=j(e);return(new Date).getTime()/1e3>t}function R(e,t){return w(e,t,"permissions")}function x(e,t){return w(e,t,"roles")}const P=(...e)=>e.join("/").replace(/\/{2,}/g,"/");async function q(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===O.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=(e,t)=>(n=t)=>t=>!e(t)&&n.replace("{val}",t),$=(...e)=>({validate:t=>(e.forEach((e=>{const n=e(t);if(n)throw new Error(n)})),!0)}),S=e=>t=>e.test(t),M=S(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),T=S(/^\+[1-9]{1}[0-9]{3,14}$/),A=E(M,'"{val}" is not a valid email'),L=E(T,'"{val}" is not a valid phone number'),z=E((H=1,e=>e.length>=H),"Minimum length is 1");var H;const J=E((e=>"string"==typeof e),"Input is not a string"),N=(...e)=>t=>(...n)=>(e.forEach(((e,t)=>$(...e).validate(n[t]))),t(...n)),C=e=>[J(`"${e}" must be a string`),z(`"${e}" must not be empty`)],_=e=>[J(`"${e}" must be a string`),A()],D=e=>[J(`"${e}" must be a string`),L()],Z=N(C("accessKey")),B=e=>({exchange:Z((t=>q(e.post(n.exchange,{},{token:t}))))});var F,K,Q,Y;!function(e){e.sms="sms",e.whatsapp="whatsapp"}(F||(F={})),function(e){e.email="email",e.sms="sms",e.whatsapp="whatsapp"}(K||(K={})),function(e){e.waiting="waiting",e.running="running",e.completed="completed",e.failed="failed"}(Q||(Q={})),function(e){e.signUp="signup",e.signIn="signin",e.verify="verify"}(Y||(Y={}));const G=C("loginId"),V=N(C("token")),W=N(G),X=N(C("pendingRef")),ee=N(G,_("email")),te=e=>({verify:V((t=>q(e.post(i.verify,{token:t})))),signIn:W(((t,n,s,o)=>q(e.post(P(i.signIn,K.email),{loginId:t,URI:n,loginOptions:s},{token:o})))),signUpOrIn:W(((t,n)=>q(e.post(P(i.signUpOrIn,K.email),{loginId:t,URI:n})))),signUp:W(((t,n,s)=>q(e.post(P(i.signUp,K.email),{loginId:t,URI:n,user:s})))),waitForSession:X(((t,n)=>new Promise((s=>{const{pollingIntervalMs:o,timeoutMs:a}=(({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||1e3,1e3),timeoutMs:Math.min(t||6e5,6e5)}))(n);let r;const u=setInterval((async()=>{const n=await e.post(i.session,{pendingRef:t});n.ok&&(clearInterval(u),r&&clearTimeout(r),s(q(Promise.resolve(n))))}),o);r=setTimeout((()=>{s({error:{errorDescription:`Session polling timeout exceeded: ${a}ms`,errorCode:"0"},ok:!1}),clearInterval(u)}),a)})))),update:{email:ee(((t,n,s,o)=>q(e.post(i.update.email,{loginId:t,email:n,URI:s},{token:o}))))}}),ne=N(C("flowId")),se=N(C("executionId"),C("stepId"),C("interactionId")),oe=e=>({start:ne(((t,n,s,o,i)=>q(e.post(h.start,{flowId:t,options:n,conditionInteractionId:s,interactionId:o,input:i})))),next:se(((t,n,s,o)=>q(e.post(h.next,{executionId:t,stepId:n,interactionId:s,input:o}))))}),ie=C("loginId"),ae=N(C("token")),re=N(ie),ue=N(ie,D("phone")),de=N(ie,_("email")),le=e=>({verify:ae((t=>q(e.post(o.verify,{token:t})))),signIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s,i,a)=>q(e.post(P(o.signIn,n),{loginId:t,URI:s,loginOptions:i},{token:a}))))})),{}),signUp:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s,i)=>q(e.post(P(o.signUp,n),{loginId:t,URI:s,user:i}))))})),{}),signUpOrIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:re(((t,s)=>q(e.post(P(o.signUpOrIn,n),{loginId:t,URI:s}))))})),{}),update:{email:de(((t,n,s,i)=>q(e.post(o.update.email,{loginId:t,email:n,URI:s},{token:i})))),phone:Object.keys(F).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:ue(((t,s,i,a)=>q(e.post(P(o.update.phone,n),{loginId:t,phone:s,URI:i},{token:a}))))})),{})}});var ce;!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"}(ce||(ce={}));const pe=N(C("code")),ge=e=>({start:Object.keys(ce).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:async(t,{redirect:s=!1}={},o,i)=>{const r=await e.post(a.start,o||{},{queryParams:Object.assign({provider:n},t&&{redirectURL:t}),token:i});if(!s||!r.ok)return q(Promise.resolve(r));const{url:u}=await r.json();window.location.href=u}})),{}),exchange:pe((t=>q(e.post(a.exchange,{code:t}))))});var he;!function(e){e.signUp="signup",e.signIn="signin",e.verify="verify",e.updatePhone="updatePhone"}(he||(he={}));const ve=C("loginId"),fe=N(ve,C("code")),me=N(ve),Ie=N(ve,D("phone")),be=N(ve,_("email")),ke=e=>({verify:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:fe(((t,o)=>q(e.post(P(s.verify,n),{code:o,loginId:t}))))})),{}),signIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me(((t,o,i)=>q(e.post(P(s.signIn,n),{loginId:t,loginOptions:o},{token:i}))))})),{}),signUp:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me(((t,o)=>q(e.post(P(s.signUp,n),{loginId:t,user:o}))))})),{}),signUpOrIn:Object.keys(K).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:me((t=>q(e.post(P(s.signUpOrIn,n),{loginId:t}))))})),{}),update:{email:be(((t,n,o)=>q(e.post(s.update.email,{loginId:t,email:n},{token:o})))),phone:Object.keys(F).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Ie(((t,o,i)=>q(e.post(P(s.update.phone,n),{loginId:t,phone:o},{token:i}))))})),{})}}),ye=N(C("tenant")),Oe=N(C("code")),we=e=>({start:ye((async(t,n,{redirect:s=!1}={},o,i)=>{const a=await e.post(r.start,o||{},{queryParams:{tenant:t,redirectURL:n},token:i});if(!s||!a.ok)return q(Promise.resolve(a));const{url:u}=await a.json();window.location.href=u})),exchange:Oe((t=>q(e.post(r.exchange,{code:t}))))}),je=C("loginId"),Ue=N(je,C("code")),Re=N(je),xe=N(je),Pe=e=>({signUp:Re(((t,n)=>q(e.post(u.signUp,{loginId:t,user:n})))),verify:Ue(((t,n,s,o)=>q(e.post(u.verify,{loginId:t,code:n,loginOptions:s},{token:o})))),update:xe(((t,n)=>q(e.post(u.update,{loginId:t},{token:n}))))}),qe=[J(`"${"loginId"}" must be a string`)];const Ee=C("loginId"),$e=C("origin"),Se=N(Ee,$e,C("name")),Me=N(Ee,$e),Te=N(qe,$e),Ae=N(Ee,$e,C("token")),Le=N(C("transactionId"),C("response")),ze=e=>({signUp:{start:Se(((t,n,s)=>q(e.post(d.signUp.start,{user:{loginId:t,name:s},origin:n})))),finish:Le(((t,n)=>q(e.post(d.signUp.finish,{transactionId:t,response:n}))))},signIn:{start:Te(((t,n,s,o)=>q(e.post(d.signIn.start,{loginId:t,origin:n,loginOptions:s},{token:o})))),finish:Le(((t,n)=>q(e.post(d.signIn.finish,{transactionId:t,response:n}))))},signUpOrIn:{start:Me(((t,n)=>q(e.post(d.signUpOrIn.start,{loginId:t,origin:n}))))},update:{start:Ae(((t,n,s)=>q(e.post(d.update.start,{loginId:t,origin:n},{token:s})))),finish:Le(((t,n)=>q(e.post(d.update.finish,{transactionId:t,response:n}))))}}),He=N(C("token"));var Je,Ne;var Ce=N([(Je="projectId",Ne=C("projectId"),E(((e,n)=>s=>$(...n).validate(t(s,e)))(Je,Ne))())])((e=>t=>{var n,s;const o=[].concat((null===(n=t.hooks)||void 0===n?void 0:n.beforeRequest)||[]),i=[].concat((null===(s=t.hooks)||void 0===s?void 0:s.afterRequest)||[]);return e(Object.assign(Object.assign({},t),{hooks:{beforeRequest:e=>null==o?void 0:o.reduce(((e,t)=>t(e)),e),afterRequest:async(e,n)=>{(await Promise.allSettled(null==i?void 0:i.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))}))}}}))})((({projectId:e,logger:t,baseUrl:n,hooks:s,cookiePolicy:o,baseHeaders:i={},fetch:a})=>{return r=y({baseUrl:n||"https://api.descope.com",projectId:e,logger:t,hooks:s,cookiePolicy:o,baseConfig:{baseHeaders:i},fetch:a}),{accessKey:B(r),otp:ke(r),magicLink:le(r),enchantedLink:te(r),oauth:ge(r),saml:we(r),totp:Pe(r),webauthn:ze(r),flow:oe(r),refresh:e=>q(r.post(l,{},{token:e})),logout:e=>q(r.post(c,{},{token:e})),logoutAll:e=>q(r.post(p,{},{token:e})),me:e=>q(r.get(g,{token:e})),isJwtExpired:He(U),getJwtPermissions:He(R),getJwtRoles:He(x),httpClient:r};var r})));const _e=(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=(e,t)=>{var n;return["beforeRequest","afterRequest"].reduce(((n,s)=>{var o;return n[s]=[].concat((null===(o=e.hooks)||void 0===o?void 0:o[s])||[]).concat((null==t?void 0:t[s])||[]),n}),null!==(n=e.hooks)&&void 0!==n?n:e.hooks={}),e};var Ze=Object.assign(Ce,{DeliveryMethods:K});export{O as HttpStatusCodes,De as addHooksToConfig,Ze as default,q as transformResponse,_e as wrapWith}; | ||
//# sourceMappingURL=index.esm.js.map |
{ | ||
"name": "@descope/core-js-sdk", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "Descope Team <info@descope.com>", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/descope/core-js-sdk", |
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