2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IDataObject,
|
|
|
|
IHookFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
JsonObject,
|
2024-02-14 07:29:09 -08:00
|
|
|
IRequestOptions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2020-03-26 12:39:31 -07:00
|
|
|
|
2023-06-16 07:26:35 -07:00
|
|
|
import get from 'lodash/get';
|
2021-04-16 09:33:36 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function mondayComApiRequest(
|
|
|
|
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
|
|
|
|
body: any = {},
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2021-04-15 15:35:39 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
|
2020-03-26 12:39:31 -07:00
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
let options: IRequestOptions = {
|
2020-03-26 12:39:31 -07:00
|
|
|
headers: {
|
2024-01-10 01:17:00 -08:00
|
|
|
'API-Version': '2023-10',
|
2020-03-26 12:39:31 -07:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body,
|
2024-01-10 01:17:00 -08:00
|
|
|
uri: 'https://api.monday.com/v2/',
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-03-26 12:39:31 -07:00
|
|
|
};
|
2024-01-10 01:17:00 -08:00
|
|
|
|
2020-03-26 12:39:31 -07:00
|
|
|
options = Object.assign({}, options, option);
|
2021-04-15 15:35:39 -07:00
|
|
|
|
2024-01-10 01:17:00 -08:00
|
|
|
try {
|
|
|
|
let credentialType = 'mondayComApi';
|
2021-04-15 15:35:39 -07:00
|
|
|
|
2024-01-10 01:17:00 -08:00
|
|
|
if (authenticationMethod === 'oAuth2') {
|
|
|
|
credentialType = 'mondayComOAuth2Api';
|
2021-04-15 15:35:39 -07:00
|
|
|
}
|
2024-01-10 01:17:00 -08:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
2020-03-26 12:39:31 -07:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-03-26 12:39:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function mondayComApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
body: any = {},
|
|
|
|
): Promise<any> {
|
2020-03-26 12:39:31 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
body.variables.limit = 50;
|
|
|
|
body.variables.page = 1;
|
|
|
|
|
|
|
|
do {
|
2020-03-27 16:34:39 -07:00
|
|
|
responseData = await mondayComApiRequest.call(this, body);
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, get(responseData, propertyName) as IDataObject[]);
|
2020-03-26 12:39:31 -07:00
|
|
|
body.variables.page++;
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (get(responseData, propertyName).length > 0);
|
2020-03-26 12:39:31 -07:00
|
|
|
return returnData;
|
|
|
|
}
|
2024-01-10 01:17:00 -08:00
|
|
|
|
|
|
|
export async function mondayComApiPaginatedRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
itemsPath: string,
|
|
|
|
fieldsToReturn: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
) {
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
const initialResponse = await mondayComApiRequest.call(this, body);
|
|
|
|
const data = get(initialResponse, itemsPath) as IDataObject;
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
returnData.push.apply(returnData, data.items as IDataObject[]);
|
|
|
|
|
|
|
|
let cursor: null | string = data.cursor as string;
|
|
|
|
|
|
|
|
while (cursor) {
|
|
|
|
const responseData = (
|
|
|
|
(await mondayComApiRequest.call(this, {
|
|
|
|
query: `query ( $cursor: String!) { next_items_page (cursor: $cursor, limit: 100) { cursor items ${fieldsToReturn} } }`,
|
|
|
|
variables: {
|
|
|
|
cursor,
|
|
|
|
},
|
|
|
|
})) as IDataObject
|
|
|
|
).data as { next_items_page: { cursor: string; items: IDataObject[] } };
|
|
|
|
|
|
|
|
if (responseData && responseData.next_items_page) {
|
|
|
|
returnData.push.apply(returnData, responseData.next_items_page.items);
|
|
|
|
cursor = responseData.next_items_page.cursor;
|
|
|
|
} else {
|
|
|
|
cursor = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|