n8n-nodes-easy-appointments
Version:
n8n nodes for Easy!Appointments API integration
194 lines • 6.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.commonFields = void 0;
exports.easyAppointmentsApiRequest = easyAppointmentsApiRequest;
exports.easyAppointmentsApiRequestAllItems = easyAppointmentsApiRequestAllItems;
exports.createResource = createResource;
exports.updateResource = updateResource;
exports.getResource = getResource;
exports.getAllResources = getAllResources;
exports.deleteResource = deleteResource;
const n8n_workflow_1 = require("n8n-workflow");
async function easyAppointmentsApiRequest(method, endpoint, body = {}, qs = {}) {
const options = {
method,
body,
qs,
url: '',
json: true,
};
if (!endpoint.startsWith('/')) {
endpoint = `/${endpoint}`;
}
options.url = endpoint;
if (!Object.keys(body).length) {
delete options.body;
}
if (!Object.keys(qs).length) {
delete options.qs;
}
const credentials = await this.getCredentials('easyAppointmentsApi');
let baseUrl = credentials.baseUrl;
if (baseUrl.endsWith('/')) {
baseUrl = baseUrl.slice(0, -1);
}
options.url = `${baseUrl}${options.url}`;
try {
const result = await this.helpers.httpRequestWithAuthentication.call(this, 'easyAppointmentsApi', options);
this.logger.debug('Easy!Appointments API request successful');
return result;
}
catch (error) {
this.logger.error('Easy!Appointments API request failed', { error });
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
async function easyAppointmentsApiRequestAllItems(method, endpoint, body = {}, qs = {}) {
const returnData = [];
let responseData;
qs.page = 1;
qs.length = 100;
do {
responseData = await easyAppointmentsApiRequest.call(this, method, endpoint, body, qs);
returnData.push.apply(returnData, responseData);
qs.page++;
} while (responseData.length === qs.length);
return returnData;
}
async function createResource(itemIndex, endpoint, fields) {
const body = {};
for (const field of fields) {
const value = this.getNodeParameter(field, itemIndex, '');
if (value !== '') {
body[field] = value;
}
}
const responseData = await easyAppointmentsApiRequest.call(this, 'POST', endpoint, body);
return this.helpers.returnJsonArray(responseData);
}
async function updateResource(itemIndex, endpoint, fields) {
const body = {};
for (const field of fields) {
const value = this.getNodeParameter(field, itemIndex, '');
if (value !== '') {
body[field] = value;
}
}
const responseData = await easyAppointmentsApiRequest.call(this, 'PUT', endpoint, body);
return this.helpers.returnJsonArray(responseData);
}
async function getResource(itemIndex, endpoint) {
const responseData = await easyAppointmentsApiRequest.call(this, 'GET', endpoint);
return this.helpers.returnJsonArray(responseData);
}
async function getAllResources(itemIndex, endpoint) {
const returnAll = this.getNodeParameter('returnAll', itemIndex);
const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
const qs = {};
if (additionalFields.fields) {
qs.fields = additionalFields.fields;
}
if (additionalFields.with) {
qs.with = additionalFields.with;
}
if (additionalFields.sort) {
qs.sort = additionalFields.sort;
}
if (additionalFields.q) {
qs.q = additionalFields.q;
}
try {
const filters = this.getNodeParameter('filters', itemIndex, {});
Object.assign(qs, filters);
}
catch (error) {
}
let responseData;
if (returnAll) {
responseData = await easyAppointmentsApiRequestAllItems.call(this, 'GET', endpoint, {}, qs);
}
else {
const limit = this.getNodeParameter('limit', itemIndex);
qs.page = 1;
qs.length = limit;
responseData = await easyAppointmentsApiRequest.call(this, 'GET', endpoint, {}, qs);
}
return this.helpers.returnJsonArray(responseData);
}
async function deleteResource(itemIndex, endpoint) {
await easyAppointmentsApiRequest.call(this, 'DELETE', endpoint);
return [{ json: { success: true } }];
}
exports.commonFields = [
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
default: false,
description: 'Whether to return all results or only up to a given limit',
displayOptions: {
show: {
operation: ['getAll'],
},
},
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
default: 50,
description: 'Max number of results to return',
typeOptions: {
minValue: 1,
},
displayOptions: {
show: {
operation: ['getAll'],
returnAll: [false],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
operation: ['getAll'],
},
},
options: [
{
displayName: 'Fields',
name: 'fields',
type: 'string',
default: '',
description: 'Comma-separated list of fields to include in the response',
},
{
displayName: 'Include Related',
name: 'with',
type: 'string',
default: '',
description: 'Comma-separated list of related data to include in the response',
},
{
displayName: 'Sort',
name: 'sort',
type: 'string',
default: '',
description: 'Sort results by field (prefix with - for descending order)',
},
{
displayName: 'Search Query',
name: 'q',
type: 'string',
default: '',
description: 'Search term to filter results',
},
],
},
];
//# sourceMappingURL=GenericFunctions.js.map