2023-12-21 09:22:32 -08:00
|
|
|
import {
|
|
|
|
Node,
|
|
|
|
NodeApiError,
|
|
|
|
type IExecuteFunctions,
|
|
|
|
type INodeExecutionData,
|
|
|
|
type INodeTypeDescription,
|
|
|
|
type JsonObject,
|
2024-08-29 06:55:53 -07:00
|
|
|
NodeConnectionType,
|
2023-01-27 03:22:44 -08:00
|
|
|
} from 'n8n-workflow';
|
2021-02-07 12:44:20 -08:00
|
|
|
|
2023-12-21 09:22:32 -08:00
|
|
|
export const apiUrl = 'https://api.peekalink.io';
|
2021-02-07 12:44:20 -08:00
|
|
|
|
2023-12-21 09:22:32 -08:00
|
|
|
type Operation = 'preview' | 'isAvailable';
|
|
|
|
|
|
|
|
export class Peekalink extends Node {
|
2021-02-07 12:44:20 -08:00
|
|
|
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',
|
|
|
|
},
|
2024-08-29 06:55:53 -07:00
|
|
|
inputs: [NodeConnectionType.Main],
|
|
|
|
outputs: [NodeConnectionType.Main],
|
2021-02-07 12:44:20 -08:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-12-21 09:22:32 -08:00
|
|
|
async execute(context: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = context.getInputData();
|
|
|
|
const operation = context.getNodeParameter('operation', 0) as Operation;
|
|
|
|
const credentials = await context.getCredentials('peekalinkApi');
|
2021-02-07 12:44:20 -08:00
|
|
|
|
2023-12-21 09:22:32 -08:00
|
|
|
const returnData = await Promise.all(
|
|
|
|
items.map(async (_, i) => {
|
|
|
|
try {
|
|
|
|
const link = context.getNodeParameter('url', i) as string;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
|
|
return await context.helpers.request({
|
|
|
|
method: 'POST',
|
|
|
|
uri: operation === 'preview' ? apiUrl : `${apiUrl}/is-available/`,
|
|
|
|
body: { link },
|
|
|
|
headers: { 'X-API-Key': credentials.apiKey },
|
|
|
|
json: true,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (context.continueOnFail()) {
|
|
|
|
return { error: error.message };
|
|
|
|
}
|
|
|
|
throw new NodeApiError(context.getNode(), error as JsonObject);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2023-12-21 09:22:32 -08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
return [context.helpers.returnJsonArray(returnData)];
|
2021-02-07 12:44:20 -08:00
|
|
|
}
|
|
|
|
}
|