搜索插件、代码仓与 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 工作流程无缝集成,并支持所有主要平台,包括 Web、移动设备和桌面。

适合场景

  • 需要刷新令牌的 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 项

源码仓库

使用此插件的项目

暂无相关数据。

所在榜单