@rasadov/lumoar-sdk 2.0.8 → 2.0.9

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/sdk.d.ts CHANGED
@@ -25,7 +25,7 @@ export declare class ApiSDK {
25
25
  private axiosInstance;
26
26
  constructor(basePath?: string);
27
27
  setToken(newToken: string): void;
28
- private getAuthHeader;
28
+ getToken(): string | null;
29
29
  private setupInterceptors;
30
30
  getApi(): {
31
31
  aPIKeysApi: APIKeysApi;
package/dist/sdk.js CHANGED
@@ -11,7 +11,6 @@ export class ApiSDK {
11
11
  this.setupInterceptors();
12
12
  const config = new Configuration({
13
13
  basePath,
14
- apiKey: () => this.getAuthHeader(),
15
14
  baseOptions: {
16
15
  axios: this.axiosInstance
17
16
  },
@@ -41,29 +40,40 @@ export class ApiSDK {
41
40
  setToken(newToken) {
42
41
  this.token = newToken;
43
42
  }
44
- getAuthHeader() {
45
- return this.token ? `Bearer ${this.token}` : '';
43
+ getToken() {
44
+ return this.token;
46
45
  }
47
46
  setupInterceptors() {
47
+ // Request interceptor: Always add Authorization header if token exists
48
48
  this.axiosInstance.interceptors.request.use((config) => {
49
49
  if (this.token) {
50
- config.headers['Authorization'] = this.getAuthHeader();
50
+ // Ensure headers object exists
51
+ if (!config.headers) {
52
+ config.headers = {};
53
+ }
54
+ config.headers['Authorization'] = `Bearer ${this.token}`;
51
55
  }
52
56
  return config;
57
+ }, (error) => {
58
+ return Promise.reject(error);
53
59
  });
60
+ // Response interceptor: Extract and update token from backend responses
54
61
  this.axiosInstance.interceptors.response.use((response) => {
55
62
  var _a;
56
63
  const newToken = response.headers['authorization'] || response.headers['Authorization'] || ((_a = response.data) === null || _a === void 0 ? void 0 : _a.newToken);
57
64
  if (newToken) {
58
- this.setToken(newToken.replace(/^Bearer /i, ''));
65
+ const tokenValue = newToken.replace(/^Bearer /i, '');
66
+ this.setToken(tokenValue);
59
67
  console.log('Token automatically updated via backend refresh');
60
68
  }
61
69
  return response;
62
70
  }, (error) => {
63
71
  var _a, _b, _c, _d, _e;
72
+ // Even on error, check if backend sent a refreshed token
64
73
  const newToken = ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b['authorization']) || ((_d = (_c = error.response) === null || _c === void 0 ? void 0 : _c.headers) === null || _d === void 0 ? void 0 : _d['Authorization']);
65
74
  if (newToken) {
66
- this.setToken(newToken.replace(/^Bearer /i, ''));
75
+ const tokenValue = newToken.replace(/^Bearer /i, '');
76
+ this.setToken(tokenValue);
67
77
  console.log('Token automatically updated via backend refresh (error response)');
68
78
  }
69
79
  if (((_e = error.response) === null || _e === void 0 ? void 0 : _e.status) === 401) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rasadov/lumoar-sdk",
3
- "version": "2.0.8",
3
+ "version": "2.0.9",
4
4
  "description": "Lumoar API SDK for dashboard use (session-based authentication)",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/sdk.ts CHANGED
@@ -57,7 +57,6 @@ export class ApiSDK {
57
57
  this.setupInterceptors();
58
58
  const config = new Configuration({
59
59
  basePath,
60
- apiKey: () => this.getAuthHeader(),
61
60
  baseOptions: {
62
61
  axios: this.axiosInstance
63
62
  },
@@ -90,29 +89,40 @@ export class ApiSDK {
90
89
  this.token = newToken;
91
90
  }
92
91
 
93
- private getAuthHeader(): string {
94
- return this.token ? `Bearer ${this.token}` : '';
92
+ public getToken(): string | null {
93
+ return this.token;
95
94
  }
96
95
 
97
96
  private setupInterceptors() {
97
+ // Request interceptor: Always add Authorization header if token exists
98
98
  this.axiosInstance.interceptors.request.use((config) => {
99
99
  if (this.token) {
100
- config.headers['Authorization'] = this.getAuthHeader();
100
+ // Ensure headers object exists
101
+ if (!config.headers) {
102
+ config.headers = {} as any;
103
+ }
104
+ config.headers['Authorization'] = `Bearer ${this.token}`;
101
105
  }
102
106
  return config;
107
+ }, (error) => {
108
+ return Promise.reject(error);
103
109
  });
104
110
 
111
+ // Response interceptor: Extract and update token from backend responses
105
112
  this.axiosInstance.interceptors.response.use((response: AxiosResponse) => {
106
113
  const newToken = response.headers['authorization'] || response.headers['Authorization'] || response.data?.newToken;
107
114
  if (newToken) {
108
- this.setToken(newToken.replace(/^Bearer /i, ''));
115
+ const tokenValue = newToken.replace(/^Bearer /i, '');
116
+ this.setToken(tokenValue);
109
117
  console.log('Token automatically updated via backend refresh');
110
118
  }
111
119
  return response;
112
120
  }, (error) => {
121
+ // Even on error, check if backend sent a refreshed token
113
122
  const newToken = error.response?.headers?.['authorization'] || error.response?.headers?.['Authorization'];
114
123
  if (newToken) {
115
- this.setToken(newToken.replace(/^Bearer /i, ''));
124
+ const tokenValue = newToken.replace(/^Bearer /i, '');
125
+ this.setToken(tokenValue);
116
126
  console.log('Token automatically updated via backend refresh (error response)');
117
127
  }
118
128
  if (error.response?.status === 401) {