@rasadov/lumoar-sdk 2.0.8 → 2.0.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/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,33 +40,60 @@ 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}`;
55
+ console.log('[SDK] Adding Authorization header to request');
56
+ }
57
+ else {
58
+ console.log('[SDK] No token available, request will use only session cookie');
51
59
  }
52
60
  return config;
61
+ }, (error) => {
62
+ return Promise.reject(error);
53
63
  });
64
+ // Response interceptor: Extract and update token from backend responses
54
65
  this.axiosInstance.interceptors.response.use((response) => {
55
- var _a;
56
- const newToken = response.headers['authorization'] || response.headers['Authorization'] || ((_a = response.data) === null || _a === void 0 ? void 0 : _a.newToken);
66
+ var _a, _b, _c;
67
+ // Try multiple sources for the token
68
+ const authHeader = response.headers['authorization'] || response.headers['Authorization'];
69
+ const dataToken = ((_a = response.data) === null || _a === void 0 ? void 0 : _a.token) || ((_b = response.data) === null || _b === void 0 ? void 0 : _b.newToken) || ((_c = response.data) === null || _c === void 0 ? void 0 : _c.authToken);
70
+ const newToken = authHeader || dataToken;
57
71
  if (newToken) {
58
- this.setToken(newToken.replace(/^Bearer /i, ''));
59
- console.log('Token automatically updated via backend refresh');
72
+ const tokenValue = newToken.replace(/^Bearer /i, '');
73
+ this.setToken(tokenValue);
74
+ console.log('[SDK] Token automatically updated from backend response');
75
+ }
76
+ else {
77
+ // Debug: Check if header exists but is blocked by CORS
78
+ console.log('[SDK] Response headers available:', Object.keys(response.headers));
79
+ if (!this.token) {
80
+ console.warn('[SDK] No token found in response. If backend sends "authorization" header, check Access-Control-Expose-Headers');
81
+ }
60
82
  }
61
83
  return response;
62
84
  }, (error) => {
63
- var _a, _b, _c, _d, _e;
64
- 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']);
85
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
86
+ // Even on error, check if backend sent a refreshed token
87
+ const authHeader = ((_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']);
88
+ const dataToken = ((_f = (_e = error.response) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f.token) || ((_h = (_g = error.response) === null || _g === void 0 ? void 0 : _g.data) === null || _h === void 0 ? void 0 : _h.newToken);
89
+ const newToken = authHeader || dataToken;
65
90
  if (newToken) {
66
- this.setToken(newToken.replace(/^Bearer /i, ''));
67
- console.log('Token automatically updated via backend refresh (error response)');
91
+ const tokenValue = newToken.replace(/^Bearer /i, '');
92
+ this.setToken(tokenValue);
93
+ console.log('[SDK] Token automatically updated from backend error response');
68
94
  }
69
- if (((_e = error.response) === null || _e === void 0 ? void 0 : _e.status) === 401) {
70
- console.error('Authentication failed - session expired');
95
+ if (((_j = error.response) === null || _j === void 0 ? void 0 : _j.status) === 401) {
96
+ console.error('[SDK] Authentication failed - session expired');
71
97
  }
72
98
  return Promise.reject(error);
73
99
  });
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.10",
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,33 +89,60 @@ 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}`;
105
+ console.log('[SDK] Adding Authorization header to request');
106
+ } else {
107
+ console.log('[SDK] No token available, request will use only session cookie');
101
108
  }
102
109
  return config;
110
+ }, (error) => {
111
+ return Promise.reject(error);
103
112
  });
104
113
 
114
+ // Response interceptor: Extract and update token from backend responses
105
115
  this.axiosInstance.interceptors.response.use((response: AxiosResponse) => {
106
- const newToken = response.headers['authorization'] || response.headers['Authorization'] || response.data?.newToken;
116
+ // Try multiple sources for the token
117
+ const authHeader = response.headers['authorization'] || response.headers['Authorization'];
118
+ const dataToken = response.data?.token || response.data?.newToken || response.data?.authToken;
119
+ const newToken = authHeader || dataToken;
120
+
107
121
  if (newToken) {
108
- this.setToken(newToken.replace(/^Bearer /i, ''));
109
- console.log('Token automatically updated via backend refresh');
122
+ const tokenValue = newToken.replace(/^Bearer /i, '');
123
+ this.setToken(tokenValue);
124
+ console.log('[SDK] Token automatically updated from backend response');
125
+ } else {
126
+ // Debug: Check if header exists but is blocked by CORS
127
+ console.log('[SDK] Response headers available:', Object.keys(response.headers));
128
+ if (!this.token) {
129
+ console.warn('[SDK] No token found in response. If backend sends "authorization" header, check Access-Control-Expose-Headers');
130
+ }
110
131
  }
111
132
  return response;
112
133
  }, (error) => {
113
- const newToken = error.response?.headers?.['authorization'] || error.response?.headers?.['Authorization'];
134
+ // Even on error, check if backend sent a refreshed token
135
+ const authHeader = error.response?.headers?.['authorization'] || error.response?.headers?.['Authorization'];
136
+ const dataToken = error.response?.data?.token || error.response?.data?.newToken;
137
+ const newToken = authHeader || dataToken;
138
+
114
139
  if (newToken) {
115
- this.setToken(newToken.replace(/^Bearer /i, ''));
116
- console.log('Token automatically updated via backend refresh (error response)');
140
+ const tokenValue = newToken.replace(/^Bearer /i, '');
141
+ this.setToken(tokenValue);
142
+ console.log('[SDK] Token automatically updated from backend error response');
117
143
  }
118
144
  if (error.response?.status === 401) {
119
- console.error('Authentication failed - session expired');
145
+ console.error('[SDK] Authentication failed - session expired');
120
146
  }
121
147
  return Promise.reject(error);
122
148
  });