astro-tokenkit 1.0.17 → 1.0.18

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.d.ts CHANGED
@@ -176,6 +176,37 @@ interface RetryConfig {
176
176
  /** Initial delay in ms */
177
177
  delay?: number;
178
178
  }
179
+ /**
180
+ * Idle timeout configuration
181
+ */
182
+ interface IdleConfig {
183
+ /** Idle timeout in seconds */
184
+ timeout: number;
185
+ /**
186
+ * Callback when idle timeout is reached.
187
+ * NOTE: This function is only used if you manually initialize IdleManager.
188
+ * If using the Astro integration, use window.addEventListener('tk:idle', ...) instead.
189
+ */
190
+ onIdle?: () => void;
191
+ /** Whether to automatically logout on idle (default: true) */
192
+ autoLogout?: boolean;
193
+ /** Whether to monitor activity only on the active tab (default: true) */
194
+ activeTabOnly?: boolean;
195
+ /**
196
+ * Custom data to pass to the 'tk:idle' event.
197
+ * Ideal for configuring client-side alerts (e.g. SweetAlert).
198
+ */
199
+ alert?: AlertOptions | any;
200
+ }
201
+ /**
202
+ * Alert options for client-side notifications (e.g. SweetAlert)
203
+ */
204
+ interface AlertOptions {
205
+ title?: string;
206
+ text?: string;
207
+ icon?: 'success' | 'error' | 'warning' | 'info' | 'question' | string;
208
+ [key: string]: any;
209
+ }
179
210
  /**
180
211
  * Request interceptor
181
212
  */
@@ -212,7 +243,9 @@ interface ClientConfig {
212
243
  retry?: RetryConfig;
213
244
  /** Interceptors */
214
245
  interceptors?: InterceptorsConfig;
215
- /** AsyncLocalStorage instance */
246
+ /** Idle timeout configuration */
247
+ idle?: IdleConfig;
248
+ /** AsyncLocalStorage instance (Node only) */
216
249
  context?: AsyncLocalStorage<any>;
217
250
  /** Custom context store getter */
218
251
  getContextStore?: () => TokenKitContext | undefined | null;
@@ -413,6 +446,13 @@ declare function createClient(config?: Partial<TokenKitConfig>): APIClient;
413
446
  * Astro integration for TokenKit
414
447
  *
415
448
  * This integration facilitates the setup of TokenKit in an Astro project.
449
+ * It performs the following:
450
+ * - Sets the global configuration for the API client.
451
+ * - Injects the configuration into the client-side via Vite's `define`.
452
+ * - Automatically registers the TokenKit middleware (unless `autoMiddleware` is set to `false`).
453
+ * - Injects a client-side script (`astro-tokenkit/client-init`) to handle idle session monitoring and automatic logout.
454
+ *
455
+ * @param config - TokenKit configuration options.
416
456
  *
417
457
  * @example
418
458
  * ```ts
@@ -426,6 +466,10 @@ declare function createClient(config?: Partial<TokenKitConfig>): APIClient;
426
466
  * auth: {
427
467
  * login: '/auth/login',
428
468
  * refresh: '/auth/refresh',
469
+ * },
470
+ * idle: {
471
+ * timeout: 3600, // 1 hour
472
+ * alert: { title: 'Session Expired' }
429
473
  * }
430
474
  * })
431
475
  * ]
@@ -434,7 +478,18 @@ declare function createClient(config?: Partial<TokenKitConfig>): APIClient;
434
478
  */
435
479
  declare function tokenKit(config: TokenKitConfig): AstroIntegration;
436
480
  /**
437
- * Helper to define middleware in a separate file if needed
481
+ * Helper to create the TokenKit middleware.
482
+ *
483
+ * Use this if you have `autoMiddleware: false` in your integration configuration
484
+ * and want to manually register the middleware in your `src/middleware.ts` file.
485
+ *
486
+ * @example
487
+ * ```ts
488
+ * // src/middleware.ts
489
+ * import { defineMiddleware } from 'astro-tokenkit';
490
+ *
491
+ * export const onRequest = defineMiddleware();
492
+ * ```
438
493
  */
439
494
  declare const defineMiddleware: () => astro.MiddlewareHandler;
440
495
 
package/dist/index.js CHANGED
@@ -1225,7 +1225,7 @@ class APIClient {
1225
1225
  throw new Error('Auth is not configured for this client');
1226
1226
  }
1227
1227
  const context = getContextStore();
1228
- return this.tokenManager.login(context, credentials, options);
1228
+ return yield this.tokenManager.login(context, credentials, options);
1229
1229
  });
1230
1230
  }
1231
1231
  /**
@@ -1282,6 +1282,13 @@ function createClient(config) {
1282
1282
  * Astro integration for TokenKit
1283
1283
  *
1284
1284
  * This integration facilitates the setup of TokenKit in an Astro project.
1285
+ * It performs the following:
1286
+ * - Sets the global configuration for the API client.
1287
+ * - Injects the configuration into the client-side via Vite's `define`.
1288
+ * - Automatically registers the TokenKit middleware (unless `autoMiddleware` is set to `false`).
1289
+ * - Injects a client-side script (`astro-tokenkit/client-init`) to handle idle session monitoring and automatic logout.
1290
+ *
1291
+ * @param config - TokenKit configuration options.
1285
1292
  *
1286
1293
  * @example
1287
1294
  * ```ts
@@ -1295,6 +1302,10 @@ function createClient(config) {
1295
1302
  * auth: {
1296
1303
  * login: '/auth/login',
1297
1304
  * refresh: '/auth/refresh',
1305
+ * },
1306
+ * idle: {
1307
+ * timeout: 3600, // 1 hour
1308
+ * alert: { title: 'Session Expired' }
1298
1309
  * }
1299
1310
  * })
1300
1311
  * ]
@@ -1312,7 +1323,7 @@ function tokenKit(config) {
1312
1323
  return {
1313
1324
  name: 'astro-tokenkit',
1314
1325
  hooks: {
1315
- 'astro:config:setup': ({ updateConfig, addMiddleware }) => {
1326
+ 'astro:config:setup': ({ updateConfig, addMiddleware, injectScript }) => {
1316
1327
  updateConfig({
1317
1328
  vite: {
1318
1329
  define: {
@@ -1327,13 +1338,26 @@ function tokenKit(config) {
1327
1338
  order: 'pre'
1328
1339
  });
1329
1340
  }
1341
+ // Always inject the client-side script for idle monitoring
1342
+ injectScript('page', `import 'astro-tokenkit/client-init';`);
1330
1343
  logger.debug('[TokenKit] Integration initialized');
1331
1344
  },
1332
1345
  },
1333
1346
  };
1334
1347
  }
1335
1348
  /**
1336
- * Helper to define middleware in a separate file if needed
1349
+ * Helper to create the TokenKit middleware.
1350
+ *
1351
+ * Use this if you have `autoMiddleware: false` in your integration configuration
1352
+ * and want to manually register the middleware in your `src/middleware.ts` file.
1353
+ *
1354
+ * @example
1355
+ * ```ts
1356
+ * // src/middleware.ts
1357
+ * import { defineMiddleware } from 'astro-tokenkit';
1358
+ *
1359
+ * export const onRequest = defineMiddleware();
1360
+ * ```
1337
1361
  */
1338
1362
  const defineMiddleware = () => createMiddleware();
1339
1363