Skip to content

Commit

Permalink
fix(InvoiceNinja Node): added support for v5
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency authored Oct 21, 2022
1 parent 8f25da5 commit 2f4649c
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 59 deletions.
48 changes: 46 additions & 2 deletions packages/nodes-base/credentials/InvoiceNinjaApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ICredentialType, INodeProperties } from 'n8n-workflow';
import {
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
} from 'n8n-workflow';

export class InvoiceNinjaApi implements ICredentialType {
name = 'invoiceNinjaApi';
Expand All @@ -9,13 +15,51 @@ export class InvoiceNinjaApi implements ICredentialType {
displayName: 'URL',
name: 'url',
type: 'string',
default: 'https://app.invoiceninja.com',
default: '',
hint: 'Default URL for v4 is https://app.invoiceninja.com, for v5 it is https://invoicing.co',
},
{
displayName: 'API Token',
name: 'apiToken',
type: 'string',
default: '',
},
{
displayName: 'Secret',
name: 'secret',
type: 'string',
default: '',
hint: 'This is optional, enter only if you did set a secret in your app and only if you are using v5',
},
];
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials?.url}}',
url: '/api/v1/clients',
method: 'GET',
},
};
async authenticate(
credentials: ICredentialDataDecryptedObject,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const VERSION_5_TOKEN_LENGTH = 64;
const { apiToken, secret } = credentials;
const tokenLength = (apiToken as string).length;

if (tokenLength < VERSION_5_TOKEN_LENGTH) {
requestOptions.headers = {
Accept: 'application/json',
'X-Ninja-Token': apiToken,
};
} else {
requestOptions.headers = {
'Content-Type': 'application/json',
'X-API-TOKEN': apiToken,
'X-Requested-With': 'XMLHttpRequest',
'X-API-SECRET': secret || '',
};
}
return requestOptions;
}
}
43 changes: 26 additions & 17 deletions packages/nodes-base/nodes/InvoiceNinja/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,60 @@ import {
ILoadOptionsFunctions,
} from 'n8n-core';

import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
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,
// tslint:disable-next-line:no-any
body: any = {},
body: IDataObject = {},
query?: IDataObject,
uri?: string,
// tslint:disable-next-line:no-any
): Promise<any> {
) {
const credentials = await this.getCredentials('invoiceNinjaApi');

const baseUrl = credentials!.url || 'https://app.invoiceninja.com';
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 = {
headers: {
Accept: 'application/json',
'X-Ninja-Token': credentials.apiToken,
},
method,
qs: query,
uri: uri || `${baseUrl}/api/v1${endpoint}`,
body,
json: true,
};

try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'invoiceNinjaApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}

export async function invoiceNinjaApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions,
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
propertyName: string,
method: string,
endpoint: string,
// tslint:disable-next-line:no-any
body: any = {},
body: IDataObject = {},
query: IDataObject = {},
// tslint:disable-next-line:no-any
): Promise<any> {
) {
const returnData: IDataObject[] = [];

let responseData;
Expand Down
Loading

0 comments on commit 2f4649c

Please sign in to comment.