2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-03-04 09:05:54 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-10-21 09:45:54 -07:00
|
|
|
import { IDataObject, JsonObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2020-03-04 09:05:54 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { get } from 'lodash';
|
2020-03-04 09:05:54 -08:00
|
|
|
|
2022-10-21 09:45:54 -07:00
|
|
|
export const eventID: { [key: string]: string } = {
|
|
|
|
create_client: '1',
|
|
|
|
create_invoice: '2',
|
|
|
|
create_quote: '3',
|
|
|
|
create_payment: '4',
|
|
|
|
create_vendor: '5',
|
|
|
|
};
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function invoiceNinjaApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-10-21 09:45:54 -07:00
|
|
|
body: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
query?: IDataObject,
|
|
|
|
uri?: string,
|
2022-10-21 09:45:54 -07:00
|
|
|
) {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('invoiceNinjaApi');
|
2020-03-16 15:26:27 -07:00
|
|
|
|
2022-10-21 09:45:54 -07:00
|
|
|
if (credentials === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const version = this.getNodeParameter('apiVersion', 0) as string;
|
|
|
|
|
|
|
|
const defaultUrl = version === 'v4' ? 'https://app.invoiceninja.com' : 'https://invoicing.co';
|
2022-12-02 12:54:28 -08:00
|
|
|
const baseUrl = credentials.url || defaultUrl;
|
2022-10-21 09:45:54 -07:00
|
|
|
|
2020-03-04 09:05:54 -08:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
qs: query,
|
2023-01-13 09:11:56 -08:00
|
|
|
uri: uri ?? `${baseUrl}/api/v1${endpoint}`,
|
2020-03-04 09:05:54 -08:00
|
|
|
body,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-03-04 09:05:54 -08:00
|
|
|
};
|
2022-10-21 09:45:54 -07:00
|
|
|
|
2020-03-04 09:05:54 -08:00
|
|
|
try {
|
2022-10-21 09:45:54 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'invoiceNinjaApi', options);
|
2020-03-04 09:05:54 -08:00
|
|
|
} catch (error) {
|
2022-10-21 09:45:54 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-03-04 09:05:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function invoiceNinjaApiRequestAllItems(
|
2022-10-21 09:45:54 -07:00
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-10-21 09:45:54 -07:00
|
|
|
body: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
query: IDataObject = {},
|
2022-10-21 09:45:54 -07:00
|
|
|
) {
|
2020-03-04 09:05:54 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
let uri;
|
|
|
|
query.per_page = 100;
|
|
|
|
|
|
|
|
do {
|
2020-03-16 15:26:27 -07:00
|
|
|
responseData = await invoiceNinjaApiRequest.call(this, method, endpoint, body, query, uri);
|
|
|
|
const next = get(responseData, 'meta.pagination.links.next') as string | undefined;
|
2020-03-04 09:05:54 -08:00
|
|
|
if (next) {
|
|
|
|
uri = next;
|
|
|
|
}
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
2022-12-02 12:54:28 -08:00
|
|
|
} while (responseData.meta?.pagination?.links?.next);
|
2020-03-04 09:05:54 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|