From 9a8faf993af99460ede2da08238282c9e23be572 Mon Sep 17 00:00:00 2001 From: Khan Winter <35942988+thecoolwinter@users.noreply.github.com> Date: Thu, 2 Sep 2021 10:24:01 -0500 Subject: [PATCH 1/3] Add `PostgrestClient.PostgrestClientConfig` --- Sources/PostgREST/PostgrestClient.swift | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Sources/PostgREST/PostgrestClient.swift b/Sources/PostgREST/PostgrestClient.swift index 5076855..679c979 100644 --- a/Sources/PostgREST/PostgrestClient.swift +++ b/Sources/PostgREST/PostgrestClient.swift @@ -4,9 +4,15 @@ This is the main class in this package. Use it to execute queries on a PostgREST instance on Supabase. */ public class PostgrestClient { - var url: String - var headers: [String: String] - var schema: String? + /// Configuration for the client + public var config: PostgrestClientConfig + + /// Struct for PostgrestClient config options + public struct PostgrestClientConfig { + public var url: String + public var headers: [String: String] + public var schema: String? + } /// Initializes the `PostgrestClient` with the correct parameters. /// - Parameters: @@ -14,16 +20,16 @@ public class PostgrestClient { /// - headers: Headers to include when querying the database. Eg, an authentication header /// - schema: Schema ID to use public init(url: String, headers: [String: String] = [:], schema: String?) { - self.url = url - self.headers = headers - self.schema = schema + self.config = PostgrestClientConfig(url: url, + headers: headers, + schema: schema) } /// Select a table to query from /// - Parameter table: The ID of the table to query /// - Returns: `PostgrestQueryBuilder` public func from(_ table: String) -> PostgrestQueryBuilder { - return PostgrestQueryBuilder(url: "\(url)/\(table)", queryParams: [], headers: headers, schema: schema, method: nil, body: nil) + return PostgrestQueryBuilder(url: "\(config.url)/\(table)", queryParams: [], headers: config.headers, schema: config.schema, method: nil, body: nil) } /// Call a stored procedure, aka a "Remote Procedure Call" @@ -32,6 +38,6 @@ public class PostgrestClient { /// - parameters: Parameters to pass to the procedure. /// - Returns: `PostgrestTransformBuilder` public func rpc(fn: String, parameters: [String: Any]?) -> PostgrestTransformBuilder { - return PostgrestRpcBuilder(url: "\(url)/rpc/\(fn)", queryParams: [], headers: headers, schema: schema, method: nil, body: nil).rpc(parameters: parameters) + return PostgrestRpcBuilder(url: "\(config.url)/rpc/\(fn)", queryParams: [], headers: config.headers, schema: config.schema, method: nil, body: nil).rpc(parameters: parameters) } } From f82704f31299c78c49f2ecda4132699b8cfa6bf8 Mon Sep 17 00:00:00 2001 From: Khan Winter <35942988+thecoolwinter@users.noreply.github.com> Date: Thu, 2 Sep 2021 10:25:46 -0500 Subject: [PATCH 2/3] Update PostgrestClient.swift --- Sources/PostgREST/PostgrestClient.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/PostgREST/PostgrestClient.swift b/Sources/PostgREST/PostgrestClient.swift index 679c979..15ca61b 100644 --- a/Sources/PostgREST/PostgrestClient.swift +++ b/Sources/PostgREST/PostgrestClient.swift @@ -24,6 +24,12 @@ public class PostgrestClient { headers: headers, schema: schema) } + + /// Initializes the `PostgrestClient` with a config object + /// - Parameter config: A `PostgrestClientConfig` struct with the correct parameters + public init(config: PostgrestClientConfig) { + self.config = config + } /// Select a table to query from /// - Parameter table: The ID of the table to query From 3d8e5642537b3cfe99d176a910d312fe750282dd Mon Sep 17 00:00:00 2001 From: Khan Winter <35942988+thecoolwinter@users.noreply.github.com> Date: Thu, 2 Sep 2021 10:29:37 -0500 Subject: [PATCH 3/3] Fix tests --- .../BuildURLRequestTests/testBuildURLRequest.call-rpc.txt | 1 + .../BuildURLRequestTests/testBuildURLRequest.insert-new-user.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.call-rpc.txt b/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.call-rpc.txt index a5168da..182a3a3 100644 --- a/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.call-rpc.txt +++ b/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.call-rpc.txt @@ -1,4 +1,5 @@ curl \ --request POST \ + --header "Content-Type: application/json" \ --data "{\"KEY\":\"VALUE\"}" \ "https://example.supabase.co/rpc/test_fcn" \ No newline at end of file diff --git a/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.insert-new-user.txt b/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.insert-new-user.txt index ed68a51..0706878 100644 --- a/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.insert-new-user.txt +++ b/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.insert-new-user.txt @@ -1,5 +1,6 @@ curl \ --request POST \ + --header "Content-Type: application/json" \ --header "Prefer: return=representation" \ --data "{\"email\":\"johndoe@supabase.io\"}" \ "https://example.supabase.co/users" \ No newline at end of file