Skip to content

Feat add regional options #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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