From 108a30a3cf11fbab3a8103a00eac0e74a04cfb4c Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Sun, 5 Nov 2023 08:22:12 -0300 Subject: [PATCH] feat: support adding filter after rpc call --- .../xcshareddata/xcschemes/PostgREST.xcscheme | 66 +++++++++++++++++++ Sources/PostgREST/PostgrestClient.swift | 8 +-- Sources/PostgREST/PostgrestRpcBuilder.swift | 4 +- .../PostgRESTTests/BuildURLRequestTests.swift | 3 + .../testBuildRequest.call-rpc-with-filter.txt | 6 ++ 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 .swiftpm/xcode/xcshareddata/xcschemes/PostgREST.xcscheme create mode 100644 Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildRequest.call-rpc-with-filter.txt diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/PostgREST.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/PostgREST.xcscheme new file mode 100644 index 00000000..764dd662 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/PostgREST.xcscheme @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Sources/PostgREST/PostgrestClient.swift b/Sources/PostgREST/PostgrestClient.swift index f01461d8..8217191f 100644 --- a/Sources/PostgREST/PostgrestClient.swift +++ b/Sources/PostgREST/PostgrestClient.swift @@ -112,13 +112,13 @@ public actor PostgrestClient { /// - params: The parameters to pass to the function call. /// - count: Count algorithm to use to count rows returned by the function. /// Only applicable for set-returning functions. - /// - Returns: A PostgrestTransformBuilder instance. + /// - Returns: A PostgrestFilterBuilder instance. /// - Throws: An error if the function call fails. public func rpc( _ fn: String, params: some Encodable, count: CountOption? = nil - ) throws -> PostgrestTransformBuilder { + ) throws -> PostgrestFilterBuilder { try PostgrestRpcBuilder( configuration: configuration, request: Request(path: "/rpc/\(fn)", method: .post, headers: configuration.headers) @@ -130,12 +130,12 @@ public actor PostgrestClient { /// - fn: The function name to call. /// - count: Count algorithm to use to count rows returned by the function. /// Only applicable for set-returning functions. - /// - Returns: A PostgrestTransformBuilder instance. + /// - Returns: A PostgrestFilterBuilder instance. /// - Throws: An error if the function call fails. public func rpc( _ fn: String, count: CountOption? = nil - ) throws -> PostgrestTransformBuilder { + ) throws -> PostgrestFilterBuilder { try rpc(fn, params: NoParams(), count: count) } } diff --git a/Sources/PostgREST/PostgrestRpcBuilder.swift b/Sources/PostgREST/PostgrestRpcBuilder.swift index 4b5b2dc3..16790fa1 100644 --- a/Sources/PostgREST/PostgrestRpcBuilder.swift +++ b/Sources/PostgREST/PostgrestRpcBuilder.swift @@ -16,7 +16,7 @@ public final class PostgrestRpcBuilder: PostgrestBuilder { params: some Encodable, head: Bool = false, count: CountOption? = nil - ) throws -> PostgrestTransformBuilder { + ) throws -> PostgrestFilterBuilder { // TODO: Support `HEAD` method // https://github.com/supabase/postgrest-js/blob/master/src/lib/PostgrestRpcBuilder.ts#L38 assert(head == false, "HEAD is not currently supported yet.") @@ -38,6 +38,6 @@ public final class PostgrestRpcBuilder: PostgrestBuilder { } } - return PostgrestTransformBuilder(self) + return PostgrestFilterBuilder(self) } } diff --git a/Tests/PostgRESTTests/BuildURLRequestTests.swift b/Tests/PostgRESTTests/BuildURLRequestTests.swift index bb315f4c..006173d6 100644 --- a/Tests/PostgRESTTests/BuildURLRequestTests.swift +++ b/Tests/PostgRESTTests/BuildURLRequestTests.swift @@ -62,6 +62,9 @@ final class BuildURLRequestTests: XCTestCase { TestCase(name: "call rpc without parameter") { client in try await client.rpc("test_fcn") }, + TestCase(name: "call rpc with filter") { client in + try await client.rpc("test_fcn").eq("id", value: 1) + }, TestCase(name: "test all filters and count") { client in var query = await client.from("todos").select() diff --git a/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildRequest.call-rpc-with-filter.txt b/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildRequest.call-rpc-with-filter.txt new file mode 100644 index 00000000..f1f12813 --- /dev/null +++ b/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildRequest.call-rpc-with-filter.txt @@ -0,0 +1,6 @@ +curl \ + --request POST \ + --header "Accept: application/json" \ + --header "Content-Type: application/json" \ + --header "X-Client-Info: postgrest-swift/x.y.z" \ + "https://example.supabase.co/rpc/test_fcn?id=eq.1" \ No newline at end of file