mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 14:44:05 -08:00
7ce7285f7a
* Changes to types so that credentials can be always loaded from DB This first commit changes all return types from the execute functions and calls to get credentials to be async so we can use await. This is a first step as previously credentials were loaded in memory and always available. We will now be loading them from the DB which requires turning the whole call chain async. * Fix updated files * Removed unnecessary credential loading to improve performance * Fix typo * ⚡ Fix issue * Updated new nodes to load credentials async * ⚡ Remove not needed comment Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IExecuteFunctions,
|
|
ILoadOptionsFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
IHookFunctions,
|
|
IWebhookFunctions,
|
|
NodeApiError,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
get,
|
|
} from 'lodash';
|
|
|
|
export async function mondayComApiRequest(this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions, body: any = {}, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
|
|
|
|
const endpoint = 'https://api.monday.com/v2/';
|
|
|
|
let options: OptionsWithUri = {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
method: 'POST',
|
|
body,
|
|
uri: endpoint,
|
|
json: true,
|
|
};
|
|
options = Object.assign({}, options, option);
|
|
try {
|
|
if (authenticationMethod === 'accessToken') {
|
|
const credentials = await this.getCredentials('mondayComApi') as IDataObject;
|
|
|
|
options.headers = { Authorization: `Bearer ${credentials.apiToken}` };
|
|
|
|
return await this.helpers.request!(options);
|
|
} else {
|
|
|
|
return await this.helpers.requestOAuth2!.call(this, 'mondayComOAuth2Api', options);
|
|
}
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
export async function mondayComApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, body: any = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
let responseData;
|
|
body.variables.limit = 50;
|
|
body.variables.page = 1;
|
|
|
|
do {
|
|
responseData = await mondayComApiRequest.call(this, body);
|
|
returnData.push.apply(returnData, get(responseData, propertyName));
|
|
body.variables.page++;
|
|
} while (
|
|
get(responseData, propertyName).length > 0
|
|
);
|
|
return returnData;
|
|
}
|