@strands.gg/accui 2.0.0 → 2.1.0

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.
@@ -10293,6 +10293,132 @@ const StrandsUIPlugin = {
10293
10293
  });
10294
10294
  }
10295
10295
  };
10296
+ function useAuthenticatedFetch() {
10297
+ const { currentSession, refreshToken, getAuthHeaders } = useStrandsAuth();
10298
+ const authenticatedFetch = async (url, options = {}) => {
10299
+ const {
10300
+ autoRefresh = true,
10301
+ requireAuth = true,
10302
+ baseURL,
10303
+ ...fetchOptions
10304
+ } = options;
10305
+ if (requireAuth && !currentSession.value?.accessToken) {
10306
+ throw new Error("User is not authenticated");
10307
+ }
10308
+ let fullUrl = url;
10309
+ if (baseURL && typeof url === "string" && !url.startsWith("http")) {
10310
+ fullUrl = new URL(url, baseURL).toString();
10311
+ }
10312
+ const headers = new Headers(fetchOptions.headers);
10313
+ if (currentSession.value?.accessToken) {
10314
+ try {
10315
+ const authHeaders = getAuthHeaders();
10316
+ Object.entries(authHeaders).forEach(([key, value]) => {
10317
+ headers.set(key, value);
10318
+ });
10319
+ } catch (error) {
10320
+ console.warn("[Strands Auth] Failed to get auth headers:", error);
10321
+ if (requireAuth) {
10322
+ throw error;
10323
+ }
10324
+ }
10325
+ }
10326
+ const enhancedOptions = {
10327
+ ...fetchOptions,
10328
+ headers
10329
+ };
10330
+ let response = await fetch(fullUrl, enhancedOptions);
10331
+ if (response.status === 401 && autoRefresh && currentSession.value?.refreshToken) {
10332
+ console.log("[Strands Auth] Request failed with 401, attempting token refresh...");
10333
+ try {
10334
+ const refreshed = await refreshToken();
10335
+ if (refreshed && currentSession.value?.accessToken) {
10336
+ const newAuthHeaders = getAuthHeaders();
10337
+ Object.entries(newAuthHeaders).forEach(([key, value]) => {
10338
+ headers.set(key, value);
10339
+ });
10340
+ console.log("[Strands Auth] Retrying request with refreshed token");
10341
+ response = await fetch(fullUrl, { ...enhancedOptions, headers });
10342
+ }
10343
+ } catch (refreshError) {
10344
+ console.error("[Strands Auth] Token refresh failed:", refreshError);
10345
+ }
10346
+ }
10347
+ return response;
10348
+ };
10349
+ const get = (url, options) => {
10350
+ return authenticatedFetch(url, { ...options, method: "GET" });
10351
+ };
10352
+ const post = (url, body, options) => {
10353
+ const headers = new Headers(options?.headers);
10354
+ if (body && typeof body === "object" && !headers.has("Content-Type")) {
10355
+ headers.set("Content-Type", "application/json");
10356
+ }
10357
+ return authenticatedFetch(url, {
10358
+ ...options,
10359
+ method: "POST",
10360
+ headers,
10361
+ body: typeof body === "object" ? JSON.stringify(body) : body
10362
+ });
10363
+ };
10364
+ const put = (url, body, options) => {
10365
+ const headers = new Headers(options?.headers);
10366
+ if (body && typeof body === "object" && !headers.has("Content-Type")) {
10367
+ headers.set("Content-Type", "application/json");
10368
+ }
10369
+ return authenticatedFetch(url, {
10370
+ ...options,
10371
+ method: "PUT",
10372
+ headers,
10373
+ body: typeof body === "object" ? JSON.stringify(body) : body
10374
+ });
10375
+ };
10376
+ const del = (url, options) => {
10377
+ return authenticatedFetch(url, { ...options, method: "DELETE" });
10378
+ };
10379
+ const patch = (url, body, options) => {
10380
+ const headers = new Headers(options?.headers);
10381
+ if (body && typeof body === "object" && !headers.has("Content-Type")) {
10382
+ headers.set("Content-Type", "application/json");
10383
+ }
10384
+ return authenticatedFetch(url, {
10385
+ ...options,
10386
+ method: "PATCH",
10387
+ headers,
10388
+ body: typeof body === "object" ? JSON.stringify(body) : body
10389
+ });
10390
+ };
10391
+ return {
10392
+ authenticatedFetch,
10393
+ get,
10394
+ post,
10395
+ put,
10396
+ delete: del,
10397
+ patch
10398
+ };
10399
+ }
10400
+ const $authFetch = {
10401
+ get: async (url, options) => {
10402
+ const { get } = useAuthenticatedFetch();
10403
+ return get(url, options);
10404
+ },
10405
+ post: async (url, body, options) => {
10406
+ const { post } = useAuthenticatedFetch();
10407
+ return post(url, body, options);
10408
+ },
10409
+ put: async (url, body, options) => {
10410
+ const { put } = useAuthenticatedFetch();
10411
+ return put(url, body, options);
10412
+ },
10413
+ delete: async (url, options) => {
10414
+ const { delete: del } = useAuthenticatedFetch();
10415
+ return del(url, options);
10416
+ },
10417
+ patch: async (url, body, options) => {
10418
+ const { patch } = useAuthenticatedFetch();
10419
+ return patch(url, body, options);
10420
+ }
10421
+ };
10296
10422
  const isValidEmail = (email) => {
10297
10423
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
10298
10424
  return emailRegex.test(email);
@@ -10364,6 +10490,7 @@ const debounce = (func, wait) => {
10364
10490
  };
10365
10491
  };
10366
10492
  export {
10493
+ $authFetch,
10367
10494
  _sfc_main$l as IconGithub,
10368
10495
  _sfc_main$m as IconGoogle,
10369
10496
  SignedIn,
@@ -10424,6 +10551,7 @@ export {
10424
10551
  preloadCriticalComponents,
10425
10552
  provideStrandsConfig,
10426
10553
  s as setStrandsConfig,
10554
+ useAuthenticatedFetch,
10427
10555
  useOAuthProviders,
10428
10556
  useStrandsAuth,
10429
10557
  useStrandsConfig,