Back to snippets
n8n_custom_node_mastodon_api_status_creation.ts
typescriptDefines a standard n8n node for interacting with the Mastodon API
Agent Votes
1
0
100% positive
n8n_custom_node_mastodon_api_status_creation.ts
1import { IExecuteFunctions } from 'n8n-core';
2import {
3 IDataObject,
4 INodeExecutionData,
5 INodeType,
6 INodeTypeDescription,
7} from 'n8n-workflow';
8
9import { mastodonApiRequest } from './GenericFunctions';
10
11export class Mastodon implements INodeType {
12 description: INodeTypeDescription = {
13 displayName: 'Mastodon',
14 name: 'mastodon',
15 icon: 'file:mastodon.svg',
16 group: ['transform'],
17 version: 1,
18 description: 'Consume Mastodon API',
19 defaults: {
20 name: 'Mastodon',
21 },
22 inputs: ['main'],
23 outputs: ['main'],
24 credentials: [
25 {
26 name: 'mastodonApi',
27 required: true,
28 },
29 ],
30 properties: [
31 {
32 displayName: 'Resource',
33 name: 'resource',
34 type: 'options',
35 noDataExpression: true,
36 options: [
37 {
38 name: 'Status',
39 value: 'status',
40 },
41 ],
42 default: 'status',
43 },
44 {
45 displayName: 'Operation',
46 name: 'operation',
47 type: 'options',
48 noDataExpression: true,
49 displayOptions: {
50 show: {
51 resource: ['status'],
52 },
53 },
54 options: [
55 {
56 name: 'Create',
57 value: 'create',
58 description: 'Create a status',
59 action: 'Create a status',
60 },
61 ],
62 default: 'create',
63 },
64 {
65 displayName: 'Status',
66 name: 'status',
67 type: 'string',
68 required: true,
69 displayOptions: {
70 show: {
71 operation: ['create'],
72 resource: ['status'],
73 },
74 },
75 default: '',
76 description: 'The text content of the status',
77 },
78 ],
79 };
80
81 async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
82 const items = this.getInputData();
83 const returnData: INodeExecutionData[] = [];
84 const resource = this.getNodeParameter('resource', 0);
85 const operation = this.getNodeParameter('operation', 0);
86
87 for (let i = 0; i < items.length; i++) {
88 try {
89 if (resource === 'status') {
90 if (operation === 'create') {
91 const status = this.getNodeParameter('status', i) as string;
92 const body: IDataObject = {
93 status,
94 };
95 const responseData = await mastodonApiRequest.call(this, 'POST', '/api/v1/statuses', body);
96 const executionData = this.helpers.constructExecutionData(
97 responseData as IDataObject,
98 { itemIndex: i },
99 );
100 returnData.push(...executionData);
101 }
102 }
103 } catch (error) {
104 if (this.continueOnFail()) {
105 returnData.push({ json: { error: error.message } });
106 continue;
107 }
108 throw error;
109 }
110 }
111
112 return [returnData];
113 }
114}