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/dist/index.js CHANGED
@@ -1,5 +1,97 @@
1
+ import {
2
+ MemoryStorage
3
+ } from "./chunk-5SSEPZKA.js";
4
+
1
5
  // src/index.ts
6
+ import axios2 from "axios";
7
+
8
+ // src/api/auth.ts
2
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
+ login(body) {
18
+ return this.authApi.login(body);
19
+ }
20
+ register(body) {
21
+ return this.authApi.register(body);
22
+ }
23
+ async logout() {
24
+ await this.authApi.logout();
25
+ this.options?.storage.onLogout?.();
26
+ }
27
+ resetPassword(body) {
28
+ this.authApi.requestPasswordReset(body);
29
+ }
30
+ async isAuthenticated() {
31
+ try {
32
+ const response = await this.userApi.findUser();
33
+ return !!response.data.username;
34
+ } catch (error) {
35
+ if (axios.isAxiosError(error) && error.response?.status === 401) {
36
+ return false;
37
+ }
38
+ throw false;
39
+ }
40
+ }
41
+ session() {
42
+ if (this.strategy !== "Session" /* Session */) {
43
+ throw new Error("session() is only available with Session auth strategy");
44
+ }
45
+ return this.authApi.getSession();
46
+ }
47
+ };
48
+
49
+ // src/api/interceptor.ts
50
+ var JwtInterceptor = class {
51
+ apply(axiosInstance, options) {
52
+ const REFRESH_URI = "/auth/refresh-token";
53
+ axiosInstance.interceptors.request.use(
54
+ (config) => {
55
+ const accessToken = options?.storage.getToken();
56
+ if (!config.url?.includes(REFRESH_URI) && accessToken) {
57
+ config.headers.Authorization = `Bearer ${accessToken}`;
58
+ }
59
+ return config;
60
+ },
61
+ (error) => {
62
+ console.error("Request error:", error);
63
+ return Promise.reject(error);
64
+ }
65
+ );
66
+ axiosInstance.interceptors.response.use(
67
+ (response) => response,
68
+ async (error) => {
69
+ const originalRequest = error.config;
70
+ if (error.response?.status === 401 && !originalRequest._retry && !originalRequest.url?.includes(REFRESH_URI)) {
71
+ originalRequest._retry = true;
72
+ try {
73
+ const refreshToken = options?.storage.getRefreshToken();
74
+ const response = await axiosInstance.post(
75
+ REFRESH_URI,
76
+ {},
77
+ { headers: { "x-refresh-token": `Bearer ${refreshToken}` } }
78
+ );
79
+ const auth_response = response.data;
80
+ if (auth_response.tokens) {
81
+ options?.storage.onTokenRefresh?.(auth_response.tokens);
82
+ }
83
+ originalRequest.headers.Authorization = `Bearer ${auth_response.tokens?.access}`;
84
+ return axiosInstance(originalRequest);
85
+ } catch (e) {
86
+ options?.storage.onSessionExpired?.();
87
+ return Promise.reject(e);
88
+ }
89
+ }
90
+ return Promise.reject(error);
91
+ }
92
+ );
93
+ }
94
+ };
3
95
 
4
96
  // src/generated/api/auth-api.ts
5
97
  import globalAxios2 from "axios";
@@ -8,9 +100,9 @@ import globalAxios2 from "axios";
8
100
  import globalAxios from "axios";
9
101
  var BASE_PATH = "http://localhost".replace(/\/+$/, "");
