搜尋套件、儲存庫與 APP 分類

套件詳情v0.2.0

fresh_http

一個自動刷新的 HTTP 客戶端。基於 package:http 建構,並透明地管理驗證令牌。

AndroidiOSLinuxmacOSWebWindows
近 30 天下載
6.1萬
喜歡數
5
Pub 分數
160

安裝套件

flutter pub add fresh_http

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Fresh Http 是一個基於 `http` 套件建構的 Flutter/Dart 套件,提供自動重新整理的 HTTP 客戶端。它會自動處理驗證金鑰的重新整理,確保在無需手動管理金鑰的情況下實現無縫 API 呼叫。對於需要安全且持續存取已驗證 API 的應用程式而言,該客戶端透過透明地重新整理過期金鑰來減少重複程式碼並提升可靠性。該客戶端可與現有的 HTTP 工作流程順暢整合,並支援所有主要平台,包括網頁、行動裝置和桌面。

適用情境

  • 需要刷新令牌的 API
  • 安全的移動和Web應用程式
  • 減少驗證樣板程式碼
  • 自動令牌管理
  • 有狀態的 API 客戶端

主要特色

  • 自動令牌刷新
  • 基於 `package:http`
  • 透明的身分驗證處理
  • 跨平台支援
  • 所需的設定最少

適合對象

  • 使用 JWT 或 OAuth2 的應用
  • 尋求降低認證複雜性的開發者
  • 需要可靠 API 連接的專案
  • 重視開發者體驗的團隊
  • 頻繁進行 API 互動的應用程式

使用建議

  • 需要自定義令牌刷新邏輯
  • 依賴外部驗證狀態管理
  • 僅限基於 HTTP 的 API
  • 不適用於一次性請求
  • 令牌刷新流程中需要適當的錯誤處理

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Fresh 🍋

build License: MIT GitHub stars


A collection of packages for automatic token refresh in Dart and Flutter. Fresh handles refreshing, caching, and attaching authentication tokens transparently so your API calls just work.

Why Fresh?

Token-based authentication seems simple until you handle the edge cases: tokens expire mid-session, multiple requests fail at the same time triggering duplicate refreshes, refresh tokens get revoked, and you need to route users to login when auth is lost. Fresh handles all of this so you don't have to.

  • No more manual 401 handling scattered across your codebase
  • No more race conditions when concurrent requests trigger simultaneous refreshes
  • No more stale tokens causing request failures - expired tokens are refreshed proactively before the request is even sent

Packages

Package Pub
fresh pub package
fresh_dio pub package
fresh_graphql pub package
fresh_http pub package

Features

  • Automatic token refresh on 401 / auth errors, with automatic request retry
  • Proactive refresh before requests when the token is expired
  • Single-flight refresh - concurrent requests share one refresh call instead of triggering multiple
  • authenticationStatus stream for reacting to login/logout events
  • Pluggable TokenStorage - bring your own persistence layer
  • Built-in OAuth2Token with expiresAt support

Usage

fresh_dio
final dio = Dio();

dio.interceptors.add(
  Fresh.oAuth2(
    tokenStorage: InMemoryTokenStorage<OAuth2Token>(),
    refreshToken: (token, client) async {
      final response = await client.post(
        'https://api.example.com/auth/refresh',
        data: {'refresh_token': token?.refreshToken},
      );
      return OAuth2Token(
        accessToken: response.data['access_token'],
        refreshToken: response.data['refresh_token'],
      );
    },
  ),
);

See the fresh_dio README for full documentation.

fresh_graphql
import 'dart:convert';

final freshLink = FreshLink.oAuth2(
  tokenStorage: InMemoryTokenStorage<OAuth2Token>(),
  refreshToken: (token, client) async {
    final response = await client.post(
      Uri.parse('https://api.example.com/auth/refresh'),
      body: jsonEncode({'refresh_token': token?.refreshToken}),
    );
    final body = jsonDecode(response.body) as Map<String, dynamic>;
    return OAuth2Token(
      accessToken: body['access_token'],
      refreshToken: body['refresh_token'],
    );
  },
  shouldRefresh: (response) =>
      response.errors?.any((e) => e.message.contains('UNAUTHENTICATED')) ?? false,
);

// HttpLink comes from package:gql_http_link
final link = Link.from([freshLink, HttpLink('https://api.example.com/graphql')]);

See the fresh_graphql README for full documentation.

fresh_http
import 'dart:convert';

final client = Fresh.oAuth2(
  tokenStorage: InMemoryTokenStorage<OAuth2Token>(),
  refreshToken: (token, client) async {
    final response = await client.post(
      Uri.parse('https://api.example.com/auth/refresh'),
      body: jsonEncode({'refresh_token': token?.refreshToken}),
    );
    final body = jsonDecode(response.body) as Map<String, dynamic>;
    return OAuth2Token(
      accessToken: body['access_token'],
      refreshToken: body['refresh_token'],
    );
  },
);

See the fresh_http README for full documentation.

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。

所在榜單