diff --git a/packages/nodes-base/credentials/KoBoToolboxApi.credentials.ts b/packages/nodes-base/credentials/KoBoToolboxApi.credentials.ts index 91b1384045e7d..145989a429de8 100644 --- a/packages/nodes-base/credentials/KoBoToolboxApi.credentials.ts +++ b/packages/nodes-base/credentials/KoBoToolboxApi.credentials.ts @@ -1,4 +1,6 @@ import { + IAuthenticateGeneric, + ICredentialTestRequest, ICredentialType, NodePropertyTypes, } from 'n8n-workflow'; @@ -23,4 +25,20 @@ export class KoBoToolboxApi implements ICredentialType { hint: 'You can get your API token at https://[api-root]/token/?format=json (for a logged in user)', }, ]; + authenticate = { + type: 'generic', + properties: { + headers: { + Authorization: '=Token {{$credentials.token}}', + }, + }, + } as IAuthenticateGeneric; + + test: ICredentialTestRequest = { + request: { + baseURL: '={{$credentials.URL}}', + url: '/api/v2/assets/', + method: 'GET', + }, + }; } diff --git a/packages/nodes-base/nodes/KoBoToolbox/GenericFunctions.ts b/packages/nodes-base/nodes/KoBoToolbox/GenericFunctions.ts index 510f08acbd71b..481aa5c482cca 100644 --- a/packages/nodes-base/nodes/KoBoToolbox/GenericFunctions.ts +++ b/packages/nodes-base/nodes/KoBoToolbox/GenericFunctions.ts @@ -30,7 +30,6 @@ export async function koBoToolboxApiRequest(this: IExecuteFunctions | IWebhookFu url: '', headers: { 'Accept': 'application/json', - 'Authorization': `Token ${credentials.token}`, }, json: true, }; @@ -44,7 +43,7 @@ export async function koBoToolboxApiRequest(this: IExecuteFunctions | IWebhookFu let results = null; let keepLooking = true; while (keepLooking) { - const response = await this.helpers.httpRequest(options); + const response = await this.helpers.httpRequestWithAuthentication.call(this, 'koBoToolboxApi', options); // Append or set results results = response.results ? _.concat(results || [], response.results) : response; if (returnAll && response.next) { @@ -106,7 +105,7 @@ const formatValue = (value: any, format: string): any => { //tslint:disable-line return _.first(points) === _.last(points) ? { type: 'Polygon', - coordinates: [coordinates] + coordinates: [coordinates], } : { type: 'LineString', @@ -183,12 +182,12 @@ export async function downloadAttachments(this: IExecuteFunctions | IWebhookFunc let relatedQuestion = null; if('question' === options.binaryNamingScheme) { - for(let question of Object.keys(submission)) { + for(const question of Object.keys(submission)) { // The logic to map attachments to question is sometimes ambiguous: // - If the attachment is linked to a question, the question's value is the same as the attachment's filename (with spaces replaced by underscores) // - BUT sometimes the attachment's filename has an extra suffix, e.g. "My_Picture_0OdlaKJ.jpg", would map to the question "picture": "My Picture.jpg" const sanitizedQuestionValue = _.toString(submission[question]).replace(/\s/g, '_'); // replace spaces with underscores - if (sanitizedFileName == sanitizedQuestionValue) { + if (sanitizedFileName === sanitizedQuestionValue) { relatedQuestion = question; // Just use the first match... break; diff --git a/packages/nodes-base/nodes/KoBoToolbox/KoBoToolbox.node.ts b/packages/nodes-base/nodes/KoBoToolbox/KoBoToolbox.node.ts index 219a1bef6babd..b85d9a248e73a 100644 --- a/packages/nodes-base/nodes/KoBoToolbox/KoBoToolbox.node.ts +++ b/packages/nodes-base/nodes/KoBoToolbox/KoBoToolbox.node.ts @@ -55,7 +55,6 @@ export class KoBoToolbox implements INodeType { { name: 'koBoToolboxApi', required: true, - testedBy: 'koBoToolboxApiCredentialTest', }, ], properties: [ @@ -91,41 +90,6 @@ export class KoBoToolbox implements INodeType { }; methods = { - credentialTest: { - async koBoToolboxApiCredentialTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise { - const credentials = credential.data; - try { - const response = await this.helpers.request({ - url: `${credentials!.URL}/api/v2/assets/hash`, - headers: { - 'Accept': 'application/json', - 'Authorization': `Token ${credentials!.token}`, - }, - json: true, - }); - - if (response.hash) { - return { - status: 'OK', - message: 'Connection successful!', - }; - } - else { - return { - status: 'Error', - message: `Credentials are not valid. Response: ${response.detail}`, - }; - } - } - catch (err) { - return { - status: 'Error', - message: `Credentials validation failed: ${(err as JsonObject).message}`, - }; - } - }, - }, - loadOptions: { loadForms, },