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>
104 lines
2.3 KiB
TypeScript
104 lines
2.3 KiB
TypeScript
import {
|
|
Credentials,
|
|
IExecuteFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
NodeApiError,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
LoaderGetResponse,
|
|
} from './types';
|
|
|
|
export async function monicaCrmApiRequest(
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
body: IDataObject = {},
|
|
qs: IDataObject = {},
|
|
) {
|
|
const credentials = await this.getCredentials('monicaCrmApi') as { apiToken: string, environment: string, domain: string };
|
|
|
|
if (credentials === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
|
}
|
|
|
|
let baseUrl = `https://app.monicahq.com`;
|
|
|
|
if (credentials.environment === 'selfHosted') {
|
|
baseUrl = credentials.domain;
|
|
}
|
|
|
|
const options: OptionsWithUri = {
|
|
headers: {
|
|
Authorization: `Bearer ${credentials.apiToken}`,
|
|
},
|
|
method,
|
|
body,
|
|
qs,
|
|
uri: `${baseUrl}/api${endpoint}`,
|
|
json: true,
|
|
};
|
|
|
|
if (!Object.keys(body).length) {
|
|
delete options.body;
|
|
}
|
|
|
|
if (!Object.keys(qs).length) {
|
|
delete options.qs;
|
|
}
|
|
|
|
try {
|
|
return await this.helpers.request!(options);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
export async function monicaCrmApiRequestAllItems(
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
body: IDataObject = {},
|
|
qs: IDataObject = {},
|
|
{ forLoader }: { forLoader: boolean } = { forLoader: false },
|
|
) {
|
|
const returnAll = this.getNodeParameter('returnAll', 0, false) as boolean;
|
|
const limit = this.getNodeParameter('limit', 0, 0) as number;
|
|
|
|
let totalItems = 0;
|
|
|
|
let responseData;
|
|
const returnData: IDataObject[] = [];
|
|
|
|
do {
|
|
responseData = await monicaCrmApiRequest.call(this, method, endpoint, body, qs);
|
|
returnData.push(...responseData.data);
|
|
|
|
if (!forLoader && !returnAll && returnData.length > limit) {
|
|
return returnData.slice(0, limit);
|
|
}
|
|
|
|
totalItems = responseData.meta.total;
|
|
} while (totalItems > returnData.length);
|
|
|
|
return returnData;
|
|
}
|
|
|
|
/**
|
|
* Get day, month, and year from the n8n UI datepicker.
|
|
*/
|
|
export const getDateParts = (date: string) =>
|
|
date.split('T')[0].split('-').map(Number).reverse();
|
|
|
|
export const toOptions = (response: LoaderGetResponse) =>
|
|
response.data.map(({ id, name }) => ({ value: id, name }));
|