Skip to content

Commit

Permalink
Merge pull request #314 from teamleadercrm/CORE_make-it-possible-to-s…
Browse files Browse the repository at this point in the history
…end-custom-headers

Feature: Make it possible to send custom headers
  • Loading branch information
JorenSaeyTL authored Apr 28, 2021
2 parents cad8ae3 + d275786 commit 5810db4
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

### Fixed

## [4.1.0]

### Added

- Possibility to add additionalHeaders to global and local configuration ([@JorenSaeyTL](https://github.com/JorenSaeyTL) in [#314](https://github.com/teamleadercrm/sdk-js/pull/314))

## [4.0.2] - 2020-01-14

### Fixed
Expand Down
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ const init = async () => {
};
```

- `additionalHeaders`: (object) request headers that are passed on top of the existing ones.

`additionalHeaders` can also be provided at local level, in that case it will override the global headers.

```js
import API from '@teamleader/api';

const { users } = API({
getAccessToken: () => 'thisisatoken',
additionalHeaders: {
'x-tl-feature-flags': 'core.some-feature-flag=true',
},
});

const init = async () => {
// (options, plugins)
const me = await users.me(undefined, {
additionalHeaders: {
'x-tl-feature-flags': 'core.some-feature-flag=true',
},
}); // The X-TL-Feature_flags header is passed
};
```

## Additional actions

You can also add additional actions (the domain will also be created if needed), which will be handled the same way as the available actions.
Expand Down Expand Up @@ -149,7 +173,7 @@ const { users } = API({
});

// own plugin
const addMeta = data => ({ ...data, meta: { size: data.length } });
const addMeta = (data) => ({ ...data, meta: { size: data.length } });

const init = async () => {
// (options, plugins)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@teamleader/api",
"version": "4.0.2",
"version": "4.1.0",
"description": "Teamleader API SDK",
"main": "dist/cjs/main.js",
"module": "dist/es/main.js",
Expand Down
9 changes: 7 additions & 2 deletions src/utils/createFetchOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import createRequestHeaders from './createRequestHeaders';
import applyPlugins from './applyPlugins';

export default async ({ configuration, parameters } = {}) => {
const { getAccessToken, plugins: { request: requestPlugins = [] } = {}, version } = configuration;
const {
getAccessToken,
plugins: { request: requestPlugins = [] } = {},
version,
additionalHeaders = {},
} = configuration;

return {
headers: await createRequestHeaders({ getAccessToken, version }),
headers: await createRequestHeaders({ getAccessToken, version, additionalHeaders }),
body: JSON.stringify(applyPlugins(parameters, requestPlugins)),
method: 'POST',
};
Expand Down
3 changes: 2 additions & 1 deletion src/utils/createRequestHeaders.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export default async ({ getAccessToken, version } = {}) => {
export default async ({ getAccessToken, version, additionalHeaders } = {}) => {
const accessToken = getAccessToken && (await getAccessToken());
return {
'Content-Type': 'application/json',
...(typeof accessToken !== 'undefined' && { Authorization: `Bearer ${accessToken}` }),
...(version && { 'X-Api-Version': version }),
...additionalHeaders,
};
};
3 changes: 3 additions & 0 deletions src/utils/mergeConfigurations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mergePlugins from './mergePlugins';
import mergeHeaders from './mergeHeaders';

export default ({ globalConfiguration = {}, localConfiguration = {} }) => {
const {
Expand All @@ -11,11 +12,13 @@ export default ({ globalConfiguration = {}, localConfiguration = {} }) => {
const { version: localVersion, fetchAll } = localConfiguration;

const plugins = mergePlugins(globalConfiguration.plugins, localConfiguration.plugins);
const additionalHeaders = mergeHeaders(globalConfiguration.additionalHeaders, localConfiguration.additionalHeaders);

return {
baseUrl,
plugins,
fetchAll,
additionalHeaders,
...((accessToken || getAccessToken) && { getAccessToken: getAccessToken || (() => accessToken) }),
...((localVersion || globalVersion) && { version: localVersion || globalVersion }),
};
Expand Down
7 changes: 7 additions & 0 deletions src/utils/mergeHeaders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default (...headers) =>
headers.reduce((combined, headerConfiguration) => {
if (!headerConfiguration || typeof headerConfiguration !== 'object') {
return combined;
}
return { ...combined, ...headerConfiguration };
}, {});
25 changes: 25 additions & 0 deletions test/utils/mergeConfigurations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ describe(`merge configurations`, () => {
response: [camelCase],
},
version: '2018-09-20',
additionalHeaders: {
'x-tl-feature-flags': 'core.elastified-companies',
},
};

const localConfiguration = {
plugins: {
request: [snakeCase],
},
additionalHeaders: {
'x-some-other-header': 'test',
},
};

it(`should merge the configurations in a correct way`, () => {
Expand All @@ -33,6 +39,10 @@ describe(`merge configurations`, () => {
response: [camelCase],
},
version: '2018-09-20',
additionalHeaders: {
'x-tl-feature-flags': 'core.elastified-companies',
'x-some-other-header': 'test',
},
};

expect(configuration).toEqual(expectedConfiguration);
Expand All @@ -54,6 +64,9 @@ describe(`merge configurations`, () => {
request: [snakeCase],
response: [camelCase],
},
additionalHeaders: {
'x-some-other-header': 'test',
},
};

expect(configuration).toEqual(expectedConfiguration);
Expand Down Expand Up @@ -85,6 +98,10 @@ describe(`merge configurations`, () => {
request: [snakeCase],
response: [camelCase],
},
additionalHeaders: {
'x-tl-feature-flags': 'core.elastified-companies',
'x-some-other-header': 'test',
},
};

expect(configuration).toEqual(expectedConfiguration);
Expand All @@ -105,6 +122,10 @@ describe(`merge configurations`, () => {
},
fetchAll: true,
version: '2018-11-20',
additionalHeaders: {
'x-tl-feature-flags': 'core.elastified-companies',
'x-some-other-header': 'test',
},
};

expect(configuration).toEqual(expectedConfiguration);
Expand All @@ -124,6 +145,10 @@ describe(`merge configurations`, () => {
response: [camelCase],
},
version: '2018-09-20',
additionalHeaders: {
'x-tl-feature-flags': 'core.elastified-companies',
'x-some-other-header': 'test',
},
};

expect(configuration).toEqual(expectedConfiguration);
Expand Down
83 changes: 83 additions & 0 deletions test/utils/mergeHeaders.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import mergeHeaders from '../../src/utils/mergeHeaders';

describe(`merge headers`, () => {
it(`should merge the headers in a correct way`, () => {
const globalHeaders = {
'x-header-1': 'headerValue1',
'x-header-2': 'headerValue2',
};

const localHeaders = {
'x-header-3': 'headerValue3',
'x-header-4': 'headerValue4',
};

const headers = mergeHeaders(globalHeaders, localHeaders);

const expectedHeaders = {
'x-header-1': 'headerValue1',
'x-header-2': 'headerValue2',
'x-header-3': 'headerValue3',
'x-header-4': 'headerValue4',
};

expect(headers).toEqual(expectedHeaders);
});

it(`should merge the headers in a correct way when one header object is given`, () => {
const globalHeaders = {
'x-header-1': 'headerValue1',
'x-header-2': 'headerValue2',
};

const headers = mergeHeaders(globalHeaders);

const expectedHeaders = {
'x-header-1': 'headerValue1',
'x-header-2': 'headerValue2',
};

expect(headers).toEqual(expectedHeaders);
});

it(`should merge the headers in a correct way when one header object is invalid`, () => {
const globalHeaders = {
'x-header-1': 'headerValue1',
'x-header-2': 'headerValue2',
};

const localHeaders = 'invalid';

const headers = mergeHeaders(globalHeaders, localHeaders);

const expectedHeaders = {
'x-header-1': 'headerValue1',
'x-header-2': 'headerValue2',
};

expect(headers).toEqual(expectedHeaders);
});

it(`should merge the headers in a correct way when header objects are undefined`, () => {
const headers = mergeHeaders(undefined, undefined);

expect(headers).toEqual({});
});
it(`should merge the headers in a correct way when one header object is invalid`, () => {
const globalHeaders = {
'x-header-1': 'headerValue1',
'x-header-2': 'headerValue2',
};

const localHeaders = 'invalid';

const headers = mergeHeaders(globalHeaders, localHeaders);

const expectedHeaders = {
'x-header-1': 'headerValue1',
'x-header-2': 'headerValue2',
};

expect(headers).toEqual(expectedHeaders);
});
});

0 comments on commit 5810db4

Please sign in to comment.