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 all 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: '',
},
];
}
104 changes: 104 additions & 0 deletions packages/nodes-base/nodes/Onfleet/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {
ICredentialDataDecryptedObject,
IDataObject,
INodePropertyOptions,
JsonObject,
NodeApiError
} from 'n8n-workflow';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';

import {
OptionsWithUri,
} from 'request';

import * as moment from 'moment-timezone';

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): 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) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}

export const resourceLoaders = {
async getTeams(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
try {
const credentials = await this.getCredentials('onfleetApi') as ICredentialDataDecryptedObject;
const encodedApiKey = Buffer.from(`${credentials.apiKey}:`).toString('base64');
const teams = await onfleetApiRequest.call(this, 'GET', encodedApiKey, 'teams') as IDataObject[];
return teams.map(({name = '', id: value = ''}) => ( {name, value} )) as INodePropertyOptions[];
} catch (error) {
return [];
}
},

async getWorkers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
try {
const credentials = await this.getCredentials('onfleetApi') as ICredentialDataDecryptedObject;
const encodedApiKey = Buffer.from(`${credentials.apiKey}:`).toString('base64');
const workers = await onfleetApiRequest.call(this, 'GET', encodedApiKey, 'workers') as IDataObject[];
return workers.map(({name = '', id: value = ''}) => ( {name, value} )) as INodePropertyOptions[];
} catch (error) {
return [];
}
},

async getAdmins (this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
try {
const credentials = await this.getCredentials('onfleetApi') as ICredentialDataDecryptedObject;
const encodedApiKey = Buffer.from(`${credentials.apiKey}:`).toString('base64');
const admins = await onfleetApiRequest.call(this, 'GET', encodedApiKey, 'admins') as IDataObject[];
return admins.map(({name = '', id: value = ''}) => ( {name, value} )) as INodePropertyOptions[];
} catch (error) {
return [];
}
},

async getHubs (this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
try {
const credentials = await this.getCredentials('onfleetApi') as ICredentialDataDecryptedObject;
const encodedApiKey = Buffer.from(`${credentials.apiKey}:`).toString('base64');
const hubs = await onfleetApiRequest.call(this, 'GET', encodedApiKey, 'hubs') as IDataObject[];
return hubs.map(({name = '', id: value = ''}) => ( {name, value} )) as INodePropertyOptions[];
} catch (error) {
return [];
}
},

async getTimezones(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData = [] as INodePropertyOptions[];
for (const timezone of moment.tz.names()) {
returnData.push({
name: timezone,
value: timezone,
});
}
return returnData;
},
};
Loading