@2bad/bitrix 2.4.0 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -0
- package/README.md +54 -49
- package/build/main/bitrix.d.ts +10 -2
- package/build/main/bitrix.integration.test.d.ts +0 -0
- package/build/main/bitrix.integration.test.js +31 -0
- package/build/main/bitrix.js +24 -10
- package/build/main/bitrix.unit.test.d.ts +1 -0
- package/build/main/bitrix.unit.test.js +44 -0
- package/build/main/client/client.unit.test.d.ts +1 -0
- package/build/main/client/client.unit.test.js +70 -0
- package/build/main/client/hooks/addAccessToken.d.ts +4 -1
- package/build/main/client/hooks/addAccessToken.js +3 -1
- package/build/main/client/hooks/addAccessToken.unit.test.d.ts +1 -0
- package/build/main/client/hooks/addAccessToken.unit.test.js +31 -0
- package/build/main/client/index.d.ts +9 -1
- package/build/main/client/index.js +17 -6
- package/build/main/client/methods/batch.d.ts +19 -0
- package/build/main/client/methods/batch.js +35 -10
- package/build/main/client/methods/batch.unit.test.d.ts +1 -0
- package/build/main/client/methods/batch.unit.test.js +494 -0
- package/build/main/client/methods/call.d.ts +7 -0
- package/build/main/client/methods/call.js +11 -4
- package/build/main/client/methods/call.unit.test.d.ts +1 -0
- package/build/main/client/methods/call.unit.test.js +183 -0
- package/build/main/client/methods/list.d.ts +12 -0
- package/build/main/client/methods/list.js +23 -7
- package/build/main/client/methods/list.unit.test.d.ts +1 -0
- package/build/main/client/methods/list.unit.test.js +277 -0
- package/build/main/commands.js +0 -1
- package/build/main/methods.d.ts +19 -0
- package/build/main/methods.js +0 -1
- package/build/main/payloads.js +0 -1
- package/build/main/services/common.d.ts +1 -0
- package/build/main/services/common.js +0 -1
- package/build/main/services/companies/entities.js +0 -1
- package/build/main/services/companies/index.integration.test.d.ts +1 -0
- package/build/main/services/companies/index.integration.test.js +19 -0
- package/build/main/services/companies/index.js +0 -1
- package/build/main/services/companies/index.unit.test.d.ts +1 -0
- package/build/main/services/companies/index.unit.test.js +72 -0
- package/build/main/services/companies/methods.js +0 -1
- package/build/main/services/contacts/entities.d.ts +2 -2
- package/build/main/services/contacts/entities.js +0 -1
- package/build/main/services/contacts/index.integration.test.d.ts +1 -0
- package/build/main/services/contacts/index.integration.test.js +19 -0
- package/build/main/services/contacts/index.js +0 -1
- package/build/main/services/contacts/index.unit.test.d.ts +1 -0
- package/build/main/services/contacts/index.unit.test.js +72 -0
- package/build/main/services/contacts/methods.js +0 -1
- package/build/main/services/currencies/entities.js +0 -1
- package/build/main/services/deals/entities.js +0 -1
- package/build/main/services/deals/index.integration.test.d.ts +1 -0
- package/build/main/services/deals/index.integration.test.js +19 -0
- package/build/main/services/deals/index.js +0 -1
- package/build/main/services/deals/index.unit.test.d.ts +1 -0
- package/build/main/services/deals/index.unit.test.js +72 -0
- package/build/main/services/deals/methods.js +0 -1
- package/build/main/services/leads/entities.js +0 -1
- package/build/main/services/leads/index.integration.test.d.ts +1 -0
- package/build/main/services/leads/index.integration.test.js +19 -0
- package/build/main/services/leads/index.js +0 -1
- package/build/main/services/leads/index.unit.test.d.ts +1 -0
- package/build/main/services/leads/index.unit.test.js +72 -0
- package/build/main/services/leads/methods.js +0 -1
- package/build/main/services/statuses/entities.js +0 -1
- package/build/main/services/statuses/index.integration.test.d.ts +1 -0
- package/build/main/services/statuses/index.integration.test.js +90 -0
- package/build/main/services/statuses/index.js +0 -1
- package/build/main/services/statuses/index.unit.test.d.ts +1 -0
- package/build/main/services/statuses/index.unit.test.js +60 -0
- package/build/main/services/statuses/methods.js +0 -1
- package/build/main/services/users/entities.js +0 -1
- package/build/main/services/users/index.d.ts +1 -1
- package/build/main/services/users/index.integration.test.d.ts +1 -0
- package/build/main/services/users/index.integration.test.js +33 -0
- package/build/main/services/users/index.js +0 -1
- package/build/main/services/users/index.unit.test.d.ts +1 -0
- package/build/main/services/users/index.unit.test.js +25 -0
- package/build/main/services/users/methods.js +0 -1
- package/build/main/utils/Diff.js +0 -1
- package/build/main/utils/ExtractValue.d.ts +5 -0
- package/build/main/utils/ExtractValue.js +0 -1
- package/build/main/utils/isArray.js +1 -1
- package/build/module/bitrix.d.ts +10 -2
- package/build/module/bitrix.integration.test.d.ts +0 -0
- package/build/module/bitrix.integration.test.js +31 -0
- package/build/module/bitrix.js +10 -2
- package/build/module/bitrix.unit.test.d.ts +1 -0
- package/build/module/bitrix.unit.test.js +16 -0
- package/build/module/client/client.unit.test.d.ts +1 -0
- package/build/module/client/client.unit.test.js +65 -0
- package/build/module/client/hooks/addAccessToken.d.ts +4 -1
- package/build/module/client/hooks/addAccessToken.js +3 -1
- package/build/module/client/hooks/addAccessToken.unit.test.d.ts +1 -0
- package/build/module/client/hooks/addAccessToken.unit.test.js +26 -0
- package/build/module/client/index.d.ts +9 -1
- package/build/module/client/index.js +14 -3
- package/build/module/client/methods/batch.d.ts +19 -0
- package/build/module/client/methods/batch.js +26 -1
- package/build/module/client/methods/batch.unit.test.d.ts +1 -0
- package/build/module/client/methods/batch.unit.test.js +466 -0
- package/build/module/client/methods/call.d.ts +7 -0
- package/build/module/client/methods/call.js +9 -3
- package/build/module/client/methods/call.unit.test.d.ts +1 -0
- package/build/module/client/methods/call.unit.test.js +155 -0
- package/build/module/client/methods/list.d.ts +12 -0
- package/build/module/client/methods/list.js +20 -6
- package/build/module/client/methods/list.unit.test.d.ts +1 -0
- package/build/module/client/methods/list.unit.test.js +252 -0
- package/build/module/commands.js +0 -1
- package/build/module/methods.d.ts +19 -0
- package/build/module/methods.js +0 -1
- package/build/module/payloads.js +0 -1
- package/build/module/services/common.d.ts +1 -0
- package/build/module/services/common.js +0 -1
- package/build/module/services/companies/entities.js +0 -1
- package/build/module/services/companies/index.integration.test.d.ts +1 -0
- package/build/module/services/companies/index.integration.test.js +14 -0
- package/build/module/services/companies/index.js +0 -1
- package/build/module/services/companies/index.unit.test.d.ts +1 -0
- package/build/module/services/companies/index.unit.test.js +67 -0
- package/build/module/services/companies/methods.js +0 -1
- package/build/module/services/contacts/entities.d.ts +2 -2
- package/build/module/services/contacts/entities.js +0 -1
- package/build/module/services/contacts/index.integration.test.d.ts +1 -0
- package/build/module/services/contacts/index.integration.test.js +14 -0
- package/build/module/services/contacts/index.js +0 -1
- package/build/module/services/contacts/index.unit.test.d.ts +1 -0
- package/build/module/services/contacts/index.unit.test.js +67 -0
- package/build/module/services/contacts/methods.js +0 -1
- package/build/module/services/currencies/entities.js +0 -1
- package/build/module/services/deals/entities.js +0 -1
- package/build/module/services/deals/index.integration.test.d.ts +1 -0
- package/build/module/services/deals/index.integration.test.js +14 -0
- package/build/module/services/deals/index.js +0 -1
- package/build/module/services/deals/index.unit.test.d.ts +1 -0
- package/build/module/services/deals/index.unit.test.js +67 -0
- package/build/module/services/deals/methods.js +0 -1
- package/build/module/services/leads/entities.js +0 -1
- package/build/module/services/leads/index.integration.test.d.ts +1 -0
- package/build/module/services/leads/index.integration.test.js +14 -0
- package/build/module/services/leads/index.js +0 -1
- package/build/module/services/leads/index.unit.test.d.ts +1 -0
- package/build/module/services/leads/index.unit.test.js +67 -0
- package/build/module/services/leads/methods.js +0 -1
- package/build/module/services/statuses/entities.js +0 -1
- package/build/module/services/statuses/index.integration.test.d.ts +1 -0
- package/build/module/services/statuses/index.integration.test.js +85 -0
- package/build/module/services/statuses/index.js +0 -1
- package/build/module/services/statuses/index.unit.test.d.ts +1 -0
- package/build/module/services/statuses/index.unit.test.js +55 -0
- package/build/module/services/statuses/methods.js +0 -1
- package/build/module/services/users/entities.js +0 -1
- package/build/module/services/users/index.d.ts +1 -1
- package/build/module/services/users/index.integration.test.d.ts +1 -0
- package/build/module/services/users/index.integration.test.js +28 -0
- package/build/module/services/users/index.js +0 -1
- package/build/module/services/users/index.unit.test.d.ts +1 -0
- package/build/module/services/users/index.unit.test.js +20 -0
- package/build/module/services/users/methods.js +0 -1
- package/build/module/utils/Diff.js +0 -1
- package/build/module/utils/ExtractValue.d.ts +5 -0
- package/build/module/utils/ExtractValue.js +0 -1
- package/build/module/utils/isArray.js +1 -1
- package/package.json +21 -22
- package/build/main/bitrix.js.map +0 -1
- package/build/main/client/hooks/addAccessToken.js.map +0 -1
- package/build/main/client/index.js.map +0 -1
- package/build/main/client/methods/batch.js.map +0 -1
- package/build/main/client/methods/call.js.map +0 -1
- package/build/main/client/methods/list.js.map +0 -1
- package/build/main/commands.js.map +0 -1
- package/build/main/methods.js.map +0 -1
- package/build/main/payloads.js.map +0 -1
- package/build/main/services/common.js.map +0 -1
- package/build/main/services/companies/entities.js.map +0 -1
- package/build/main/services/companies/index.js.map +0 -1
- package/build/main/services/companies/methods.js.map +0 -1
- package/build/main/services/contacts/entities.js.map +0 -1
- package/build/main/services/contacts/index.js.map +0 -1
- package/build/main/services/contacts/methods.js.map +0 -1
- package/build/main/services/currencies/entities.js.map +0 -1
- package/build/main/services/deals/entities.js.map +0 -1
- package/build/main/services/deals/index.js.map +0 -1
- package/build/main/services/deals/methods.js.map +0 -1
- package/build/main/services/leads/entities.js.map +0 -1
- package/build/main/services/leads/index.js.map +0 -1
- package/build/main/services/leads/methods.js.map +0 -1
- package/build/main/services/statuses/entities.js.map +0 -1
- package/build/main/services/statuses/index.js.map +0 -1
- package/build/main/services/statuses/methods.js.map +0 -1
- package/build/main/services/users/entities.js.map +0 -1
- package/build/main/services/users/index.js.map +0 -1
- package/build/main/services/users/methods.js.map +0 -1
- package/build/main/utils/Diff.js.map +0 -1
- package/build/main/utils/ExtractValue.js.map +0 -1
- package/build/main/utils/isArray.js.map +0 -1
- package/build/module/bitrix.js.map +0 -1
- package/build/module/client/hooks/addAccessToken.js.map +0 -1
- package/build/module/client/index.js.map +0 -1
- package/build/module/client/methods/batch.js.map +0 -1
- package/build/module/client/methods/call.js.map +0 -1
- package/build/module/client/methods/list.js.map +0 -1
- package/build/module/commands.js.map +0 -1
- package/build/module/methods.js.map +0 -1
- package/build/module/payloads.js.map +0 -1
- package/build/module/services/common.js.map +0 -1
- package/build/module/services/companies/entities.js.map +0 -1
- package/build/module/services/companies/index.js.map +0 -1
- package/build/module/services/companies/methods.js.map +0 -1
- package/build/module/services/contacts/entities.js.map +0 -1
- package/build/module/services/contacts/index.js.map +0 -1
- package/build/module/services/contacts/methods.js.map +0 -1
- package/build/module/services/currencies/entities.js.map +0 -1
- package/build/module/services/deals/entities.js.map +0 -1
- package/build/module/services/deals/index.js.map +0 -1
- package/build/module/services/deals/methods.js.map +0 -1
- package/build/module/services/leads/entities.js.map +0 -1
- package/build/module/services/leads/index.js.map +0 -1
- package/build/module/services/leads/methods.js.map +0 -1
- package/build/module/services/statuses/entities.js.map +0 -1
- package/build/module/services/statuses/index.js.map +0 -1
- package/build/module/services/statuses/methods.js.map +0 -1
- package/build/module/services/users/entities.js.map +0 -1
- package/build/module/services/users/index.js.map +0 -1
- package/build/module/services/users/methods.js.map +0 -1
- package/build/module/utils/Diff.js.map +0 -1
- package/build/module/utils/ExtractValue.js.map +0 -1
- package/build/module/utils/isArray.js.map +0 -1
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const bitrix_1 = __importDefault(require("./../../bitrix"));
|
|
7
|
+
const WEBHOOK_URL = process.env['WEBHOOK_URL'];
|
|
8
|
+
if (!WEBHOOK_URL) {
|
|
9
|
+
throw Error('Integration tests require environmental variable `WEBHOOK_URL` to be set');
|
|
10
|
+
}
|
|
11
|
+
const { users } = (0, bitrix_1.default)(WEBHOOK_URL);
|
|
12
|
+
describe('Users', () => {
|
|
13
|
+
describe('fields', () => {
|
|
14
|
+
it('should get all fields', async () => {
|
|
15
|
+
const { result } = await users.fields();
|
|
16
|
+
expect(result).toMatchSnapshot();
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
describe('get', () => {
|
|
20
|
+
it('should get all users', async () => {
|
|
21
|
+
const { result } = await users.get();
|
|
22
|
+
expect(result).toMatchSnapshot();
|
|
23
|
+
});
|
|
24
|
+
it('should get entry with ID = 1', async () => {
|
|
25
|
+
const { result } = await users.get('1');
|
|
26
|
+
expect(result).toMatchSnapshot();
|
|
27
|
+
});
|
|
28
|
+
it('should return an error in case non existent ID is requested', async () => {
|
|
29
|
+
const { result } = await users.get('1001');
|
|
30
|
+
expect(result).toEqual([]);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const _1 = __importDefault(require("."));
|
|
7
|
+
const mockCall = jest.fn(() => Promise.resolve());
|
|
8
|
+
const mockList = jest.fn(() => Promise.resolve());
|
|
9
|
+
const users = (0, _1.default)({ call: mockCall });
|
|
10
|
+
const ID = '77';
|
|
11
|
+
describe('Users', () => {
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
mockCall.mockClear();
|
|
14
|
+
mockList.mockClear();
|
|
15
|
+
});
|
|
16
|
+
it('should expose API methods', () => {
|
|
17
|
+
expect(users).toMatchSnapshot();
|
|
18
|
+
});
|
|
19
|
+
describe('`get`', () => {
|
|
20
|
+
it('should invoke `call`', async () => {
|
|
21
|
+
await users.get(ID);
|
|
22
|
+
expect(mockCall.mock.calls).toMatchSnapshot();
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
});
|
package/build/main/utils/Diff.js
CHANGED
|
@@ -1 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extract value from the `MAP` following the `KEY`
|
|
3
|
+
* It works with `enum` as a `KEY` when matched against dict as `enum` values as a keys,
|
|
4
|
+
* which justifies whole existence of that helper.
|
|
5
|
+
*/
|
|
1
6
|
export declare type ExtractValue<MAP extends Record<string, unknown>, KEY extends string | number | symbol, MAPKEY extends keyof MAP = KEY extends keyof MAP ? KEY : never> = MAP[MAPKEY];
|
package/build/module/bitrix.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { ExtendOptions } from 'got';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Construct a Bitrix client with generic methods
|
|
4
|
+
* @param restURI REST endpoint, like a `https://hello.bitrix24.ua/rest` or an inbound webhook endpoint,
|
|
5
|
+
* like a `https://hello.bitrix24.ua/rest/1/WEBHOOK_TOKEN`.
|
|
6
|
+
* @param accessToken Bitrix application Access Token. Do not specify in case inbound webhook endpoint used.
|
|
7
|
+
* @param clientOptions an object that will overwrite underlying configuration for HTTP client,
|
|
8
|
+
* see `https://github.com/sindresorhus/got/blob/main/documentation/2-options.md`.
|
|
9
|
+
*/
|
|
10
|
+
export declare const Bitrix: (restURI: string, accessToken?: string, clientOptions?: ExtendOptions) => {
|
|
3
11
|
call: import("./client/methods/call").Call;
|
|
4
12
|
batch: import("./client/methods/batch").Batch;
|
|
5
13
|
list: import("./client/methods/list").List;
|
|
@@ -93,7 +101,7 @@ declare const _default: (restURI: string, accessToken?: string | undefined, clie
|
|
|
93
101
|
get: (id?: string | undefined) => Promise<import("./payloads").GetPayload<readonly import("./services/users/entities").User[]>>;
|
|
94
102
|
};
|
|
95
103
|
};
|
|
96
|
-
export default
|
|
104
|
+
export default Bitrix;
|
|
97
105
|
export * from './methods';
|
|
98
106
|
export * from './commands';
|
|
99
107
|
export * from './services/common';
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
describe('Bitrix API', () => {
|
|
3
|
+
describe('`get` method', () => {
|
|
4
|
+
it.todo('should get result');
|
|
5
|
+
it.todo('should reject on error');
|
|
6
|
+
});
|
|
7
|
+
describe('`batch` method', () => {
|
|
8
|
+
it.todo('should execute the batch');
|
|
9
|
+
it.todo('should reject on errors');
|
|
10
|
+
});
|
|
11
|
+
describe('`deals`', () => {
|
|
12
|
+
describe('`get` method', () => {
|
|
13
|
+
it.todo('should get entry');
|
|
14
|
+
it.todo('should reject on error');
|
|
15
|
+
});
|
|
16
|
+
describe('`list` method', () => {
|
|
17
|
+
it.todo('should all entries');
|
|
18
|
+
it.todo('should reject on error');
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
describe('`leads`', () => {
|
|
22
|
+
describe('`get` method', () => {
|
|
23
|
+
it.todo('should get entry');
|
|
24
|
+
it.todo('should reject on error');
|
|
25
|
+
});
|
|
26
|
+
describe('`list` method', () => {
|
|
27
|
+
it.todo('should get all entries');
|
|
28
|
+
it.todo('should reject on error');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
});
|
package/build/module/bitrix.js
CHANGED
|
@@ -5,7 +5,15 @@ import DealsService from './services/deals';
|
|
|
5
5
|
import LeadsService from './services/leads';
|
|
6
6
|
import StatusesService from './services/statuses';
|
|
7
7
|
import UsersService from './services/users';
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Construct a Bitrix client with generic methods
|
|
10
|
+
* @param restURI REST endpoint, like a `https://hello.bitrix24.ua/rest` or an inbound webhook endpoint,
|
|
11
|
+
* like a `https://hello.bitrix24.ua/rest/1/WEBHOOK_TOKEN`.
|
|
12
|
+
* @param accessToken Bitrix application Access Token. Do not specify in case inbound webhook endpoint used.
|
|
13
|
+
* @param clientOptions an object that will overwrite underlying configuration for HTTP client,
|
|
14
|
+
* see `https://github.com/sindresorhus/got/blob/main/documentation/2-options.md`.
|
|
15
|
+
*/
|
|
16
|
+
export const Bitrix = (restURI, accessToken, clientOptions) => {
|
|
9
17
|
const { call, batch, list } = Client(restURI, accessToken, clientOptions);
|
|
10
18
|
return {
|
|
11
19
|
call,
|
|
@@ -19,6 +27,7 @@ export default (restURI, accessToken, clientOptions) => {
|
|
|
19
27
|
users: UsersService({ call })
|
|
20
28
|
};
|
|
21
29
|
};
|
|
30
|
+
export default Bitrix;
|
|
22
31
|
export * from './methods';
|
|
23
32
|
export * from './commands';
|
|
24
33
|
export * from './services/common';
|
|
@@ -29,4 +38,3 @@ export * from './services/deals/entities';
|
|
|
29
38
|
export * from './services/leads/entities';
|
|
30
39
|
export * from './services/statuses/entities';
|
|
31
40
|
export * from './services/users/entities';
|
|
32
|
-
//# sourceMappingURL=bitrix.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Bitrix from './bitrix';
|
|
2
|
+
import * as client from './client';
|
|
3
|
+
describe('Bitrix', () => {
|
|
4
|
+
it('should init', () => {
|
|
5
|
+
expect(() => Bitrix('https://test.com', 'test_token')).not.toThrow();
|
|
6
|
+
});
|
|
7
|
+
it('should pass endpoint and access token to the client', () => {
|
|
8
|
+
const SpiedClient = jest.spyOn(client, 'default');
|
|
9
|
+
Bitrix('https://test.com', 'test_token');
|
|
10
|
+
expect(SpiedClient.mock.calls).toMatchSnapshot();
|
|
11
|
+
});
|
|
12
|
+
it('should expose API methods', () => {
|
|
13
|
+
const bitrix = Bitrix('https://test.com', 'test_token');
|
|
14
|
+
expect(bitrix).toMatchSnapshot();
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import got from 'got';
|
|
2
|
+
import nock from 'nock';
|
|
3
|
+
import Queue from 'p-queue';
|
|
4
|
+
import { Method } from '../methods';
|
|
5
|
+
import Client from './index';
|
|
6
|
+
const TEST_URI = 'https://test.com/rest';
|
|
7
|
+
const TEST_ACCESS_TOKEN = 'test_access_token';
|
|
8
|
+
const RESPONSE_200 = 200;
|
|
9
|
+
const spiedQueueAdd = jest.spyOn(Queue.prototype, 'add');
|
|
10
|
+
const client = Client(TEST_URI, TEST_ACCESS_TOKEN);
|
|
11
|
+
describe('Client', () => {
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
spiedQueueAdd.mockClear();
|
|
14
|
+
});
|
|
15
|
+
it('should init', () => {
|
|
16
|
+
expect(() => Client(TEST_URI, TEST_ACCESS_TOKEN)).not.toThrow();
|
|
17
|
+
});
|
|
18
|
+
it('should extend `Got` instance with a specified parameters', () => {
|
|
19
|
+
const spiedGotExtend = jest.spyOn(got, 'extend');
|
|
20
|
+
Client('https://test.com', 'test_token', { https: { rejectUnauthorized: false } });
|
|
21
|
+
expect(spiedGotExtend.mock.calls).toMatchSnapshot();
|
|
22
|
+
});
|
|
23
|
+
it('should expose API methods', () => {
|
|
24
|
+
expect(client).toMatchSnapshot();
|
|
25
|
+
});
|
|
26
|
+
describe('`call`', () => {
|
|
27
|
+
// @todo restore test when issue is resolved (https://github.com/nock/nock/issues/1832)
|
|
28
|
+
it.skip('should add to queue', async () => {
|
|
29
|
+
const payload = {};
|
|
30
|
+
const method = Method.CRM_DEAL_LIST;
|
|
31
|
+
nock(TEST_URI)
|
|
32
|
+
.get(`/${method}?access_token=${TEST_ACCESS_TOKEN}`)
|
|
33
|
+
.twice()
|
|
34
|
+
.reply(RESPONSE_200, payload);
|
|
35
|
+
await client.call(method, {});
|
|
36
|
+
await client.call(method, {});
|
|
37
|
+
expect(spiedQueueAdd.mock.calls).toMatchSnapshot();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
describe('`batch`', () => {
|
|
41
|
+
// @todo restore test when issue is resolved (https://github.com/nock/nock/issues/1832)
|
|
42
|
+
it.skip('should add to queue', async () => {
|
|
43
|
+
const payload = {
|
|
44
|
+
result: {
|
|
45
|
+
result: ['done'],
|
|
46
|
+
result_error: []
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const commands = [
|
|
50
|
+
{ method: Method.CRM_DEAL_GET },
|
|
51
|
+
{ method: Method.CRM_DEAL_LIST }
|
|
52
|
+
];
|
|
53
|
+
nock(TEST_URI)
|
|
54
|
+
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
55
|
+
// fails to match request when it contains `cmd[someName]`. The issue definitely connected
|
|
56
|
+
// to the `[]`, since it does not appear when only one bracket present
|
|
57
|
+
.get(`/${Method.BATCH}?cmd%5B0%5D=${commands[0].method}&access_token=${TEST_ACCESS_TOKEN}`)
|
|
58
|
+
.reply(RESPONSE_200, payload)
|
|
59
|
+
.get(`/${Method.BATCH}?cmd%5B0%5D=${commands[1].method}&access_token=${TEST_ACCESS_TOKEN}`)
|
|
60
|
+
.reply(RESPONSE_200, payload);
|
|
61
|
+
await client.batch(commands, 1);
|
|
62
|
+
expect(spiedQueueAdd.mock.calls).toMatchSnapshot();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import { BeforeRequestHook } from 'got';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Got can't merge `query` option with other queries if they are string. But that hook can.
|
|
4
|
+
*/
|
|
5
|
+
declare const _default: (accessToken?: string) => BeforeRequestHook;
|
|
3
6
|
export default _default;
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Got can't merge `query` option with other queries if they are string. But that hook can.
|
|
3
|
+
*/
|
|
1
4
|
export default (accessToken) => (options) => {
|
|
2
5
|
if (!accessToken)
|
|
3
6
|
return;
|
|
@@ -6,4 +9,3 @@ export default (accessToken) => (options) => {
|
|
|
6
9
|
const hasQuery = options.path.includes('?');
|
|
7
10
|
options.path = `${options.path}${hasQuery ? '&' : '?'}access_token=${accessToken}`;
|
|
8
11
|
};
|
|
9
|
-
//# sourceMappingURL=addAccessToken.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import got from 'got';
|
|
2
|
+
import addAccessToken from './addAccessToken';
|
|
3
|
+
const token = 'mock_token';
|
|
4
|
+
const merge = got.mergeOptions;
|
|
5
|
+
describe('Client `addAccessToken` hook', () => {
|
|
6
|
+
it('should add access token to URL without a query', () => {
|
|
7
|
+
const options = merge({ path: 'test', responseType: 'json' });
|
|
8
|
+
addAccessToken(token)(options);
|
|
9
|
+
expect(options.path).toBe(`test?access_token=${token}`);
|
|
10
|
+
});
|
|
11
|
+
it('should add access token to URL with a query', () => {
|
|
12
|
+
const options = merge({ path: 'test?value=1', responseType: 'json' });
|
|
13
|
+
addAccessToken(token)(options);
|
|
14
|
+
expect(options.path).toBe(`test?value=1&access_token=${token}`);
|
|
15
|
+
});
|
|
16
|
+
it('should not add access token when it is undefined', () => {
|
|
17
|
+
const options = merge({ path: 'test', responseType: 'json' });
|
|
18
|
+
addAccessToken()(options);
|
|
19
|
+
expect(options.path).toBe('test');
|
|
20
|
+
});
|
|
21
|
+
it('should not add access token when URL is undefined', () => {
|
|
22
|
+
const options = merge({ path: undefined, responseType: 'json' });
|
|
23
|
+
addAccessToken(token)(options);
|
|
24
|
+
expect(options.path).toBe(undefined);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { ExtendOptions } from 'got';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Construct a Bitrix client with generic methods
|
|
4
|
+
* @param restURI REST endpoint, like a `https://hello.bitrix24.ru/rest` or an inbound webhook endpoint,
|
|
5
|
+
* like a `https://hello.bitrix24.ru/rest/1/WEBHOOK_TOKEN`.
|
|
6
|
+
* @param accessToken Bitrix application Access Token. Do not specify in case inbound webhook endpoint used.
|
|
7
|
+
* @param options an object that will overwrite underlying configuration for HTTP client,
|
|
8
|
+
* see `https://github.com/sindresorhus/got/blob/main/documentation/2-options.md`.
|
|
9
|
+
*/
|
|
10
|
+
declare const _default: (restURI: string, accessToken?: string, options?: ExtendOptions) => {
|
|
3
11
|
call: import("./methods/call").Call;
|
|
4
12
|
batch: import("./methods/batch").Batch;
|
|
5
13
|
list: import("./methods/list").List;
|
|
@@ -5,7 +5,15 @@ import Batch from './methods/batch';
|
|
|
5
5
|
import Call from './methods/call';
|
|
6
6
|
import List from './methods/list';
|
|
7
7
|
const BITRIX_API_RATE_LIMIT = 2;
|
|
8
|
-
const BITRIX_API_RATE_INTERVAL = 1000;
|
|
8
|
+
const BITRIX_API_RATE_INTERVAL = 1000; // 1 second
|
|
9
|
+
/**
|
|
10
|
+
* Construct a Bitrix client with generic methods
|
|
11
|
+
* @param restURI REST endpoint, like a `https://hello.bitrix24.ru/rest` or an inbound webhook endpoint,
|
|
12
|
+
* like a `https://hello.bitrix24.ru/rest/1/WEBHOOK_TOKEN`.
|
|
13
|
+
* @param accessToken Bitrix application Access Token. Do not specify in case inbound webhook endpoint used.
|
|
14
|
+
* @param options an object that will overwrite underlying configuration for HTTP client,
|
|
15
|
+
* see `https://github.com/sindresorhus/got/blob/main/documentation/2-options.md`.
|
|
16
|
+
*/
|
|
9
17
|
export default (restURI, accessToken, options) => {
|
|
10
18
|
const client = got.extend({
|
|
11
19
|
prefixUrl: restURI,
|
|
@@ -18,14 +26,18 @@ export default (restURI, accessToken, options) => {
|
|
|
18
26
|
addAccessToken(accessToken)
|
|
19
27
|
]
|
|
20
28
|
},
|
|
21
|
-
...options
|
|
29
|
+
...options ?? {}
|
|
22
30
|
});
|
|
23
31
|
const queue = new Queue({
|
|
24
32
|
intervalCap: BITRIX_API_RATE_LIMIT,
|
|
25
33
|
interval: BITRIX_API_RATE_INTERVAL
|
|
26
34
|
});
|
|
27
35
|
const queuedGet = (...args) => queue.add(() => client.get(...args));
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
37
|
+
// @ts-expect-error @todo remove after issue is resolved (https://github.com/sindresorhus/got/issues/954)
|
|
28
38
|
const call = Call({ get: queuedGet });
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
40
|
+
// @ts-expect-error @todo remove after issue is resolved (https://github.com/sindresorhus/got/issues/954)
|
|
29
41
|
const batch = Batch({ get: queuedGet });
|
|
30
42
|
const list = List({ call, batch });
|
|
31
43
|
return {
|
|
@@ -34,4 +46,3 @@ export default (restURI, accessToken, options) => {
|
|
|
34
46
|
list
|
|
35
47
|
};
|
|
36
48
|
};
|
|
37
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -3,8 +3,20 @@ import { Commands } from '../../commands';
|
|
|
3
3
|
import { Method, MethodPayloadType } from '../../methods';
|
|
4
4
|
import { BatchPayload } from '../../payloads';
|
|
5
5
|
export declare const MAX_COMMANDS_PER_BATCH = 50;
|
|
6
|
+
/**
|
|
7
|
+
* Split any number of commands into the chunks with max commands per batch in each chunk
|
|
8
|
+
*/
|
|
6
9
|
export declare const chunkCommands: <C extends Commands>(commands: C, size?: number) => readonly C[];
|
|
10
|
+
/**
|
|
11
|
+
* Prepares list of commands to be used in requests to the Bitrix
|
|
12
|
+
* @note We could avoid that function... if only Bitrix API would support posting of the batch commands as
|
|
13
|
+
* plain objects, like it does with other methods. Instead, it should be a dict or array of string queries
|
|
14
|
+
*/
|
|
7
15
|
export declare const prepareCommandsQueries: <C extends Commands, R = { [K in keyof C]: string; }>(commands: C) => R;
|
|
16
|
+
/**
|
|
17
|
+
* Merges list of batch responses into a single batch
|
|
18
|
+
* @todo Generics inference is complicated here and might be not super accurate
|
|
19
|
+
*/
|
|
8
20
|
export declare const mergeBatchPayloads: <B extends BatchPayload<unknown>, P = B extends BatchPayload<infer U> ? U : never>(payloads: readonly B[]) => BatchPayload<P>;
|
|
9
21
|
export declare type CommandsPayloads<C extends Commands, CM = {
|
|
10
22
|
[K in keyof C]: C[K] extends {
|
|
@@ -17,5 +29,12 @@ export declare type Batch = <C extends Commands, P = CommandsPayloads<C>>(comman
|
|
|
17
29
|
declare type Dependencies = {
|
|
18
30
|
readonly get: typeof got.get;
|
|
19
31
|
};
|
|
32
|
+
/**
|
|
33
|
+
* Dispatches a batch request with specified commands. Will fill figure out payload type based on the Methods.
|
|
34
|
+
* Supports unlimited number of commands. If they do exceed max amount of commands per batch, will dispatch
|
|
35
|
+
* multiple request and merge result into single batch payload
|
|
36
|
+
* @todo For now due to issues with types it won't check `params` of command, so any Method can be used
|
|
37
|
+
* with any (even wrong) params
|
|
38
|
+
*/
|
|
20
39
|
declare const _default: ({ get }: Dependencies) => Batch;
|
|
21
40
|
export default _default;
|
|
@@ -5,22 +5,40 @@ import { Method } from '../../methods';
|
|
|
5
5
|
import isArray from '../../utils/isArray';
|
|
6
6
|
import { handlePayload } from './call';
|
|
7
7
|
export const MAX_COMMANDS_PER_BATCH = 50;
|
|
8
|
+
/**
|
|
9
|
+
* Split any number of commands into the chunks with max commands per batch in each chunk
|
|
10
|
+
*/
|
|
8
11
|
export const chunkCommands = (commands, size = MAX_COMMANDS_PER_BATCH) => {
|
|
9
12
|
const commandsIsArray = isArray(commands);
|
|
10
13
|
const chunks = chunk(Object.entries(commands), size);
|
|
14
|
+
// @todo Generated return type isn't very accurate since it says that it will make array of <C>, while
|
|
15
|
+
// in fact <C> will be distributed among chunks. That gives wrong hints with named commands and arrays as const.
|
|
11
16
|
return chunks.map((c) => commandsIsArray
|
|
17
|
+
// @todo Figure out how to avoid unsafe casting to <C> here
|
|
18
|
+
// Without `<C>` generic it won't work with complex functions like `batch`
|
|
12
19
|
? c.map(([_key, command]) => command)
|
|
13
20
|
: fromPairs(c));
|
|
14
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* Prepares list of commands to be used in requests to the Bitrix
|
|
24
|
+
* @note We could avoid that function... if only Bitrix API would support posting of the batch commands as
|
|
25
|
+
* plain objects, like it does with other methods. Instead, it should be a dict or array of string queries
|
|
26
|
+
*/
|
|
15
27
|
export const prepareCommandsQueries = (commands) => Object.entries(commands).reduce((calls, [name, { method, params }]) => {
|
|
16
28
|
const stringifiedParams = params ? `?${toQuery(params)}` : '';
|
|
17
29
|
return {
|
|
18
30
|
...calls,
|
|
19
31
|
[`cmd[${name}]`]: `${method}${stringifiedParams}`
|
|
20
32
|
};
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
|
|
21
34
|
}, {});
|
|
35
|
+
/**
|
|
36
|
+
* Merges list of batch responses into a single batch
|
|
37
|
+
* @todo Generics inference is complicated here and might be not super accurate
|
|
38
|
+
*/
|
|
22
39
|
export const mergeBatchPayloads = (payloads) => {
|
|
23
40
|
const merge = (a, b) => isArray(a) && isArray(b)
|
|
41
|
+
// @todo Gotta find way to avoid unsafe casting here
|
|
24
42
|
? [...a, ...b]
|
|
25
43
|
: ({ ...a, ...b });
|
|
26
44
|
return payloads.reduce((merged, payload) => ({
|
|
@@ -32,8 +50,16 @@ export const mergeBatchPayloads = (payloads) => {
|
|
|
32
50
|
result_total: merge(merged.result.result_total || [], payload.result.result_total)
|
|
33
51
|
},
|
|
34
52
|
time: { ...merged.time, ...payload.time }
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
|
|
35
54
|
}), { result: {}, time: {} });
|
|
36
55
|
};
|
|
56
|
+
/**
|
|
57
|
+
* Dispatches a batch request with specified commands. Will fill figure out payload type based on the Methods.
|
|
58
|
+
* Supports unlimited number of commands. If they do exceed max amount of commands per batch, will dispatch
|
|
59
|
+
* multiple request and merge result into single batch payload
|
|
60
|
+
* @todo For now due to issues with types it won't check `params` of command, so any Method can be used
|
|
61
|
+
* with any (even wrong) params
|
|
62
|
+
*/
|
|
37
63
|
export default ({ get }) => {
|
|
38
64
|
const batch = async (commands, commandsPerRequest = MAX_COMMANDS_PER_BATCH) => {
|
|
39
65
|
const call = (c) => get(Method.BATCH, { searchParams: prepareCommandsQueries(c) })
|
|
@@ -48,4 +74,3 @@ export default ({ get }) => {
|
|
|
48
74
|
};
|
|
49
75
|
return batch;
|
|
50
76
|
};
|
|
51
|
-
//# sourceMappingURL=batch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|