Skip to content

Commit cd22ffe

Browse files
author
Guilherme Souza
authored
Add AnyCodable (#23)
* Add AnyCodable and refactor httpBody * Add rpc overload without parameter
1 parent 44b6453 commit cd22ffe

11 files changed

+63
-51
lines changed

Package.resolved

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
{
22
"object": {
33
"pins": [
4+
{
5+
"package": "AnyCodable",
6+
"repositoryURL": "https://github.com/Flight-School/AnyCodable",
7+
"state": {
8+
"branch": null,
9+
"revision": "b1a7a8a6186f2fcb28f7bda67cfc545de48b3c80",
10+
"version": "0.6.2"
11+
}
12+
},
413
{
514
"package": "SnapshotTesting",
615
"repositoryURL": "https://github.com/pointfreeco/swift-snapshot-testing.git",

Package.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ let package = Package(
1818
.package(
1919
name: "SnapshotTesting",
2020
url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.8.1"
21-
)
21+
),
22+
.package(name: "AnyCodable", url: "https://github.com/Flight-School/AnyCodable", from: "0.6.2"),
2223
],
2324
targets: [
2425
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2526
// Targets can depend on other targets in this package, and on products in packages this package depends on.
2627
.target(
2728
name: "PostgREST",
28-
dependencies: []
29+
dependencies: ["AnyCodable"]
2930
),
3031
.target(
3132
name: "example",

Sources/PostgREST/PostgrestBuilder.swift

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import AnyCodable
12
import Foundation
23

34
public class PostgrestBuilder {
@@ -6,11 +7,11 @@ public class PostgrestBuilder {
67
var headers: [String: String]
78
var schema: String?
89
var method: String?
9-
var body: Any?
10+
var body: AnyEncodable?
1011

1112
init(
1213
url: String, queryParams: [(name: String, value: String)], headers: [String: String],
13-
schema: String?, method: String?, body: Any?
14+
schema: String?, method: String?, body: AnyEncodable?
1415
) {
1516
self.url = url
1617
self.queryParams = queryParams
@@ -152,11 +153,7 @@ public class PostgrestBuilder {
152153
request.httpMethod = method
153154
request.allHTTPHeaderFields = headers
154155
if let body = body {
155-
if let httpBody = body as? Data {
156-
request.httpBody = httpBody
157-
} else {
158-
request.httpBody = try JSONSerialization.data(withJSONObject: body, options: [])
159-
}
156+
request.httpBody = try JSONEncoder().encode(body)
160157
}
161158
return request
162159
}

Sources/PostgREST/PostgrestClient.swift

+12-1
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,21 @@ public class PostgrestClient {
4444
/// - fn: Procedure name to call.
4545
/// - parameters: Parameters to pass to the procedure.
4646
/// - Returns: `PostgrestTransformBuilder`
47-
public func rpc(fn: String, parameters: [String: Any]?) -> PostgrestTransformBuilder {
47+
public func rpc<U: Encodable>(fn: String, parameters: U?) -> PostgrestTransformBuilder {
4848
return PostgrestRpcBuilder(
4949
url: "\(config.url)/rpc/\(fn)", queryParams: [], headers: config.headers,
5050
schema: config.schema, method: nil, body: nil
5151
).rpc(parameters: parameters)
5252
}
53+
54+
/// Call a stored procedure, aka a "Remote Procedure Call"
55+
/// - Parameters:
56+
/// - fn: Procedure name to call.
57+
/// - Returns: `PostgrestTransformBuilder`
58+
public func rpc(fn: String) -> PostgrestTransformBuilder {
59+
return PostgrestRpcBuilder(
60+
url: "\(config.url)/rpc/\(fn)", queryParams: [], headers: config.headers,
61+
schema: config.schema, method: nil, body: nil
62+
).rpc()
63+
}
5364
}

Sources/PostgREST/PostgrestQueryBuilder.swift

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import AnyCodable
2+
13
public class PostgrestQueryBuilder: PostgrestBuilder {
24
public func select(columns: String = "*") -> PostgrestFilterBuilder {
35
method = "GET"
@@ -18,8 +20,8 @@ public class PostgrestQueryBuilder: PostgrestBuilder {
1820
)
1921
}
2022

21-
public func insert(
22-
values: Any, upsert: Bool = false, onConflict: String? = nil,
23+
public func insert<U: Encodable>(
24+
values: U, upsert: Bool = false, onConflict: String? = nil,
2325
returning: PostgrestReturningOptions = .representation
2426
) -> PostgrestBuilder {
2527
method = "POST"
@@ -30,29 +32,31 @@ public class PostgrestQueryBuilder: PostgrestBuilder {
3032
appendSearchParams(name: "on_conflict", value: onConflict)
3133
}
3234

33-
body = values
35+
body = AnyEncodable(values)
3436
return self
3537
}
3638

37-
public func upsert(
38-
values: Any, onConflict: String? = nil, returning: PostgrestReturningOptions = .representation
39+
public func upsert<U: Encodable>(
40+
values: U, onConflict: String? = nil, returning: PostgrestReturningOptions = .representation
3941
) -> PostgrestBuilder {
4042
method = "POST"
4143
headers["Prefer"] = "return=\(returning.rawValue),resolution=merge-duplicates"
4244
if let onConflict = onConflict {
4345
appendSearchParams(name: "on_conflict", value: onConflict)
4446
}
4547

46-
body = values
48+
body = AnyEncodable(values)
4749
return self
4850
}
4951

50-
public func update(values: Any, returning: PostgrestReturningOptions = .representation)
52+
public func update<U: Encodable>(
53+
values: U, returning: PostgrestReturningOptions = .representation
54+
)
5155
-> PostgrestFilterBuilder
5256
{
5357
method = "PATCH"
5458
headers["Prefer"] = "return=\(returning.rawValue)"
55-
body = values
59+
body = AnyEncodable(values)
5660
return PostgrestFilterBuilder(
5761
url: url, queryParams: queryParams, headers: headers, schema: schema, method: method,
5862
body: body

Sources/PostgREST/PostgrestRpcBuilder.swift

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
import AnyCodable
2+
13
public class PostgrestRpcBuilder: PostgrestBuilder {
2-
public func rpc(parameters: [String: Any]?) -> PostgrestTransformBuilder {
4+
public func rpc<U: Encodable>(parameters: U?) -> PostgrestTransformBuilder {
5+
method = "POST"
6+
body = AnyEncodable(parameters)
7+
return PostgrestTransformBuilder(
8+
url: url,
9+
queryParams: queryParams,
10+
headers: headers,
11+
schema: schema,
12+
method: method,
13+
body: body)
14+
}
15+
16+
public func rpc() -> PostgrestTransformBuilder {
317
method = "POST"
4-
body = parameters
518
return PostgrestTransformBuilder(
619
url: url,
720
queryParams: queryParams,

Tests/LinuxMain.swift

-6
This file was deleted.

Tests/PostgRESTTests/BuildURLRequestTests.swift

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ final class BuildURLRequestTests: XCTestCase {
3232
try client.rpc(fn: "test_fcn", parameters: ["KEY": "VALUE"])
3333
.buildURLRequest(head: false, count: nil)
3434
},
35+
TestCase(name: "call rpc without parameter") { client in
36+
try client.rpc(fn: "test_fcn")
37+
.buildURLRequest(head: false, count: nil)
38+
},
3539
]
3640

3741
for testCase in testCases {

Tests/PostgRESTTests/PostgRESTTests.swift

-16
This file was deleted.

Tests/PostgRESTTests/XCTestManifests.swift

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
curl \
2+
--request POST \
3+
--header "Content-Type: application/json" \
4+
"https://example.supabase.co/rpc/test_fcn"

0 commit comments

Comments
 (0)