10
102
  var BaseAPI = class {
11
- constructor(configuration, basePath = BASE_PATH, axios2 = globalAxios) {
103
+ constructor(configuration, basePath = BASE_PATH, axios3 = globalAxios) {
12
104
  this.basePath = basePath;
13
- this.axios = axios2;
105
+ this.axios = axios3;
14
106
  if (configuration) {
15
107
  this.configuration = configuration;
16
108
  this.basePath = configuration.basePath ?? basePath;
@@ -79,9 +171,9 @@ var toPathString = function(url) {
79
171
  return url.pathname + url.search + url.hash;
80
172
  };
81
173
  var createRequestFunction = function(axiosArgs, globalAxios5, BASE_PATH2, configuration) {
82
- return (axios2 = globalAxios5, basePath = BASE_PATH2) => {
83
- const axiosRequestArgs = { ...axiosArgs.options, url: (axios2.defaults.baseURL ? "" : configuration?.basePath ?? basePath) + axiosArgs.url };
84
- return axios2.request(axiosRequestArgs);
174
+ return (axios3 = globalAxios5, basePath = BASE_PATH2) => {
175
+ const axiosRequestArgs = { ...axiosArgs.options, url: (axios3.defaults.baseURL ? "" : configuration?.basePath ?? basePath) + axiosArgs.url };
176
+ return axios3.request(axiosRequestArgs);
85
177
  };
86
178
  };
87
179
 
@@ -329,7 +421,7 @@ var AuthApiFp = function(configuration) {
329
421
  const localVarAxiosArgs = await localVarAxiosParamCreator.getSession(options);
330
422
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
331
423
  const localVarOperationServerBasePath = operationServerMap["AuthApi.getSession"]?.[localVarOperationServerIndex]?.url;
332
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
424
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
333
425
  },
334
426
  /**
335
427
  * Authenticates a user with email and password. Returns an access token and refresh token on success.
@@ -342,7 +434,7 @@ var AuthApiFp = function(configuration) {
342
434
  const localVarAxiosArgs = await localVarAxiosParamCreator.login(loginRequest, options);
343
435
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
344
436
  const localVarOperationServerBasePath = operationServerMap["AuthApi.login"]?.[localVarOperationServerIndex]?.url;
345
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
437
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
346
438
  },
347
439
  /**
348
440
  * Invalidates the current session and refresh token. The client should discard stored tokens.
@@ -354,7 +446,7 @@ var AuthApiFp = function(configuration) {
354
446
  const localVarAxiosArgs = await localVarAxiosParamCreator.logout(options);
355
447
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
356
448
  const localVarOperationServerBasePath = operationServerMap["AuthApi.logout"]?.[localVarOperationServerIndex]?.url;
357
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
449
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
358
450
  },
359
451
  /**
360
452
  * Issues a new access token using a valid refresh token. Rotate refresh tokens on each call.
@@ -366,7 +458,7 @@ var AuthApiFp = function(configuration) {
366
458
  const localVarAxiosArgs = await localVarAxiosParamCreator.refreshToken(options);
367
459
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
368
460
  const localVarOperationServerBasePath = operationServerMap["AuthApi.refreshToken"]?.[localVarOperationServerIndex]?.url;
369
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
461
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
370
462
  },
371
463
  /**
372
464
  * Creates a new user account. Sends a verification email upon successful registration.
@@ -379,7 +471,7 @@ var AuthApiFp = function(configuration) {
379
471
  const localVarAxiosArgs = await localVarAxiosParamCreator.register(registerRequest, options);
380
472
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
381
473
  const localVarOperationServerBasePath = operationServerMap["AuthApi.register"]?.[localVarOperationServerIndex]?.url;
382
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
474
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
383
475
  },
384
476
  /**
385
477
  * Validates the reset token from the email link and renders the password reset form.
@@ -392,7 +484,7 @@ var AuthApiFp = function(configuration) {
392
484
  const localVarAxiosArgs = await localVarAxiosParamCreator.renderResetForm(token, options);
393
485
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
394
486
  const localVarOperationServerBasePath = operationServerMap["AuthApi.renderResetForm"]?.[localVarOperationServerIndex]?.url;
395
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
487
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
396
488
  },
397
489
  /**
398
490
  * Sends a password reset link to the provided email address if an account exists.
@@ -405,7 +497,7 @@ var AuthApiFp = function(configuration) {
405
497
  const localVarAxiosArgs = await localVarAxiosParamCreator.requestPasswordReset(emailRequest, options);
406
498
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
407
499
  const localVarOperationServerBasePath = operationServerMap["AuthApi.requestPasswordReset"]?.[localVarOperationServerIndex]?.url;
408
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
500
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
409
501
  },
410
502
  /**
411
503
  * Submits a new password using a valid reset token. Invalidates the token after use.
@@ -418,7 +510,7 @@ var AuthApiFp = function(configuration) {
418
510
  const localVarAxiosArgs = await localVarAxiosParamCreator.submitNewPassword(resetPasswordRequest, options);
419
511
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
420
512
  const localVarOperationServerBasePath = operationServerMap["AuthApi.submitNewPassword"]?.[localVarOperationServerIndex]?.url;
421
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
513
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios2, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
422
514
  }
423
515
  };
424
516
  };
@@ -554,7 +646,7 @@ var EmailApiFp = function(configuration) {
554
646
  const localVarAxiosArgs = await localVarAxiosParamCreator.verifyEmail(token, options);
555
647
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
556
648
  const localVarOperationServerBasePath = operationServerMap["EmailApi.verifyEmail"]?.[localVarOperationServerIndex]?.url;
557
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios3, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
649
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios3, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
558
650
  }
559
651
  };
560
652
  };
@@ -616,7 +708,7 @@ var UsersApiFp = function(configuration) {
616
708
  const localVarAxiosArgs = await localVarAxiosParamCreator.findUser(options);
617
709
  const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
618
710
  const localVarOperationServerBasePath = operationServerMap["UsersApi.findUser"]?.[localVarOperationServerIndex]?.url;
619
- return (axios2, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios4, BASE_PATH, configuration)(axios2, localVarOperationServerBasePath || basePath);
711
+ return (axios3, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios4, BASE_PATH, configuration)(axios3, localVarOperationServerBasePath || basePath);
620
712
  }
621
713
  };
622
714
  };
@@ -716,97 +808,49 @@ var Configuration = class {
716
808
  }
717
809
  };
718
810
 
719
- // src/http/jwt_interceptor.ts
720
- var JwtInterceptor = class {
721
- apply(axiosInstance, options) {
722
- const REFRESH_URI = "/auth/refresh-token";
723
- axiosInstance.interceptors.request.use(
724
- (config) => {
725
- const accessToken = options?.getToken();
726
- if (!config.url?.includes(REFRESH_URI) && accessToken) {
727
- config.headers.Authorization = `Bearer ${accessToken}`;
728
- }
729
- return config;
730
- },
731
- (error) => {
732
- console.error("Request error:", error);
733
- return Promise.reject(error);
734
- }
735
- );
736
- axiosInstance.interceptors.response.use(
737
- (response) => response,
738
- async (error) => {
739
- const originalRequest = error.config;
740
- if (error.response?.status === 401 && !originalRequest._retry && !originalRequest.url?.includes(REFRESH_URI)) {
741
- originalRequest._retry = true;
742
- try {
743
- const refreshToken = options?.getRefreshToken();
744
- const response = await axiosInstance.post(
745
- REFRESH_URI,
746
- {},
747
- { headers: { "x-refresh-token": `Bearer ${refreshToken}` } }
748
- );
749
- const auth_response = response.data;
750
- if (auth_response.tokens) {
751
- options?.onTokenRefresh?.(auth_response.tokens);
752
- }
753
- originalRequest.headers.Authorization = `Bearer ${auth_response.tokens?.access}`;
754
- return axiosInstance(originalRequest);
755
- } catch (e) {
756
- options?.onSessionExpired?.();
757
- return Promise.reject(e);
758
- }
759
- }
760
- return Promise.reject(error);
761
- }
762
- );
763
- }
764
- };
765
-
766
811
  // src/index.ts
767
- function Anzar(anzarConfig, options) {
768
- const basePath = anzarConfig.api_url;
769
- const configuration = new Configuration({ basePath });
770
- const axiosInstance = axios.create({
771
- baseURL: basePath,
772
- withCredentials: true,
773
- headers: {
774
- "Content-Type": "application/json",
775
- Accept: "application/json"
812
+ var DEFAULT_OPTIONS = {
813
+ storage: new MemoryStorage()
814
+ };
815
+ var Anzar = class {
816
+ Auth;
817
+ User;
818
+ Email;
819
+ constructor(anzarConfig, options = DEFAULT_OPTIONS) {
820
+ const basePath = anzarConfig.api_url;
821
+ const axiosInstance = this.createAxiosInstance(basePath);
822
+ const configuration = new Configuration({ basePath });
823
+ if (anzarConfig.auth?.strategy === "Jwt" /* Jwt */) {
824
+ new JwtInterceptor().apply(axiosInstance, options);
776
825
  }
777
- });
778
- if (anzarConfig.auth?.strategy === "Jwt" /* Jwt */) {
779
- new JwtInterceptor().apply(axiosInstance, options);
826
+ const apis = this.createApis(configuration, basePath, axiosInstance);
827
+ this.Auth = new AuthModule(
828
+ apis.auth,
829
+ apis.user,
830
+ anzarConfig.auth?.strategy ?? "Session" /* Session */,
831
+ options
832
+ );
833
+ this.User = apis.user;
834
+ this.Email = apis.email;
780
835
  }
781
- const generatedAuthApi = new AuthApi(configuration, basePath, axiosInstance);
782
- const generatedUserApi = new UsersApi(configuration, basePath, axiosInstance);
783
- return {
784
- Auth: {
785
- login: (body) => generatedAuthApi.login(body),
786
- register: (body) => generatedAuthApi.register(body),
787
- logout: async () => {
788
- await generatedAuthApi.logout();
789
- options?.onLogout?.();
790
- },
791
- resetPassword: (body) => {
792
- generatedAuthApi.requestPasswordReset(body);
793
- },
794
- ...anzarConfig.auth?.strategy === "Session" /* Session */ && {
795
- session: () => generatedAuthApi.getSession()
796
- },
797
- isAuthenticated: async () => {
798
- try {
799
- const response = await generatedUserApi.findUser();
800
- return !!response.data;
801
- } catch {
802
- return false;
803
- }
836
+ createAxiosInstance(baseURL) {
837
+ return axios2.create({
838
+ baseURL,
839
+ withCredentials: true,
840
+ headers: {
841
+ "Content-Type": "application/json",
842
+ Accept: "application/json"
804
843
  }
805
- },
806
- User: generatedUserApi,
807
- Email: new EmailApi(configuration, basePath, axiosInstance)
808
- };
809
- }
844
+ });
845
+ }
846
+ createApis(configuration, basePath, axiosInstance) {
847
+ return {
848
+ auth: new AuthApi(configuration, basePath, axiosInstance),
849
+ user: new UsersApi(configuration, basePath, axiosInstance),
850
+ email: new EmailApi(configuration, basePath, axiosInstance)
851
+ };
852
+ }
853
+ };
810
854
  export {
811
855
  Anzar
812
856
  };
package/package.json CHANGED
@@ -1,11 +1,23 @@
1
1
  {
2
2
  "name": "anzar",
3
- "version": "1.2.9",
3
+ "version": "1.2.10",
4
4
  "description": "Anzar SDK",
5
5
  "type": "module",
6
- "main": "./dist/index.cjs",
7
6
  "module": "./dist/index.js",
7
+ "main": "./dist/index.cjs",
8
8
  "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ },
15
+ "./adapters": {
16
+ "types": "./dist/adapters/index.d.ts",
17
+ "import": "./dist/adapters/index.js",
18
+ "require": "./dist/adapters/index.cjs"
19
+ }
20
+ },
9
21
  "files": [
10
22
  "dist",
11
23
  "README.md",
@@ -13,7 +25,7 @@
13
25
  ],
14
26
  "scripts": {
15
27
  "test": "echo \"Error: no test specified\" && exit 1",
16
- "build": "tsup src/index.ts --format cjs,esm --dts --clean --platform neutral",
28
+ "build": "tsup",
17
29
  "prepublishOnly": "npm run build"
18
30
  },
19
31
  "keywords": [],
package/README.MD DELETED
File without changes