2022-08-01 13:47:55 -07:00
|
|
|
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2021-03-03 16:09:31 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { IDataObject, ILoadOptionsFunctions, NodeApiError } from 'n8n-workflow';
|
2021-03-03 16:09:31 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2021-03-03 16:09:31 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an authenticated API request to Bubble.
|
|
|
|
*/
|
|
|
|
export async function bubbleApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject,
|
|
|
|
qs: IDataObject,
|
|
|
|
) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const { apiToken, appName, domain, environment, hosting } = (await this.getCredentials(
|
|
|
|
'bubbleApi',
|
|
|
|
)) as {
|
|
|
|
apiToken: string;
|
|
|
|
appName: string;
|
|
|
|
domain: string;
|
|
|
|
environment: 'development' | 'live';
|
|
|
|
hosting: 'bubbleHosted' | 'selfHosted';
|
2021-03-03 16:09:31 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const rootUrl = hosting === 'bubbleHosted' ? `https://${appName}.bubbleapps.io` : domain;
|
|
|
|
const urlSegment = environment === 'development' ? '/version-test/api/1.1' : '/api/1.1';
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'user-agent': 'n8n',
|
2022-08-01 13:47:55 -07:00
|
|
|
Authorization: `Bearer ${apiToken}`,
|
2021-03-03 16:09:31 -08:00
|
|
|
},
|
|
|
|
method,
|
|
|
|
uri: `${rootUrl}${urlSegment}${endpoint}`,
|
|
|
|
qs,
|
|
|
|
body,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(qs).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-02 12:54:28 -08:00
|
|
|
return this.helpers.request!(options);
|
2021-03-03 16:09:31 -08:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2021-03-03 16:09:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an authenticated API request to Bubble and return all results.
|
|
|
|
*/
|
|
|
|
export async function bubbleApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject,
|
|
|
|
qs: IDataObject,
|
|
|
|
) {
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
qs.limit = 100;
|
|
|
|
do {
|
|
|
|
responseData = await bubbleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
qs.cursor = responseData.cursor;
|
2022-12-02 12:54:28 -08:00
|
|
|
returnData.push.apply(returnData, responseData.response.results);
|
2022-08-01 13:47:55 -07:00
|
|
|
} while (responseData.response.remaining !== 0);
|
2021-03-03 16:09:31 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export function validateJSON(json: string | undefined): any {
|
2021-03-03 16:09:31 -08:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = undefined;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|