Skip to content

Commit a15efb1

Browse files
authored
feat(postgrest): set header on a per call basis (#508)
1 parent 37e5a03 commit a15efb1

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Sources/PostgREST/PostgrestBuilder.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ public class PostgrestBuilder: @unchecked Sendable {
4949
)
5050
}
5151

52+
/// Set a HTTP header for the request.
53+
@discardableResult
54+
public func setHeader(name: String, value: String) -> Self {
55+
mutableState.withValue {
56+
$0.request.headers.update(name: name, value: value)
57+
}
58+
return self
59+
}
60+
5261
/// Executes the request and returns a response of type Void.
5362
/// - Parameters:
5463
/// - options: Options for querying Supabase.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// PostgrestBuilderTests.swift
3+
// Supabase
4+
//
5+
// Created by Guilherme Souza on 20/08/24.
6+
//
7+
8+
@testable import PostgREST
9+
import XCTest
10+
11+
final class PostgrestBuilderTests: XCTestCase {
12+
let url = URL(string: "http://localhost:54321/rest/v1")!
13+
14+
func testCustomHeaderOnAPerCallBasis() throws {
15+
let postgrest1 = PostgrestClient(url: url, headers: ["apikey": "foo"], logger: nil)
16+
let postgrest2 = try postgrest1.rpc("void_func").setHeader(name: "apikey", value: "bar")
17+
18+
// Original client object isn't affected
19+
XCTAssertEqual(postgrest1.from("users").select().mutableState.request.headers["apikey"], "foo")
20+
// Derived client object uses new header value
21+
XCTAssertEqual(postgrest2.mutableState.request.headers["apikey"], "bar")
22+
}
23+
}

0 commit comments

Comments
 (0)