2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2020-01-25 13:44:33 -08:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeExecutionData,
|
2020-01-25 13:44:33 -08:00
|
|
|
INodePropertyOptions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2020-01-25 13:44:33 -08:00
|
|
|
} from 'n8n-workflow';
|
2024-08-29 06:55:53 -07:00
|
|
|
import { NodeConnectionType } from 'n8n-workflow';
|
2020-08-09 16:23:51 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { linkFields, linkOperations } from './LinkDescription';
|
2020-08-09 16:23:51 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { bitlyApiRequest, bitlyApiRequestAllItems } from './GenericFunctions';
|
2020-01-25 13:44:33 -08:00
|
|
|
|
|
|
|
export class Bitly implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Bitly',
|
|
|
|
name: 'bitly',
|
2021-06-12 12:00:37 -07:00
|
|
|
icon: 'file:bitly.svg',
|
2020-01-25 13:44:33 -08:00
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Bitly API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Bitly',
|
|
|
|
},
|
2024-08-29 06:55:53 -07:00
|
|
|
inputs: [NodeConnectionType.Main],
|
|
|
|
outputs: [NodeConnectionType.Main],
|
2020-01-25 13:44:33 -08:00
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'bitlyApi',
|
|
|
|
required: true,
|
2020-07-03 02:22:39 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
authentication: ['accessToken'],
|
2020-07-03 02:22:39 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'bitlyOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
authentication: ['oAuth2'],
|
2020-07-03 02:22:39 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-01-25 13:44:33 -08:00
|
|
|
],
|
|
|
|
properties: [
|
2020-07-03 02:22:39 -07:00
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Access Token',
|
|
|
|
value: 'accessToken',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'OAuth2',
|
|
|
|
value: 'oAuth2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'accessToken',
|
|
|
|
},
|
2020-01-25 13:44:33 -08:00
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-01-25 13:44:33 -08:00
|
|
|
options: [
|
|
|
|
{
|
2022-05-20 14:47:24 -07:00
|
|
|
name: 'Link',
|
2020-01-25 13:44:33 -08:00
|
|
|
value: 'link',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'link',
|
|
|
|
},
|
|
|
|
...linkOperations,
|
|
|
|
...linkFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the available groups to display them to user so that they can
|
2020-01-25 13:44:33 -08:00
|
|
|
// select them easily
|
|
|
|
async getGroups(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const groups = await bitlyApiRequestAllItems.call(this, 'groups', 'GET', '/groups');
|
|
|
|
for (const group of groups) {
|
|
|
|
const groupName = group.name;
|
|
|
|
const groupId = group.guid;
|
|
|
|
returnData.push({
|
|
|
|
name: groupName,
|
|
|
|
value: groupId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the available tags to display them to user so that they can
|
2020-01-25 13:44:33 -08:00
|
|
|
// select them easily
|
|
|
|
async getTags(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const groupId = this.getCurrentNodeParameter('group') as string;
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-01 13:47:55 -07:00
|
|
|
const tags = await bitlyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'tags',
|
|
|
|
'GET',
|
|
|
|
`groups/${groupId}/tags`,
|
|
|
|
);
|
2020-01-25 13:44:33 -08:00
|
|
|
for (const tag of tags) {
|
|
|
|
const tagName = tag;
|
|
|
|
const tagId = tag;
|
|
|
|
returnData.push({
|
|
|
|
name: tagName,
|
|
|
|
value: tagId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-01-25 13:44:33 -08:00
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-01-25 13:44:33 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'link') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const longUrl = this.getNodeParameter('longUrl', 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
|
|
|
const body: IDataObject = {
|
|
|
|
long_url: longUrl,
|
|
|
|
};
|
|
|
|
if (additionalFields.title) {
|
|
|
|
body.title = additionalFields.title as string;
|
2020-01-25 13:44:33 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.domain) {
|
|
|
|
body.domain = additionalFields.domain as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.group) {
|
|
|
|
body.group = additionalFields.group as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.tags) {
|
|
|
|
body.tags = additionalFields.tags as string[];
|
|
|
|
}
|
2022-08-01 13:47:55 -07:00
|
|
|
const deeplinks = (this.getNodeParameter('deeplink', i) as IDataObject)
|
|
|
|
.deeplinkUi as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (deeplinks) {
|
|
|
|
for (const deeplink of deeplinks) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.deeplinks.push({
|
|
|
|
app_uri_path: deeplink.appUriPath,
|
|
|
|
install_type: deeplink.installType,
|
|
|
|
install_url: deeplink.installUrl,
|
|
|
|
app_id: deeplink.appId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
responseData = await bitlyApiRequest.call(this, 'POST', '/bitlinks', body);
|
2020-01-25 13:44:33 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
|
|
|
const linkId = this.getNodeParameter('id', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
|
|
|
if (updateFields.longUrl) {
|
|
|
|
body.long_url = updateFields.longUrl as string;
|
|
|
|
}
|
|
|
|
if (updateFields.title) {
|
|
|
|
body.title = updateFields.title as string;
|
|
|
|
}
|
|
|
|
if (updateFields.archived !== undefined) {
|
|
|
|
body.archived = updateFields.archived as boolean;
|
|
|
|
}
|
|
|
|
if (updateFields.group) {
|
|
|
|
body.group = updateFields.group as string;
|
2020-01-25 13:44:33 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.tags) {
|
|
|
|
body.tags = updateFields.tags as string[];
|
|
|
|
}
|
2022-08-01 13:47:55 -07:00
|
|
|
const deeplinks = (this.getNodeParameter('deeplink', i) as IDataObject)
|
|
|
|
.deeplinkUi as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (deeplinks) {
|
|
|
|
for (const deeplink of deeplinks) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.deeplinks.push({
|
|
|
|
app_uri_path: deeplink.appUriPath,
|
|
|
|
install_type: deeplink.installType,
|
|
|
|
install_url: deeplink.installUrl,
|
|
|
|
app_id: deeplink.appId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
responseData = await bitlyApiRequest.call(this, 'PATCH', `/bitlinks/${linkId}`, body);
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
const linkId = this.getNodeParameter('id', i) as string;
|
|
|
|
responseData = await bitlyApiRequest.call(this, 'GET', `/bitlinks/${linkId}`);
|
2020-01-25 13:44:33 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
2024-08-30 00:59:30 -07:00
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
returnData.push({ error: error.message, json: {}, itemIndex: i });
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-01-25 13:44:33 -08:00
|
|
|
}
|
|
|
|
}
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2020-01-25 13:44:33 -08:00
|
|
|
}
|
|
|
|
}
|