Skip to content

Commit

Permalink
feat: client headers for CubejsApi (#242). Thanks to @ferrants!
Browse files Browse the repository at this point in the history
* client headers for CubejsApi

* headers docs

* default for headers

Fixes #241
  • Loading branch information
ferrants authored and paveltiunov committed Oct 25, 2019
1 parent 470792f commit 2f75ef3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/Cube.js-Frontend/@cubejs-client-core.md
Expand Up @@ -32,6 +32,8 @@ Can be an async function without arguments that returns API token.
- `options` - options object.
- `options.apiUrl` - URL of your Cube.js Backend.
By default, in the development environment it is `http://localhost:4000/cubejs-api/v1`.
- `options.headers` - optional object of additional headers to apply to requests


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

Expand Down
5 changes: 3 additions & 2 deletions packages/cubejs-client-core/src/HttpTransport.js
Expand Up @@ -2,9 +2,10 @@ import fetch from 'cross-fetch';
import 'url-search-params-polyfill';

class HttpTransport {
constructor({ authorization, apiUrl }) {
constructor({ authorization, apiUrl, headers = {} }) {
this.authorization = authorization;
this.apiUrl = apiUrl;
this.headers = headers
}

request(method, params) {
Expand All @@ -16,7 +17,7 @@ class HttpTransport {

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

Expand Down
4 changes: 3 additions & 1 deletion packages/cubejs-client-core/src/index.js
Expand Up @@ -35,9 +35,11 @@ class CubejsApi {
options = options || {};
this.apiToken = apiToken;
this.apiUrl = options.apiUrl || API_URL;
this.headers = options.headers || {};
this.transport = options.transport || new HttpTransport({
authorization: typeof apiToken === 'function' ? undefined : apiToken,
apiUrl: this.apiUrl
apiUrl: this.apiUrl,
headers: this.headers
});
this.pollInterval = options.pollInterval || 5;
}
Expand Down

0 comments on commit 2f75ef3

Please sign in to comment.