Skip to content

Commit

Permalink
fix: set region on invoke and in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
egor-romanov committed Jan 30, 2024
1 parent c7d7b17 commit e4a86d8
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 12 deletions.
15 changes: 11 additions & 4 deletions src/FunctionsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@ import {
FunctionsRelayError,
FunctionsResponse,
FunctionInvokeOptions,
FunctionRegion,
} from './types'

export class FunctionsClient {
protected url: string
protected headers: Record<string, string>
protected region: string
protected region: FunctionRegion
protected fetch: Fetch

constructor(
url: string,
{
headers = {},
customFetch,
region = FunctionRegion.Any,
}: {
headers?: Record<string, string>
customFetch?: Fetch
region?: FunctionRegion
} = {}
) {
this.url = url
this.headers = headers
this.region = "any"
this.region = region
this.fetch = resolveFetch(customFetch)
}

Expand All @@ -50,8 +53,12 @@ export class FunctionsClient {
try {
const { headers, method, body: functionArgs } = options
let _headers: Record<string, string> = {}
if (this.region !== 'any') {
_headers['x-region'] = this.region
let { region } = options
if (!region) {
region = this.region
}
if (region && region !== 'any') {
_headers['x-region'] = region
}
let body: any
if (
Expand Down
120 changes: 112 additions & 8 deletions test/spec/params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,125 @@ describe('params reached to function', () => {
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)

log('invoke mirror')
const customHeader = nanoid();
const validRegion = FunctionRegion.ApNortheast1;
const customHeader = nanoid()
const validRegion = FunctionRegion.ApNortheast1

const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
headers: {
'custom-header': customHeader,
Authorization: `Bearer ${apiKey}`,
},
region: validRegion
region: validRegion,
})

log('assert no error')
expect(data).not.toBeNull()
const expected = {
url: 'http://localhost:8000/mirror',
method: 'POST',
headers: data?.headers ?? [],
body: '',
}
expect(data).toEqual(expected)
attach(
'check headers from function',
`expected to include: ${['custom-header', customHeader]}\n actual: ${JSON.stringify(
data?.headers
)}`,
ContentType.TEXT
)
console.log(data?.headers)
expect(
(data?.headers as [Array<string>]).filter(([k, v]) => k === 'x-region' && v === validRegion)
.length > 0
).toBe(true)
})

test('invoke with region overrides region in the client', async () => {
/**
* @feature headers
*/
log('create FunctionsClient')
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
region: FunctionRegion.ApNortheast1,
})

log('invoke mirror')
const customHeader = nanoid()
const validRegion = FunctionRegion.ApSoutheast1

const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
headers: {
'custom-header': customHeader,
Authorization: `Bearer ${apiKey}`,
},
region: validRegion,
})

log('assert no error')
const expected = {
url: 'http://localhost:8000/mirror',
method: 'POST',
headers: data?.headers ?? [],
body: '',
}
expect(data).toEqual(expected)
attach(
'check headers from function',
`expected to include: ${['custom-header', customHeader]}\n actual: ${JSON.stringify(
data?.headers
)}`,
ContentType.TEXT
)
console.log(data?.headers)
expect(
(data?.headers as [Array<string>]).filter(([k, v]) => k === 'x-region' && v === validRegion)
.length > 0
).toBe(true)
})

test('invoke with region overrides region in the client', async () => {
/**
* @feature headers
*/
log('create FunctionsClient')
const validRegion = FunctionRegion.ApSoutheast1
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
region: validRegion,
})

log('invoke mirror')
const customHeader = nanoid()

const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
headers: {
'custom-header': customHeader,
Authorization: `Bearer ${apiKey}`,
},
})

log('assert no error')
const expected = {
url: 'http://localhost:8000/mirror',
method: 'POST',
headers: data?.headers ?? [],
body: '',
}
expect(data).toEqual(expected)
attach(
'check headers from function',
`expected to include: ${['custom-header', customHeader]}\n actual: ${JSON.stringify(
data?.headers
)}`,
ContentType.TEXT
)
console.log(data?.headers)
expect(
(data?.headers as [Array<string>]).filter(([k, v]) => k === 'x-region' && v === validRegion)
.length > 0
).toBe(true)
})

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

log('invoke mirror')
const customHeader = nanoid();
const validRegion = FunctionRegion.EuWest1;
const customHeader = nanoid()
const validRegion = FunctionRegion.EuWest1

const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
headers: {
'custom-header': customHeader,
Authorization: `Bearer ${apiKey}`,
"x-region": validRegion
}
"x-region": validRegion,
},
})

log('assert no error')
Expand Down

0 comments on commit e4a86d8

Please sign in to comment.