Skip to content

Update sub-libraries to use dependency-free branches #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.