Skip to content
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

Add AnyCodable #23

Merged
merged 2 commits into from
Feb 21, 2022
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
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"object": {
"pins": [
{
"package": "AnyCodable",
"repositoryURL": "https://github.com/Flight-School/AnyCodable",
"state": {
"branch": null,
"revision": "b1a7a8a6186f2fcb28f7bda67cfc545de48b3c80",
"version": "0.6.2"
}
},
{
"package": "SnapshotTesting",
"repositoryURL": "https://github.com/pointfreeco/swift-snapshot-testing.git",
Expand Down
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ let package = Package(
.package(
name: "SnapshotTesting",
url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.8.1"
)
),
.package(name: "AnyCodable", url: "https://github.com/Flight-School/AnyCodable", from: "0.6.2"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "PostgREST",
dependencies: []
dependencies: ["AnyCodable"]
),
.target(
name: "example",
Expand Down
11 changes: 4 additions & 7 deletions Sources/PostgREST/PostgrestBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AnyCodable
import Foundation

public class PostgrestBuilder {
Expand All @@ -6,11 +7,11 @@ public class PostgrestBuilder {
var headers: [String: String]
var schema: String?
var method: String?
var body: Any?
var body: AnyEncodable?

init(
url: String, queryParams: [(name: String, value: String)], headers: [String: String],
schema: String?, method: String?, body: Any?
schema: String?, method: String?, body: AnyEncodable?
) {
self.url = url
self.queryParams = queryParams
Expand Down Expand Up @@ -152,11 +153,7 @@ public class PostgrestBuilder {
request.httpMethod = method
request.allHTTPHeaderFields = headers
if let body = body {
if let httpBody = body as? Data {
request.httpBody = httpBody
} else {
request.httpBody = try JSONSerialization.data(withJSONObject: body, options: [])
}
request.httpBody = try JSONEncoder().encode(body)
}
return request
}
Expand Down
13 changes: 12 additions & 1 deletion Sources/PostgREST/PostgrestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,21 @@ public class PostgrestClient {
/// - fn: Procedure name to call.
/// - parameters: Parameters to pass to the procedure.
/// - Returns: `PostgrestTransformBuilder`
public func rpc(fn: String, parameters: [String: Any]?) -> PostgrestTransformBuilder {
public func rpc<U: Encodable>(fn: String, parameters: U?) -> PostgrestTransformBuilder {
return PostgrestRpcBuilder(
url: "\(config.url)/rpc/\(fn)", queryParams: [], headers: config.headers,
schema: config.schema, method: nil, body: nil
).rpc(parameters: parameters)
}

/// Call a stored procedure, aka a "Remote Procedure Call"
/// - Parameters:
/// - fn: Procedure name to call.
/// - Returns: `PostgrestTransformBuilder`
public func rpc(fn: String) -> PostgrestTransformBuilder {
return PostgrestRpcBuilder(
url: "\(config.url)/rpc/\(fn)", queryParams: [], headers: config.headers,
schema: config.schema, method: nil, body: nil
).rpc()
}
}
20 changes: 12 additions & 8 deletions Sources/PostgREST/PostgrestQueryBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import AnyCodable

public class PostgrestQueryBuilder: PostgrestBuilder {
public func select(columns: String = "*") -> PostgrestFilterBuilder {
method = "GET"
Expand All @@ -18,8 +20,8 @@ public class PostgrestQueryBuilder: PostgrestBuilder {
)
}

public func insert(
values: Any, upsert: Bool = false, onConflict: String? = nil,
public func insert<U: Encodable>(
values: U, upsert: Bool = false, onConflict: String? = nil,
returning: PostgrestReturningOptions = .representation
) -> PostgrestBuilder {
method = "POST"
Expand All @@ -30,29 +32,31 @@ public class PostgrestQueryBuilder: PostgrestBuilder {
appendSearchParams(name: "on_conflict", value: onConflict)
}

body = values
body = AnyEncodable(values)
return self
}

public func upsert(
values: Any, onConflict: String? = nil, returning: PostgrestReturningOptions = .representation
public func upsert<U: Encodable>(
values: U, onConflict: String? = nil, returning: PostgrestReturningOptions = .representation
) -> PostgrestBuilder {
method = "POST"
headers["Prefer"] = "return=\(returning.rawValue),resolution=merge-duplicates"
if let onConflict = onConflict {
appendSearchParams(name: "on_conflict", value: onConflict)
}

body = values
body = AnyEncodable(values)
return self
}

public func update(values: Any, returning: PostgrestReturningOptions = .representation)
public func update<U: Encodable>(
values: U, returning: PostgrestReturningOptions = .representation
)
-> PostgrestFilterBuilder
{
method = "PATCH"
headers["Prefer"] = "return=\(returning.rawValue)"
body = values
body = AnyEncodable(values)
return PostgrestFilterBuilder(
url: url, queryParams: queryParams, headers: headers, schema: schema, method: method,
body: body
Expand Down
17 changes: 15 additions & 2 deletions Sources/PostgREST/PostgrestRpcBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import AnyCodable

public class PostgrestRpcBuilder: PostgrestBuilder {
public func rpc(parameters: [String: Any]?) -> PostgrestTransformBuilder {
public func rpc<U: Encodable>(parameters: U?) -> PostgrestTransformBuilder {
method = "POST"
body = AnyEncodable(parameters)
return PostgrestTransformBuilder(
url: url,
queryParams: queryParams,
headers: headers,
schema: schema,
method: method,
body: body)
}

public func rpc() -> PostgrestTransformBuilder {
method = "POST"
body = parameters
return PostgrestTransformBuilder(
url: url,
queryParams: queryParams,
Expand Down
6 changes: 0 additions & 6 deletions Tests/LinuxMain.swift

This file was deleted.

4 changes: 4 additions & 0 deletions Tests/PostgRESTTests/BuildURLRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ final class BuildURLRequestTests: XCTestCase {
try client.rpc(fn: "test_fcn", parameters: ["KEY": "VALUE"])
.buildURLRequest(head: false, count: nil)
},
TestCase(name: "call rpc without parameter") { client in
try client.rpc(fn: "test_fcn")
.buildURLRequest(head: false, count: nil)
},
]

for testCase in testCases {
Expand Down
16 changes: 0 additions & 16 deletions Tests/PostgRESTTests/PostgRESTTests.swift

This file was deleted.

9 changes: 0 additions & 9 deletions Tests/PostgRESTTests/XCTestManifests.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
curl \
--request POST \
--header "Content-Type: application/json" \
"https://example.supabase.co/rpc/test_fcn"