n8n-nodes-base
Version:
Base nodes of n8n
105 lines • 3.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resourceLoaders = void 0;
exports.onfleetApiRequest = onfleetApiRequest;
exports.onfleetApiRequestAllItems = onfleetApiRequestAllItems;
const moment_timezone_1 = __importDefault(require("moment-timezone"));
const n8n_workflow_1 = require("n8n-workflow");
async function onfleetApiRequest(method, resource, body = {}, qs, uri) {
const credentials = await this.getCredentials('onfleetApi');
const options = {
headers: {
'Content-Type': 'application/json',
'User-Agent': 'n8n-onfleet',
},
auth: {
user: credentials.apiKey,
pass: '',
},
method,
body,
qs,
uri: uri || `https://onfleet.com/api/v2/${resource}`,
json: true,
};
try {
return await this.helpers.request(options);
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
async function onfleetApiRequestAllItems(propertyName, method, endpoint, body = {}, query = {}) {
const returnData = [];
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;
}
exports.resourceLoaders = {
async getTeams() {
try {
const teams = (await onfleetApiRequest.call(this, 'GET', 'teams'));
return teams.map(({ name = '', id: value = '' }) => ({
name,
value,
}));
}
catch (error) {
return [];
}
},
async getWorkers() {
try {
const workers = (await onfleetApiRequest.call(this, 'GET', 'workers'));
return workers.map(({ name = '', id: value = '' }) => ({
name,
value,
}));
}
catch (error) {
return [];
}
},
async getAdmins() {
try {
const admins = (await onfleetApiRequest.call(this, 'GET', 'admins'));
return admins.map(({ name = '', id: value = '' }) => ({
name,
value,
}));
}
catch (error) {
return [];
}
},
async getHubs() {
try {
const hubs = (await onfleetApiRequest.call(this, 'GET', 'hubs'));
return hubs.map(({ name = '', id: value = '' }) => ({
name,
value,
}));
}
catch (error) {
return [];
}
},
async getTimezones() {
const returnData = [];
for (const timezone of moment_timezone_1.default.tz.names()) {
returnData.push({
name: timezone,
value: timezone,
});
}
return returnData;
},
};
//# sourceMappingURL=GenericFunctions.js.map