Skip to content

Commit 2f75ef3

Browse files
ferrantspaveltiunov
authored andcommitted
feat: client headers for CubejsApi (#242). Thanks to @ferrants!
* client headers for CubejsApi * headers docs * default for headers Fixes #241
1 parent 470792f commit 2f75ef3

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

docs/Cube.js-Frontend/@cubejs-client-core.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Can be an async function without arguments that returns API token.
3232
- `options` - options object.
3333
- `options.apiUrl` - URL of your Cube.js Backend.
3434
By default, in the development environment it is `http://localhost:4000/cubejs-api/v1`.
35+
- `options.headers` - optional object of additional headers to apply to requests
36+
3537

3638
**Returns:** [CubejsApi](#cubejs-api)
3739

packages/cubejs-client-core/src/HttpTransport.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import fetch from 'cross-fetch';
22
import 'url-search-params-polyfill';
33

44
class HttpTransport {
5-
constructor({ authorization, apiUrl }) {
5+
constructor({ authorization, apiUrl, headers = {} }) {
66
this.authorization = authorization;
77
this.apiUrl = apiUrl;
8+
this.headers = headers
89
}
910

1011
request(method, params) {
@@ -16,7 +17,7 @@ class HttpTransport {
1617

1718
const runRequest = () => fetch(
1819
`${this.apiUrl}/${method}${searchParams.toString().length ? `?${searchParams}` : ''}`, {
19-
headers: { Authorization: this.authorization, 'Content-Type': 'application/json' }
20+
headers: Object.assign({ Authorization: this.authorization, 'Content-Type': 'application/json' }, this.headers)
2021
}
2122
);
2223

packages/cubejs-client-core/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ class CubejsApi {
3535
options = options || {};
3636
this.apiToken = apiToken;
3737
this.apiUrl = options.apiUrl || API_URL;
38+
this.headers = options.headers || {};
3839
this.transport = options.transport || new HttpTransport({
3940
authorization: typeof apiToken === 'function' ? undefined : apiToken,
40-
apiUrl: this.apiUrl
41+
apiUrl: this.apiUrl,
42+
headers: this.headers
4143
});
4244
this.pollInterval = options.pollInterval || 5;
4345
}

0 commit comments

Comments
 (0)