2021-04-02 05:31:44 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
2020-03-04 09:05:54 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError, NodeOperationError,
|
2020-03-04 09:05:54 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2021-04-02 05:31:44 -07:00
|
|
|
import {
|
|
|
|
get,
|
|
|
|
} from 'lodash';
|
2020-03-04 09:05:54 -08:00
|
|
|
|
2020-03-16 15:26:27 -07:00
|
|
|
export async function invoiceNinjaApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('invoiceNinjaApi');
|
2020-03-16 15:26:27 -07:00
|
|
|
|
|
|
|
const baseUrl = credentials!.url || 'https://app.invoiceninja.com';
|
2020-03-04 09:05:54 -08:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
2020-03-16 15:26:27 -07:00
|
|
|
'X-Ninja-Token': credentials.apiToken,
|
2020-03-04 09:05:54 -08:00
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs: query,
|
2020-03-16 15:26:27 -07: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
|
|
|
};
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-03-04 09:05:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 15:26:27 -07:00
|
|
|
export async function invoiceNinjaApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
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]);
|
|
|
|
} while (
|
|
|
|
responseData.meta !== undefined &&
|
|
|
|
responseData.meta.pagination &&
|
|
|
|
responseData.meta.pagination.links &&
|
|
|
|
responseData.meta.pagination.links.next
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|