n8n/packages/nodes-base/nodes/InvoiceNinja/GenericFunctions.ts
Michael Kret 61e26804ba
refactor(core): Remove linting exceptions in nodes-base (#4794)
*  enabled array-type

*  await-thenable on

*  ban-types on

*  default-param-last on

*  dot-notation on

*  member-delimiter-style on

*  no-duplicate-imports on

*  no-empty-interface on

*  no-floating-promises on

*  no-for-in-array on

*  no-invalid-void-type on

*  no-loop-func on

*  no-shadow on

*  ban-ts-comment re enabled

*  @typescript-eslint/lines-between-class-members on

* address my own comment

* @typescript-eslint/return-await on

* @typescript-eslint/promise-function-async on

* @typescript-eslint/no-unnecessary-boolean-literal-compare on

* @typescript-eslint/no-unnecessary-type-assertion on

* prefer-const on

* @typescript-eslint/prefer-optional-chain on

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
2022-12-02 21:54:28 +01:00

81 lines
2 KiB
TypeScript

import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import { IDataObject, JsonObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
import { get } from 'lodash';
export const eventID: { [key: string]: string } = {
create_client: '1',
create_invoice: '2',
create_quote: '3',
create_payment: '4',
create_vendor: '5',
};
export async function invoiceNinjaApiRequest(
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: IDataObject = {},
query?: IDataObject,
uri?: string,
) {
const credentials = await this.getCredentials('invoiceNinjaApi');
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';
const baseUrl = credentials.url || defaultUrl;
const options: OptionsWithUri = {
method,
qs: query,
uri: uri || `${baseUrl}/api/v1${endpoint}`,
body,
json: true,
};
try {
return await this.helpers.requestWithAuthentication.call(this, 'invoiceNinjaApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}
export async function invoiceNinjaApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
propertyName: string,
method: string,
endpoint: string,
body: IDataObject = {},
query: IDataObject = {},
) {
const returnData: IDataObject[] = [];
let responseData;
let uri;
query.per_page = 100;
do {
responseData = await invoiceNinjaApiRequest.call(this, method, endpoint, body, query, uri);
const next = get(responseData, 'meta.pagination.links.next') as string | undefined;
if (next) {
uri = next;
}
returnData.push.apply(returnData, responseData[propertyName]);
} while (responseData.meta?.pagination?.links?.next);
return returnData;
}