Skip to content

Commit 1391629

Browse files
author
Guilherme Souza
committed
Refactor default initialization values
1 parent 77d9da5 commit 1391629

File tree

6 files changed

+105
-88
lines changed

6 files changed

+105
-88
lines changed

Sources/Auth/AuthClient.swift

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,20 @@ public actor AuthClient {
3333
public init(
3434
url: URL,
3535
headers: [String: String] = [:],
36-
flowType: AuthFlowType? = nil,
37-
localStorage: AuthLocalStorage? = nil,
38-
encoder: JSONEncoder? = nil,
39-
decoder: JSONDecoder? = nil,
36+
flowType: AuthFlowType = Configuration.defaultFlowType,
37+
localStorage: AuthLocalStorage = Configuration.defaultLocalStorage,
38+
encoder: JSONEncoder = AuthClient.Configuration.jsonEncoder,
39+
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
4040
fetch: @escaping FetchHandler = { try await URLSession.shared.data(for: $0) }
4141
) {
42-
var headers = headers
43-
if headers["X-Client-Info"] == nil {
44-
headers["X-Client-Info"] = "gotrue-swift/\(version)"
45-
}
42+
let headers = headers.merging(Configuration.defaultHeaders) { l, _ in l }
4643

4744
self.url = url
4845
self.headers = headers
49-
self.flowType = flowType ?? .implicit
50-
self.localStorage =
51-
localStorage
52-
?? KeychainLocalStorage(
53-
service: "supabase.gotrue.swift",
54-
accessGroup: nil
55-
)
56-
self.encoder = encoder ?? .auth
57-
self.decoder = decoder ?? .auth
46+
self.flowType = flowType
47+
self.localStorage = localStorage
48+
self.encoder = encoder
49+
self.decoder = decoder
5850
self.fetch = fetch
5951
}
6052
}
@@ -96,18 +88,18 @@ public actor AuthClient {
9688
/// - Parameters:
9789
/// - url: The base URL of the Auth server.
9890
/// - headers: Custom headers to be included in requests.
99-
/// - flowType: The authentication flow type. Default is `.implicit`.
91+
/// - flowType: The authentication flow type..
10092
/// - localStorage: The storage mechanism for local data..
10193
/// - encoder: The JSON encoder to use for encoding requests.
10294
/// - decoder: The JSON decoder to use for decoding responses.
10395
/// - fetch: The asynchronous fetch handler for network requests.
10496
public init(
10597
url: URL,
10698
headers: [String: String] = [:],
107-
flowType: AuthFlowType? = nil,
108-
localStorage: AuthLocalStorage? = nil,
109-
encoder: JSONEncoder? = nil,
110-
decoder: JSONDecoder? = nil,
99+
flowType: AuthFlowType = AuthClient.Configuration.defaultFlowType,
100+
localStorage: AuthLocalStorage = AuthClient.Configuration.defaultLocalStorage,
101+
encoder: JSONEncoder = AuthClient.Configuration.jsonEncoder,
102+
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
111103
fetch: @escaping FetchHandler = { try await URLSession.shared.data(for: $0) }
112104
) {
113105
self.init(

Sources/Auth/Defaults.swift

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// Defaults.swift
3+
//
4+
//
5+
// Created by Guilherme Souza on 14/12/23.
6+
//
7+
8+
@_spi(Internal) import _Helpers
9+
import Foundation
10+
11+
extension AuthClient.Configuration {
12+
private static let dateFormatterWithFractionalSeconds = { () -> ISO8601DateFormatter in
13+
let formatter = ISO8601DateFormatter()
14+
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
15+
return formatter
16+
}()
17+
18+
private static let dateFormatter = { () -> ISO8601DateFormatter in
19+
let formatter = ISO8601DateFormatter()
20+
formatter.formatOptions = [.withInternetDateTime]
21+
return formatter
22+
}()
23+
24+
/// The default JSONEncoder instance used by the ``AuthClient``.
25+
public static let jsonEncoder: JSONEncoder = {
26+
let encoder = JSONEncoder()
27+
encoder.keyEncodingStrategy = .convertToSnakeCase
28+
encoder.dateEncodingStrategy = .custom { date, encoder in
29+
var container = encoder.singleValueContainer()
30+
let string = dateFormatter.string(from: date)
31+
try container.encode(string)
32+
}
33+
return encoder
34+
}()
35+
36+
/// The default JSONDecoder instance used by the ``AuthClient``.
37+
public static let jsonDecoder: JSONDecoder = {
38+
let decoder = JSONDecoder()
39+
decoder.keyDecodingStrategy = .convertFromSnakeCase
40+
decoder.dateDecodingStrategy = .custom { decoder in
41+
let container = try decoder.singleValueContainer()
42+
let string = try container.decode(String.self)
43+
44+
let supportedFormatters = [dateFormatterWithFractionalSeconds, dateFormatter]
45+
46+
for formatter in supportedFormatters {
47+
if let date = formatter.date(from: string) {
48+
return date
49+
}
50+
}
51+
52+
throw DecodingError.dataCorruptedError(
53+
in: container, debugDescription: "Invalid date format: \(string)"
54+
)
55+
}
56+
return decoder
57+
}()
58+
59+
public static let defaultHeaders: [String: String] = [
60+
"X-Client-Info": "auth-swift/\(version)",
61+
]
62+
63+
/// The default ``AuthFlowType`` used when initializing a ``AuthClient`` instance.
64+
public static let defaultFlowType: AuthFlowType = .pkce
65+
66+
/// The default ``AuthLocalStorage`` instance used by the ``AuthClient``.
67+
public static let defaultLocalStorage: AuthLocalStorage = KeychainLocalStorage(
68+
service: "supabase.gotrue.swift",
69+
accessGroup: nil
70+
)
71+
}

Sources/Auth/Deprecated.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,22 @@ extension JSONEncoder {
2626
@available(
2727
*,
2828
deprecated,
29-
message: "Access to the default JSONEncoder instance will be removed on the next major release, please use your own instance of JSONEncoder."
29+
renamed: "AuthClient.Configuration.jsonEncoder",
30+
message: "Access to the default JSONEncoder instance moved to AuthClient.Configuration.jsonEncoder"
3031
)
3132
public static var goTrue: JSONEncoder {
32-
auth
33+
AuthClient.Configuration.jsonEncoder
3334
}
3435
}
3536

3637
extension JSONDecoder {
3738
@available(
3839
*,
3940
deprecated,
40-
message: "Access to the default JSONDecoder instance will be removed on the next major release, please use your own instance of JSONDecoder."
41+
renamed: "AuthClient.Configuration.jsonDecoder",
42+
message: "Access to the default JSONDecoder instance moved to AuthClient.Configuration.jsonDecoder"
4143
)
4244
public static var goTrue: JSONDecoder {
43-
auth
45+
AuthClient.Configuration.jsonDecoder
4446
}
4547
}

Sources/Auth/Internal/SessionStorage.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ extension SessionStorage {
3838
return Self(
3939
getSession: {
4040
try localStorage.retrieve(key: "supabase.session").flatMap {
41-
try JSONDecoder.auth.decode(StoredSession.self, from: $0)
41+
try AuthClient.Configuration.jsonDecoder.decode(StoredSession.self, from: $0)
4242
}
4343
},
4444
storeSession: {
45-
try localStorage.store(key: "supabase.session", value: JSONEncoder.auth.encode($0))
45+
try localStorage.store(
46+
key: "supabase.session",
47+
value: AuthClient.Configuration.jsonEncoder.encode($0)
48+
)
4649
},
4750
deleteSession: {
4851
try localStorage.remove(key: "supabase.session")

Sources/Auth/Types.swift

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -586,54 +586,3 @@ public enum SignOutScope: String, Sendable {
586586
/// session.
587587
case others
588588
}
589-
590-
// MARK: - Encodable & Decodable
591-
592-
private let dateFormatterWithFractionalSeconds = { () -> ISO8601DateFormatter in
593-
let formatter = ISO8601DateFormatter()
594-
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
595-
return formatter
596-
}()
597-
598-
private let dateFormatter = { () -> ISO8601DateFormatter in
599-
let formatter = ISO8601DateFormatter()
600-
formatter.formatOptions = [.withInternetDateTime]
601-
return formatter
602-
}()
603-
604-
extension JSONDecoder {
605-
static let auth = { () -> JSONDecoder in
606-
let decoder = JSONDecoder()
607-
decoder.keyDecodingStrategy = .convertFromSnakeCase
608-
decoder.dateDecodingStrategy = .custom { decoder in
609-
let container = try decoder.singleValueContainer()
610-
let string = try container.decode(String.self)
611-
612-
let supportedFormatters = [dateFormatterWithFractionalSeconds, dateFormatter]
613-
614-
for formatter in supportedFormatters {
615-
if let date = formatter.date(from: string) {
616-
return date
617-
}
618-
}
619-
620-
throw DecodingError.dataCorruptedError(
621-
in: container, debugDescription: "Invalid date format: \(string)"
622-
)
623-
}
624-
return decoder
625-
}()
626-
}
627-
628-
extension JSONEncoder {
629-
static let auth = { () -> JSONEncoder in
630-
let encoder = JSONEncoder()
631-
encoder.keyEncodingStrategy = .convertToSnakeCase
632-
encoder.dateEncodingStrategy = .custom { date, encoder in
633-
var container = encoder.singleValueContainer()
634-
let string = dateFormatter.string(from: date)
635-
try container.encode(string)
636-
}
637-
return encoder
638-
}()
639-
}

Sources/Supabase/Types.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ public struct SupabaseClientOptions: Sendable {
3030

3131
public struct AuthOptions: Sendable {
3232
/// A storage provider. Used to store the logged-in session.
33-
public let storage: AuthLocalStorage?
33+
public let storage: AuthLocalStorage
3434

3535
/// OAuth flow to use - defaults to PKCE flow. PKCE is recommended for mobile and server-side
3636
/// applications.
3737
public let flowType: AuthFlowType
3838

39-
/// The JSONEncoder to use when encoding database request objects.
40-
public let encoder: JSONEncoder?
39+
/// The JSON encoder to use for encoding requests.
40+
public let encoder: JSONEncoder
4141

42-
/// The JSONDecoder to use when decoding database response objects.
43-
public let decoder: JSONDecoder?
42+
/// The JSON decoder to use for decoding responses.
43+
public let decoder: JSONDecoder
4444

4545
public init(
46-
storage: AuthLocalStorage? = nil,
47-
flowType: AuthFlowType = .pkce,
48-
encoder: JSONEncoder? = nil,
49-
decoder: JSONDecoder? = nil
46+
storage: AuthLocalStorage = AuthClient.Configuration.defaultLocalStorage,
47+
flowType: AuthFlowType = AuthClient.Configuration.defaultFlowType,
48+
encoder: JSONEncoder = AuthClient.Configuration.jsonEncoder,
49+
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder
5050
) {
5151
self.storage = storage
5252
self.flowType = flowType

0 commit comments

Comments
 (0)