2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-03-26 12:39:31 -07:00
|
|
|
|
2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IDataObject,
|
|
|
|
IHookFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
JsonObject,
|
|
|
|
} 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-02-23 07:16:05 -08: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,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
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
|
|
|
|
|
|
|
const endpoint = 'https://api.monday.com/v2/';
|
|
|
|
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body,
|
|
|
|
uri: endpoint,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-03-26 12:39:31 -07:00
|
|
|
};
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
try {
|
2021-04-15 15:35:39 -07:00
|
|
|
if (authenticationMethod === 'accessToken') {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('mondayComApi');
|
2021-04-15 15:35:39 -07:00
|
|
|
|
|
|
|
options.headers = { Authorization: `Bearer ${credentials.apiToken}` };
|
|
|
|
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2021-04-15 15:35:39 -07:00
|
|
|
} else {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.requestOAuth2.call(this, 'mondayComOAuth2Api', options);
|
2021-04-15 15:35:39 -07:00
|
|
|
}
|
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,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
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;
|
|
|
|
}
|