Skip to content

Commit

Permalink
Merge pull request #4 from YokySantiago/master
Browse files Browse the repository at this point in the history
Refactor according to n8n PR comments
  • Loading branch information
jamesliupenn authored Feb 2, 2022
2 parents 4424dc8 + 78842f8 commit 7d14e43
Show file tree
Hide file tree
Showing 18 changed files with 1,551 additions and 1,090 deletions.
78 changes: 63 additions & 15 deletions packages/nodes-base/nodes/Onfleet/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
ICredentialDataDecryptedObject,
IDataObject,
INodePropertyOptions,
JsonObject,
NodeApiError
} from 'n8n-workflow';
Expand All @@ -14,16 +16,16 @@ 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,
headers: IDataObject = {},
option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
uri?: string): Promise<any> { // tslint:disable-line:no-any
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
Expand All @@ -40,17 +42,63 @@ export async function onfleetApiRequest(
//@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;
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);
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

0 comments on commit 7d14e43

Please sign in to comment.