Skip to content

Add public config #11

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 3 commits into from
Sep 3, 2021
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
28 changes: 20 additions & 8 deletions Sources/PostgREST/PostgrestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,38 @@
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:
/// - url: Url of your supabase db instance
/// - 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)
}

/// 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
/// - 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"
Expand All @@ -32,6 +44,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)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
curl \
--request POST \
--header "Content-Type: application/json" \
--data "{\"KEY\":\"VALUE\"}" \
"https://example.supabase.co/rpc/test_fcn"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
curl \
--request POST \
--header "Content-Type: application/json" \
--header "Prefer: return=representation" \
--data "{\"email\":\"[email protected]\"}" \
"https://example.supabase.co/users"