From b1863a99f8d772afc5164a16760923cc56f3ff38 Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Tue, 17 Oct 2023 16:41:52 -0300 Subject: [PATCH] Update sub-libraries to use `dependency-free` branches (#117) * Update libs * Remove Get import * Update storage * Update GoTrue to dependency-free branch --- Examples/Examples/AddTodoListView.swift | 13 +-- Examples/Examples/AuthView.swift | 3 +- Makefile | 2 +- Package.resolved | 43 ++-------- Package.swift | 22 ++++-- Sources/Supabase/Deprecations.swift | 28 ------- Sources/Supabase/SupabaseClient.swift | 79 +++++++------------ Sources/Supabase/Types.swift | 6 +- Tests/SupabaseTests/SupabaseClientTests.swift | 3 +- .../xcshareddata/swiftpm/Package.resolved | 21 ++--- 10 files changed, 76 insertions(+), 144 deletions(-) delete mode 100644 Sources/Supabase/Deprecations.swift diff --git a/Examples/Examples/AddTodoListView.swift b/Examples/Examples/AddTodoListView.swift index 9325b461..d4eb5630 100644 --- a/Examples/Examples/AddTodoListView.swift +++ b/Examples/Examples/AddTodoListView.swift @@ -36,11 +36,14 @@ struct AddTodoListView: View { struct AddTodoListView_Previews: PreviewProvider { static var previews: some View { - AddTodoListView(request: .constant(.init( - description: "", - isComplete: false, - ownerID: UUID() - ))) { _ in + AddTodoListView( + request: .constant( + .init( + description: "", + isComplete: false, + ownerID: UUID() + )) + ) { _ in } } } diff --git a/Examples/Examples/AuthView.swift b/Examples/Examples/AuthView.swift index bb040dda..ba34c111 100644 --- a/Examples/Examples/AuthView.swift +++ b/Examples/Examples/AuthView.swift @@ -57,8 +57,7 @@ struct AuthView: View { Section { Button( - mode == .signIn ? "Don't have an account? Sign up." : - "Already have an account? Sign in." + mode == .signIn ? "Don't have an account? Sign up." : "Already have an account? Sign in." ) { withAnimation { mode = mode == .signIn ? .signUp : .signIn diff --git a/Makefile b/Makefile index e3867858..722107f2 100644 --- a/Makefile +++ b/Makefile @@ -13,4 +13,4 @@ build-example: -destination platform="$(PLATFORM_IOS)" || exit 1; format: - @swiftformat . + @swift format -i -r . diff --git a/Package.resolved b/Package.resolved index 8cb1f5be..b5381ff9 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,26 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/supabase-community/functions-swift", "state" : { - "revision" : "c680cdfc53399376bdece299b1387b4fb3da514e", - "version" : "1.0.0" - } - }, - { - "identity" : "get", - "kind" : "remoteSourceControl", - "location" : "https://github.com/kean/Get", - "state" : { - "revision" : "12830cc64f31789ae6f4352d2d51d03a25fc3741", - "version" : "2.1.6" - } - }, - { - "identity" : "getextensions", - "kind" : "remoteSourceControl", - "location" : "https://github.com/binaryscraping/GetExtensions", - "state" : { - "revision" : "aa20f38721142eb6592b2c8f11179d32d7d70ae3", - "version" : "1.0.0" + "branch" : "dependency-free", + "revision" : "5e12b7c4a206a29910dd64f6f23faed437955089" } }, { @@ -32,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/supabase-community/gotrue-swift", "state" : { - "revision" : "6c7d119bf236fe0071ff05f3639fdde6f05e759a", - "version" : "1.2.0" + "branch" : "dependency-free", + "revision" : "4eece4fe9d8a6596ec5dedd2ffc14a9594cd2134" } }, { @@ -50,8 +32,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/supabase-community/postgrest-swift", "state" : { - "revision" : "a3c2d6a2ede94d2529edf063b056b1a66fd9cdc2", - "version" : "1.0.0" + "branch" : "dependency-free", + "revision" : "ef3faa36a6987d34c8d32bab1cf545d40bf1a182" } }, { @@ -68,17 +50,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/supabase-community/storage-swift.git", "state" : { - "revision" : "db67ce7764ef80e7941cea6bc4a0104c88e060f8", - "version" : "0.1.3" - } - }, - { - "identity" : "urlqueryencoder", - "kind" : "remoteSourceControl", - "location" : "https://github.com/kean/URLQueryEncoder", - "state" : { - "revision" : "4ce950479707ea109f229d7230ec074a133b15d7", - "version" : "0.2.1" + "branch" : "dependency-free", + "revision" : "62bf80cc46e22088ca390e506b1a712f4774a018" } } ], diff --git a/Package.swift b/Package.swift index 2713edc0..e2859669 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ var package = Package( .library( name: "Supabase", targets: ["Supabase"] - ), + ) ], dependencies: [], targets: [ @@ -48,11 +48,23 @@ if ProcessInfo.processInfo.environment["USE_LOCAL_PACKAGES"] != nil { } else { package.dependencies.append( contentsOf: [ - .package(url: "https://github.com/supabase-community/gotrue-swift", from: "1.0.0"), - .package(url: "https://github.com/supabase-community/storage-swift.git", from: "0.1.1"), + .package( + url: "https://github.com/supabase-community/gotrue-swift", + branch: "dependency-free" + ), + .package( + url: "https://github.com/supabase-community/storage-swift.git", + branch: "dependency-free" + ), .package(url: "https://github.com/supabase-community/realtime-swift.git", from: "0.0.2"), - .package(url: "https://github.com/supabase-community/postgrest-swift", from: "1.0.0"), - .package(url: "https://github.com/supabase-community/functions-swift", from: "1.0.0"), + .package( + url: "https://github.com/supabase-community/postgrest-swift", + branch: "dependency-free" + ), + .package( + url: "https://github.com/supabase-community/functions-swift", + branch: "dependency-free" + ), ] ) } diff --git a/Sources/Supabase/Deprecations.swift b/Sources/Supabase/Deprecations.swift deleted file mode 100644 index e67b4d79..00000000 --- a/Sources/Supabase/Deprecations.swift +++ /dev/null @@ -1,28 +0,0 @@ -import Foundation - -extension SupabaseClient { - @available( - *, - deprecated, - message: "Deprecated initializer, use init(supabaseURL:supabaseKey:options) instead." - ) - public convenience init( - supabaseURL: URL, - supabaseKey: String, - schema: String = "public", - headers: [String: String] = [:], - httpClient: HTTPClient = HTTPClient() - ) { - self.init( - supabaseURL: supabaseURL, - supabaseKey: supabaseKey, - options: SupabaseClientOptions( - db: SupabaseClientOptions.DatabaseOptions(schema: schema), - global: SupabaseClientOptions.GlobalOptions( - headers: headers, - httpClient: httpClient - ) - ) - ) - } -} diff --git a/Sources/Supabase/SupabaseClient.swift b/Sources/Supabase/SupabaseClient.swift index b149a10f..bbf80e24 100644 --- a/Sources/Supabase/SupabaseClient.swift +++ b/Sources/Supabase/SupabaseClient.swift @@ -1,6 +1,5 @@ import Foundation @_exported import Functions -import Get @_exported import GoTrue @_exported import PostgREST @_exported import Realtime @@ -8,6 +7,7 @@ import Get /// Supabase Client. public class SupabaseClient { + let options: SupabaseClientOptions let supabaseURL: URL let supabaseKey: String let storageURL: URL @@ -16,18 +16,26 @@ public class SupabaseClient { let authURL: URL let functionsURL: URL - let schema: String - /// Supabase Auth allows you to create and manage user sessions for access to data that is secured /// by access policies. - public let auth: GoTrueClient + public var auth: GoTrueClient { + GoTrueClient( + url: authURL, + headers: defaultHeaders, + localStorage: options.auth.storage, + fetch: fetch + ) + } /// Supabase Storage allows you to manage user-generated content, such as photos or videos. public var storage: SupabaseStorageClient { SupabaseStorageClient( url: storageURL.absoluteString, headers: defaultHeaders, - http: self + session: StorageHTTPSession( + fetch: fetch, + upload: upload + ) ) } @@ -35,9 +43,9 @@ public class SupabaseClient { public var database: PostgrestClient { PostgrestClient( url: databaseURL, + schema: options.db.schema, headers: defaultHeaders, - schema: schema, - apiClientDelegate: self + fetch: fetch ) } @@ -54,11 +62,14 @@ public class SupabaseClient { FunctionsClient( url: functionsURL, headers: defaultHeaders, - apiClientDelegate: self + fetch: fetch ) } private(set) var defaultHeaders: [String: String] + private var session: URLSession { + options.global.session + } /// Create a new client. public init( @@ -73,44 +84,29 @@ public class SupabaseClient { databaseURL = supabaseURL.appendingPathComponent("/rest/v1") realtimeURL = supabaseURL.appendingPathComponent("/realtime/v1") functionsURL = supabaseURL.appendingPathComponent("/functions/v1") - - schema = options.db.schema - httpClient = options.global.httpClient + self.options = options defaultHeaders = [ "X-Client-Info": "supabase-swift/\(version)", "Authorization": "Bearer \(supabaseKey)", "apikey": supabaseKey, ].merging(options.global.headers) { _, new in new } - - auth = GoTrueClient( - url: authURL, - headers: defaultHeaders, - localStorage: options.auth.storage - ) } - public struct HTTPClient { - let storage: StorageHTTPClient - - public init( - storage: StorageHTTPClient? = nil - ) { - self.storage = storage ?? DefaultStorageHTTPClient() - } + @Sendable + private func fetch(_ request: URLRequest) async throws -> (Data, URLResponse) { + try await session.data(for: adapt(request: request)) } - private let httpClient: HTTPClient -} - -extension SupabaseClient: APIClientDelegate { - public func client(_: APIClient, willSendRequest request: inout URLRequest) async throws { - request = await adapt(request: request) + @Sendable + private func upload( + _ request: URLRequest, + from data: Data + ) async throws -> (Data, URLResponse) { + try await session.upload(for: adapt(request: request), from: data) } -} -extension SupabaseClient { - func adapt(request: URLRequest) async -> URLRequest { + private func adapt(request: URLRequest) async -> URLRequest { var request = request if let accessToken = try? await auth.session.accessToken { request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization") @@ -118,18 +114,3 @@ extension SupabaseClient { return request } } - -extension SupabaseClient: StorageHTTPClient { - public func fetch(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) { - let request = await adapt(request: request) - return try await httpClient.storage.fetch(request) - } - - public func upload( - _ request: URLRequest, - from data: Data - ) async throws -> (Data, HTTPURLResponse) { - let request = await adapt(request: request) - return try await httpClient.storage.upload(request, from: data) - } -} diff --git a/Sources/Supabase/Types.swift b/Sources/Supabase/Types.swift index 82cba4f4..b5faa73f 100644 --- a/Sources/Supabase/Types.swift +++ b/Sources/Supabase/Types.swift @@ -27,11 +27,11 @@ public struct SupabaseClientOptions { public struct GlobalOptions { public let headers: [String: String] - public let httpClient: SupabaseClient.HTTPClient + public let session: URLSession - public init(headers: [String: String] = [:], httpClient: SupabaseClient.HTTPClient = .init()) { + public init(headers: [String: String] = [:], session: URLSession = .shared) { self.headers = headers - self.httpClient = httpClient + self.session = session } } diff --git a/Tests/SupabaseTests/SupabaseClientTests.swift b/Tests/SupabaseTests/SupabaseClientTests.swift index a6a0df9d..0fc2c334 100644 --- a/Tests/SupabaseTests/SupabaseClientTests.swift +++ b/Tests/SupabaseTests/SupabaseClientTests.swift @@ -1,7 +1,8 @@ import GoTrue -@testable import Supabase import XCTest +@testable import Supabase + final class GoTrueLocalStorageMock: GoTrueLocalStorage { func store(key _: String, value _: Data) throws {} diff --git a/supabase-swift.xcworkspace/xcshareddata/swiftpm/Package.resolved b/supabase-swift.xcworkspace/xcshareddata/swiftpm/Package.resolved index ca762f49..47415a64 100644 --- a/supabase-swift.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/supabase-swift.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/supabase-community/functions-swift", "state" : { - "revision" : "c680cdfc53399376bdece299b1387b4fb3da514e", - "version" : "1.0.0" + "branch" : "dependency-free", + "revision" : "479a3112f5ffdbeaec05f9b19124ab621c69d44a" } }, { @@ -18,15 +18,6 @@ "version" : "2.1.6" } }, - { - "identity" : "getextensions", - "kind" : "remoteSourceControl", - "location" : "https://github.com/binaryscraping/GetExtensions", - "state" : { - "revision" : "aa20f38721142eb6592b2c8f11179d32d7d70ae3", - "version" : "1.0.0" - } - }, { "identity" : "gotrue-swift", "kind" : "remoteSourceControl", @@ -50,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/supabase-community/postgrest-swift", "state" : { - "revision" : "a3c2d6a2ede94d2529edf063b056b1a66fd9cdc2", - "version" : "1.0.0" + "branch" : "dependency-free", + "revision" : "ef3faa36a6987d34c8d32bab1cf545d40bf1a182" } }, { @@ -68,8 +59,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/supabase-community/storage-swift.git", "state" : { - "revision" : "db67ce7764ef80e7941cea6bc4a0104c88e060f8", - "version" : "0.1.3" + "branch" : "dependency-free", + "revision" : "62bf80cc46e22088ca390e506b1a712f4774a018" } }, {