2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2021-03-03 16:09:31 -08:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2021-03-03 16:09:31 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { bubbleApiRequest, bubbleApiRequestAllItems, validateJSON } from './GenericFunctions';
|
2021-03-03 16:09:31 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { objectFields, objectOperations } from './ObjectDescription';
|
2021-03-03 16:09:31 -08:00
|
|
|
|
|
|
|
export class Bubble implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Bubble',
|
|
|
|
name: 'bubble',
|
|
|
|
icon: 'file:bubble.svg',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume the Bubble Data API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Bubble',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'bubbleApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-03-03 16:09:31 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Object',
|
|
|
|
value: 'object',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'object',
|
|
|
|
},
|
|
|
|
...objectOperations,
|
|
|
|
...objectFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-03-03 16:09:31 -08:00
|
|
|
|
|
|
|
let responseData;
|
|
|
|
const qs: IDataObject = {};
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2021-03-03 16:09:31 -08:00
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
if (resource === 'object') {
|
|
|
|
// *********************************************************************
|
|
|
|
// object
|
|
|
|
// *********************************************************************
|
|
|
|
|
|
|
|
// https://bubble.io/reference#API
|
|
|
|
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// object: create
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const typeNameInput = this.getNodeParameter('typeName', i) as string;
|
|
|
|
const typeName = typeNameInput.replace(/\s/g, '').toLowerCase();
|
|
|
|
|
|
|
|
const { property } = this.getNodeParameter('properties', i) as {
|
2022-08-01 13:47:55 -07:00
|
|
|
property: [{ key: string; value: string }];
|
2021-03-03 16:09:31 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const body = {} as IDataObject;
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
property.forEach((data) => (body[data.key] = data.value));
|
2021-03-03 16:09:31 -08:00
|
|
|
|
|
|
|
responseData = await bubbleApiRequest.call(this, 'POST', `/obj/${typeName}`, body, {});
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
|
|
// object: delete
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const typeNameInput = this.getNodeParameter('typeName', i) as string;
|
|
|
|
const typeName = typeNameInput.replace(/\s/g, '').toLowerCase();
|
|
|
|
const objectId = this.getNodeParameter('objectId', i) as string;
|
|
|
|
|
|
|
|
const endpoint = `/obj/${typeName}/${objectId}`;
|
|
|
|
responseData = await bubbleApiRequest.call(this, 'DELETE', endpoint, {}, {});
|
|
|
|
responseData = { success: true };
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// object: get
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const typeNameInput = this.getNodeParameter('typeName', i) as string;
|
|
|
|
const typeName = typeNameInput.replace(/\s/g, '').toLowerCase();
|
|
|
|
const objectId = this.getNodeParameter('objectId', i) as string;
|
|
|
|
|
|
|
|
const endpoint = `/obj/${typeName}/${objectId}`;
|
|
|
|
responseData = await bubbleApiRequest.call(this, 'GET', endpoint, {}, {});
|
|
|
|
responseData = responseData.response;
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// object: getAll
|
|
|
|
// ----------------------------------
|
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2021-03-03 16:09:31 -08:00
|
|
|
const typeNameInput = this.getNodeParameter('typeName', i) as string;
|
|
|
|
const typeName = typeNameInput.replace(/\s/g, '').toLowerCase();
|
|
|
|
|
|
|
|
const endpoint = `/obj/${typeName}`;
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', 0);
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-03-03 16:09:31 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!jsonParameters) {
|
2021-03-03 16:09:31 -08:00
|
|
|
if (options.filters) {
|
|
|
|
const { filter } = options.filters as IDataObject;
|
|
|
|
qs.constraints = JSON.stringify(filter);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const filter = options.filtersJson as string;
|
|
|
|
const data = validateJSON(filter);
|
|
|
|
if (data === undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Filters must be a valid JSON', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2021-03-03 16:09:31 -08:00
|
|
|
}
|
|
|
|
qs.constraints = JSON.stringify(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.sort) {
|
|
|
|
const { sortValue } = options.sort as IDataObject;
|
|
|
|
Object.assign(qs, sortValue);
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2021-03-03 16:09:31 -08:00
|
|
|
responseData = await bubbleApiRequestAllItems.call(this, 'GET', endpoint, {}, qs);
|
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', 0);
|
2021-03-03 16:09:31 -08:00
|
|
|
responseData = await bubbleApiRequest.call(this, 'GET', endpoint, {}, qs);
|
|
|
|
responseData = responseData.response.results;
|
|
|
|
}
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// object: update
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const typeNameInput = this.getNodeParameter('typeName', i) as string;
|
|
|
|
const typeName = typeNameInput.replace(/\s/g, '').toLowerCase();
|
|
|
|
const objectId = this.getNodeParameter('objectId', i) as string;
|
|
|
|
const endpoint = `/obj/${typeName}/${objectId}`;
|
|
|
|
const { property } = this.getNodeParameter('properties', i) as {
|
2022-08-01 13:47:55 -07:00
|
|
|
property: [{ key: string; value: string }];
|
2021-03-03 16:09:31 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const body = {} as IDataObject;
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
property.forEach((data) => (body[data.key] = data.value));
|
2021-03-03 16:09:31 -08:00
|
|
|
responseData = await bubbleApiRequest.call(this, 'PATCH', endpoint, body, {});
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = { success: true };
|
2021-03-03 16:09:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-03-03 16:09:31 -08:00
|
|
|
}
|
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2021-03-03 16:09:31 -08:00
|
|
|
}
|
|
|
|
}
|