Skip to content

Commit

Permalink
Update sub-libraries to use dependency-free branches (#117)
Browse files Browse the repository at this point in the history
* Update libs

* Remove Get import

* Update storage

* Update GoTrue to dependency-free branch
  • Loading branch information
grdsdev authored Oct 17, 2023
1 parent 7088b85 commit b1863a9
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 144 deletions.
13 changes: 8 additions & 5 deletions Examples/Examples/AddTodoListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
3 changes: 1 addition & 2 deletions Examples/Examples/AuthView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ build-example:
-destination platform="$(PLATFORM_IOS)" || exit 1;

format:
@swiftformat .
@swift format -i -r .
43 changes: 8 additions & 35 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,17 @@
"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"
}
},
{
"identity" : "gotrue-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/supabase-community/gotrue-swift",
"state" : {
"revision" : "6c7d119bf236fe0071ff05f3639fdde6f05e759a",
"version" : "1.2.0"
"branch" : "dependency-free",
"revision" : "4eece4fe9d8a6596ec5dedd2ffc14a9594cd2134"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
}
],
Expand Down
22 changes: 17 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var package = Package(
.library(
name: "Supabase",
targets: ["Supabase"]
),
)
],
dependencies: [],
targets: [
Expand Down Expand Up @@ -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"
),
]
)
}
28 changes: 0 additions & 28 deletions Sources/Supabase/Deprecations.swift

This file was deleted.

79 changes: 30 additions & 49 deletions Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Foundation
@_exported import Functions
import Get
@_exported import GoTrue
@_exported import PostgREST
@_exported import Realtime
@_exported import SupabaseStorage

/// Supabase Client.
public class SupabaseClient {
let options: SupabaseClientOptions
let supabaseURL: URL
let supabaseKey: String
let storageURL: URL
Expand All @@ -16,28 +16,36 @@ 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
)
)
}

/// Database client for Supabase.
public var database: PostgrestClient {
PostgrestClient(
url: databaseURL,
schema: options.db.schema,
headers: defaultHeaders,
schema: schema,
apiClientDelegate: self
fetch: fetch
)
}

Expand All @@ -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(
Expand All @@ -73,63 +84,33 @@ 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")
}
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)
}
}
6 changes: 3 additions & 3 deletions Sources/Supabase/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/SupabaseTests/SupabaseClientTests.swift
Original file line number Diff line number Diff line change
@@ -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 {}

Expand Down
21 changes: 6 additions & 15 deletions supabase-swift.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand All @@ -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",
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand Down

0 comments on commit b1863a9

Please sign in to comment.