Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 6d0ee93

Browse files
laktekclaude
andcommitted
Add region as forceFunctionRegion query parameter
- Updated FunctionsClient to add region as both x-region header and forceFunctionRegion query param - Used URL API to properly construct query parameters - Added comprehensive tests to verify both header and query parameter functionality - Updated existing test to check for both region mechanisms - Maintains backward compatibility with existing x-region header 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 749ff05 commit 6d0ee93

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

src/FunctionsClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ export class FunctionsClient {
5757
if (!region) {
5858
region = this.region
5959
}
60+
// Add region as query parameter using URL API
61+
const url = new URL(`${this.url}/${functionName}`)
6062
if (region && region !== 'any') {
6163
_headers['x-region'] = region
64+
url.searchParams.set('forceFunctionRegion', region)
6265
}
6366
let body: any
6467
if (
@@ -88,7 +91,7 @@ export class FunctionsClient {
8891
}
8992
}
9093

91-
const response = await this.fetch(`${this.url}/${functionName}`, {
94+
const response = await this.fetch(url.toString(), {
9295
method: method || 'POST',
9396
// headers priority is (high to low):
9497
// 1. invoke-level headers

test/spec/params.spec.ts

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,24 @@ describe('params reached to function', () => {
166166
})
167167

168168
log('assert no error')
169-
const expected = {
170-
url: 'http://localhost:8000/mirror',
171-
method: 'POST',
172-
headers: data?.headers ?? [],
173-
body: '',
174-
}
175-
expect(data).toEqual(expected)
169+
expect(error).toBeNull()
170+
171+
// Check that x-region header is present
172+
expect(
173+
(data?.headers as [Array<string>]).filter(([k, v]) => k === 'x-region' && v === validRegion)
174+
.length > 0
175+
).toBe(true)
176+
177+
// Check that the URL contains the forceFunctionRegion query parameter
178+
expect(data?.url).toContain(`forceFunctionRegion=${validRegion}`)
179+
176180
attach(
177181
'check headers from function',
178182
`expected to include: ${['custom-header', customHeader]}\n actual: ${JSON.stringify(
179183
data?.headers
180184
)}`,
181185
ContentType.TEXT
182186
)
183-
console.log(data?.headers)
184-
expect(
185-
(data?.headers as [Array<string>]).filter(([k, v]) => k === 'x-region' && v === validRegion)
186-
.length > 0
187-
).toBe(true)
188187
})
189188

190189
test('invoke with region overrides region in the client', async () => {
@@ -490,4 +489,42 @@ describe('params reached to function', () => {
490489
}
491490
expect(data).toMatchObject(expected)
492491
})
492+
493+
test('invoke mirror with region adds forceFunctionRegion query parameter', async () => {
494+
/**
495+
* @feature headers
496+
*/
497+
log('create FunctionsClient')
498+
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)
499+
500+
log('invoke mirror')
501+
const customHeader = nanoid()
502+
const validRegion = FunctionRegion.ApNortheast1
503+
504+
const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
505+
headers: {
506+
'custom-header': customHeader,
507+
Authorization: `Bearer ${apiKey}`,
508+
},
509+
region: validRegion,
510+
})
511+
512+
log('assert no error')
513+
expect(error).toBeNull()
514+
515+
// Check that both x-region header and forceFunctionRegion query param are present
516+
expect(
517+
(data?.headers as [Array<string>]).filter(([k, v]) => k === 'x-region' && v === validRegion)
518+
.length > 0
519+
).toBe(true)
520+
521+
// Check that the URL contains the forceFunctionRegion query parameter
522+
expect(data?.url).toContain(`forceFunctionRegion=${validRegion}`)
523+
524+
attach(
525+
'check URL contains forceFunctionRegion query param',
526+
`expected URL to contain: forceFunctionRegion=${validRegion}\n actual URL: ${data?.url}`,
527+
ContentType.TEXT
528+
)
529+
})
493530
})

0 commit comments

Comments
 (0)