@descope/core-js-sdk 0.0.41-alpha.17 → 0.0.41-alpha.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,15 +1,18 @@
1
- declare type SdkFn = (...args: any[]) => Promise<SdkResponse>;
2
- interface UserResponse extends User {
3
- externalIds: string[];
4
- verifiedEmail?: boolean;
5
- verifiedPhone?: boolean;
6
- }
1
+ declare type SdkFn = (...args: any[]) => Promise<SdkResponse<ResponseData>>;
2
+ /** User base details from Descope API */
7
3
  declare type User = {
8
4
  email?: string;
9
5
  name?: string;
10
6
  phone?: string;
11
7
  };
12
- interface AuthInfo {
8
+ /** User extended details from Descope API */
9
+ declare type UserResponse = User & {
10
+ externalIds: string[];
11
+ verifiedEmail?: boolean;
12
+ verifiedPhone?: boolean;
13
+ };
14
+ /** Authentication info result from the various JWT validations */
15
+ declare type JWTResponse = {
13
16
  sessionJwt: string;
14
17
  refreshJwt?: string;
15
18
  cookieDomain?: string;
@@ -18,14 +21,38 @@ interface AuthInfo {
18
21
  cookieExpiration?: number;
19
22
  user?: UserResponse;
20
23
  firstSeen?: boolean;
21
- }
24
+ };
25
+ declare type ExchangeAccessKeyResponse = {
26
+ keyId: string;
27
+ sessionJwt: string;
28
+ expiration: number;
29
+ };
30
+ /** Pending reference URL to poll while waiting for user to click magic link */
31
+ declare type PendingRefResponse = {
32
+ pendingRef: string;
33
+ };
34
+ /** URL response to redirect user in case of OAuth or SSO */
35
+ declare type URLResponse = {
36
+ url: string;
37
+ };
38
+ /** TOTP response with the TOTP details */
39
+ declare type TOTPResponse = {
40
+ provisioningURL: string;
41
+ image: string;
42
+ key: string;
43
+ };
44
+ /** All delivery methods currently supported */
22
45
  declare enum DeliveryMethods {
23
46
  email = "email",
24
47
  sms = "sms",
25
48
  whatsapp = "whatsapp"
26
49
  }
27
- declare type Deliveries<T extends SdkFn> = Record<DeliveryMethods, T>;
28
- declare type SdkResponse = {
50
+ declare type ResponseData = Record<string, any>;
51
+ /**
52
+ * Response from our SDK calls which includes the result (ok, code, error).
53
+ * The relevant data is provided in the more specific interfaces extending SdkResponse.
54
+ */
55
+ declare type SdkResponse<T extends ResponseData> = {
29
56
  code?: number;
30
57
  ok: boolean;
31
58
  response?: Response;
@@ -33,10 +60,14 @@ declare type SdkResponse = {
33
60
  message: string;
34
61
  code: string;
35
62
  };
36
- data?: any;
63
+ data?: T;
37
64
  };
65
+ /** Different delivery method */
66
+ declare type Deliveries<T extends SdkFn> = Record<DeliveryMethods, T>;
67
+ /** Logger type that supports the given levels (debug, log, error) */
38
68
  declare type Logger = Pick<Console, 'debug' | 'log' | 'error'>;
39
69
 
70
+ /** Request configuration including headers, query params and token */
40
71
  declare type HttpClientReqConfig = {
41
72
  headers?: HeadersInit;
42
73
  queryParams?: {
@@ -44,18 +75,21 @@ declare type HttpClientReqConfig = {
44
75
  };
45
76
  token?: string;
46
77
  };
78
+ /** HTTP methods we use in the client */
47
79
  declare enum HTTPMethods {
48
80
  get = "GET",
49
81
  delete = "DELETE",
50
82
  post = "POST",
51
83
  put = "PUT"
52
84
  }
85
+ /** HTTP Client type that implements the HTTP method calls. Descopers can provide their own HTTP client although required only in rare cases. */
53
86
  declare type HttpClient = {
54
87
  get: (path: string, config?: HttpClientReqConfig) => Promise<Response>;
55
88
  post: (path: string, body?: any, config?: HttpClientReqConfig) => Promise<Response>;
56
89
  put: (path: string, body?: any, config?: HttpClientReqConfig) => Promise<Response>;
57
90
  delete: (path: string, body?: any, config?: HttpClientReqConfig) => Promise<Response>;
58
91
  };
92
+ /** For before-request hook allows overriding parts of the request */
59
93
  declare type RequestConfig = {
60
94
  path: string;
61
95
  headers?: HeadersInit;
@@ -66,14 +100,16 @@ declare type RequestConfig = {
66
100
  method: HTTPMethods;
67
101
  token?: string;
68
102
  };
103
+ /** Hooks before and after the request is made */
69
104
  declare type Hooks = {
70
105
  beforeRequest?: (config: RequestConfig) => RequestConfig;
71
106
  afterRequest?: (req: RequestConfig, res: Response) => void;
72
107
  };
73
108
 
74
- declare type SignInFn = (identifier: string, uri: string) => Promise<SdkResponse>;
75
- declare type SignUpFn = (identifier: string, uri: string, user?: User) => Promise<SdkResponse>;
76
- declare type UpdatePhoneFn = (identifier: string, phone: string) => Promise<SdkResponse>;
109
+ declare type SignInFn = (identifier: string, uri: string) => Promise<SdkResponse<PendingRefResponse>>;
110
+ declare type SignUpFn = (identifier: string, uri: string, user?: User) => Promise<SdkResponse<PendingRefResponse>>;
111
+ declare type UpdatePhoneFn = (identifier: string, phone: string) => Promise<SdkResponse<never>>;
112
+ /** Polling configuration for session waiting */
77
113
  declare type WaitForSessionConfig = {
78
114
  pollingIntervalMs: number;
79
115
  timeoutMs: number;
@@ -88,6 +124,22 @@ declare enum OAuthProviders {
88
124
  apple = "apple"
89
125
  }
90
126
 
127
+ /** Descope SDK client with delivery methods enum.
128
+ *
129
+ * Please see full documentation at {@link https://docs.descope.com/guides Descope Docs}
130
+ * @example Usage
131
+ *
132
+ * ```js
133
+ * import descopeSdk from '@descope/core-js-sdk';
134
+ *
135
+ * const myProjectId = 'xxx';
136
+ * const sdk = descopeSdk({ projectId: myProjectId });
137
+ *
138
+ * const userIdentifier = 'identifier';
139
+ * sdk.otp.signIn.email(userIdentifier);
140
+ * const jwtResponse = sdk.otp.verify.email(userIdentifier, codeFromEmail);
141
+ * ```
142
+ */
91
143
  declare const sdkWithAttributes: ((args_0: {
92
144
  projectId: string;
93
145
  logger?: Logger;
@@ -95,112 +147,118 @@ declare const sdkWithAttributes: ((args_0: {
95
147
  hooks?: Hooks;
96
148
  cookiePolicy?: RequestCredentials;
97
149
  }) => {
150
+ accessKey: {
151
+ exchange: (accessKey: string) => Promise<SdkResponse<ExchangeAccessKeyResponse>>;
152
+ };
98
153
  otp: {
99
- verify: Deliveries<(identifier: string, code: string) => Promise<SdkResponse>>;
100
- signIn: Deliveries<(identifier: string) => Promise<SdkResponse>>;
101
- signUp: Deliveries<(identifier: string, user?: User) => Promise<SdkResponse>>;
102
- signUpOrIn: Deliveries<(identifier: string) => Promise<SdkResponse>>;
154
+ verify: Deliveries<(identifier: string, code: string) => Promise<SdkResponse<JWTResponse>>>;
155
+ signIn: Deliveries<(identifier: string) => Promise<SdkResponse<never>>>;
156
+ signUp: Deliveries<(identifier: string, user?: User) => Promise<SdkResponse<never>>>;
157
+ signUpOrIn: Deliveries<(identifier: string) => Promise<SdkResponse<never>>>;
103
158
  update: {
104
- email: (identifier: string, email: string, token?: string) => Promise<SdkResponse>;
105
- phone: Deliveries<(identifier: string, phone: string) => Promise<SdkResponse>>;
159
+ email: (identifier: string, email: string, token?: string) => Promise<SdkResponse<never>>;
160
+ phone: Deliveries<(identifier: string, phone: string) => Promise<SdkResponse<never>>>;
106
161
  };
107
162
  };
108
163
  magicLink: {
109
- verify: (token: string) => Promise<SdkResponse>;
164
+ verify: (token: string) => Promise<SdkResponse<JWTResponse>>;
110
165
  signIn: Deliveries<SignInFn>;
111
166
  signUp: Deliveries<SignUpFn>;
112
167
  signUpOrIn: Deliveries<SignInFn>;
113
168
  update: {
114
- email: (identifier: string, email: string, uri: string, token?: string) => Promise<SdkResponse>;
169
+ email: (identifier: string, email: string, uri: string, token?: string) => Promise<SdkResponse<never>>;
115
170
  phone: Deliveries<UpdatePhoneFn>;
116
171
  };
117
172
  crossDevice: {
118
- verify: (token: string) => Promise<SdkResponse>;
173
+ verify: (token: string) => Promise<SdkResponse<JWTResponse>>;
119
174
  signIn: Deliveries<SignInFn>;
120
175
  signUpOrIn: Deliveries<SignInFn>;
121
176
  signUp: Deliveries<SignUpFn>;
122
- waitForSession: (pendingRef: string, config?: WaitForSessionConfig) => Promise<SdkResponse>;
177
+ waitForSession: (pendingRef: string, config?: WaitForSessionConfig) => Promise<SdkResponse<JWTResponse>>;
123
178
  update: {
124
- email: (identifier: string, email: string, uri: string, token?: string) => Promise<SdkResponse>;
179
+ email: (identifier: string, email: string, uri: string, token?: string) => Promise<SdkResponse<never>>;
125
180
  phone: Deliveries<UpdatePhoneFn>;
126
181
  };
127
182
  };
128
183
  };
129
184
  oauth: {
130
- exchange: (code: string) => Promise<SdkResponse>;
185
+ exchange: (code: string) => Promise<SdkResponse<JWTResponse>>;
131
186
  start: {
132
187
  facebook: <B extends {
133
188
  redirect: boolean;
134
189
  }>(redirectURL?: string, config?: B) => Promise<B extends {
135
190
  redirect: true;
136
- } ? undefined : SdkResponse>;
191
+ } ? undefined : SdkResponse<URLResponse>>;
137
192
  github: <B extends {
138
193
  redirect: boolean;
139
194
  }>(redirectURL?: string, config?: B) => Promise<B extends {
140
195
  redirect: true;
141
- } ? undefined : SdkResponse>;
196
+ } ? undefined : SdkResponse<URLResponse>>;
142
197
  google: <B extends {
143
198
  redirect: boolean;
144
199
  }>(redirectURL?: string, config?: B) => Promise<B extends {
145
200
  redirect: true;
146
- } ? undefined : SdkResponse>;
201
+ } ? undefined : SdkResponse<URLResponse>>;
147
202
  microsoft: <B extends {
148
203
  redirect: boolean;
149
204
  }>(redirectURL?: string, config?: B) => Promise<B extends {
150
205
  redirect: true;
151
- } ? undefined : SdkResponse>;
206
+ } ? undefined : SdkResponse<URLResponse>>;
152
207
  gitlab: <B extends {
153
208
  redirect: boolean;
154
209
  }>(redirectURL?: string, config?: B) => Promise<B extends {
155
210
  redirect: true;
156
- } ? undefined : SdkResponse>;
211
+ } ? undefined : SdkResponse<URLResponse>>;
157
212
  apple: <B extends {
158
213
  redirect: boolean;
159
214
  }>(redirectURL?: string, config?: B) => Promise<B extends {
160
215
  redirect: true;
161
- } ? undefined : SdkResponse>;
216
+ } ? undefined : SdkResponse<URLResponse>>;
162
217
  };
163
218
  };
164
219
  saml: {
165
- exchange: (code: string) => Promise<SdkResponse>;
220
+ exchange: (code: string) => Promise<SdkResponse<JWTResponse>>;
166
221
  start: <B_1 extends {
167
222
  redirect: boolean;
168
223
  }>(tenantNameOrEmail: string, config?: B_1) => Promise<B_1 extends {
169
224
  redirect: true;
170
- } ? undefined : SdkResponse>;
225
+ } ? undefined : SdkResponse<URLResponse>>;
171
226
  };
172
227
  totp: {
173
- signUp: (identifier: string, user?: User) => Promise<SdkResponse>;
174
- verify: (identifier: string, code: string) => Promise<SdkResponse>;
175
- update: (identifier: string, token?: string) => Promise<SdkResponse>;
228
+ signUp: (identifier: string, user?: User) => Promise<SdkResponse<TOTPResponse>>;
229
+ verify: (identifier: string, code: string) => Promise<SdkResponse<JWTResponse>>;
230
+ update: (identifier: string, token?: string) => Promise<SdkResponse<TOTPResponse>>;
176
231
  };
177
232
  webauthn: {
178
233
  signUp: {
179
- start: (identifier: string, origin: string, name: string) => Promise<SdkResponse>;
180
- finish: (transactionId: string, response: string) => Promise<SdkResponse>;
234
+ start: (identifier: string, origin: string, name: string) => Promise<SdkResponse<ResponseData>>;
235
+ finish: (transactionId: string, response: string) => Promise<SdkResponse<ResponseData>>;
181
236
  };
182
237
  signIn: {
183
- start: (identifier: string, origin: string) => Promise<SdkResponse>;
184
- finish: (transactionId: string, response: string) => Promise<SdkResponse>;
238
+ start: (identifier: string, origin: string) => Promise<SdkResponse<ResponseData>>;
239
+ finish: (transactionId: string, response: string) => Promise<SdkResponse<ResponseData>>;
185
240
  };
186
241
  update: {
187
- start: (identifier: string, origin: string, token: string) => Promise<SdkResponse>;
188
- finish: (transactionId: string, response: string) => Promise<SdkResponse>;
242
+ start: (identifier: string, origin: string, token: string) => Promise<SdkResponse<ResponseData>>;
243
+ finish: (transactionId: string, response: string) => Promise<SdkResponse<ResponseData>>;
189
244
  };
190
245
  };
191
246
  flow: {
192
- start: (flowId: string) => Promise<SdkResponse>;
193
- next: (executionId: string, stepId: string, interactionId: string, input?: Record<string, FormDataEntryValue>) => Promise<SdkResponse>;
247
+ start: (flowId: string) => Promise<SdkResponse<ResponseData>>; /** Descope SDK client */
248
+ next: (executionId: string, stepId: string, interactionId: string, input?: Record<string, FormDataEntryValue>) => Promise<SdkResponse<ResponseData>>;
194
249
  };
195
- refresh: (token?: string) => Promise<SdkResponse>;
196
- logout: (token?: string) => Promise<SdkResponse>;
250
+ refresh: (token?: string) => Promise<SdkResponse<SdkResponse<JWTResponse>>>;
251
+ logout: (token?: string) => Promise<SdkResponse<SdkResponse<never>>>;
252
+ me: (token?: string) => Promise<SdkResponse<SdkResponse<UserResponse>>>;
197
253
  isJwtExpired: (token: string) => boolean;
198
254
  httpClient: HttpClient;
199
255
  }) & {
200
256
  DeliveryMethods: typeof DeliveryMethods;
201
257
  };
202
258
 
259
+ /** Type to restrict to valid delivery methods */
203
260
  declare type DeliveryMethod = keyof typeof DeliveryMethods;
261
+ /** Type to restrict to valid OAuth providers */
204
262
  declare type OAuthProvider = keyof typeof OAuthProviders;
205
263
 
206
- export { AuthInfo, DeliveryMethod, HTTPMethods, OAuthProvider, SdkResponse, sdkWithAttributes as default };
264
+ export { DeliveryMethod, HTTPMethods, JWTResponse, OAuthProvider, PendingRefResponse, ResponseData, SdkResponse, TOTPResponse, URLResponse, UserResponse, sdkWithAttributes as default };
package/dist/index.esm.js CHANGED
@@ -1,2 +1,2 @@
1
- import e from"jwt-decode";import t from"lodash.get";var 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"},n={verify:"/v1/auth/magiclink/verify",signIn:"/v1/auth/magiclink/signin",signUp:"/v1/auth/magiclink/signup",session:"/v1/auth/magiclink/pending-session",update:{email:"/v1/auth/magiclink/update/email",phone:"/v1/auth/magiclink/update/email"},signUpOrIn:"/v1/auth/magiclink/signup-in"},a={start:"/v1/auth/oauth/authorize"},i={start:"/v1/auth/saml/authorize"},r={verify:"/v1/auth/totp/verify",signUp:"/v1/auth/totp/signup",update:"/v1/user/totp/update"},o={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"},update:{start:"v1/auth/webauthn/update/start",finish:"/v1/auth/webauthn/update/finish"}},u="/v1/auth/refresh",p="/v1/auth/logoutall",c={start:"/v1/flow/start",next:"/v1/flow/next"},d="/v1/auth/exchange";var l;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT"}(l||(l={}));const g=()=>{const e={};return{headers(t){const s="function"==typeof t.entries?Object.fromEntries(t.entries()):t;return e.Headers=JSON.stringify(s),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")}},h=(e,t)=>{const s=t||fetch;if(!s)throw new Error("fetch is not defined");return e?async(...t)=>{e.log((e=>g().title("Request").url(e[0]).method(e[1].method).headers(e[1].headers).body(e[1].body).build())(t));const n=await s(...t);return e[n.ok?"log":"error"](await(async e=>{const t=await e.text();return e.text=()=>Promise.resolve(t),e.json=()=>Promise.resolve(JSON.parse(t)),g().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(n)),n}:s},m=(...e)=>new Headers(e.reduce(((e,t)=>{const s=(e=>Array.isArray(e)?e:e instanceof Headers?Array.from(e.entries()):e?Object.entries(e):[])(t);return s.reduce(((t,[s,n])=>(e[s]=n,e)),e),e}),{})),v=e=>void 0===e?void 0:JSON.stringify(e),b=(e,t="")=>{let s=e;return""!==t&&(s=s+":"+t),{Authorization:`Bearer ${s}`}},f=({baseUrl:e,projectId:t,baseConfig:s,logger:n,hooks:a,cookiePolicy:i})=>{const r=h(n),o=async n=>{const o=(null==a?void 0:a.beforeRequest)?a.beforeRequest(n):n,{path:u,body:p,headers:c,queryParams:d,method:l,token:g}=o,h=await r((({path:e,baseUrl:t,queryParams:s})=>{const n=new URL(e,t);return s&&(n.search=new URLSearchParams(s).toString()),n})({path:u,baseUrl:e,queryParams:d}),{headers:m(b(t,g),(null==s?void 0:s.baseHeaders)||{},c),method:l,body:v(p),credentials:i||"include"});return(null==a?void 0:a.afterRequest)&&a.afterRequest(n,null==h?void 0:h.clone()),h};return{get:(e,{headers:t,queryParams:s,token:n}={})=>o({path:e,headers:t,queryParams:s,body:void 0,method:l.get,token:n}),post:(e,t,{headers:s,queryParams:n,token:a}={})=>o({path:e,headers:s,queryParams:n,body:t,method:l.post,token:a}),put:(e,t,{headers:s,queryParams:n,token:a}={})=>o({path:e,headers:s,queryParams:n,body:t,method:l.put,token:a}),delete:(e,t,{headers:s,queryParams:n,token:a}={})=>o({path:e,headers:s,queryParams:n,body:t,method:l.delete,token:a})}},I=t=>{if("string"!=typeof t||!t)throw new Error("Invalid token provided");const{exp:s}=e(t);return(new Date).getTime()/1e3>s},y=(...e)=>e.join("/").replace(/\/{2,}/g,"/"),k=async e=>{const t=await e,s={code:t.status,ok:t.ok,response:t},n=await t.json();return t.ok?s.data=n:s.error=n,s};var j,O,U;!function(e){e.sms="sms",e.whatsapp="whatsapp"}(j||(j={})),function(e){e.email="email",e.sms="sms",e.whatsapp="whatsapp"}(O||(O={})),function(e){e.signUp="signup",e.signIn="signin",e.verify="verify"}(U||(U={}));const w=(e,t)=>(s=t)=>t=>!e(t)&&s.replace("{val}",t),x=(...e)=>({validate:t=>(e.forEach((e=>{const s=e(t);if(s)throw new Error(s)})),!0)}),P=e=>t=>e.test(t),R=P(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),q=P(/^\+[1-9]{1}[0-9]{3,14}$/),$=w(R,'"{val}" is not a valid email'),M=w(q,'"{val}" is not a valid phone number'),E=w((S=1,e=>e.length>=S),"Minimum length is 1");var S;const T=w((e=>"string"==typeof e),"Input is not a string"),D=(...e)=>t=>(...s)=>(e.forEach(((e,t)=>x(...e).validate(s[t]))),t(...s)),A=e=>[T(`"${e}" must be a string`),E(`"${e}" must not be empty`)],z=e=>[T(`"${e}" must be a string`),$()],L=e=>[T(`"${e}" must be a string`),M()];var H;!function(e){e.signUp="signup",e.signIn="signin",e.verify="verify",e.updatePhone="updatePhone"}(H||(H={}));const J=A("identifier"),N=D(J,A("code")),Z=D(J),B=D(J,L("phone")),C=D(J,z("email")),F=e=>({verify:Object.keys(O).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:N(((t,a)=>k(e.post(y(s.verify,n),{code:a,externalId:t}))))})),{}),signIn:Object.keys(O).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Z((t=>k(e.post(y(s.signIn,n),{externalId:t}))))})),{}),signUp:Object.keys(O).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Z(((t,a)=>k(e.post(y(s.signUp,n),{externalId:t,user:a}))))})),{}),signUpOrIn:Object.keys(O).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:Z((t=>k(e.post(y(s.signUpOrIn,n),{externalId:t}))))})),{}),update:{email:C(((t,n,a)=>k(e.post(s.update.email,{externalId:t,email:n},{token:a})))),phone:Object.keys(j).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:B(((t,a,i)=>k(e.post(y(s.update.phone,n),{externalId:t,phone:a},{token:i}))))})),{})}}),G=A("identifier"),_=A("uri"),K=D(A("token")),Q=D(G,_),V=D(A("pendingRef")),W=D(G,L("phone"),_),X=D(G,z("email"),_),Y=e=>({verify:K((t=>k(e.post(n.verify,{token:t})))),signIn:Object.keys(O).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Q(((t,a)=>k(e.post(y(n.signIn,s),{externalId:t,URI:a,crossDevice:!0}))))})),{}),signUpOrIn:Object.keys(O).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Q(((t,a)=>k(e.post(y(n.signUpOrIn,s),{externalId:t,URI:a,crossDevice:!0}))))})),{}),signUp:Object.keys(O).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Q(((t,a,i)=>k(e.post(y(n.signUp,s),{externalId:t,URI:a,user:i,crossDevice:!0}))))})),{}),waitForSession:V(((t,s)=>new Promise((a=>{const{pollingIntervalMs:i,timeoutMs:r}=(({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||1e3,1e3),timeoutMs:Math.min(t||6e5,6e5)}))(s);let o;const u=setInterval((async()=>{const s=await e.post(n.session,{pendingRef:t});s.ok&&(clearInterval(u),o&&clearTimeout(o),a(k(Promise.resolve(s))))}),i);o=setTimeout((()=>{a({error:{message:`Session polling timeout exceeded: ${r}ms`,code:"0"},ok:!1}),clearInterval(u)}),r)})))),update:{email:X(((t,s,a,i)=>k(e.post(n.update.email,{externalId:t,email:s,URI:a,crossDevice:!0},{token:i})))),phone:Object.keys(j).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:W(((t,a,i,r)=>k(e.post(y(n.update.phone,s),{externalId:t,phone:a,URI:i,crossDevice:!0},{token:r}))))})),{})}}),ee=e=>({verify:K((t=>k(e.post(n.verify,{token:t})))),signIn:Object.keys(O).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Q(((t,a)=>k(e.post(y(n.signIn,s),{externalId:t,URI:a}))))})),{}),signUp:Object.keys(O).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Q(((t,a,i)=>k(e.post(y(n.signUp,s),{externalId:t,URI:a,user:i}))))})),{}),signUpOrIn:Object.keys(O).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Q(((t,a)=>k(e.post(y(n.signUpOrIn,s),{externalId:t,URI:a}))))})),{}),update:{email:X(((t,s,a,i)=>k(e.post(n.update.email,{externalId:t,email:s,URI:a},{token:i})))),phone:Object.keys(j).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:W(((t,a,i,r)=>k(e.post(y(n.update.phone,s),{externalId:t,phone:a,URI:i},{token:r}))))})),{})},crossDevice:Y(e)}),te=D(A("code")),se=e=>({exchange:te((t=>k(e.get(d,{queryParams:{code:t}}))))});var ne;!function(e){e.facebook="facebook",e.github="github",e.google="google",e.microsoft="microsoft",e.gitlab="gitlab",e.apple="apple"}(ne||(ne={}));const ae=e=>Object.assign({start:Object.keys(ne).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:async(t,{redirect:n=!1}={})=>{const i=await e.get(a.start,{queryParams:Object.assign({provider:s},t&&{redirectURL:t})});if(!n||!i.ok)return k(Promise.resolve(i));const{url:r}=await i.json();window.location.href=r}})),{})},se(e)),ie=D(A("flowId")),re=D(A("executionId"),A("stepId"),A("interactionId")),oe=e=>({start:ie((t=>k(e.post(c.start,{flowId:t})))),next:re(((t,s,n,a)=>k(e.post(c.next,{executionId:t,stepId:s,interactionId:n,input:a}))))}),ue=D(A("tenant")),pe=e=>Object.assign({start:ue((async(t,s,{redirect:n=!1}={})=>{const a=await e.get(i.start,{queryParams:{tenant:t,redirectURL:s}});if(!n||!a.ok)return k(Promise.resolve(a));const{url:r}=await a.json();window.location.href=r}))},se(e)),ce=A("identifier"),de=D(ce,A("code")),le=D(ce),ge=D(ce),he=e=>({signUp:le(((t,s)=>k(e.post(r.signUp,{externalId:t,user:s})))),verify:de(((t,s)=>k(e.post(r.verify,{externalId:t,code:s})))),update:ge(((t,s)=>k(e.post(r.update,{externalId:t},{token:s}))))}),me=A("identifier"),ve=A("origin"),be=D(me,ve,A("name")),fe=D(me,ve),Ie=D(me,ve,A("token")),ye=D(A("transactionId"),A("response")),ke=e=>({signUp:{start:be(((t,s,n)=>k(e.post(o.signUp.start,{user:{externalId:t,name:n},origin:s})))),finish:ye(((t,s)=>k(e.post(o.signUp.finish,{transactionId:t,response:s}))))},signIn:{start:fe(((t,s)=>k(e.post(o.signIn.start,{externalId:t,origin:s})))),finish:ye(((t,s)=>k(e.post(o.signIn.finish,{transactionId:t,response:s}))))},update:{start:Ie(((t,s,n)=>k(e.post(o.update.start,{externalId:t,origin:s},{token:n})))),finish:ye(((t,s)=>k(e.post(o.update.finish,{transactionId:t,response:s}))))}}),je=D(A("token"));var Oe,Ue;const we=D([(Oe="projectId",Ue=A("projectId"),w(((e,s)=>n=>x(...s).validate(t(n,e)))(Oe,Ue))())])((({projectId:e,logger:t,baseUrl:s,hooks:n,cookiePolicy:a})=>{return i=f({baseUrl:s||"https://api.descope.com",projectId:e,logger:t,hooks:n,cookiePolicy:a}),{otp:F(i),magicLink:ee(i),oauth:ae(i),saml:pe(i),totp:he(i),webauthn:ke(i),flow:oe(i),refresh:e=>k(i.get(u,{token:e})),logout:e=>k(i.get(p,{token:e})),isJwtExpired:je(I),httpClient:i};var i}));we.DeliveryMethods=O;export{we as default};
1
+ import e from"jwt-decode";import t from"lodash.get";var s={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"},a={verify:"/v1/auth/magiclink/verify",signIn:"/v1/auth/magiclink/signin",signUp:"/v1/auth/magiclink/signup",session:"/v1/auth/magiclink/pending-session",update:{email:"/v1/auth/magiclink/update/email",phone:"/v1/auth/magiclink/update/email"},signUpOrIn:"/v1/auth/magiclink/signup-in"},i={start:"/v1/auth/oauth/authorize"},r={start:"/v1/auth/saml/authorize"},o={verify:"/v1/auth/totp/verify",signUp:"/v1/auth/totp/signup",update:"/v1/user/totp/update"},c={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"},update:{start:"v1/auth/webauthn/update/start",finish:"/v1/auth/webauthn/update/finish"}},u="/v1/auth/refresh",p="/v1/auth/logoutall",d="/v1/auth/me",g={start:"/v1/flow/start",next:"/v1/flow/next"},l="/v1/auth/exchange";var h;!function(e){e.get="GET",e.delete="DELETE",e.post="POST",e.put="PUT"}(h||(h={}));const m=()=>{const e={};return{headers(t){const s="function"==typeof t.entries?Object.fromEntries(t.entries()):t;return e.Headers=JSON.stringify(s),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")}},v=(e,t)=>{const s=t||fetch;if(!s)throw new Error("fetch is not defined");return e?async(...t)=>{e.log((e=>m().title("Request").url(e[0]).method(e[1].method).headers(e[1].headers).body(e[1].body).build())(t));const n=await s(...t);return e[n.ok?"log":"error"](await(async e=>{const t=await e.text();return e.text=()=>Promise.resolve(t),e.json=()=>Promise.resolve(JSON.parse(t)),m().title("Response").url(e.url.toString()).status(`${e.status} ${e.statusText}`).headers(e.headers).body(t).build()})(n)),n}:s},b=(...e)=>new Headers(e.reduce(((e,t)=>{const s=(e=>Array.isArray(e)?e:e instanceof Headers?Array.from(e.entries()):e?Object.entries(e):[])(t);return s.reduce(((t,[s,n])=>(e[s]=n,e)),e),e}),{})),f=e=>void 0===e?void 0:JSON.stringify(e),y=(e,t="")=>{let s=e;return""!==t&&(s=s+":"+t),{Authorization:`Bearer ${s}`}},I=({baseUrl:e,projectId:t,baseConfig:s,logger:n,hooks:a,cookiePolicy:i})=>{const r=v(n),o=async n=>{const o=(null==a?void 0:a.beforeRequest)?a.beforeRequest(n):n,{path:c,body:u,headers:p,queryParams:d,method:g,token:l}=o,h=await r((({path:e,baseUrl:t,queryParams:s})=>{const n=new URL(e,t);return s&&(n.search=new URLSearchParams(s).toString()),n})({path:c,baseUrl:e,queryParams:d}),{headers:b(y(t,l),(null==s?void 0:s.baseHeaders)||{},p),method:g,body:f(u),credentials:i||"include"});return(null==a?void 0:a.afterRequest)&&a.afterRequest(n,null==h?void 0:h.clone()),h};return{get:(e,{headers:t,queryParams:s,token:n}={})=>o({path:e,headers:t,queryParams:s,body:void 0,method:h.get,token:n}),post:(e,t,{headers:s,queryParams:n,token:a}={})=>o({path:e,headers:s,queryParams:n,body:t,method:h.post,token:a}),put:(e,t,{headers:s,queryParams:n,token:a}={})=>o({path:e,headers:s,queryParams:n,body:t,method:h.put,token:a}),delete:(e,t,{headers:s,queryParams:n,token:a}={})=>o({path:e,headers:s,queryParams:n,body:t,method:h.delete,token:a})}},k=t=>{if("string"!=typeof t||!t)throw new Error("Invalid token provided");const{exp:s}=e(t);return(new Date).getTime()/1e3>s},j=(...e)=>e.join("/").replace(/\/{2,}/g,"/");async function O(e){const t=await e,s={code:t.status,ok:t.ok,response:t},n=await t.json();return t.ok?s.data=n:s.error=n,s}const U=(e,t)=>(s=t)=>t=>!e(t)&&s.replace("{val}",t),w=(...e)=>({validate:t=>(e.forEach((e=>{const s=e(t);if(s)throw new Error(s)})),!0)}),x=e=>t=>e.test(t),P=x(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/),R=x(/^\+[1-9]{1}[0-9]{3,14}$/),q=U(P,'"{val}" is not a valid email'),$=U(R,'"{val}" is not a valid phone number'),M=U((E=1,e=>e.length>=E),"Minimum length is 1");var E;const S=U((e=>"string"==typeof e),"Input is not a string"),T=(...e)=>t=>(...s)=>(e.forEach(((e,t)=>w(...e).validate(s[t]))),t(...s)),D=e=>[S(`"${e}" must be a string`),M(`"${e}" must not be empty`)],A=e=>[S(`"${e}" must be a string`),q()],z=e=>[S(`"${e}" must be a string`),$()],L=T(D("accessKey")),H=e=>({exchange:L((t=>O(e.get(s.exchange,{token:t}))))});var J,N,Z,B;!function(e){e.sms="sms",e.whatsapp="whatsapp"}(J||(J={})),function(e){e.email="email",e.sms="sms",e.whatsapp="whatsapp"}(N||(N={})),function(e){e.signUp="signup",e.signIn="signin",e.verify="verify"}(Z||(Z={})),function(e){e.signUp="signup",e.signIn="signin",e.verify="verify",e.updatePhone="updatePhone"}(B||(B={}));const C=D("identifier"),K=T(C,D("code")),F=T(C),G=T(C,z("phone")),_=T(C,A("email")),Q=e=>({verify:Object.keys(N).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:K(((t,a)=>O(e.post(j(n.verify,s),{code:a,externalId:t}))))})),{}),signIn:Object.keys(N).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:F((t=>O(e.post(j(n.signIn,s),{externalId:t}))))})),{}),signUp:Object.keys(N).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:F(((t,a)=>O(e.post(j(n.signUp,s),{externalId:t,user:a}))))})),{}),signUpOrIn:Object.keys(N).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:F((t=>O(e.post(j(n.signUpOrIn,s),{externalId:t}))))})),{}),update:{email:_(((t,s,a)=>O(e.post(n.update.email,{externalId:t,email:s},{token:a})))),phone:Object.keys(J).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:G(((t,a,i)=>O(e.post(j(n.update.phone,s),{externalId:t,phone:a},{token:i}))))})),{})}}),V=D("identifier"),W=D("uri"),X=T(D("token")),Y=T(V,W),ee=T(D("pendingRef")),te=T(V,z("phone"),W),se=T(V,A("email"),W),ne=e=>({verify:X((t=>O(e.post(a.verify,{token:t})))),signIn:Object.keys(N).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Y(((t,n)=>O(e.post(j(a.signIn,s),{externalId:t,URI:n,crossDevice:!0}))))})),{}),signUpOrIn:Object.keys(N).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Y(((t,n)=>O(e.post(j(a.signUpOrIn,s),{externalId:t,URI:n,crossDevice:!0}))))})),{}),signUp:Object.keys(N).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Y(((t,n,i)=>O(e.post(j(a.signUp,s),{externalId:t,URI:n,user:i,crossDevice:!0}))))})),{}),waitForSession:ee(((t,s)=>new Promise((n=>{const{pollingIntervalMs:i,timeoutMs:r}=(({pollingIntervalMs:e=1e3,timeoutMs:t=6e5}={})=>({pollingIntervalMs:Math.max(e||1e3,1e3),timeoutMs:Math.min(t||6e5,6e5)}))(s);let o;const c=setInterval((async()=>{const s=await e.post(a.session,{pendingRef:t});s.ok&&(clearInterval(c),o&&clearTimeout(o),n(O(Promise.resolve(s))))}),i);o=setTimeout((()=>{n({error:{message:`Session polling timeout exceeded: ${r}ms`,code:"0"},ok:!1}),clearInterval(c)}),r)})))),update:{email:se(((t,s,n,i)=>O(e.post(a.update.email,{externalId:t,email:s,URI:n,crossDevice:!0},{token:i})))),phone:Object.keys(J).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:te(((t,n,i,r)=>O(e.post(j(a.update.phone,s),{externalId:t,phone:n,URI:i,crossDevice:!0},{token:r}))))})),{})}}),ae=e=>({verify:X((t=>O(e.post(a.verify,{token:t})))),signIn:Object.keys(N).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Y(((t,n)=>O(e.post(j(a.signIn,s),{externalId:t,URI:n}))))})),{}),signUp:Object.keys(N).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Y(((t,n,i)=>O(e.post(j(a.signUp,s),{externalId:t,URI:n,user:i}))))})),{}),signUpOrIn:Object.keys(N).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:Y(((t,n)=>O(e.post(j(a.signUpOrIn,s),{externalId:t,URI:n}))))})),{}),update:{email:se(((t,s,n,i)=>O(e.post(a.update.email,{externalId:t,email:s,URI:n},{token:i})))),phone:Object.keys(J).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:te(((t,n,i,r)=>O(e.post(j(a.update.phone,s),{externalId:t,phone:n,URI:i},{token:r}))))})),{})},crossDevice:ne(e)}),ie=T(D("code")),re=e=>({exchange:ie((t=>O(e.get(l,{queryParams:{code:t}}))))});var oe;!function(e){e.facebook="facebook",e.github="github",e.google="google",e.microsoft="microsoft",e.gitlab="gitlab",e.apple="apple"}(oe||(oe={}));const ce=e=>Object.assign({start:Object.keys(oe).reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:async(t,{redirect:n=!1}={})=>{const a=await e.get(i.start,{queryParams:Object.assign({provider:s},t&&{redirectURL:t})});if(!n||!a.ok)return O(Promise.resolve(a));const{url:r}=await a.json();window.location.href=r}})),{})},re(e)),ue=T(D("flowId")),pe=T(D("executionId"),D("stepId"),D("interactionId")),de=e=>({start:ue((t=>O(e.post(g.start,{flowId:t})))),next:pe(((t,s,n,a)=>O(e.post(g.next,{executionId:t,stepId:s,interactionId:n,input:a}))))}),ge=T(D("tenant")),le=e=>Object.assign({start:ge((async(t,s,{redirect:n=!1}={})=>{const a=await e.get(r.start,{queryParams:{tenant:t,redirectURL:s}});if(!n||!a.ok)return O(Promise.resolve(a));const{url:i}=await a.json();window.location.href=i}))},re(e)),he=D("identifier"),me=T(he,D("code")),ve=T(he),be=T(he),fe=e=>({signUp:ve(((t,s)=>O(e.post(o.signUp,{externalId:t,user:s})))),verify:me(((t,s)=>O(e.post(o.verify,{externalId:t,code:s})))),update:be(((t,s)=>O(e.post(o.update,{externalId:t},{token:s}))))}),ye=D("identifier"),Ie=D("origin"),ke=T(ye,Ie,D("name")),je=T(ye,Ie),Oe=T(ye,Ie,D("token")),Ue=T(D("transactionId"),D("response")),we=e=>({signUp:{start:ke(((t,s,n)=>O(e.post(c.signUp.start,{user:{externalId:t,name:n},origin:s})))),finish:Ue(((t,s)=>O(e.post(c.signUp.finish,{transactionId:t,response:s}))))},signIn:{start:je(((t,s)=>O(e.post(c.signIn.start,{externalId:t,origin:s})))),finish:Ue(((t,s)=>O(e.post(c.signIn.finish,{transactionId:t,response:s}))))},update:{start:Oe(((t,s,n)=>O(e.post(c.update.start,{externalId:t,origin:s},{token:n})))),finish:Ue(((t,s)=>O(e.post(c.update.finish,{transactionId:t,response:s}))))}}),xe=T(D("token"));var Pe,Re;const qe=T([(Pe="projectId",Re=D("projectId"),U(((e,s)=>n=>w(...s).validate(t(n,e)))(Pe,Re))())])((({projectId:e,logger:t,baseUrl:s,hooks:n,cookiePolicy:a})=>{return i=I({baseUrl:s||"https://api.descope.com",projectId:e,logger:t,hooks:n,cookiePolicy:a}),{accessKey:H(i),otp:Q(i),magicLink:ae(i),oauth:ce(i),saml:le(i),totp:fe(i),webauthn:we(i),flow:de(i),refresh:e=>O(i.get(u,{token:e})),logout:e=>O(i.get(p,{token:e})),me:e=>O(i.get(d,{token:e})),isJwtExpired:xe(k),httpClient:i};var i}));qe.DeliveryMethods=N;export{qe as default};
2
2
  //# sourceMappingURL=index.esm.js.map