Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onfleet node #2593

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/nodes-base/credentials/OnfleetApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';

export class OnfleetApi implements ICredentialType {
name = 'onfleetApi';
displayName = 'Onfleet API';
documentationUrl = 'onfleet';
properties = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}
56 changes: 56 additions & 0 deletions packages/nodes-base/nodes/Onfleet/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
IDataObject,
JsonObject,
NodeApiError
} from 'n8n-workflow';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';

import {
OptionsWithUri,
} from 'request';

export async function onfleetApiRequest(
this: IWebhookFunctions | IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string,
apikey: string,
resource: string,
body: any = {}, // tslint:disable-line:no-any
qs?: any, // tslint:disable-line:no-any
uri?: string,
headers: IDataObject = {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Header is a parameter but it's never used in the function?

option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${apikey}`,
'User-Agent': 'n8n-onfleet',
},
method,
body,
qs,
uri: uri || `https://onfleet.com/api/v2/${resource}`,
json: true,
};
try {
//@ts-ignore
return await this.helpers.request(options);
} catch (error) {
const apiError = error as IDataObject;
const { message: messageError } = apiError.error as IDataObject;
if (messageError) {
const { message = '', cause = '' } = messageError as IDataObject;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you simply pass the error object to the NodeApiError?

if (message && cause) {
apiError.message = `${message}: ${cause}`;
} else {
apiError.message = message;
}
throw new NodeApiError(this.getNode(), apiError as JsonObject);
}
throw new NodeApiError(this.getNode(), apiError as JsonObject);
}
}
Loading