2023-01-27 14:22:44 +03:00
|
|
|
import type {
|
2023-03-09 20:13:15 +03:00
|
|
|
IDataObject,
|
2019-11-17 01:16:11 +03:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2023-03-09 20:13:15 +03:00
|
|
|
JsonObject,
|
2024-02-14 18:29:09 +03:00
|
|
|
IRequestOptions,
|
|
|
|
IHttpRequestMethods,
|
2023-03-09 20:13:15 +03:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 14:22:44 +03:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2022-08-17 18:50:24 +03:00
|
|
|
|
|
|
|
export async function intercomApiRequest(
|
2023-08-16 16:52:41 +03:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
2022-08-17 18:50:24 +03:00
|
|
|
endpoint: string,
|
2024-02-14 18:29:09 +03:00
|
|
|
method: IHttpRequestMethods,
|
2022-12-02 17:25:21 +03:00
|
|
|
|
2022-08-17 18:50:24 +03:00
|
|
|
body: any = {},
|
|
|
|
query?: IDataObject,
|
|
|
|
uri?: string,
|
|
|
|
): Promise<any> {
|
2021-08-20 19:57:30 +03:00
|
|
|
const credentials = await this.getCredentials('intercomApi');
|
2019-11-17 01:16:11 +03:00
|
|
|
|
2022-08-17 18:50:24 +03:00
|
|
|
const headerWithAuthentication = Object.assign(
|
|
|
|
{},
|
|
|
|
{ Authorization: `Bearer ${credentials.apiKey}`, Accept: 'application/json' },
|
|
|
|
);
|
2019-11-17 01:16:11 +03:00
|
|
|
|
2024-02-14 18:29:09 +03:00
|
|
|
const options: IRequestOptions = {
|
2019-11-17 01:16:11 +03:00
|
|
|
headers: headerWithAuthentication,
|
|
|
|
method,
|
2019-11-23 01:04:06 +03:00
|
|
|
qs: query,
|
2023-01-19 15:37:19 +03:00
|
|
|
uri: uri || `https://api.intercom.io${endpoint}`,
|
2019-11-17 01:16:11 +03:00
|
|
|
body,
|
2020-10-22 16:46:03 +03:00
|
|
|
json: true,
|
2019-11-17 01:16:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2022-12-23 21:09:52 +03:00
|
|
|
return await this.helpers.request(options);
|
2019-11-17 01:16:11 +03:00
|
|
|
} catch (error) {
|
2023-02-28 06:39:43 +03:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2019-11-17 01:16:11 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-17 02:47:28 +03:00
|
|
|
|
2019-11-23 01:04:06 +03:00
|
|
|
/**
|
|
|
|
* Make an API request to paginated intercom endpoint
|
|
|
|
* and return all results
|
|
|
|
*/
|
2022-08-17 18:50:24 +03:00
|
|
|
export async function intercomApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
endpoint: string,
|
2024-02-14 18:29:09 +03:00
|
|
|
method: IHttpRequestMethods,
|
2022-12-02 17:25:21 +03:00
|
|
|
|
2022-08-17 18:50:24 +03:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2019-11-23 01:04:06 +03:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.per_page = 60;
|
|
|
|
|
|
|
|
let uri: string | undefined;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await intercomApiRequest.call(this, endpoint, method, body, query, uri);
|
|
|
|
uri = responseData.pages.next;
|
2023-02-28 06:39:43 +03:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2022-12-02 23:54:28 +03:00
|
|
|
} while (responseData.pages?.next !== null);
|
2019-11-23 01:04:06 +03:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:50:24 +03:00
|
|
|
export function validateJSON(json: string | undefined): any {
|
2019-11-17 02:47:28 +03:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = '';
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|