Skip to content

Commit

Permalink
Merge pull request #334 from teamleadercrm/allow-to-overwrite-endpoints
Browse files Browse the repository at this point in the history
Allow overwriting the action endpoints.
  • Loading branch information
lowiebenoot authored Aug 31, 2022
2 parents 71945b2 + aab7684 commit f317c11
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Changed

- Allow overwriting action endpoints. This is useful for mocking the API. ([@lowiebenoot](https://github.com/lowiebenoot) in [#334](https://github.com/teamleadercrm/sdk-js/pull/334))

### Deprecated

### Removed
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,17 @@ describe('fetch response handling', () => {
expect(api.companies.info).toEqual(api.companies.info);
expect(api.companies.info).not.toEqual(api.contacts.info);
});

it('can overwrite an endpoint', async () => {
const getAccessToken = () => 'thisisatoken';

const api = API({
getAccessToken,
});

const mockFunction = jest.fn();
api.contacts.info = mockFunction;

expect(api.contacts.info).toEqual(mockFunction);
});
});
12 changes: 12 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ const API = (globalConfiguration: GlobalConfiguration) => {
cachedActionEndpoints[domainName][actionName] = actionEndpoint;
return actionEndpoint;
},
set(_target, actionNameKey, value) {
const domainName = String(domainNameKey);
const actionName = String(actionNameKey);

if (typeof cachedActionEndpoints[domainName] === 'undefined') {
cachedActionEndpoints[domainName] = {};
}

cachedActionEndpoints[domainName][actionName] = value;

return true;
},
},
);
},
Expand Down

0 comments on commit f317c11

Please sign in to comment.