2020-06-15 17:31:18 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
2020-01-17 09:34:36 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
2020-06-15 17:31:18 -07:00
|
|
|
|
|
|
|
import {
|
2021-04-17 00:25:51 -07:00
|
|
|
IDataObject,
|
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
|
|
|
} 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 = {},
|
|
|
|
) {
|
2020-06-08 08:50:18 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
2020-01-17 09:34:36 -08:00
|
|
|
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'accept-version': '1.0.0',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2021-04-17 00:25:51 -07: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;
|
|
|
|
}
|
|
|
|
try {
|
2020-06-08 08:50:18 -07:00
|
|
|
if (authenticationMethod === 'accessToken') {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('webflowApi');
|
2020-01-17 20:36:07 -08:00
|
|
|
|
2020-06-08 08:50:18 -07:00
|
|
|
options.headers!['authorization'] = `Bearer ${credentials.accessToken}`;
|
2020-01-17 20:36:07 -08:00
|
|
|
|
2020-06-08 08:50:18 -07:00
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} else {
|
|
|
|
return await this.helpers.requestOAuth2!.call(this, 'webflowOAuth2Api', options);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
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);
|
|
|
|
} while (
|
|
|
|
returnData.length < responseData.total
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|