n8n/packages/nodes-base/nodes/Asana/GenericFunctions.ts
Ricardo Espinoza dc583bd81b
Feature/asana improvements (#920)
*  Improvements to Asana-Node

* Minor change to comment strings

helpful when filtering the code based on indentation

* Minor rephrasing of description in search action

* Add loadOption to get all projects

* Add loadOption to get all sections in a project

* Add UI fields to move task to a specific section

* Add execution for moveToSection operation

* Add loadOptions helper to get all teams

* Add UI fields to get projects

* Add execution methods for projects getter

* Add loadOptions helper to get all users

* Add loadOptions helper to get all tags

* Add UI fields for adding a tag to a task

* Add execution method to add a tag to a task

* Add functionality to remove Tag from Task

* Add option to set 'Assignee' and 'Assignee Status' on a task

to unset an assignee 'null' has to be send. Unfortunately this gives a warning in the UI.

* a few fixes

* Only show existing task tags when removing a tag

* few more fixes

*  Improvements to #855

Co-authored-by: Silvio <silvio@sintuity.com>
2020-09-08 14:00:54 +02:00

85 lines
2.2 KiB
TypeScript

import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
OptionsWithUri,
} from 'request';
import {
IDataObject,
} from 'n8n-workflow';
import {
get,
} from 'lodash';
/**
* Make an API request to Asana
*
* @param {IHookFunctions} this
* @param {string} method
* @param {string} url
* @param {object} body
* @returns {Promise<any>}
*/
export async function asanaApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query?: object, uri?: string | undefined): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('asanaApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const options: OptionsWithUri = {
headers: {
Authorization: `Bearer ${credentials.accessToken}`,
},
method,
body: { data: body },
qs: query,
uri: uri || `https://app.asana.com/api/1.0${endpoint}`,
json: true,
};
try {
return await this.helpers.request!(options);
} catch (error) {
if (error.statusCode === 401) {
// Return a clear error
throw new Error('The Asana credentials are not valid!');
}
if (error.response && error.response.body && error.response.body.errors) {
// Try to return the error prettier
const errorMessages = error.response.body.errors.map((errorData: { message: string }) => {
return errorData.message;
});
throw new Error(`Asana error response [${error.statusCode}]: ${errorMessages.join(' | ')}`);
}
// If that data does not exist for some reason return the actual error
throw error;
}
}
export async function asanaApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions ,method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
let uri: string | undefined;
query.limit = 100;
do {
responseData = await asanaApiRequest.call(this, method, endpoint, body, query, uri);
uri = get(responseData, 'next_page.uri');
returnData.push.apply(returnData, responseData['data']);
} while (
responseData['next_page'] !== null
);
return returnData;
}