anzar 1.2.9 → 1.2.10

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/README.md CHANGED
@@ -39,21 +39,17 @@ in your main entry file (`main.ts` for example), import Anzar and create your au
39
39
  import { Anzar } from "anzar";
40
40
  import anzarConfig from "anzar.yml";
41
41
 
42
- const anzar = Anzar(anzarConfig);
42
+ const anzar = new Anzar(anzarConfig);
43
43
  ```
44
44
 
45
- ⚠️ **Warning:**
46
- Only extend these functionality if you are using **JWT** authentication
47
-
48
- ⚠️ **Warning:** Implement a secure Storage solution
45
+ ⚠️ **Note**
46
+ Choose your token storage adapter
49
47
 
50
48
  ```typescript
51
- import { SessionTokens } from "anzar.types";
52
- const anzar = Anzar(anzarConfig, {
53
- getToken: () => "AccessToken",
54
- getRefreshToken: () => "RefreshToken",
55
- onTokenRefresh(tokens) => //store tokens.access, tokens.refresh,
56
- ...
49
+ import { LocalStorage } from "anzar/adapters";
50
+
51
+ const anzar = new Anzar(anzarConfig, {
52
+ storage: new LocalStorage(),
57
53
  });
58
54
  ```
59
55
 
@@ -68,9 +64,6 @@ To sign up a user you need to call the method register with the user's informati
68
64
  try {
69
65
  const response = await anzar.Auth.register({ username, email, password });
70
66
  const data: AuthResponse = response.data;
71
- if (data.tokens) {
72
- // Store securely the `access`,`refresh` tokens
73
- }
74
67
  } catch (e) {
75
68
  const message = e.response?.data?.message ?? "An unknown error occurred";
76
69
  console.log(message);
@@ -86,9 +79,6 @@ To sign in a user you need to call the method login.
86
79
  try {
87
80
  const response = await anzar.Auth.login({ email, password });
88
81
  const data: AuthResponse = response.data;
89
- if (data.tokens) {
90
- // Store securely the `access`,`refresh` tokens
91
- }
92
82
  } catch (e) {
93
83
  const message = e.response?.data?.message ?? "An unknown error occurred";
94
84
  console.log(message);
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/index.ts
21
+ var adapters_exports = {};
22
+ __export(adapters_exports, {
23
+ LocalStorage: () => LocalStorage,
24
+ MemoryStorage: () => MemoryStorage
25
+ });
26
+ module.exports = __toCommonJS(adapters_exports);
27
+
28
+ // src/adapters/local_storage.ts
29
+ var LocalStorage = class {
30
+ getToken() {
31
+ return localStorage.getItem("AccessToken");
32
+ }
33
+ getRefreshToken() {
34
+ return localStorage.getItem("RefreshToken");
35
+ }
36
+ onTokenRefresh(tokens) {
37
+ localStorage.setItem("AccessToken", tokens.access);
38
+ localStorage.setItem("RefreshToken", tokens.refresh);
39
+ }
40
+ onSessionExpired() {
41
+ localStorage.clear();
42
+ }
43
+ onLogout() {
44
+ localStorage.clear();
45
+ }
46
+ };
47
+
48
+ // src/adapters/memory_storage.ts
49
+ var MemoryStorage = class {
50
+ acessToken = null;
51
+ refreshToken = null;
52
+ getToken() {
53
+ return this.acessToken;
54
+ }
55
+ getRefreshToken() {
56
+ return this.refreshToken;
57
+ }
58
+ onTokenRefresh(tokens) {
59
+ this.acessToken = tokens.access;
60
+ this.refreshToken = tokens.refresh;
61
+ }
62
+ onSessionExpired() {
63
+ this.acessToken = null;
64
+ this.refreshToken = null;
65
+ }
66
+ onLogout() {
67
+ this.acessToken = null;
68
+ this.refreshToken = null;
69
+ }
70
+ };
@@ -0,0 +1,21 @@
1
+ import { B as BaseStorage, S as SessionTokens } from '../base_storage-CQKHFbrb.cjs';
2
+
3
+ declare class LocalStorage implements BaseStorage {
4
+ getToken(): string | null;
5
+ getRefreshToken(): string | null;
6
+ onTokenRefresh(tokens: SessionTokens): void;
7
+ onSessionExpired?(): void;
8
+ onLogout?(): void;
9
+ }
10
+
11
+ declare class MemoryStorage implements BaseStorage {
12
+ private acessToken;
13
+ private refreshToken;
14
+ getToken(): string | null;
15
+ getRefreshToken(): string | null;
16
+ onTokenRefresh(tokens: SessionTokens): void;
17
+ onSessionExpired?(): void;
18
+ onLogout?(): void;
19
+ }
20
+
21
+ export { LocalStorage, MemoryStorage };
@@ -0,0 +1,21 @@
1
+ import { B as BaseStorage, S as SessionTokens } from '../base_storage-CQKHFbrb.js';
2
+
3
+ declare class LocalStorage implements BaseStorage {
4
+ getToken(): string | null;
5
+ getRefreshToken(): string | null;
6
+ onTokenRefresh(tokens: SessionTokens): void;
7
+ onSessionExpired?(): void;
8
+ onLogout?(): void;
9
+ }
10
+
11
+ declare class MemoryStorage implements BaseStorage {
12
+ private acessToken;
13
+ private refreshToken;
14
+ getToken(): string | null;
15
+ getRefreshToken(): string | null;
16
+ onTokenRefresh(tokens: SessionTokens): void;
17
+ onSessionExpired?(): void;
18
+ onLogout?(): void;
19
+ }
20
+
21
+ export { LocalStorage, MemoryStorage };
@@ -0,0 +1,27 @@
1
+ import {
2
+ MemoryStorage
3
+ } from "../chunk-5SSEPZKA.js";
4
+
5
+ // src/adapters/local_storage.ts
6
+ var LocalStorage = class {
7
+ getToken() {
8
+ return localStorage.getItem("AccessToken");
9
+ }
10
+ getRefreshToken() {
11
+ return localStorage.getItem("RefreshToken");
12
+ }
13
+ onTokenRefresh(tokens) {
14
+ localStorage.setItem("AccessToken", tokens.access);
15
+ localStorage.setItem("RefreshToken", tokens.refresh);
16
+ }
17
+ onSessionExpired() {
18
+ localStorage.clear();
19
+ }
20
+ onLogout() {
21
+ localStorage.clear();
22
+ }
23
+ };
24
+ export {
25
+ LocalStorage,
26
+ MemoryStorage
27
+ };
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Anzar Software API
3
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
4
+ *
5
+ * The version of the OpenAPI document: 0.6.2
6
+ * Contact: dev@anzar.io
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ interface SessionTokens {
13
+ 'access': string;
14
+ 'refresh': string;
15
+ }
16
+
17
+ interface BaseStorage {
18
+ getToken(): string | null;
19
+ getRefreshToken(): string | null;
20
+ onTokenRefresh(tokens: SessionTokens): void;
21
+ onSessionExpired?(): void;
22
+ onLogout?(): void;
23
+ }
24
+
25
+ export type { BaseStorage as B, SessionTokens as S };
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Anzar Software API
3
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
4
+ *
5
+ * The version of the OpenAPI document: 0.6.2
6
+ * Contact: dev@anzar.io
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ interface SessionTokens {
13
+ 'access': string;
14
+ 'refresh': string;
15
+ }
16
+
17
+ interface BaseStorage {
18
+ getToken(): string | null;
19
+ getRefreshToken(): string | null;
20
+ onTokenRefresh(tokens: SessionTokens): void;
21
+ onSessionExpired?(): void;
22
+ onLogout?(): void;
23
+ }
24
+
25
+ export type { BaseStorage as B, SessionTokens as S };
@@ -0,0 +1,27 @@
1
+ // src/adapters/memory_storage.ts
2
+ var MemoryStorage = class {
3
+ acessToken = null;
4
+ refreshToken = null;
5
+ getToken() {
6
+ return this.acessToken;
7
+ }
8
+ getRefreshToken() {
9
+ return this.refreshToken;
10
+ }
11
+ onTokenRefresh(tokens) {
12
+ this.acessToken = tokens.access;
13
+ this.refreshToken = tokens.refresh;
14
+ }
15
+ onSessionExpired() {
16
+ this.acessToken = null;
17
+ this.refreshToken = null;
18
+ }
19
+ onLogout() {
20
+ this.acessToken = null;
21
+ this.refreshToken = null;
22
+ }
23
+ };
24
+
25
+ export {
26
+ MemoryStorage
27
+ };
package/dist/index.cjs CHANGED
@@ -28,23 +28,135 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
 
30
30
  // src/index.ts
31
- var index_exports = {};
32
- __export(index_exports, {
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
33
  Anzar: () => Anzar
34
34
  });
35
- module.exports = __toCommonJS(index_exports);
36
- var import_axios5 = __toESM(require("axios"), 1);
35
+ module.exports = __toCommonJS(src_exports);
36
+ var import_axios6 = __toESM(require("axios"), 1);
37
+
38
+ // src/adapters/memory_storage.ts
39
+ var MemoryStorage = class {
40
+ acessToken = null;
41
+ refreshToken = null;
42
+ getToken() {
43
+ return this.acessToken;
44
+ }
45
+ getRefreshToken() {
46
+ return this.refreshToken;
47
+ }
48
+ onTokenRefresh(tokens) {
49
+ this.acessToken = tokens.access;
50
+ this.refreshToken = tokens.refresh;
51
+ }
52
+ onSessionExpired() {
53
+ this.acessToken = null;
54
+ this.refreshToken = null;
55
+ }
56
+ onLogout() {
57
+ this.acessToken = null;
58
+ this.refreshToken = null;
59
+ }
60
+ };
61
+
62
+ // src/api/auth.ts
63
+ var import_axios = __toESM(require("axios"), 1);
64
+ var AuthModule = class {
65
+ constructor(authApi, userApi, strategy, options) {
66
+ this.authApi = authApi;
67
+ this.userApi = userApi;
68
+ this.strategy = strategy;
69
+ this.options = options;
70
+ }
71
+ login(body) {
72
+ return this.authApi.login(body);
73
+ }
74
+ register(body) {
75
+ return this.authApi.register(body);
76
+ }
77
+ async logout() {
78
+ await this.authApi.logout();
79
+ this.options?.storage.onLogout?.();
80
+ }
81
+ resetPassword(body) {
82
+ this.authApi.requestPasswordReset(body);
83
+ }
84
+ async isAuthenticated() {
85
+ try {
86
+ const response = await this.userApi.findUser();
87
+ return !!response.data.username;
88
+ } catch (error) {
89
+ if (import_axios.default.isAxiosError(error) && error.response?.status === 401) {
90
+ return false;
91
+ }
92
+ throw false;
93
+ }
94
+ }
95
+ session() {
96
+ if (this.strategy !== "Session" /* Session */) {
97
+ throw new Error("session() is only available with Session auth strategy");
98
+ }
99
+ return this.authApi.getSession();
100
+ }
101
+ };
102
+
103
+ // src/api/interceptor.ts
104
+ var JwtInterceptor = class {
105
+ apply(axiosInstance, options) {
106
+ const REFRESH_URI = "/auth/refresh-token";
107
+ axiosInstance.interceptors.request.use(
108
+ (config) => {
109
+ const accessToken = options?.storage.getToken();
110
+ if (!config.url?.includes(REFRESH_URI) && accessToken) {
111
+ config.headers.Authorization = `Bearer ${accessToken}`;
112
+ }
113
+ return config;
114
+ },
115
+ (error) => {
116
+ console.error("Request error:", error);
117
+ return Promise.reject(error);
118
+ }
119
+ );
120
+ axiosInstance.interceptors.response.use(
121
+ (response) => response,
122
+ async (error) => {
123
+ const originalRequest = error.config;
124
+ if (error.response?.status === 401 && !originalRequest._retry && !originalRequest.url?.includes(REFRESH_URI)) {
125
+ originalRequest._retry = true;
126
+ try {
127
+ const refreshToken = options?.storage.getRefreshToken();
128
+ const response = await axiosInstance.post(
129
+ REFRESH_URI,
130
+ {},
131
+ { headers: { "x-refresh-token": `Bearer ${refreshToken}` } }
132
+ );
133
+ const auth_response = response.data;
134
+ if (auth_response.tokens) {
135
+ options?.storage.onTokenRefresh?.(auth_response.tokens);
136
+ }
137
+ originalRequest.headers.Authorization = `Bearer ${auth_response.tokens?.access}`;
138
+ return axiosInstance(originalRequest);
139
+ } catch (e) {
140
+ options?.storage.onSessionExpired?.();
141
+ return Promise.reject(e);
142
+ }
143
+ }
144
+ return Promise.reject(error);
145
+ }
146
+ );
147
+ }
148
+ };
37
149
 
38
150
  // src/generated/api/auth-api.ts
39
- var import_axios2 = __toESM(require("axios"), 1);
151
+ var import_axios3 = __toESM(require("axios"), 1);
40
152
 
41
153
  // src/generated/base.ts
42
- var import_axios = __toESM(require("axios"), 1);
154
+ var import_axios2 = __toESM(require("axios"), 1);
43
155
  var BASE_PATH = "http://localhost".replace(/\/+$/, "");
44
156
  var BaseAPI = class {
45
- constructor(configuration, basePath = BASE_PATH, axios2 = import_axios.default) {
157
+ constructor(configuration, basePath = BASE_PATH, axios3 = import_axios2.default) {
46
158
  this.basePath = basePath;
47
- this.axios = axios2;
159
+ this.axios = axios3;
48
160
  if (configuration) {
49
161
  this.configuration = configuration;
50
162
  this.basePath = configuration.basePath ?? basePath;
@@ -113,9 +225,9 @@ var toPathString = function(url) {
113
225
  return url.pathname + url.search + url.hash;
114
226
  };
115
227
  var createRequestFunction = function(axiosArgs, globalAxios5, BASE_PATH2, configuration) {
116
- return (axios2 = globalAxios5, basePath = BASE_PATH2) => {
117
- const axiosRequestArgs = { ...axiosArgs.options, url: (axios2.defaults.baseURL ? "" : configuration?.basePath ?? basePath) + axiosArgs.url };
118
- return axios2.request(axiosRequestArgs);
228
+ return (axios3 = globalAxios5, basePath = BASE_PATH2) => {
229
+ const axiosRequestArgs = { ...axiosArgs.options, url: (axios3.defaults.baseURL ? "" : configuration?.basePath ?? basePath) + axiosArgs.url };
230
+ return axios3.request(axiosRequestArgs);
119
231
  };
120
232
  };
121
233
 
@@ -363,7 +475,7 @@ var AuthApiFp = function(configuration) {
363
475
  const localVarAxiosArgs = await localVarAxiosParamCreator.getSession(options);
364
476
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
365
477
  const localVarOperationServerBasePath = operationServerMap["AuthApi.getSession"]?.[localVarOperationServerIndex]?.url;
366
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, import_axios2.default, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
478
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, import_axios3.default, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
367
479
  },
368
480
  /**
369
481
  * Authenticates a user with email and password. Returns an access token and refresh token on success.
@@ -376,7 +488,7 @@ var AuthApiFp = function(configuration) {
376
488
  const localVarAxiosArgs = await localVarAxiosParamCreator.login(loginRequest, options);
377
489
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
378
490
  const localVarOperationServerBasePath = operationServerMap["AuthApi.login"]?.[localVarOperationServerIndex]?.url;
379
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, import_axios2.default, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
491
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, import_axios3.default, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
380
492
  },
381
493
  /**
382
494
  * Invalidates the current session and refresh token. The client should discard stored tokens.
@@ -388,7 +500,7 @@ var AuthApiFp = function(configuration) {
388
500
  const localVarAxiosArgs = await localVarAxiosParamCreator.logout(options);
389
501
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
390
502
  const localVarOperationServerBasePath = operationServerMap["AuthApi.logout"]?.[localVarOperationServerIndex]?.url;
391
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, import_axios2.default, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
503
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, import_axios3.default, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
392
504
  },
393
505
  /**
394
506
  * Issues a new access token using a valid refresh token. Rotate refresh tokens on each call.
@@ -400,7 +512,7 @@ var AuthApiFp = function(configuration) {
400
512
  const localVarAxiosArgs = await localVarAxiosParamCreator.refreshToken(options);
401
513
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
402
514
  const localVarOperationServerBasePath = operationServerMap["AuthApi.refreshToken"]?.[localVarOperationServerIndex]?.url;
403
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, import_axios2.default, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
515
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, import_axios3.default, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
404
516
  },
405
517
  /**
406
518
  * Creates a new user account. Sends a verification email upon successful registration.
@@ -413,7 +525,7 @@ var AuthApiFp = function(configuration) {
413
525
  const localVarAxiosArgs = await localVarAxiosParamCreator.register(registerRequest, options);
414
526
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
415
527
  const localVarOperationServerBasePath = operationServerMap["AuthApi.register"]?.[localVarOperationServerIndex]?.url;
416
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, import_axios2.default, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
528
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, import_axios3.default, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
417
529
  },
418
530
  /**
419
531
  * Validates the reset token from the email link and renders the password reset form.
@@ -426,7 +538,7 @@ var AuthApiFp = function(configuration) {
426
538
  const localVarAxiosArgs = await localVarAxiosParamCreator.renderResetForm(token, options);
427
539
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
428
540
  const localVarOperationServerBasePath = operationServerMap["AuthApi.renderResetForm"]?.[localVarOperationServerIndex]?.url;
429
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, import_axios2.default, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
541
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, import_axios3.default, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
430
542
  },
431
543
  /**
432
544
  * Sends a password reset link to the provided email address if an account exists.
@@ -439,7 +551,7 @@ var AuthApiFp = function(configuration) {
439
551
  const localVarAxiosArgs = await localVarAxiosParamCreator.requestPasswordReset(emailRequest, options);
440
552
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
441
553
  const localVarOperationServerBasePath = operationServerMap["AuthApi.requestPasswordReset"]?.[localVarOperationServerIndex]?.url;
442
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, import_axios2.default, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
554
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, import_axios3.default, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
443
555
  },
444
556
  /**
445
557
  * Submits a new password using a valid reset token. Invalidates the token after use.
@@ -452,7 +564,7 @@ var AuthApiFp = function(configuration) {
452
564
  const localVarAxiosArgs = await localVarAxiosParamCreator.submitNewPassword(resetPasswordRequest, options);
453
565
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
454
566
  const localVarOperationServerBasePath = operationServerMap["AuthApi.submitNewPassword"]?.[localVarOperationServerIndex]?.url;
455
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, import_axios2.default, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
567
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, import_axios3.default, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
456
568
  }
457
569
  };
458
570
  };
@@ -537,7 +649,7 @@ var AuthApi = class extends BaseAPI {
537
649
  };
538
650
 
539
651
  // src/generated/api/email-api.ts
540
- var import_axios3 = __toESM(require("axios"), 1);
652
+ var import_axios4 = __toESM(require("axios"), 1);
541
653
  var EmailApiAxiosParamCreator = function(configuration) {
542
654
  return {
543
655
  /**
@@ -588,7 +700,7 @@ var EmailApiFp = function(configuration) {
588
700
  const localVarAxiosArgs = await localVarAxiosParamCreator.verifyEmail(token, options);
589
701
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
590
702
  const localVarOperationServerBasePath = operationServerMap["EmailApi.verifyEmail"]?.[localVarOperationServerIndex]?.url;
591
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, import_axios3.default, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
703
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, import_axios4.default, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
592
704
  }
593
705
  };
594
706
  };
@@ -606,7 +718,7 @@ var EmailApi = class extends BaseAPI {
606
718
  };
607
719
 
608
720
  // src/generated/api/users-api.ts
609
- var import_axios4 = __toESM(require("axios"), 1);
721
+ var import_axios5 = __toESM(require("axios"), 1);
610
722
  var UsersApiAxiosParamCreator = function(configuration) {
611
723
  return {
612
724
  /**
@@ -650,7 +762,7 @@ var UsersApiFp = function(configuration) {
650
762
  const localVarAxiosArgs = await localVarAxiosParamCreator.findUser(options);
651
763
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
652
764
  const localVarOperationServerBasePath = operationServerMap["UsersApi.findUser"]?.[localVarOperationServerIndex]?.url;
653
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, import_axios4.default, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
765
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, import_axios5.default, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
654
766
  }
655
767
  };
656
768
  };
@@ -750,94 +862,46 @@ var Configuration = class {
750
862
  }
751
863
  };
752
864
 
753
- // src/http/jwt_interceptor.ts
754
- var JwtInterceptor = class {
755
- apply(axiosInstance, options) {
756
- const REFRESH_URI = "/auth/refresh-token";
757
- axiosInstance.interceptors.request.use(
758
- (config) => {
759
- const accessToken = options?.getToken();
760
- if (!config.url?.includes(REFRESH_URI) && accessToken) {
761
- config.headers.Authorization = `Bearer ${accessToken}`;
762
- }
763
- return config;
764
- },
765
- (error) => {
766
- console.error("Request error:", error);
767
- return Promise.reject(error);
768
- }
769
- );
770
- axiosInstance.interceptors.response.use(
771
- (response) => response,
772
- async (error) => {
773
- const originalRequest = error.config;
774
- if (error.response?.status === 401 && !originalRequest._retry && !originalRequest.url?.includes(REFRESH_URI)) {
775
- originalRequest._retry = true;
776
- try {
777
- const refreshToken = options?.getRefreshToken();
778
- const response = await axiosInstance.post(
779
- REFRESH_URI,
780
- {},
781
- { headers: { "x-refresh-token": `Bearer ${refreshToken}` } }
782
- );
783
- const auth_response = response.data;
784
- if (auth_response.tokens) {
785
- options?.onTokenRefresh?.(auth_response.tokens);
786
- }
787
- originalRequest.headers.Authorization = `Bearer ${auth_response.tokens?.access}`;
788
- return axiosInstance(originalRequest);
789
- } catch (e) {
790
- options?.onSessionExpired?.();
791
- return Promise.reject(e);
792
- }
793
- }
794
- return Promise.reject(error);
795
- }
796
- );
797
- }
798
- };
799
-
800
865
  // src/index.ts
801
- function Anzar(anzarConfig, options) {
802
- const basePath = anzarConfig.api_url;
803
- const configuration = new Configuration({ basePath });
804
- const axiosInstance = import_axios5.default.create({
805
- baseURL: basePath,
806
- withCredentials: true,
807
- headers: {
808
- "Content-Type": "application/json",
809
- Accept: "application/json"
866
+ var DEFAULT_OPTIONS = {
867
+ storage: new MemoryStorage()
868
+ };
869
+ var Anzar = class {
870
+ Auth;
871
+ User;
872
+ Email;
873
+ constructor(anzarConfig, options = DEFAULT_OPTIONS) {
874
+ const basePath = anzarConfig.api_url;
875
+ const axiosInstance = this.createAxiosInstance(basePath);
876
+ const configuration = new Configuration({ basePath });
877
+ if (anzarConfig.auth?.strategy === "Jwt" /* Jwt */) {
878
+ new JwtInterceptor().apply(axiosInstance, options);
810
879
  }
811
- });
812
- if (anzarConfig.auth?.strategy === "Jwt" /* Jwt */) {
813
- new JwtInterceptor().apply(axiosInstance, options);
880
+ const apis = this.createApis(configuration, basePath, axiosInstance);
881
+ this.Auth = new AuthModule(
882
+ apis.auth,
883
+ apis.user,
884
+ anzarConfig.auth?.strategy ?? "Session" /* Session */,
885
+ options
886
+ );
887
+ this.User = apis.user;
888
+ this.Email = apis.email;
814
889
  }
815
- const generatedAuthApi = new AuthApi(configuration, basePath, axiosInstance);
816
- const generatedUserApi = new UsersApi(configuration, basePath, axiosInstance);
817
- return {
818
- Auth: {
819
- login: (body) => generatedAuthApi.login(body),
820
- register: (body) => generatedAuthApi.register(body),
821
- logout: async () => {
822
- await generatedAuthApi.logout();
823
- options?.onLogout?.();
824
- },
825
- resetPassword: (body) => {
826
- generatedAuthApi.requestPasswordReset(body);
827
- },
828
- ...anzarConfig.auth?.strategy === "Session" /* Session */ && {
829
- session: () => generatedAuthApi.getSession()
830
- },
831
- isAuthenticated: async () => {
832
- try {
833
- const response = await generatedUserApi.findUser();
834
- return !!response.data;
835
- } catch {
836
- return false;
837
- }
890
+ createAxiosInstance(baseURL) {
891
+ return import_axios6.default.create({
892
+ baseURL,
893
+ withCredentials: true,
894
+ headers: {
895
+ "Content-Type": "application/json",
896
+ Accept: "application/json"
838
897
  }
839
- },
840
- User: generatedUserApi,
841
- Email: new EmailApi(configuration, basePath, axiosInstance)
842
- };
843
- }
898
+ });
899
+ }
900
+ createApis(configuration, basePath, axiosInstance) {
901
+ return {
902
+ auth: new AuthApi(configuration, basePath, axiosInstance),
903
+ user: new UsersApi(configuration, basePath, axiosInstance),
904
+ email: new EmailApi(configuration, basePath, axiosInstance)
905
+ };
906
+ }
907
+ };