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

feat: add filter to RPC call #150

Merged
merged 2 commits into from
Nov 5, 2023
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
66 changes: 66 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/PostgREST.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "PostgREST"
BuildableName = "PostgREST"
BlueprintName = "PostgREST"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "PostgREST"
BuildableName = "PostgREST"
BlueprintName = "PostgREST"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
10 changes: 10 additions & 0 deletions Sources/GoTrue/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ public struct AuthMFAEnrollResponse: Decodable, Hashable, Sendable {
public struct MFAChallengeParams: Encodable, Hashable {
/// ID of the factor to be challenged. Returned in ``GoTrueMFA/enroll(params:)``.
public let factorId: String

public init(factorId: String) {
self.factorId = factorId
}
}

public struct MFAVerifyParams: Encodable, Hashable {
Expand All @@ -465,6 +469,12 @@ public struct MFAVerifyParams: Encodable, Hashable {

/// Verification code provided by the user.
public let code: String

public init(factorId: String, challengeId: String, code: String) {
self.factorId = factorId
self.challengeId = challengeId
self.code = code
}
}

public struct MFAUnenrollParams: Encodable, Hashable, Sendable {
Expand Down
8 changes: 4 additions & 4 deletions Sources/PostgREST/PostgrestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/PostgREST/PostgrestRpcBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -38,6 +38,6 @@ public final class PostgrestRpcBuilder: PostgrestBuilder {
}
}

return PostgrestTransformBuilder(self)
return PostgrestFilterBuilder(self)
}
}
3 changes: 3 additions & 0 deletions Tests/PostgRESTTests/BuildURLRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
@@ -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"