2020-03-27 12:17:10 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
2020-03-26 12:39:31 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
IHookFunctions,
|
|
|
|
IWebhookFunctions
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2020-03-27 12:17:10 -07:00
|
|
|
import {
|
|
|
|
get,
|
|
|
|
} from 'lodash';
|
|
|
|
|
2020-03-27 16:34:39 -07:00
|
|
|
export async function mondayComApiRequest(this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions, body: any = {}, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2020-03-26 12:39:31 -07:00
|
|
|
|
2020-03-27 16:34:39 -07:00
|
|
|
const credentials = this.getCredentials('mondayComApi');
|
2020-03-26 12:39:31 -07:00
|
|
|
|
|
|
|
if (credentials === undefined) {
|
|
|
|
throw new Error('No credentials got returned!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpoint = 'https://api.monday.com/v2/';
|
|
|
|
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': credentials.apiToken,
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body,
|
|
|
|
uri: endpoint,
|
|
|
|
json: true
|
|
|
|
};
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.response) {
|
|
|
|
const errorMessage = error.response.body.error_message;
|
|
|
|
throw new Error(`Monday error response [${error.statusCode}]: ${errorMessage}`);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:34:39 -07:00
|
|
|
export async function mondayComApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, body: any = {}): Promise<any> { // tslint:disable-line:no-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);
|
2020-03-27 12:17:10 -07:00
|
|
|
returnData.push.apply(returnData, get(responseData, propertyName));
|
2020-03-26 12:39:31 -07:00
|
|
|
body.variables.page++;
|
|
|
|
} while (
|
2020-03-27 12:17:10 -07:00
|
|
|
get(responseData, propertyName).length > 0
|
2020-03-26 12:39:31 -07:00
|
|
|
);
|
|
|
|
return returnData;
|
|
|
|
}
|