Skip to content

Commit e4a86d8

Browse files
committed
fix: set region on invoke and in constructor
1 parent c7d7b17 commit e4a86d8

File tree

2 files changed

+123
-12
lines changed

2 files changed

+123
-12
lines changed

src/FunctionsClient.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,30 @@ import {
66
FunctionsRelayError,
77
FunctionsResponse,
88
FunctionInvokeOptions,
9+
FunctionRegion,
910
} from './types'
1011

1112
export class FunctionsClient {
1213
protected url: string
1314
protected headers: Record<string, string>
14-
protected region: string
15+
protected region: FunctionRegion
1516
protected fetch: Fetch
1617

1718
constructor(
1819
url: string,
1920
{
2021
headers = {},
2122
customFetch,
23+
region = FunctionRegion.Any,
2224
}: {
2325
headers?: Record<string, string>
2426
customFetch?: Fetch
27+
region?: FunctionRegion
2528
} = {}
2629
) {
2730
this.url = url
2831
this.headers = headers
29-
this.region = 'any'
32+
this.region = region
3033
this.fetch = resolveFetch(customFetch)
3134
}
3235

@@ -50,8 +53,12 @@ export class FunctionsClient {
5053
try {
5154
const { headers, method, body: functionArgs } = options
5255
let _headers: Record<string, string> = {}
53-
if (this.region !== 'any') {
54-
_headers['x-region'] = this.region
56+
let { region } = options
57+
if (!region) {
58+
region = this.region
59+
}
60+
if (region && region !== 'any') {
61+
_headers['x-region'] = region
5562
}
5663
let body: any
5764
if (

test/spec/params.spec.ts

+112-8
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,125 @@ describe('params reached to function', () => {
154154
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)
155155

156156
log('invoke mirror')
157-
const customHeader = nanoid();
158-
const validRegion = FunctionRegion.ApNortheast1;
157+
const customHeader = nanoid()
158+
const validRegion = FunctionRegion.ApNortheast1
159159

160160
const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
161161
headers: {
162162
'custom-header': customHeader,
163163
Authorization: `Bearer ${apiKey}`,
164164
},
165-
region: validRegion
165+
region: validRegion,
166166
})
167167

168168
log('assert no error')
169-
expect(data).not.toBeNull()
169+
const expected = {
170+
url: 'http://localhost:8000/mirror',
171+
method: 'POST',
172+
headers: data?.headers ?? [],
173+
body: '',
174+
}
175+
expect(data).toEqual(expected)
176+
attach(
177+
'check headers from function',
178+
`expected to include: ${['custom-header', customHeader]}\n actual: ${JSON.stringify(
179+
data?.headers
180+
)}`,
181+
ContentType.TEXT
182+
)
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)
188+
})
189+
190+
test('invoke with region overrides region in the client', async () => {
191+
/**
192+
* @feature headers
193+
*/
194+
log('create FunctionsClient')
195+
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
196+
region: FunctionRegion.ApNortheast1,
197+
})
198+
199+
log('invoke mirror')
200+
const customHeader = nanoid()
201+
const validRegion = FunctionRegion.ApSoutheast1
202+
203+
const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
204+
headers: {
205+
'custom-header': customHeader,
206+
Authorization: `Bearer ${apiKey}`,
207+
},
208+
region: validRegion,
209+
})
210+
211+
log('assert no error')
212+
const expected = {
213+
url: 'http://localhost:8000/mirror',
214+
method: 'POST',
215+
headers: data?.headers ?? [],
216+
body: '',
217+
}
218+
expect(data).toEqual(expected)
219+
attach(
220+
'check headers from function',
221+
`expected to include: ${['custom-header', customHeader]}\n actual: ${JSON.stringify(
222+
data?.headers
223+
)}`,
224+
ContentType.TEXT
225+
)
226+
console.log(data?.headers)
227+
expect(
228+
(data?.headers as [Array<string>]).filter(([k, v]) => k === 'x-region' && v === validRegion)
229+
.length > 0
230+
).toBe(true)
231+
})
232+
233+
test('invoke with region overrides region in the client', async () => {
234+
/**
235+
* @feature headers
236+
*/
237+
log('create FunctionsClient')
238+
const validRegion = FunctionRegion.ApSoutheast1
239+
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
240+
region: validRegion,
241+
})
242+
243+
log('invoke mirror')
244+
const customHeader = nanoid()
245+
246+
const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
247+
headers: {
248+
'custom-header': customHeader,
249+
Authorization: `Bearer ${apiKey}`,
250+
},
251+
})
252+
253+
log('assert no error')
254+
const expected = {
255+
url: 'http://localhost:8000/mirror',
256+
method: 'POST',
257+
headers: data?.headers ?? [],
258+
body: '',
259+
}
260+
expect(data).toEqual(expected)
261+
attach(
262+
'check headers from function',
263+
`expected to include: ${['custom-header', customHeader]}\n actual: ${JSON.stringify(
264+
data?.headers
265+
)}`,
266+
ContentType.TEXT
267+
)
268+
console.log(data?.headers)
269+
expect(
270+
(data?.headers as [Array<string>]).filter(([k, v]) => k === 'x-region' && v === validRegion)
271+
.length > 0
272+
).toBe(true)
170273
})
171274

275+
// todo: update test to check for the correct header value
172276
test('invoke mirror with invoke header and valid region', async () => {
173277
/**
174278
* @feature headers
@@ -177,15 +281,15 @@ describe('params reached to function', () => {
177281
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)
178282

179283
log('invoke mirror')
180-
const customHeader = nanoid();
181-
const validRegion = FunctionRegion.EuWest1;
284+
const customHeader = nanoid()
285+
const validRegion = FunctionRegion.EuWest1
182286

183287
const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
184288
headers: {
185289
'custom-header': customHeader,
186290
Authorization: `Bearer ${apiKey}`,
187-
'x-region': validRegion
188-
}
291+
'x-region': validRegion,
292+
},
189293
})
190294

191295
log('assert no error')

0 commit comments

Comments
 (0)