mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 21:19:43 -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>
114 lines
2.7 KiB
TypeScript
114 lines
2.7 KiB
TypeScript
import {
|
|
OptionsWithUrl,
|
|
} from 'request';
|
|
|
|
import {
|
|
IExecuteFunctions,
|
|
IExecuteSingleFunctions,
|
|
IHookFunctions,
|
|
ILoadOptionsFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject, NodeApiError,
|
|
} from 'n8n-workflow';
|
|
|
|
interface IContact {
|
|
tags: [];
|
|
base: IDataObject;
|
|
extra: IDataObject[];
|
|
}
|
|
|
|
const fieldCache: {
|
|
[key: string]: IDataObject[];
|
|
} = {};
|
|
|
|
|
|
export async function getFields(this: IExecuteFunctions, listId: string) {
|
|
if (fieldCache[listId]) {
|
|
return fieldCache[listId];
|
|
}
|
|
fieldCache[listId] = await egoiApiRequest.call(this, 'GET', `/lists/${listId}/fields`);
|
|
return fieldCache[listId];
|
|
}
|
|
|
|
|
|
export async function egoiApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, qs: IDataObject = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const credentials = await this.getCredentials('egoiApi') as IDataObject;
|
|
|
|
const options: OptionsWithUrl = {
|
|
headers: {
|
|
'accept': 'application/json',
|
|
'Apikey': `${credentials.apiKey}`,
|
|
},
|
|
method,
|
|
qs,
|
|
body,
|
|
url: `https://api.egoiapp.com${endpoint}`,
|
|
json: true,
|
|
};
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
delete options.body;
|
|
}
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
export async function egoiApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
let responseData;
|
|
|
|
query.offset = 0;
|
|
query.count = 500;
|
|
|
|
do {
|
|
responseData = await egoiApiRequest.call(this, method, endpoint, body, query);
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
query.offset += query.count;
|
|
} while (
|
|
responseData[propertyName] && responseData[propertyName].length !== 0
|
|
);
|
|
|
|
return returnData;
|
|
}
|
|
|
|
|
|
export async function simplify(this: IExecuteFunctions, contacts: IContact[], listId: string) {
|
|
let fields = await getFields.call(this, listId);
|
|
|
|
fields = fields.filter((element: IDataObject) => element.type === 'extra');
|
|
const fieldsKeyValue: IDataObject = {};
|
|
for (const field of fields) {
|
|
fieldsKeyValue[field.field_id as string] = field.name;
|
|
}
|
|
|
|
const data: IDataObject[] = [];
|
|
|
|
for (const contact of contacts) {
|
|
const extras = contact.extra.reduce(
|
|
(acumulator: IDataObject, currentValue: IDataObject): any => { // tslint:disable-line:no-any
|
|
const key = fieldsKeyValue[currentValue.field_id as string] as string;
|
|
return { [key]: currentValue.value, ...acumulator };
|
|
},
|
|
{},
|
|
);
|
|
data.push({
|
|
...contact.base,
|
|
...extras,
|
|
tags: contact.tags,
|
|
});
|
|
}
|
|
|
|
return data;
|
|
}
|