Skip to content
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 Sources/PostgREST/PostgrestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public class PostgrestBuilder: @unchecked Sendable {
)
}

/// Set a HTTP header for the request.
@discardableResult
public func setHeader(name: String, value: String) -> Self {
mutableState.withValue {
$0.request.headers.update(name: name, value: value)
}
return self
}

/// Executes the request and returns a response of type Void.
/// - Parameters:
/// - options: Options for querying Supabase.
Expand Down
23 changes: 23 additions & 0 deletions Tests/PostgRESTTests/PostgrestBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// PostgrestBuilderTests.swift
// Supabase
//
// Created by Guilherme Souza on 20/08/24.
//

@testable import PostgREST
import XCTest

final class PostgrestBuilderTests: XCTestCase {
let url = URL(string: "http://localhost:54321/rest/v1")!

func testCustomHeaderOnAPerCallBasis() throws {
let postgrest1 = PostgrestClient(url: url, headers: ["apikey": "foo"], logger: nil)
let postgrest2 = try postgrest1.rpc("void_func").setHeader(name: "apikey", value: "bar")

// Original client object isn't affected
XCTAssertEqual(postgrest1.from("users").select().mutableState.request.headers["apikey"], "foo")
// Derived client object uses new header value
XCTAssertEqual(postgrest2.mutableState.request.headers["apikey"], "bar")
}
}