Skip to content

Commit 7462c72

Browse files
Add public config (#11)
* Add `PostgrestClient.PostgrestClientConfig` * Update PostgrestClient.swift * Fix tests
1 parent fcc10ce commit 7462c72

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

Sources/PostgREST/PostgrestClient.swift

+20-8
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,38 @@
44
This is the main class in this package. Use it to execute queries on a PostgREST instance on Supabase.
55
*/
66
public class PostgrestClient {
7-
var url: String
8-
var headers: [String: String]
9-
var schema: String?
7+
/// Configuration for the client
8+
public var config: PostgrestClientConfig
9+
10+
/// Struct for PostgrestClient config options
11+
public struct PostgrestClientConfig {
12+
public var url: String
13+
public var headers: [String: String]
14+
public var schema: String?
15+
}
1016

1117
/// Initializes the `PostgrestClient` with the correct parameters.
1218
/// - Parameters:
1319
/// - url: Url of your supabase db instance
1420
/// - headers: Headers to include when querying the database. Eg, an authentication header
1521
/// - schema: Schema ID to use
1622
public init(url: String, headers: [String: String] = [:], schema: String?) {
17-
self.url = url
18-
self.headers = headers
19-
self.schema = schema
23+
self.config = PostgrestClientConfig(url: url,
24+
headers: headers,
25+
schema: schema)
26+
}
27+
28+
/// Initializes the `PostgrestClient` with a config object
29+
/// - Parameter config: A `PostgrestClientConfig` struct with the correct parameters
30+
public init(config: PostgrestClientConfig) {
31+
self.config = config
2032
}
2133

2234
/// Select a table to query from
2335
/// - Parameter table: The ID of the table to query
2436
/// - Returns: `PostgrestQueryBuilder`
2537
public func from(_ table: String) -> PostgrestQueryBuilder {
26-
return PostgrestQueryBuilder(url: "\(url)/\(table)", queryParams: [], headers: headers, schema: schema, method: nil, body: nil)
38+
return PostgrestQueryBuilder(url: "\(config.url)/\(table)", queryParams: [], headers: config.headers, schema: config.schema, method: nil, body: nil)
2739
}
2840

2941
/// Call a stored procedure, aka a "Remote Procedure Call"
@@ -32,6 +44,6 @@ public class PostgrestClient {
3244
/// - parameters: Parameters to pass to the procedure.
3345
/// - Returns: `PostgrestTransformBuilder`
3446
public func rpc(fn: String, parameters: [String: Any]?) -> PostgrestTransformBuilder {
35-
return PostgrestRpcBuilder(url: "\(url)/rpc/\(fn)", queryParams: [], headers: headers, schema: schema, method: nil, body: nil).rpc(parameters: parameters)
47+
return PostgrestRpcBuilder(url: "\(config.url)/rpc/\(fn)", queryParams: [], headers: config.headers, schema: config.schema, method: nil, body: nil).rpc(parameters: parameters)
3648
}
3749
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
curl \
22
--request POST \
3+
--header "Content-Type: application/json" \
34
--data "{\"KEY\":\"VALUE\"}" \
45
"https://example.supabase.co/rpc/test_fcn"
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
curl \
22
--request POST \
3+
--header "Content-Type: application/json" \
34
--header "Prefer: return=representation" \
45
--data "{\"email\":\"[email protected]\"}" \
56
"https://example.supabase.co/users"

0 commit comments

Comments
 (0)