Skip to content

Commit

Permalink
⚡ Improvements to #2593
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoE105 committed Feb 18, 2022
1 parent e505a69 commit 146e202
Show file tree
Hide file tree
Showing 9 changed files with 675 additions and 499 deletions.
65 changes: 44 additions & 21 deletions packages/nodes-base/nodes/Onfleet/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@ 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 credentials = await this.getCredentials('onfleetApi') as ICredentialDataDecryptedObject;

const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${apikey}`,
'User-Agent': 'n8n-onfleet',
},
auth: {
user: credentials.apiKey as string,
pass: '',
},
method,
body,
qs,
Expand All @@ -42,50 +47,68 @@ export async function onfleetApiRequest(
//@ts-ignore
return await this.helpers.request(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
console.log(error.message);
//throw new NodeApiError(this.getNode(), error as JsonObject);
}
}

export async function onfleetApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions,
propertyName: string,
method: string,
apiKey: string,
endpoint: string,
// tslint:disable-next-line: no-any
body: any = {},
query: IDataObject = {},
): Promise<any> { // tslint:disable-line:no-any

const returnData: IDataObject[] = [];

let responseData;

do {
responseData = await onfleetApiRequest.call(this, method, endpoint, body, query);
query.lastId = responseData['lastId'];
returnData.push.apply(returnData, responseData[propertyName]);
} while (
responseData['lastId'] !== undefined
);

return returnData;
}
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[];
const teams = await onfleetApiRequest.call(this, 'GET', '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[];
const workers = await onfleetApiRequest.call(this, 'GET', 'workers') as IDataObject[];
return workers.map(({ name = '', id: value = '' }) => ({ name, value })) as INodePropertyOptions[];
} catch (error) {
return [];
}
},

async getAdmins (this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
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[];
const admins = await onfleetApiRequest.call(this, 'GET', 'admins') as IDataObject[];
return admins.map(({ name = '', id: value = '' }) => ({ name, value })) as INodePropertyOptions[];
} catch (error) {
return [];
}
},

async getHubs (this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
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[];
const hubs = await onfleetApiRequest.call(this, 'GET', 'hubs') as IDataObject[];
return hubs.map(({ name = '', id: value = '' }) => ({ name, value })) as INodePropertyOptions[];
} catch (error) {
return [];
}
Expand Down
Loading

0 comments on commit 146e202

Please sign in to comment.