2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-06-15 17:31:18 -07:00
|
|
|
|
2020-01-17 09:34:36 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
2020-06-15 17:31:18 -07:00
|
|
|
|
2022-11-11 07:07:50 -08:00
|
|
|
import { IDataObject } from 'n8n-workflow';
|
2020-01-17 09:34:36 -08:00
|
|
|
|
2021-04-17 00:25:51 -07:00
|
|
|
export async function webflowApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
) {
|
2022-07-04 01:44:26 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0, 'accessToken');
|
|
|
|
let credentialsType = '';
|
|
|
|
|
|
|
|
if (authenticationMethod === 'accessToken') {
|
|
|
|
credentialsType = 'webflowApi';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authenticationMethod === 'oAuth2') {
|
|
|
|
credentialsType = 'webflowOAuth2Api';
|
|
|
|
}
|
|
|
|
|
2020-01-17 09:34:36 -08:00
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'accept-version': '1.0.0',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://api.webflow.com${resource}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-01-17 09:34:36 -08:00
|
|
|
};
|
|
|
|
options = Object.assign({}, options, option);
|
2021-04-17 00:25:51 -07:00
|
|
|
|
|
|
|
if (Object.keys(options.qs).length === 0) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
2020-01-17 09:34:36 -08:00
|
|
|
if (Object.keys(options.body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
2022-11-22 08:57:17 -08:00
|
|
|
return this.helpers.requestWithAuthentication.call(this, credentialsType, options);
|
2020-01-17 09:34:36 -08:00
|
|
|
}
|
2021-04-17 00:25:51 -07:00
|
|
|
|
|
|
|
export async function webflowApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
) {
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.limit = 100;
|
|
|
|
query.offset = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await webflowApiRequest.call(this, method, endpoint, body, query);
|
|
|
|
if (responseData.offset !== undefined) {
|
|
|
|
query.offset += query.limit;
|
|
|
|
}
|
|
|
|
returnData.push.apply(returnData, responseData.items);
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (returnData.length < responseData.total);
|
2021-04-17 00:25:51 -07:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|