Skip to content

Commit 45ec3d6

Browse files
grdsdevclaude
andauthored
feat(functions): add region as forceFunctionRegion query parameter (#806)
* feat(functions): add region as forceFunctionRegion query parameter - Updated FunctionsClient to add region as both x-region header and forceFunctionRegion query param - Updated tests to verify both header and query parameter functionality - Maintains backward compatibility with existing x-region header Ported from supabase/functions-js#100 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * test: update snapshot --------- Co-authored-by: Claude <[email protected]>
1 parent fda262b commit 45ec3d6

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

Sources/Functions/FunctionsClient.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,20 @@ public final class FunctionsClient: Sendable {
246246
private func buildRequest(functionName: String, options: FunctionInvokeOptions)
247247
-> Helpers.HTTPRequest
248248
{
249+
var query = options.query
249250
var request = HTTPRequest(
250251
url: url.appendingPathComponent(functionName),
251252
method: FunctionInvokeOptions.httpMethod(options.method) ?? .post,
252-
query: options.query,
253+
query: query,
253254
headers: mutableState.headers.merging(with: options.headers),
254255
body: options.body,
255256
timeoutInterval: FunctionsClient.requestIdleTimeout
256257
)
257258

258259
if let region = options.region ?? region {
259260
request.headers[.xRegion] = region
261+
query.appendOrUpdate(URLQueryItem(name: "forceFunctionRegion", value: region))
262+
request.query = query
260263
}
261264

262265
return request

Tests/FunctionsTests/FunctionsClientTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ final class FunctionsClientTests: XCTestCase {
164164

165165
Mock(
166166
url: url.appendingPathComponent("hello-world"),
167+
ignoreQuery: true,
167168
statusCode: 200,
168169
data: [.post: Data()]
169170
)
@@ -174,7 +175,7 @@ final class FunctionsClientTests: XCTestCase {
174175
--header "X-Client-Info: functions-swift/0.0.0" \
175176
--header "apikey: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" \
176177
--header "x-region: ca-central-1" \
177-
"http://localhost:5432/functions/v1/hello-world"
178+
"http://localhost:5432/functions/v1/hello-world?forceFunctionRegion=ca-central-1"
178179
"""#
179180
}
180181
.register()
@@ -185,6 +186,7 @@ final class FunctionsClientTests: XCTestCase {
185186
func testInvokeWithRegion() async throws {
186187
Mock(
187188
url: url.appendingPathComponent("hello-world"),
189+
ignoreQuery: true,
188190
statusCode: 200,
189191
data: [.post: Data()]
190192
)
@@ -195,7 +197,7 @@ final class FunctionsClientTests: XCTestCase {
195197
--header "X-Client-Info: functions-swift/0.0.0" \
196198
--header "apikey: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" \
197199
--header "x-region: ca-central-1" \
198-
"http://localhost:5432/functions/v1/hello-world"
200+
"http://localhost:5432/functions/v1/hello-world?forceFunctionRegion=ca-central-1"
199201
"""#
200202
}
201203
.register()

Tests/FunctionsTests/__Snapshots__/RequestTests/testInvokeWithCustomRegion.1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ curl \
33
--header "apikey: supabase.anon.key" \
44
--header "x-client-info: functions-swift/x.y.z" \
55
--header "x-region: ap-northeast-1" \
6-
"http://localhost:5432/functions/v1/hello-world"
6+
"http://localhost:5432/functions/v1/hello-world?forceFunctionRegion=ap-northeast-1"

0 commit comments

Comments
 (0)