2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-10-21 09:45:54 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
ICredentialTestRequest,
|
|
|
|
ICredentialType,
|
|
|
|
IHttpRequestOptions,
|
|
|
|
INodeProperties,
|
|
|
|
} from 'n8n-workflow';
|
2020-03-04 09:05:54 -08:00
|
|
|
|
2020-03-16 15:26:27 -07:00
|
|
|
export class InvoiceNinjaApi implements ICredentialType {
|
|
|
|
name = 'invoiceNinjaApi';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2020-03-04 09:05:54 -08:00
|
|
|
displayName = 'Invoice Ninja API';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2020-08-17 05:42:09 -07:00
|
|
|
documentationUrl = 'invoiceNinja';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2021-06-12 09:39:55 -07:00
|
|
|
properties: INodeProperties[] = [
|
2020-03-13 07:05:17 -07:00
|
|
|
{
|
2020-03-16 15:26:27 -07:00
|
|
|
displayName: 'URL',
|
|
|
|
name: 'url',
|
2021-06-12 09:39:55 -07:00
|
|
|
type: 'string',
|
2022-10-21 09:45:54 -07:00
|
|
|
default: '',
|
|
|
|
hint: 'Default URL for v4 is https://app.invoiceninja.com, for v5 it is https://invoicing.co',
|
2020-03-13 07:05:17 -07:00
|
|
|
},
|
2020-03-04 09:05:54 -08:00
|
|
|
{
|
|
|
|
displayName: 'API Token',
|
|
|
|
name: 'apiToken',
|
2021-06-12 09:39:55 -07:00
|
|
|
type: 'string',
|
2023-08-01 04:08:25 -07:00
|
|
|
typeOptions: { password: true },
|
2020-03-04 09:05:54 -08:00
|
|
|
default: '',
|
|
|
|
},
|
2022-10-21 09:45:54 -07:00
|
|
|
{
|
|
|
|
displayName: 'Secret',
|
|
|
|
name: 'secret',
|
|
|
|
type: 'string',
|
2023-08-01 04:08:25 -07:00
|
|
|
typeOptions: { password: true },
|
2022-10-21 09:45:54 -07:00
|
|
|
default: '',
|
|
|
|
hint: 'This is optional, enter only if you did set a secret in your app and only if you are using v5',
|
|
|
|
},
|
2020-03-04 09:05:54 -08:00
|
|
|
];
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2022-10-21 09:45:54 -07:00
|
|
|
test: ICredentialTestRequest = {
|
|
|
|
request: {
|
|
|
|
baseURL: '={{$credentials?.url}}',
|
|
|
|
url: '/api/v1/clients',
|
|
|
|
method: 'GET',
|
|
|
|
},
|
|
|
|
};
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2022-10-21 09:45:54 -07:00
|
|
|
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;
|
|
|
|
}
|
2020-03-04 09:05:54 -08:00
|
|
|
}
|