From 04ccd3e5988f00fb9258b482da01a08dedb910c1 Mon Sep 17 00:00:00 2001 From: dankinsoid <30962149+dankinsoid@users.noreply.github.com> Date: Sun, 17 Nov 2024 00:04:28 +0300 Subject: [PATCH] 1.39.0 --- .../TokenRefresher/TokenRefresher.swift | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftAPIClient/Modifiers/TokenRefresher/TokenRefresher.swift b/Sources/SwiftAPIClient/Modifiers/TokenRefresher/TokenRefresher.swift index 599ff57..da9dbcb 100644 --- a/Sources/SwiftAPIClient/Modifiers/TokenRefresher/TokenRefresher.swift +++ b/Sources/SwiftAPIClient/Modifiers/TokenRefresher/TokenRefresher.swift @@ -13,6 +13,24 @@ public extension APIClient { /// - auth: The closure that creates an `AuthModifier` for the new token. Default to `.bearer(token:)`. /// /// - Warning: Don't use this modifier with `.auth(_ modifier:)` as it will be override it. + /// + /// ### Example Usage: + /// #### Initial Token Request + /// ```swift + /// let client = APIClient() + /// .tokenRefresher { refreshToken, client, _ in + /// let response: AuthResponse = try await client("refresh").post(RefreshRequest(token: refreshToken))) + /// return (response.accessToken, response.refreshToken, response.expiryDate) + /// } + /// + /// func login(_ body: LoginBody) async throws { + /// let response: AuthResponse = try await client("login").auth(enabled: false).post(body) + /// // Tokens must be cached. + /// try await SecureCacheService.keychain.save(response.accessToken, for: .accessToken) + /// try await SecureCacheService.keychain.save(response.refreshToken, for: .refreshToken) + /// try await SecureCacheService.keychain.save(response.expiryDate, for: .expiryDate) + /// } + /// ``` func tokenRefresher( cacheService: SecureCacheService = valueFor(live: .keychain, test: .mock), expiredStatusCodes: Set = [.unauthorized], @@ -41,6 +59,24 @@ public extension APIClient { /// - auth: The closure that creates an `AuthModifier` for the new token. Default to `.bearer(token:)`. /// /// - Warning: Don't use this modifier with `.auth(_ modifier:)` as it will be override it. + /// + /// ### Example Usage: + /// #### Initial Token Request + /// ```swift + /// let client = APIClient() + /// .tokenRefresher(cacheService: cacheService) { refreshToken, client, _ in + /// let response: AuthResponse = try await client("refresh").post(RefreshRequest(token: refreshToken))) + /// return (response.accessToken, response.refreshToken, response.expiryDate) + /// } + /// + /// func login(_ body: LoginBody) async throws { + /// let response: AuthResponse = try await client("login").auth(enabled: false).post(body) + /// // Tokens must be cached. + /// try await cacheService.save(response.accessToken, for: .accessToken) + /// try await cacheService.save(response.refreshToken, for: .refreshToken) + /// try await cacheService.save(response.expiryDate, for: .expiryDate) + /// } + /// ``` func tokenRefresher( cacheService: SecureCacheService, expiredStatusCodes: Set = [.unauthorized], @@ -148,6 +184,6 @@ public struct TokenRefresherMiddleware: HTTPClientMiddleware { } public struct TokenNotFound: Error { - - public init() {} + + public init() {} }