2024-08-29 06:55:53 -07:00
|
|
|
import {
|
|
|
|
type IExecuteFunctions,
|
|
|
|
type IDataObject,
|
|
|
|
type INodeExecutionData,
|
|
|
|
type INodeType,
|
|
|
|
type INodeTypeDescription,
|
|
|
|
NodeConnectionType,
|
2023-01-27 03:22:44 -08:00
|
|
|
} from 'n8n-workflow';
|
2020-12-02 14:13:59 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { yourlsApiRequest } from './GenericFunctions';
|
2020-12-02 14:13:59 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { urlFields, urlOperations } from './UrlDescription';
|
2020-12-02 14:13:59 -08:00
|
|
|
|
|
|
|
export class Yourls implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Yourls',
|
|
|
|
name: 'yourls',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-12-02 14:13:59 -08:00
|
|
|
icon: 'file:yourls.png',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Yourls API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Yourls',
|
|
|
|
},
|
2024-08-29 06:55:53 -07:00
|
|
|
inputs: [NodeConnectionType.Main],
|
|
|
|
outputs: [NodeConnectionType.Main],
|
2020-12-02 14:13:59 -08:00
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'yourlsApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-12-02 14:13:59 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'URL',
|
|
|
|
value: 'url',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'url',
|
|
|
|
},
|
|
|
|
...urlOperations,
|
|
|
|
...urlFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2020-12-02 14:13:59 -08:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-12-02 14:13:59 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'url') {
|
|
|
|
if (operation === 'shorten') {
|
|
|
|
const url = this.getNodeParameter('url', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.url = url;
|
|
|
|
qs.action = 'shorturl';
|
|
|
|
Object.assign(qs, additionalFields);
|
|
|
|
responseData = await yourlsApiRequest.call(this, 'GET', {}, qs);
|
|
|
|
}
|
2020-12-02 14:13:59 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'expand') {
|
|
|
|
const shortUrl = this.getNodeParameter('shortUrl', i) as string;
|
|
|
|
qs.shorturl = shortUrl;
|
|
|
|
qs.action = 'expand';
|
|
|
|
responseData = await yourlsApiRequest.call(this, 'GET', {}, qs);
|
|
|
|
}
|
2020-12-02 14:13:59 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'stats') {
|
|
|
|
const shortUrl = this.getNodeParameter('shortUrl', i) as string;
|
|
|
|
qs.shorturl = shortUrl;
|
|
|
|
qs.action = 'url-stats';
|
|
|
|
responseData = await yourlsApiRequest.call(this, 'GET', {}, qs);
|
|
|
|
responseData = responseData.link;
|
|
|
|
}
|
2020-12-02 14:13:59 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-08-30 00:59:30 -07:00
|
|
|
if (this.continueOnFail()) {
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-12-02 14:13:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|