Skip to content

Commit b7a9faa

Browse files
committed
feat: add Api Base URL option to client
1 parent 866075c commit b7a9faa

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/create-client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export const clientConstructor = ({ token, ...options }: Typeform.ClientArg = {}
1313
...otherArgs
1414
} = args
1515

16-
const requestUrl = buildUrlWithParams(`${API_BASE_URL}${url}`, params)
16+
const { apiBaseUrl } = options
17+
const requestApiBaseUrl = apiBaseUrl || API_BASE_URL
18+
const requestUrl = buildUrlWithParams(`${requestApiBaseUrl}${url}`, params)
1719

1820
const { headers = {} } = options
1921
const authorization = token ? { Authorization: `bearer ${token}` } : {}

tests/unit/create-client.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,23 @@ test('falsy values should be passed', () => {
6868
}
6969
expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=0&b=0')
7070
})
71+
72+
test('Specify apiBaseUrl', async () => {
73+
74+
75+
const rainbowApi = 'https://api.rainbow.typeform.com'
76+
77+
const clientWithApiBaseUrl = clientConstructor({
78+
token: 'abc',
79+
apiBaseUrl: rainbowApi
80+
})
81+
82+
await clientWithApiBaseUrl.request({
83+
url: '/forms',
84+
method: 'get',
85+
headers: {
86+
Accepts: 'application/json'
87+
}
88+
})
89+
expect(axios.history.get[0].url).toBe(`${rainbowApi}/forms`)
90+
})

0 commit comments

Comments
 (0)