2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-03-04 09:05:54 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2020-03-04 09:05:54 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
JsonObject,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2020-03-04 09:05:54 -08:00
|
|
|
|
2023-06-16 07:26:35 -07:00
|
|
|
import get from 'lodash/get';
|
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(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
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-19 04:37:19 -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;
|
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2022-12-02 12:54:28 -08:00
|
|
|
} while (responseData.meta?.pagination?.links?.next);
|
2020-03-04 09:05:54 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|