@flexbe/sdk 0.1.2 → 0.1.3

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.
@@ -7,81 +7,49 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ import { FlexbeAuthType } from '../types';
10
11
  import { TokenManager } from './token-manager';
11
12
  export class FlexbeAuth {
12
13
  constructor(config) {
13
14
  this.initialized = false;
14
- this.initializing = false;
15
+ this.initializationPromise = null;
15
16
  this.config = config;
16
17
  this.tokenManager = TokenManager.getInstance();
17
- if (this.config.authType === 'bearer') {
18
- // Check if we have a valid token in storage
19
- const existingToken = this.tokenManager.getToken();
20
- if (existingToken) {
21
- this.initialized = true;
22
- }
23
- // Don't start initialization here, let ensureInitialized handle it
18
+ if (this.config.authType === FlexbeAuthType.BEARER) {
19
+ // Start initialization but don't wait for it
20
+ this.initializationPromise = this.initialize();
24
21
  }
25
22
  else {
26
23
  this.initialized = true;
27
24
  }
28
25
  }
29
- initializeBearerAuth() {
26
+ initialize() {
30
27
  return __awaiter(this, void 0, void 0, function* () {
31
- if (this.initializing) {
32
- // Wait for the ongoing initialization to complete
33
- while (this.initializing) {
34
- yield new Promise(resolve => setTimeout(resolve, 100));
35
- }
36
- return;
37
- }
38
28
  try {
39
- this.initializing = true;
40
- const controller = new AbortController();
41
- const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
42
- const response = yield fetch('/oauth/token', {
43
- method: 'POST',
44
- headers: {
45
- 'Content-Type': 'application/json',
46
- },
47
- body: JSON.stringify({
48
- grant_type: 'client_credentials',
49
- }),
50
- credentials: 'include',
51
- signal: controller.signal,
52
- });
53
- clearTimeout(timeoutId);
54
- if (!response.ok) {
55
- const defaultError = { message: response.statusText };
56
- const errorData = yield response.json().catch(() => defaultError);
57
- const error = {
58
- message: errorData.message || response.statusText,
59
- code: errorData.code,
60
- status: response.status,
61
- details: errorData.details,
62
- };
63
- throw error;
64
- }
65
- const data = yield response.json();
66
- this.tokenManager.setToken(data);
67
- this.initialized = true;
29
+ const token = yield this.tokenManager.getToken();
30
+ this.initialized = !!token;
68
31
  }
69
32
  catch (error) {
70
- console.error('Failed to initialize bearer authentication:', error);
71
- this.initialized = false; // Reset initialized state on error
72
- throw error;
33
+ console.error('Failed to initialize auth:', error);
34
+ this.initialized = false;
73
35
  }
74
36
  finally {
75
- this.initializing = false;
37
+ this.initializationPromise = null;
76
38
  }
77
39
  });
78
40
  }
79
41
  ensureInitialized() {
80
42
  return __awaiter(this, void 0, void 0, function* () {
81
- if (this.config.authType !== 'bearer' || this.initialized) {
43
+ if (this.config.authType !== FlexbeAuthType.BEARER || this.initialized) {
82
44
  return;
83
45
  }
84
- yield this.initializeBearerAuth();
46
+ // If initialization is in progress, wait for it
47
+ if (this.initializationPromise) {
48
+ yield this.initializationPromise;
49
+ return;
50
+ }
51
+ // If not initialized and no initialization in progress, start one
52
+ yield this.initialize();
85
53
  });
86
54
  }
87
55
  getAuthHeaders() {
@@ -90,29 +58,17 @@ export class FlexbeAuth {
90
58
  const headers = {
91
59
  'Content-Type': 'application/json',
92
60
  };
93
- if (this.config.authType === 'apiKey') {
61
+ if (this.config.authType === FlexbeAuthType.API_KEY) {
94
62
  headers['x-api-key'] = this.config.apiKey;
95
63
  }
96
- else if (this.config.authType === 'bearer') {
97
- const token = this.tokenManager.getToken();
64
+ else if (this.config.authType === FlexbeAuthType.BEARER) {
65
+ const token = yield this.tokenManager.getToken();
98
66
  if (!token) {
99
- // If no token is available, try to initialize again
100
- this.initialized = false;
101
- yield this.ensureInitialized();
102
- const newToken = this.tokenManager.getToken();
103
- if (!newToken) {
104
- throw new Error('No valid bearer token available');
105
- }
106
- headers['Authorization'] = `Bearer ${newToken}`;
107
- }
108
- else {
109
- headers['Authorization'] = `Bearer ${token}`;
67
+ throw new Error('No valid bearer token available');
110
68
  }
69
+ headers['Authorization'] = `Bearer ${token}`;
111
70
  }
112
71
  return headers;
113
72
  });
114
73
  }
115
- isInitialized() {
116
- return this.initialized;
117
- }
118
74
  }
@@ -1,3 +1,4 @@
1
+ import { FlexbeAuthType } from '../types';
1
2
  import { Pages } from './pages';
2
3
  import { ApiClient } from './api-client';
3
4
  export class FlexbeClient {
@@ -13,7 +14,7 @@ export class FlexbeClient {
13
14
  timeout: (config === null || config === void 0 ? void 0 : config.timeout) || 30000,
14
15
  apiKey: (config === null || config === void 0 ? void 0 : config.apiKey) || getEnvVar('FLEXBE_API_KEY') || '',
15
16
  siteId: (config === null || config === void 0 ? void 0 : config.siteId) || getEnvVar('FLEXBE_SITE_ID'),
16
- authType: (config === null || config === void 0 ? void 0 : config.authType) || 'apiKey',
17
+ authType: (config === null || config === void 0 ? void 0 : config.authType) || FlexbeAuthType.API_KEY,
17
18
  };
18
19
  if (this.config.authType === 'apiKey' && !this.config.apiKey) {
19
20
  throw new Error('API key is required when using apiKey authentication. Please provide it either through config or FLEXBE_API_KEY environment variable.');
@@ -1,9 +1,23 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  const TOKEN_STORAGE_KEY = 'flexbe_jwt_token';
2
11
  const REFRESH_THRESHOLD = 0.8; // Refresh when 80% of token lifetime has passed
12
+ const REFRESH_CHECK_INTERVAL = 30 * 1000; // Check every 30 seconds
13
+ const MAX_REFRESH_DELAY = 10000; // Maximum random delay of 10 seconds
3
14
  export class TokenManager {
4
15
  constructor() {
5
16
  this.token = null;
6
17
  this.refreshInterval = null;
18
+ this.refreshTimeout = null;
19
+ this.tokenPromise = null;
20
+ this.debug = false;
7
21
  this.initializeFromStorage();
8
22
  this.setupStorageListener();
9
23
  }
@@ -14,59 +28,61 @@ export class TokenManager {
14
28
  return TokenManager.instance;
15
29
  }
16
30
  initializeFromStorage() {
17
- if (typeof window !== 'undefined') {
18
- const storedToken = localStorage.getItem(TOKEN_STORAGE_KEY);
19
- if (storedToken) {
20
- try {
21
- this.token = JSON.parse(storedToken);
22
- if (this.token.expiresAt > Date.now()) {
23
- console.log('Reusing stored token:', {
24
- expiresIn: `${Math.round((this.token.expiresAt - Date.now()) / 1000)} seconds`,
25
- expiresAt: new Date(this.token.expiresAt).toISOString(),
26
- });
27
- this.startRefreshInterval();
28
- }
29
- else {
30
- this.clearToken();
31
- }
32
- }
33
- catch (error) {
34
- console.error('Failed to parse stored token:', error);
35
- this.clearToken();
36
- }
31
+ if (typeof window === 'undefined')
32
+ return;
33
+ const storedToken = localStorage.getItem(TOKEN_STORAGE_KEY);
34
+ if (!storedToken)
35
+ return;
36
+ try {
37
+ this.token = JSON.parse(storedToken);
38
+ if (this.token.expiresAt > Date.now()) {
39
+ this.logTokenStatus('Token loaded from storage');
40
+ this.startRefreshInterval();
41
+ }
42
+ else {
43
+ this.clearToken();
37
44
  }
38
45
  }
46
+ catch (error) {
47
+ console.error('Failed to parse stored token:', error);
48
+ this.clearToken();
49
+ }
39
50
  }
40
51
  setupStorageListener() {
41
- if (typeof window !== 'undefined') {
42
- window.addEventListener('storage', (event) => {
43
- if (event.key === TOKEN_STORAGE_KEY) {
44
- if (event.newValue) {
45
- try {
46
- const newToken = JSON.parse(event.newValue);
47
- if (newToken.expiresAt > Date.now()) {
48
- this.token = newToken;
49
- console.log('Token updated from storage:', {
50
- expiresIn: `${Math.round((newToken.expiresAt - Date.now()) / 1000)} seconds`,
51
- expiresAt: new Date(newToken.expiresAt).toISOString(),
52
- });
53
- this.startRefreshInterval();
54
- }
55
- else {
56
- this.clearToken();
57
- }
58
- }
59
- catch (error) {
60
- console.error('Failed to parse token from storage event:', error);
61
- this.clearToken();
62
- }
63
- }
64
- else {
65
- this.clearToken();
66
- }
52
+ if (typeof window === 'undefined')
53
+ return;
54
+ window.addEventListener('storage', (event) => {
55
+ if (event.key !== TOKEN_STORAGE_KEY)
56
+ return;
57
+ if (!event.newValue) {
58
+ this.clearToken();
59
+ void this.retrieveToken();
60
+ return;
61
+ }
62
+ try {
63
+ const newToken = JSON.parse(event.newValue);
64
+ // Skip if the new token is exactly the same as current token
65
+ if (this.token &&
66
+ this.token.accessToken === newToken.accessToken &&
67
+ this.token.expiresAt === newToken.expiresAt) {
68
+ return;
67
69
  }
68
- });
69
- }
70
+ if (newToken.expiresAt > Date.now()) {
71
+ this.token = newToken;
72
+ this.logTokenStatus('Token updated from storage');
73
+ this.startRefreshInterval();
74
+ }
75
+ else {
76
+ this.clearToken();
77
+ void this.retrieveToken();
78
+ }
79
+ }
80
+ catch (error) {
81
+ console.error('Failed to parse token from storage event:', error);
82
+ this.clearToken();
83
+ void this.retrieveToken();
84
+ }
85
+ });
70
86
  }
71
87
  getExpirationFromToken(token) {
72
88
  try {
@@ -79,32 +95,128 @@ export class TokenManager {
79
95
  return Date.now() + (4 * 60 * 1000); // Default to 4 minutes if parsing fails
80
96
  }
81
97
  }
98
+ getToken() {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ var _a, _b;
101
+ const token = this.token;
102
+ if (token && token.expiresAt && token.expiresAt > Date.now()) {
103
+ return token.accessToken;
104
+ }
105
+ yield this.retrieveToken();
106
+ return (_b = (_a = this.token) === null || _a === void 0 ? void 0 : _a.accessToken) !== null && _b !== void 0 ? _b : null;
107
+ });
108
+ }
109
+ retrieveToken() {
110
+ return __awaiter(this, void 0, void 0, function* () {
111
+ if (this.tokenPromise) {
112
+ yield this.tokenPromise;
113
+ return;
114
+ }
115
+ this.tokenPromise = this.doRetrieveToken();
116
+ try {
117
+ yield this.tokenPromise;
118
+ }
119
+ finally {
120
+ this.tokenPromise = null;
121
+ }
122
+ });
123
+ }
124
+ doRetrieveToken() {
125
+ return __awaiter(this, void 0, void 0, function* () {
126
+ const controller = new AbortController();
127
+ const timeoutId = setTimeout(() => controller.abort(), 30000);
128
+ try {
129
+ const response = yield fetch('/oauth/token', {
130
+ method: 'POST',
131
+ headers: { 'Content-Type': 'application/json' },
132
+ body: JSON.stringify({ grant_type: 'client_credentials' }),
133
+ credentials: 'include',
134
+ signal: controller.signal,
135
+ });
136
+ clearTimeout(timeoutId);
137
+ if (!response.ok) {
138
+ const errorData = yield response.json().catch(() => ({ message: response.statusText }));
139
+ throw new Error(errorData.message || response.statusText);
140
+ }
141
+ const data = yield response.json();
142
+ this.setToken(data);
143
+ }
144
+ catch (error) {
145
+ console.error('Failed to retrieve token:', error);
146
+ this.clearToken();
147
+ throw error;
148
+ }
149
+ });
150
+ }
82
151
  startRefreshInterval() {
152
+ this.clearRefreshTimers();
153
+ if (!this.token)
154
+ return;
155
+ const tokenLifetime = this.token.expiresAt - Date.now();
156
+ const refreshThreshold = Math.round(tokenLifetime * REFRESH_THRESHOLD);
157
+ const timeUntilRefresh = refreshThreshold - (Math.random() * MAX_REFRESH_DELAY);
158
+ this.logTokenStatus('Starting refresh interval', {
159
+ tokenLifetime: `${Math.round(tokenLifetime / 1000)} seconds`,
160
+ refreshThreshold: `${Math.round(refreshThreshold / 1000)} seconds`,
161
+ timeUntilRefresh: `${Math.round(timeUntilRefresh / 1000)} seconds`,
162
+ });
163
+ this.scheduleRefresh(timeUntilRefresh);
164
+ this.refreshInterval = window.setInterval(() => {
165
+ if (!this.token)
166
+ return;
167
+ const timeUntilExpiry = this.token.expiresAt - Date.now();
168
+ if (timeUntilExpiry <= 0) {
169
+ this.logTokenStatus('Token expired');
170
+ this.clearToken();
171
+ void this.retrieveToken();
172
+ return;
173
+ }
174
+ const refreshThreshold = Math.round(timeUntilExpiry * REFRESH_THRESHOLD);
175
+ if (timeUntilExpiry <= refreshThreshold) {
176
+ this.logTokenStatus('Refreshing token', {
177
+ timeUntilExpiry: `${Math.round(timeUntilExpiry / 1000)} seconds`,
178
+ refreshThreshold: `${Math.round(refreshThreshold / 1000)} seconds`,
179
+ });
180
+ this.scheduleRefresh(refreshThreshold - (Math.random() * MAX_REFRESH_DELAY));
181
+ }
182
+ }, REFRESH_CHECK_INTERVAL);
183
+ }
184
+ scheduleRefresh(delay) {
185
+ if (this.refreshTimeout) {
186
+ window.clearTimeout(this.refreshTimeout);
187
+ }
188
+ this.refreshTimeout = window.setTimeout(() => {
189
+ const token = this.token;
190
+ if (token && token.expiresAt - Date.now() <= token.expiresAt * REFRESH_THRESHOLD) {
191
+ void this.retrieveToken();
192
+ }
193
+ }, delay);
194
+ }
195
+ clearRefreshTimers() {
83
196
  if (this.refreshInterval) {
84
197
  clearInterval(this.refreshInterval);
198
+ this.refreshInterval = null;
85
199
  }
86
- if (this.token) {
87
- const tokenLifetime = this.token.expiresAt - (this.token.expiresAt - 4 * 60 * 1000); // 4 minutes in milliseconds
88
- const refreshTime = Math.round(tokenLifetime * REFRESH_THRESHOLD);
89
- console.log('Setting up token refresh:', {
90
- tokenLifetime: `${Math.round(tokenLifetime / 1000)} seconds`,
91
- refreshIn: `${Math.round(refreshTime / 1000)} seconds`,
92
- refreshAt: new Date(Date.now() + refreshTime).toISOString(),
93
- });
94
- this.refreshInterval = window.setInterval(() => {
95
- this.clearToken();
96
- }, refreshTime);
200
+ if (this.refreshTimeout) {
201
+ window.clearTimeout(this.refreshTimeout);
202
+ this.refreshTimeout = null;
97
203
  }
98
204
  }
205
+ logTokenStatus(message, additionalInfo = {}) {
206
+ if (!this.debug)
207
+ return;
208
+ const token = this.token;
209
+ if (!token)
210
+ return;
211
+ console.log(message, Object.assign({ expiresIn: `${Math.round((token.expiresAt - Date.now()) / 1000)} seconds`, expiresAt: new Date(token.expiresAt).toISOString() }, additionalInfo));
212
+ }
99
213
  setToken(tokenResponse) {
100
214
  const expiresAt = this.getExpirationFromToken(tokenResponse.accessToken);
101
215
  this.token = {
102
216
  accessToken: tokenResponse.accessToken,
103
217
  expiresAt,
104
218
  };
105
- const expiresIn = Math.round((expiresAt - Date.now()) / 1000);
106
- console.log('New access token obtained:', {
107
- expiresIn: `${expiresIn} seconds`,
219
+ this.logTokenStatus('Token set', {
108
220
  expiresAt: new Date(expiresAt).toISOString(),
109
221
  });
110
222
  if (typeof window !== 'undefined') {
@@ -112,19 +224,9 @@ export class TokenManager {
112
224
  }
113
225
  this.startRefreshInterval();
114
226
  }
115
- getToken() {
116
- if (this.token && this.token.expiresAt > Date.now()) {
117
- return this.token.accessToken;
118
- }
119
- this.clearToken();
120
- return null;
121
- }
122
227
  clearToken() {
123
228
  this.token = null;
124
- if (this.refreshInterval) {
125
- clearInterval(this.refreshInterval);
126
- this.refreshInterval = null;
127
- }
229
+ this.clearRefreshTimers();
128
230
  if (typeof window !== 'undefined') {
129
231
  localStorage.removeItem(TOKEN_STORAGE_KEY);
130
232
  }
@@ -1 +1,5 @@
1
- export {};
1
+ export var FlexbeAuthType;
2
+ (function (FlexbeAuthType) {
3
+ FlexbeAuthType["API_KEY"] = "apiKey";
4
+ FlexbeAuthType["BEARER"] = "bearer";
5
+ })(FlexbeAuthType || (FlexbeAuthType = {}));
@@ -1,107 +1,63 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FlexbeAuth = void 0;
4
+ const types_1 = require("../types");
4
5
  const token_manager_1 = require("./token-manager");
5
6
  class FlexbeAuth {
6
7
  constructor(config) {
7
8
  this.initialized = false;
8
- this.initializing = false;
9
+ this.initializationPromise = null;
9
10
  this.config = config;
10
11
  this.tokenManager = token_manager_1.TokenManager.getInstance();
11
- if (this.config.authType === 'bearer') {
12
- // Check if we have a valid token in storage
13
- const existingToken = this.tokenManager.getToken();
14
- if (existingToken) {
15
- this.initialized = true;
16
- }
17
- // Don't start initialization here, let ensureInitialized handle it
12
+ if (this.config.authType === types_1.FlexbeAuthType.BEARER) {
13
+ // Start initialization but don't wait for it
14
+ this.initializationPromise = this.initialize();
18
15
  }
19
16
  else {
20
17
  this.initialized = true;
21
18
  }
22
19
  }
23
- async initializeBearerAuth() {
24
- if (this.initializing) {
25
- // Wait for the ongoing initialization to complete
26
- while (this.initializing) {
27
- await new Promise(resolve => setTimeout(resolve, 100));
28
- }
29
- return;
30
- }
20
+ async initialize() {
31
21
  try {
32
- this.initializing = true;
33
- const controller = new AbortController();
34
- const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
35
- const response = await fetch('/oauth/token', {
36
- method: 'POST',
37
- headers: {
38
- 'Content-Type': 'application/json',
39
- },
40
- body: JSON.stringify({
41
- grant_type: 'client_credentials',
42
- }),
43
- credentials: 'include',
44
- signal: controller.signal,
45
- });
46
- clearTimeout(timeoutId);
47
- if (!response.ok) {
48
- const defaultError = { message: response.statusText };
49
- const errorData = await response.json().catch(() => defaultError);
50
- const error = {
51
- message: errorData.message || response.statusText,
52
- code: errorData.code,
53
- status: response.status,
54
- details: errorData.details,
55
- };
56
- throw error;
57
- }
58
- const data = await response.json();
59
- this.tokenManager.setToken(data);
60
- this.initialized = true;
22
+ const token = await this.tokenManager.getToken();
23
+ this.initialized = !!token;
61
24
  }
62
25
  catch (error) {
63
- console.error('Failed to initialize bearer authentication:', error);
64
- this.initialized = false; // Reset initialized state on error
65
- throw error;
26
+ console.error('Failed to initialize auth:', error);
27
+ this.initialized = false;
66
28
  }
67
29
  finally {
68
- this.initializing = false;
30
+ this.initializationPromise = null;
69
31
  }
70
32
  }
71
33
  async ensureInitialized() {
72
- if (this.config.authType !== 'bearer' || this.initialized) {
34
+ if (this.config.authType !== types_1.FlexbeAuthType.BEARER || this.initialized) {
35
+ return;
36
+ }
37
+ // If initialization is in progress, wait for it
38
+ if (this.initializationPromise) {
39
+ await this.initializationPromise;
73
40
  return;
74
41
  }
75
- await this.initializeBearerAuth();
42
+ // If not initialized and no initialization in progress, start one
43
+ await this.initialize();
76
44
  }
77
45
  async getAuthHeaders() {
78
46
  await this.ensureInitialized();
79
47
  const headers = {
80
48
  'Content-Type': 'application/json',
81
49
  };
82
- if (this.config.authType === 'apiKey') {
50
+ if (this.config.authType === types_1.FlexbeAuthType.API_KEY) {
83
51
  headers['x-api-key'] = this.config.apiKey;
84
52
  }
85
- else if (this.config.authType === 'bearer') {
86
- const token = this.tokenManager.getToken();
53
+ else if (this.config.authType === types_1.FlexbeAuthType.BEARER) {
54
+ const token = await this.tokenManager.getToken();
87
55
  if (!token) {
88
- // If no token is available, try to initialize again
89
- this.initialized = false;
90
- await this.ensureInitialized();
91
- const newToken = this.tokenManager.getToken();
92
- if (!newToken) {
93
- throw new Error('No valid bearer token available');
94
- }
95
- headers['Authorization'] = `Bearer ${newToken}`;
96
- }
97
- else {
98
- headers['Authorization'] = `Bearer ${token}`;
56
+ throw new Error('No valid bearer token available');
99
57
  }
58
+ headers['Authorization'] = `Bearer ${token}`;
100
59
  }
101
60
  return headers;
102
61
  }
103
- isInitialized() {
104
- return this.initialized;
105
- }
106
62
  }
107
63
  exports.FlexbeAuth = FlexbeAuth;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FlexbeClient = void 0;
4
+ const types_1 = require("../types");
4
5
  const pages_1 = require("./pages");
5
6
  const api_client_1 = require("./api-client");
6
7
  class FlexbeClient {
@@ -16,7 +17,7 @@ class FlexbeClient {
16
17
  timeout: config?.timeout || 30000,
17
18
  apiKey: config?.apiKey || getEnvVar('FLEXBE_API_KEY') || '',
18
19
  siteId: config?.siteId || getEnvVar('FLEXBE_SITE_ID'),
19
- authType: config?.authType || 'apiKey',
20
+ authType: config?.authType || types_1.FlexbeAuthType.API_KEY,
20
21
  };
21
22
  if (this.config.authType === 'apiKey' && !this.config.apiKey) {
22
23
  throw new Error('API key is required when using apiKey authentication. Please provide it either through config or FLEXBE_API_KEY environment variable.');