2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2021-02-07 12:44:20 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
|
2021-02-07 12:44:20 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { peekalinkApiRequest } from './GenericFunctions';
|
2021-02-07 12:44:20 -08:00
|
|
|
|
|
|
|
export class Peekalink implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Peekalink',
|
|
|
|
name: 'peekalink',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2021-02-07 12:44:20 -08:00
|
|
|
icon: 'file:peekalink.png',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"]',
|
|
|
|
description: 'Consume the Peekalink API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Peekalink',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'peekalinkApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-02-07 12:44:20 -08:00
|
|
|
options: [
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
name: 'Is Available',
|
2021-02-07 12:44:20 -08:00
|
|
|
value: 'isAvailable',
|
|
|
|
description: 'Check whether preview for a given link is available',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Check whether the preview for a given link is available',
|
2021-02-07 12:44:20 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Preview',
|
|
|
|
value: 'preview',
|
|
|
|
description: 'Return the preview for a link',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Return the preview for a link',
|
2021-02-07 12:44:20 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'preview',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'URL',
|
|
|
|
name: 'url',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-02-07 12:44:20 -08:00
|
|
|
let responseData;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (operation === 'isAvailable') {
|
|
|
|
const url = this.getNodeParameter('url', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
link: url,
|
|
|
|
};
|
2021-02-07 12:44:20 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await peekalinkApiRequest.call(this, 'POST', `/is-available/`, body);
|
|
|
|
}
|
|
|
|
if (operation === 'preview') {
|
|
|
|
const url = this.getNodeParameter('url', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
link: url,
|
|
|
|
};
|
2021-02-07 12:44:20 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await peekalinkApiRequest.call(this, 'POST', `/`, body);
|
|
|
|
}
|
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2021-02-07 12:44:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|