@anzar-auth/client 1.3.6

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.js ADDED
@@ -0,0 +1,919 @@
1
+ import {
2
+ MemoryStorage
3
+ } from "./chunk-5SSEPZKA.js";
4
+
5
+ // src/index.ts
6
+ import axios2 from "axios";
7
+
8
+ // src/api/auth.ts
9
+ import axios from "axios";
10
+ var AuthModule = class {
11
+ constructor(authApi, userApi, strategy, options) {
12
+ this.authApi = authApi;
13
+ this.userApi = userApi;
14
+ this.strategy = strategy;
15
+ this.options = options;
16
+ }
17
+ async login(body) {
18
+ const response = await this.authApi.login(body);
19
+ const auth_response = response.data;
20
+ if (auth_response.tokens) {
21
+ this.options?.storage.onTokenRefresh?.(auth_response.tokens);
22
+ }
23
+ return {
24
+ user: auth_response.user,
25
+ verification: auth_response.verification
26
+ };
27
+ }
28
+ async register(body) {
29
+ const response = await this.authApi.register(body);
30
+ const auth_response = response.data;
31
+ if (auth_response.tokens) {
32
+ this.options?.storage.onTokenRefresh?.(auth_response.tokens);
33
+ }
34
+ return {
35
+ user: auth_response.user,
36
+ verification: auth_response.verification
37
+ };
38
+ }
39
+ async logout() {
40
+ const refreshToken = this.options?.storage.getRefreshToken();
41
+ await this.authApi.logout({ refresh_token: refreshToken ?? "" });
42
+ this.options?.storage.onLogout?.();
43
+ }
44
+ resetPassword(body) {
45
+ this.authApi.requestPasswordReset(body);
46
+ }
47
+ async isAuthenticated() {
48
+ try {
49
+ const response = await this.userApi.findUser();
50
+ return !!response.data.username;
51
+ } catch (error) {
52
+ if (axios.isAxiosError(error) && error.response?.status === 401) {
53
+ return false;
54
+ }
55
+ throw false;
56
+ }
57
+ }
58
+ async session() {
59
+ if (this.strategy !== "Session" /* Session */) {
60
+ throw new Error("session() is only available with Session auth strategy");
61
+ }
62
+ const response = await this.authApi.getSession();
63
+ return response.data;
64
+ }
65
+ };
66
+
67
+ // src/api/interceptor.ts
68
+ var JwtInterceptor = class {
69
+ isRefreshing = false;
70
+ waitingQueue = [];
71
+ drainQueue(token) {
72
+ this.waitingQueue.forEach(({ resolve }) => resolve(token));
73
+ this.waitingQueue = [];
74
+ }
75
+ rejectQueue(error) {
76
+ this.waitingQueue.forEach(({ reject }) => reject(error));
77
+ this.waitingQueue = [];
78
+ }
79
+ apply(axiosInstance, options) {
80
+ const REFRESH_URI = "/auth/refresh-token";
81
+ axiosInstance.interceptors.request.use(
82
+ (config) => {
83
+ const accessToken = options?.storage.getToken();
84
+ if (!config.url?.includes(REFRESH_URI) && accessToken) {
85
+ config.headers.Authorization = `Bearer ${accessToken}`;
86
+ }
87
+ return config;
88
+ },
89
+ (error) => {
90
+ return Promise.reject(error);
91
+ }
92
+ );
93
+ axiosInstance.interceptors.response.use(
94
+ (response) => response,
95
+ async (error) => {
96
+ const originalRequest = error.config;
97
+ if (error.response?.status !== 401 || originalRequest._retry || originalRequest.url?.includes(REFRESH_URI)) {
98
+ return Promise.reject(error);
99
+ }
100
+ if (this.isRefreshing) {
101
+ return new Promise((resolve, reject) => {
102
+ this.waitingQueue.push({ resolve, reject });
103
+ }).then((newToken) => {
104
+ originalRequest.headers.Authorization = `Bearer ${newToken}`;
105
+ return axiosInstance(originalRequest);
106
+ });
107
+ }
108
+ originalRequest._retry = true;
109
+ this.isRefreshing = true;
110
+ try {
111
+ const refreshToken = options?.storage.getRefreshToken();
112
+ if (!refreshToken) return Promise.reject(error);
113
+ const response = await axiosInstance.post(REFRESH_URI, {
114
+ refresh_token: refreshToken
115
+ });
116
+ const auth_response = response.data;
117
+ if (auth_response.tokens) {
118
+ options?.storage.onTokenRefresh?.(auth_response.tokens);
119
+ }
120
+ this.drainQueue(auth_response.tokens?.access);
121
+ originalRequest.headers.Authorization = `Bearer ${auth_response.tokens?.access}`;
122
+ return axiosInstance(originalRequest);
123
+ } catch (e) {
124
+ this.rejectQueue(e);
125
+ options?.storage.onSessionExpired?.();
126
+ return Promise.reject(e);
127
+ } finally {
128
+ this.isRefreshing = false;
129
+ }
130
+ }
131
+ );
132
+ }
133
+ };
134
+
135
+ // src/generated/api/auth-api.ts
136
+ import globalAxios2 from "axios";
137
+
138
+ // src/generated/base.ts
139
+ import globalAxios from "axios";
140
+ var BASE_PATH = "http://localhost".replace(/\/+$/, "");
141
+ var BaseAPI = class {
142
+ constructor(configuration, basePath = BASE_PATH, axios3 = globalAxios) {
143
+ this.basePath = basePath;
144
+ this.axios = axios3;
145
+ if (configuration) {
146
+ this.configuration = configuration;
147
+ this.basePath = configuration.basePath ?? basePath;
148
+ }
149
+ }
150
+ configuration;
151
+ };
152
+ var RequiredError = class extends Error {
153
+ constructor(field, msg) {
154
+ super(msg);
155
+ this.field = field;
156
+ this.name = "RequiredError";
157
+ }
158
+ };
159
+ var operationServerMap = {};
160
+
161
+ // src/generated/common.ts
162
+ var DUMMY_BASE_URL = "https://example.com";
163
+ var assertParamExists = function(functionName, paramName, paramValue) {
164
+ if (paramValue === null || paramValue === void 0) {
165
+ throw new RequiredError(paramName, `Required parameter ${paramName} was null or undefined when calling ${functionName}.`);
166
+ }
167
+ };
168
+ var setBearerAuthToObject = async function(object, configuration) {
169
+ if (configuration && configuration.accessToken) {
170
+ const accessToken = typeof configuration.accessToken === "function" ? await configuration.accessToken() : await configuration.accessToken;
171
+ object["Authorization"] = "Bearer " + accessToken;
172
+ }
173
+ };
174
+ function setFlattenedQueryParams(urlSearchParams, parameter, key = "") {
175
+ if (parameter == null) return;
176
+ if (typeof parameter === "object") {
177
+ if (Array.isArray(parameter) || parameter instanceof Set) {
178
+ parameter.forEach((item) => setFlattenedQueryParams(urlSearchParams, item, key));
179
+ } else {
180
+ Object.keys(parameter).forEach(
181
+ (currentKey) => setFlattenedQueryParams(urlSearchParams, parameter[currentKey], `${key}${key !== "" ? "." : ""}${currentKey}`)
182
+ );
183
+ }
184
+ } else {
185
+ if (urlSearchParams.has(key)) {
186
+ urlSearchParams.append(key, parameter);
187
+ } else {
188
+ urlSearchParams.set(key, parameter);
189
+ }
190
+ }
191
+ }
192
+ var setSearchParams = function(url, ...objects) {
193
+ const searchParams = new URLSearchParams(url.search);
194
+ setFlattenedQueryParams(searchParams, objects);
195
+ url.search = searchParams.toString();
196
+ };
197
+ var replaceWithSerializableTypeIfNeeded = function(key, value) {
198
+ if (value instanceof Set) {
199
+ return Array.from(value);
200
+ } else {
201
+ return value;
202
+ }
203
+ };
204
+ var serializeDataIfNeeded = function(value, requestOptions, configuration) {
205
+ const nonString = typeof value !== "string";
206
+ const needsSerialization = nonString && configuration && configuration.isJsonMime ? configuration.isJsonMime(requestOptions.headers["Content-Type"]) : nonString;
207
+ return needsSerialization ? JSON.stringify(value !== void 0 ? value : {}, replaceWithSerializableTypeIfNeeded) : value || "";
208
+ };
209
+ var toPathString = function(url) {
210
+ return url.pathname + url.search + url.hash;
211
+ };
212
+ var createRequestFunction = function(axiosArgs, globalAxios5, BASE_PATH2, configuration) {
213
+ return (axios3 = globalAxios5, basePath = BASE_PATH2) => {
214
+ const axiosRequestArgs = { ...axiosArgs.options, url: (axios3.defaults.baseURL ? "" : configuration?.basePath ?? basePath) + axiosArgs.url };
215
+ return axios3.request(axiosRequestArgs);
216
+ };
217
+ };
218
+
219
+ // src/generated/api/auth-api.ts
220
+ var AuthApiAxiosParamCreator = function(configuration) {
221
+ return {
222
+ /**
223
+ * Returns the currently authenticated user\'s session data. Requires a valid Bearer token.
224
+ * @summary Get current session
225
+ * @param {*} [options] Override http request option.
226
+ * @throws {RequiredError}
227
+ */
228
+ getSession: async (options = {}) => {
229
+ const localVarPath = `/auth/session`;
230
+ const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
231
+ let baseOptions;
232
+ if (configuration) {
233
+ baseOptions = configuration.baseOptions;
234
+ }
235
+ const localVarRequestOptions = { method: "GET", ...baseOptions, ...options };
236
+ const localVarHeaderParameter = {};
237
+ const localVarQueryParameter = {};
238
+ await setBearerAuthToObject(localVarHeaderParameter, configuration);
239
+ localVarHeaderParameter["Accept"] = "application/json";
240
+ setSearchParams(localVarUrlObj, localVarQueryParameter);
241
+ let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
242
+ localVarRequestOptions.headers = { ...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers };
243
+ return {
244
+ url: toPathString(localVarUrlObj),
245
+ options: localVarRequestOptions
246
+ };
247
+ },
248
+ /**
249
+ * Authenticates a user with email and password. Returns an access token and refresh token on success.
250
+ * @summary User login
251
+ * @param {LoginRequest} loginRequest User login credentials
252
+ * @param {*} [options] Override http request option.
253
+ * @throws {RequiredError}
254
+ */
255
+ login: async (loginRequest, options = {}) => {
256
+ assertParamExists("login", "loginRequest", loginRequest);
257
+ const localVarPath = `/auth/login`;
258
+ const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
259
+ let baseOptions;
260
+ if (configuration) {
261
+ baseOptions = configuration.baseOptions;
262
+ }
263
+ const localVarRequestOptions = { method: "POST", ...baseOptions, ...options };
264
+ const localVarHeaderParameter = {};
265
+ const localVarQueryParameter = {};
266
+ localVarHeaderParameter["Content-Type"] = "application/json";
267
+ localVarHeaderParameter["Accept"] = "application/json";
268
+ setSearchParams(localVarUrlObj, localVarQueryParameter);
269
+ let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
270
+ localVarRequestOptions.headers = { ...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers };
271
+ localVarRequestOptions.data = serializeDataIfNeeded(loginRequest, localVarRequestOptions, configuration);
272
+ return {
273
+ url: toPathString(localVarUrlObj),
274
+ options: localVarRequestOptions
275
+ };
276
+ },
277
+ /**
278
+ * Invalidates the current session and refresh token. The client should discard stored tokens.
279
+ * @summary Logout
280
+ * @param {RefreshTokenRequest} refreshTokenRequest
281
+ * @param {*} [options] Override http request option.
282
+ * @throws {RequiredError}
283
+ */
284
+ logout: async (refreshTokenRequest, options = {}) => {
285
+ assertParamExists("logout", "refreshTokenRequest", refreshTokenRequest);
286
+ const localVarPath = `/auth/logout`;
287
+ const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
288
+ let baseOptions;
289
+ if (configuration) {
290
+ baseOptions = configuration.baseOptions;
291
+ }
292
+ const localVarRequestOptions = { method: "POST", ...baseOptions, ...options };
293
+ const localVarHeaderParameter = {};
294
+ const localVarQueryParameter = {};
295
+ await setBearerAuthToObject(localVarHeaderParameter, configuration);
296
+ localVarHeaderParameter["Content-Type"] = "application/json";
297
+ localVarHeaderParameter["Accept"] = "application/json";
298
+ setSearchParams(localVarUrlObj, localVarQueryParameter);
299
+ let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
300
+ localVarRequestOptions.headers = { ...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers };
301
+ localVarRequestOptions.data = serializeDataIfNeeded(refreshTokenRequest, localVarRequestOptions, configuration);
302
+ return {
303
+ url: toPathString(localVarUrlObj),
304
+ options: localVarRequestOptions
305
+ };
306
+ },
307
+ /**
308
+ * Issues a new access token using a valid refresh token. Rotate refresh tokens on each call.
309
+ * @summary Refresh access token
310
+ * @param {RefreshTokenRequest} refreshTokenRequest
311
+ * @param {*} [options] Override http request option.
312
+ * @throws {RequiredError}
313
+ */
314
+ refreshToken: async (refreshTokenRequest, options = {}) => {
315
+ assertParamExists("refreshToken", "refreshTokenRequest", refreshTokenRequest);
316
+ const localVarPath = `/auth/refresh-token`;
317
+ const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
318
+ let baseOptions;
319
+ if (configuration) {
320
+ baseOptions = configuration.baseOptions;
321
+ }
322
+ const localVarRequestOptions = { method: "POST", ...baseOptions, ...options };
323
+ const localVarHeaderParameter = {};
324
+ const localVarQueryParameter = {};
325
+ await setBearerAuthToObject(localVarHeaderParameter, configuration);
326
+ localVarHeaderParameter["Content-Type"] = "application/json";
327
+ localVarHeaderParameter["Accept"] = "application/json";
328
+ setSearchParams(localVarUrlObj, localVarQueryParameter);
329
+ let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
330
+ localVarRequestOptions.headers = { ...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers };
331
+ localVarRequestOptions.data = serializeDataIfNeeded(refreshTokenRequest, localVarRequestOptions, configuration);
332
+ return {
333
+ url: toPathString(localVarUrlObj),
334
+ options: localVarRequestOptions
335
+ };
336
+ },
337
+ /**
338
+ * Creates a new user account. Sends a verification email upon successful registration.
339
+ * @summary Register a new user
340
+ * @param {RegisterRequest} registerRequest User register credentials
341
+ * @param {*} [options] Override http request option.
342
+ * @throws {RequiredError}
343
+ */
344
+ register: async (registerRequest, options = {}) => {
345
+ assertParamExists("register", "registerRequest", registerRequest);
346
+ const localVarPath = `/auth/register`;
347
+ const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
348
+ let baseOptions;
349
+ if (configuration) {
350
+ baseOptions = configuration.baseOptions;
351
+ }
352
+ const localVarRequestOptions = { method: "POST", ...baseOptions, ...options };
353
+ const localVarHeaderParameter = {};
354
+ const localVarQueryParameter = {};
355
+ localVarHeaderParameter["Content-Type"] = "application/json";
356
+ localVarHeaderParameter["Accept"] = "application/json";
357
+ setSearchParams(localVarUrlObj, localVarQueryParameter);
358
+ let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
359
+ localVarRequestOptions.headers = { ...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers };
360
+ localVarRequestOptions.data = serializeDataIfNeeded(registerRequest, localVarRequestOptions, configuration);
361
+ return {
362
+ url: toPathString(localVarUrlObj),
363
+ options: localVarRequestOptions
364
+ };
365
+ },
366
+ /**
367
+ * Validates the reset token from the email link and renders the password reset form.
368
+ * @summary Render password reset form
369
+ * @param {TokenQuery} token Password reset token
370
+ * @param {*} [options] Override http request option.
371
+ * @throws {RequiredError}
372
+ */
373
+ renderResetForm: async (token, options = {}) => {
374
+ assertParamExists("renderResetForm", "token", token);
375
+ const localVarPath = `/auth/password/reset`;
376
+ const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
377
+ let baseOptions;
378
+ if (configuration) {
379
+ baseOptions = configuration.baseOptions;
380
+ }
381
+ const localVarRequestOptions = { method: "GET", ...baseOptions, ...options };
382
+ const localVarHeaderParameter = {};
383
+ const localVarQueryParameter = {};
384
+ if (token !== void 0) {
385
+ for (const [key, value] of Object.entries(token)) {
386
+ localVarQueryParameter[key] = value;
387
+ }
388
+ }
389
+ localVarHeaderParameter["Accept"] = "text/html,application/json";
390
+ setSearchParams(localVarUrlObj, localVarQueryParameter);
391
+ let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
392
+ localVarRequestOptions.headers = { ...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers };
393
+ return {
394
+ url: toPathString(localVarUrlObj),
395
+ options: localVarRequestOptions
396
+ };
397
+ },
398
+ /**
399
+ * Sends a password reset link to the provided email address if an account exists.
400
+ * @summary Request a password reset
401
+ * @param {EmailRequest} emailRequest Email address to send the reset link to
402
+ * @param {*} [options] Override http request option.
403
+ * @throws {RequiredError}
404
+ */
405
+ requestPasswordReset: async (emailRequest, options = {}) => {
406
+ assertParamExists("requestPasswordReset", "emailRequest", emailRequest);
407
+ const localVarPath = `/auth/password/forgot`;
408
+ const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
409
+ let baseOptions;
410
+ if (configuration) {
411
+ baseOptions = configuration.baseOptions;
412
+ }
413
+ const localVarRequestOptions = { method: "POST", ...baseOptions, ...options };
414
+ const localVarHeaderParameter = {};
415
+ const localVarQueryParameter = {};
416
+ localVarHeaderParameter["Content-Type"] = "application/json";
417
+ localVarHeaderParameter["Accept"] = "application/json";
418
+ setSearchParams(localVarUrlObj, localVarQueryParameter);
419
+ let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
420
+ localVarRequestOptions.headers = { ...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers };
421
+ localVarRequestOptions.data = serializeDataIfNeeded(emailRequest, localVarRequestOptions, configuration);
422
+ return {
423
+ url: toPathString(localVarUrlObj),
424
+ options: localVarRequestOptions
425
+ };
426
+ },
427
+ /**
428
+ * Submits a new password using a valid reset token. Invalidates the token after use.
429
+ * @summary Submit new password
430
+ * @param {ResetPasswordRequest} resetPasswordRequest Reset token and the new password
431
+ * @param {*} [options] Override http request option.
432
+ * @throws {RequiredError}
433
+ */
434
+ submitNewPassword: async (resetPasswordRequest, options = {}) => {
435
+ assertParamExists("submitNewPassword", "resetPasswordRequest", resetPasswordRequest);
436
+ const localVarPath = `/auth/password/reset`;
437
+ const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
438
+ let baseOptions;
439
+ if (configuration) {
440
+ baseOptions = configuration.baseOptions;
441
+ }
442
+ const localVarRequestOptions = { method: "POST", ...baseOptions, ...options };
443
+ const localVarHeaderParameter = {};
444
+ const localVarQueryParameter = {};
445
+ localVarHeaderParameter["Content-Type"] = "application/json";
446
+ localVarHeaderParameter["Accept"] = "application/json";
447
+ setSearchParams(localVarUrlObj, localVarQueryParameter);
448
+ let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
449
+ localVarRequestOptions.headers = { ...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers };
450
+ localVarRequestOptions.data = serializeDataIfNeeded(resetPasswordRequest, localVarRequestOptions, configuration);
451
+ return {
452
+ url: toPathString(localVarUrlObj),
453
+ options: localVarRequestOptions
454
+ };
455
+ }
456
+ };
457
+ };
458
+ var AuthApiFp = function(configuration) {
459
+ const localVarAxiosParamCreator = AuthApiAxiosParamCreator(configuration);
460
+ return {
461
+ /**
462
+ * Returns the currently authenticated user\'s session data. Requires a valid Bearer token.
463
+ * @summary Get current session
464
+ * @param {*} [options] Override http request option.
465
+ * @throws {RequiredError}
466
+ */
467
+ async getSession(options) {
468
+ const localVarAxiosArgs = await localVarAxiosParamCreator.getSession(options);
469
+ const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
470
+ const localVarOperationServerBasePath = operationServerMap["AuthApi.getSession"]?.[localVarOperationServerIndex]?.url;
471
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
472
+ },
473
+ /**
474
+ * Authenticates a user with email and password. Returns an access token and refresh token on success.
475
+ * @summary User login
476
+ * @param {LoginRequest} loginRequest User login credentials
477
+ * @param {*} [options] Override http request option.
478
+ * @throws {RequiredError}
479
+ */
480
+ async login(loginRequest, options) {
481
+ const localVarAxiosArgs = await localVarAxiosParamCreator.login(loginRequest, options);
482
+ const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
483
+ const localVarOperationServerBasePath = operationServerMap["AuthApi.login"]?.[localVarOperationServerIndex]?.url;
484
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
485
+ },
486
+ /**
487
+ * Invalidates the current session and refresh token. The client should discard stored tokens.
488
+ * @summary Logout
489
+ * @param {RefreshTokenRequest} refreshTokenRequest
490
+ * @param {*} [options] Override http request option.
491
+ * @throws {RequiredError}
492
+ */
493
+ async logout(refreshTokenRequest, options) {
494
+ const localVarAxiosArgs = await localVarAxiosParamCreator.logout(refreshTokenRequest, options);
495
+ const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
496
+ const localVarOperationServerBasePath = operationServerMap["AuthApi.logout"]?.[localVarOperationServerIndex]?.url;
497
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
498
+ },
499
+ /**
500
+ * Issues a new access token using a valid refresh token. Rotate refresh tokens on each call.
501
+ * @summary Refresh access token
502
+ * @param {RefreshTokenRequest} refreshTokenRequest
503
+ * @param {*} [options] Override http request option.
504
+ * @throws {RequiredError}
505
+ */
506
+ async refreshToken(refreshTokenRequest, options) {
507
+ const localVarAxiosArgs = await localVarAxiosParamCreator.refreshToken(refreshTokenRequest, options);
508
+ const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
509
+ const localVarOperationServerBasePath = operationServerMap["AuthApi.refreshToken"]?.[localVarOperationServerIndex]?.url;
510
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
511
+ },
512
+ /**
513
+ * Creates a new user account. Sends a verification email upon successful registration.
514
+ * @summary Register a new user
515
+ * @param {RegisterRequest} registerRequest User register credentials
516
+ * @param {*} [options] Override http request option.
517
+ * @throws {RequiredError}
518
+ */
519
+ async register(registerRequest, options) {
520
+ const localVarAxiosArgs = await localVarAxiosParamCreator.register(registerRequest, options);
521
+ const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
522
+ const localVarOperationServerBasePath = operationServerMap["AuthApi.register"]?.[localVarOperationServerIndex]?.url;
523
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
524
+ },
525
+ /**
526
+ * Validates the reset token from the email link and renders the password reset form.
527
+ * @summary Render password reset form
528
+ * @param {TokenQuery} token Password reset token
529
+ * @param {*} [options] Override http request option.
530
+ * @throws {RequiredError}
531
+ */
532
+ async renderResetForm(token, options) {
533
+ const localVarAxiosArgs = await localVarAxiosParamCreator.renderResetForm(token, options);
534
+ const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
535
+ const localVarOperationServerBasePath = operationServerMap["AuthApi.renderResetForm"]?.[localVarOperationServerIndex]?.url;
536
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
537
+ },
538
+ /**
539
+ * Sends a password reset link to the provided email address if an account exists.
540
+ * @summary Request a password reset
541
+ * @param {EmailRequest} emailRequest Email address to send the reset link to
542
+ * @param {*} [options] Override http request option.
543
+ * @throws {RequiredError}
544
+ */
545
+ async requestPasswordReset(emailRequest, options) {
546
+ const localVarAxiosArgs = await localVarAxiosParamCreator.requestPasswordReset(emailRequest, options);
547
+ const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
548
+ const localVarOperationServerBasePath = operationServerMap["AuthApi.requestPasswordReset"]?.[localVarOperationServerIndex]?.url;
549
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
550
+ },
551
+ /**
552
+ * Submits a new password using a valid reset token. Invalidates the token after use.
553
+ * @summary Submit new password
554
+ * @param {ResetPasswordRequest} resetPasswordRequest Reset token and the new password
555
+ * @param {*} [options] Override http request option.
556
+ * @throws {RequiredError}
557
+ */
558
+ async submitNewPassword(resetPasswordRequest, options) {
559
+ const localVarAxiosArgs = await localVarAxiosParamCreator.submitNewPassword(resetPasswordRequest, options);
560
+ const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
561
+ const localVarOperationServerBasePath = operationServerMap["AuthApi.submitNewPassword"]?.[localVarOperationServerIndex]?.url;
562
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
563
+ }
564
+ };
565
+ };
566
+ var AuthApi = class extends BaseAPI {
567
+ /**
568
+ * Returns the currently authenticated user\'s session data. Requires a valid Bearer token.
569
+ * @summary Get current session
570
+ * @param {*} [options] Override http request option.
571
+ * @throws {RequiredError}
572
+ */
573
+ getSession(options) {
574
+ return AuthApiFp(this.configuration).getSession(options).then((request) => request(this.axios, this.basePath));
575
+ }
576
+ /**
577
+ * Authenticates a user with email and password. Returns an access token and refresh token on success.
578
+ * @summary User login
579
+ * @param {LoginRequest} loginRequest User login credentials
580
+ * @param {*} [options] Override http request option.
581
+ * @throws {RequiredError}
582
+ */
583
+ login(loginRequest, options) {
584
+ return AuthApiFp(this.configuration).login(loginRequest, options).then((request) => request(this.axios, this.basePath));
585
+ }
586
+ /**
587
+ * Invalidates the current session and refresh token. The client should discard stored tokens.
588
+ * @summary Logout
589
+ * @param {RefreshTokenRequest} refreshTokenRequest
590
+ * @param {*} [options] Override http request option.
591
+ * @throws {RequiredError}
592
+ */
593
+ logout(refreshTokenRequest, options) {
594
+ return AuthApiFp(this.configuration).logout(refreshTokenRequest, options).then((request) => request(this.axios, this.basePath));
595
+ }
596
+ /**
597
+ * Issues a new access token using a valid refresh token. Rotate refresh tokens on each call.
598
+ * @summary Refresh access token
599
+ * @param {RefreshTokenRequest} refreshTokenRequest
600
+ * @param {*} [options] Override http request option.
601
+ * @throws {RequiredError}
602
+ */
603
+ refreshToken(refreshTokenRequest, options) {
604
+ return AuthApiFp(this.configuration).refreshToken(refreshTokenRequest, options).then((request) => request(this.axios, this.basePath));
605
+ }
606
+ /**
607
+ * Creates a new user account. Sends a verification email upon successful registration.
608
+ * @summary Register a new user
609
+ * @param {RegisterRequest} registerRequest User register credentials
610
+ * @param {*} [options] Override http request option.
611
+ * @throws {RequiredError}
612
+ */
613
+ register(registerRequest, options) {
614
+ return AuthApiFp(this.configuration).register(registerRequest, options).then((request) => request(this.axios, this.basePath));
615
+ }
616
+ /**
617
+ * Validates the reset token from the email link and renders the password reset form.
618
+ * @summary Render password reset form
619
+ * @param {TokenQuery} token Password reset token
620
+ * @param {*} [options] Override http request option.
621
+ * @throws {RequiredError}
622
+ */
623
+ renderResetForm(token, options) {
624
+ return AuthApiFp(this.configuration).renderResetForm(token, options).then((request) => request(this.axios, this.basePath));
625
+ }
626
+ /**
627
+ * Sends a password reset link to the provided email address if an account exists.
628
+ * @summary Request a password reset
629
+ * @param {EmailRequest} emailRequest Email address to send the reset link to
630
+ * @param {*} [options] Override http request option.
631
+ * @throws {RequiredError}
632
+ */
633
+ requestPasswordReset(emailRequest, options) {
634
+ return AuthApiFp(this.configuration).requestPasswordReset(emailRequest, options).then((request) => request(this.axios, this.basePath));
635
+ }
636
+ /**
637
+ * Submits a new password using a valid reset token. Invalidates the token after use.
638
+ * @summary Submit new password
639
+ * @param {ResetPasswordRequest} resetPasswordRequest Reset token and the new password
640
+ * @param {*} [options] Override http request option.
641
+ * @throws {RequiredError}
642
+ */
643
+ submitNewPassword(resetPasswordRequest, options) {
644
+ return AuthApiFp(this.configuration).submitNewPassword(resetPasswordRequest, options).then((request) => request(this.axios, this.basePath));
645
+ }
646
+ };
647
+
648
+ // src/generated/api/email-api.ts
649
+ import globalAxios3 from "axios";
650
+ var EmailApiAxiosParamCreator = function(configuration) {
651
+ return {
652
+ /**
653
+ * Validates the email token and update the user account.
654
+ * @summary Verify user email
655
+ * @param {TokenQuery} token Email Verification Token
656
+ * @param {*} [options] Override http request option.
657
+ * @throws {RequiredError}
658
+ */
659
+ verifyEmail: async (token, options = {}) => {
660
+ assertParamExists("verifyEmail", "token", token);
661
+ const localVarPath = `/email`;
662
+ const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
663
+ let baseOptions;
664
+ if (configuration) {
665
+ baseOptions = configuration.baseOptions;
666
+ }
667
+ const localVarRequestOptions = { method: "GET", ...baseOptions, ...options };
668
+ const localVarHeaderParameter = {};
669
+ const localVarQueryParameter = {};
670
+ if (token !== void 0) {
671
+ for (const [key, value] of Object.entries(token)) {
672
+ localVarQueryParameter[key] = value;
673
+ }
674
+ }
675
+ localVarHeaderParameter["Accept"] = "application/json";
676
+ setSearchParams(localVarUrlObj, localVarQueryParameter);
677
+ let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
678
+ localVarRequestOptions.headers = { ...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers };
679
+ return {
680
+ url: toPathString(localVarUrlObj),
681
+ options: localVarRequestOptions
682
+ };
683
+ }
684
+ };
685
+ };
686
+ var EmailApiFp = function(configuration) {
687
+ const localVarAxiosParamCreator = EmailApiAxiosParamCreator(configuration);
688
+ return {
689
+ /**
690
+ * Validates the email token and update the user account.
691
+ * @summary Verify user email
692
+ * @param {TokenQuery} token Email Verification Token
693
+ * @param {*} [options] Override http request option.
694
+ * @throws {RequiredError}
695
+ */
696
+ async verifyEmail(token, options) {
697
+ const localVarAxiosArgs = await localVarAxiosParamCreator.verifyEmail(token, options);
698
+ const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
699
+ const localVarOperationServerBasePath = operationServerMap["EmailApi.verifyEmail"]?.[localVarOperationServerIndex]?.url;
700
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios3, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
701
+ }
702
+ };
703
+ };
704
+ var EmailApi = class extends BaseAPI {
705
+ /**
706
+ * Validates the email token and update the user account.
707
+ * @summary Verify user email
708
+ * @param {TokenQuery} token Email Verification Token
709
+ * @param {*} [options] Override http request option.
710
+ * @throws {RequiredError}
711
+ */
712
+ verifyEmail(token, options) {
713
+ return EmailApiFp(this.configuration).verifyEmail(token, options).then((request) => request(this.axios, this.basePath));
714
+ }
715
+ };
716
+
717
+ // src/generated/api/users-api.ts
718
+ import globalAxios4 from "axios";
719
+ var UsersApiAxiosParamCreator = function(configuration) {
720
+ return {
721
+ /**
722
+ * Returns the currently authenticated user\'s data. Requires a valid Bearer token.
723
+ * @summary Get current User
724
+ * @param {*} [options] Override http request option.
725
+ * @throws {RequiredError}
726
+ */
727
+ findUser: async (options = {}) => {
728
+ const localVarPath = `/user`;
729
+ const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
730
+ let baseOptions;
731
+ if (configuration) {
732
+ baseOptions = configuration.baseOptions;
733
+ }
734
+ const localVarRequestOptions = { method: "GET", ...baseOptions, ...options };
735
+ const localVarHeaderParameter = {};
736
+ const localVarQueryParameter = {};
737
+ await setBearerAuthToObject(localVarHeaderParameter, configuration);
738
+ localVarHeaderParameter["Accept"] = "application/json";
739
+ setSearchParams(localVarUrlObj, localVarQueryParameter);
740
+ let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
741
+ localVarRequestOptions.headers = { ...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers };
742
+ return {
743
+ url: toPathString(localVarUrlObj),
744
+ options: localVarRequestOptions
745
+ };
746
+ }
747
+ };
748
+ };
749
+ var UsersApiFp = function(configuration) {
750
+ const localVarAxiosParamCreator = UsersApiAxiosParamCreator(configuration);
751
+ return {
752
+ /**
753
+ * Returns the currently authenticated user\'s data. Requires a valid Bearer token.
754
+ * @summary Get current User
755
+ * @param {*} [options] Override http request option.
756
+ * @throws {RequiredError}
757
+ */
758
+ async findUser(options) {
759
+ const localVarAxiosArgs = await localVarAxiosParamCreator.findUser(options);
760
+ const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
761
+ const localVarOperationServerBasePath = operationServerMap["UsersApi.findUser"]?.[localVarOperationServerIndex]?.url;
762
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios4, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
763
+ }
764
+ };
765
+ };
766
+ var UsersApi = class extends BaseAPI {
767
+ /**
768
+ * Returns the currently authenticated user\'s data. Requires a valid Bearer token.
769
+ * @summary Get current User
770
+ * @param {*} [options] Override http request option.
771
+ * @throws {RequiredError}
772
+ */
773
+ findUser(options) {
774
+ return UsersApiFp(this.configuration).findUser(options).then((request) => request(this.axios, this.basePath));
775
+ }
776
+ };
777
+
778
+ // src/generated/configuration.ts
779
+ var Configuration = class {
780
+ /**
781
+ * parameter for apiKey security
782
+ * @param name security name
783
+ */
784
+ apiKey;
785
+ /**
786
+ * parameter for basic security
787
+ */
788
+ username;
789
+ /**
790
+ * parameter for basic security
791
+ */
792
+ password;
793
+ /**
794
+ * parameter for oauth2 security
795
+ * @param name security name
796
+ * @param scopes oauth2 scope
797
+ */
798
+ accessToken;
799
+ /**
800
+ * parameter for aws4 signature security
801
+ * @param {Object} AWS4Signature - AWS4 Signature security
802
+ * @param {string} options.region - aws region
803
+ * @param {string} options.service - name of the service.
804
+ * @param {string} credentials.accessKeyId - aws access key id
805
+ * @param {string} credentials.secretAccessKey - aws access key
806
+ * @param {string} credentials.sessionToken - aws session token
807
+ * @memberof Configuration
808
+ */
809
+ awsv4;
810
+ /**
811
+ * override base path
812
+ */
813
+ basePath;
814
+ /**
815
+ * override server index
816
+ */
817
+ serverIndex;
818
+ /**
819
+ * base options for axios calls
820
+ */
821
+ baseOptions;
822
+ /**
823
+ * The FormData constructor that will be used to create multipart form data
824
+ * requests. You can inject this here so that execution environments that
825
+ * do not support the FormData class can still run the generated client.
826
+ *
827
+ * @type {new () => FormData}
828
+ */
829
+ formDataCtor;
830
+ constructor(param = {}) {
831
+ this.apiKey = param.apiKey;
832
+ this.username = param.username;
833
+ this.password = param.password;
834
+ this.accessToken = param.accessToken;
835
+ this.awsv4 = param.awsv4;
836
+ this.basePath = param.basePath;
837
+ this.serverIndex = param.serverIndex;
838
+ this.baseOptions = {
839
+ ...param.baseOptions,
840
+ headers: {
841
+ ...param.baseOptions?.headers
842
+ }
843
+ };
844
+ this.formDataCtor = param.formDataCtor;
845
+ }
846
+ /**
847
+ * Check if the given MIME is a JSON MIME.
848
+ * JSON MIME examples:
849
+ * application/json
850
+ * application/json; charset=UTF8
851
+ * APPLICATION/JSON
852
+ * application/vnd.company+json
853
+ * @param mime - MIME (Multipurpose Internet Mail Extensions)
854
+ * @return True if the given MIME is JSON, false otherwise.
855
+ */
856
+ isJsonMime(mime) {
857
+ const jsonMime = new RegExp("^(application/json|[^;/ ]+/[^;/ ]+[+]json)[ ]*(;.*)?$", "i");
858
+ return mime !== null && (jsonMime.test(mime) || mime.toLowerCase() === "application/json-patch+json");
859
+ }
860
+ };
861
+
862
+ // src/index.ts
863
+ var DEFAULT_OPTIONS = {
864
+ storage: new MemoryStorage()
865
+ };
866
+ var Anzar = class {
867
+ Auth;
868
+ User;
869
+ Email;
870
+ constructor(anzarConfig, options = DEFAULT_OPTIONS) {
871
+ const basePath = anzarConfig.api_url;
872
+ const axiosInstance = this.createAxiosInstance(basePath);
873
+ const configuration = new Configuration({ basePath });
874
+ if (anzarConfig.auth?.strategy === "Jwt" /* Jwt */) {
875
+ new JwtInterceptor().apply(axiosInstance, options);
876
+ }
877
+ const apis = this.createApis(configuration, basePath, axiosInstance);
878
+ this.Auth = new AuthModule(
879
+ apis.auth,
880
+ apis.user,
881
+ anzarConfig.auth?.strategy ?? "Session" /* Session */,
882
+ options
883
+ );
884
+ this.User = apis.user;
885
+ this.Email = apis.email;
886
+ const originalFetch = window.fetch;
887
+ window.fetch = async (url, _options = {}) => {
888
+ const accessToken = options.storage.getToken();
889
+ if (!accessToken) return originalFetch(url, _options);
890
+ return originalFetch(url, {
891
+ ..._options,
892
+ headers: {
893
+ ..._options.headers,
894
+ Authorization: `Bearer ${accessToken}`
895
+ }
896
+ });
897
+ };
898
+ }
899
+ createAxiosInstance(baseURL) {
900
+ return axios2.create({
901
+ baseURL,
902
+ withCredentials: true,
903
+ headers: {
904
+ "Content-Type": "application/json",
905
+ Accept: "application/json"
906
+ }
907
+ });
908
+ }
909
+ createApis(configuration, basePath, axiosInstance) {
910
+ return {
911
+ auth: new AuthApi(configuration, basePath, axiosInstance),
912
+ user: new UsersApi(configuration, basePath, axiosInstance),
913
+ email: new EmailApi(configuration, basePath, axiosInstance)
914
+ };
915
+ }
916
+ };
917
+ export {
918
+ Anzar
919
+ };