|
| 1 | +import { |
| 2 | + IDataObject, |
| 3 | + IExecuteFunctions, |
| 4 | + INodeType, |
| 5 | + INodeTypeDescription, |
| 6 | +} from 'n8n-workflow'; |
| 7 | + |
| 8 | +import { twitchApiRequest } from './GenericFunctions.js'; |
| 9 | + |
| 10 | +export class Twitch implements INodeType { |
| 11 | + description: INodeTypeDescription = { |
| 12 | + displayName: 'Twitch', |
| 13 | + name: 'twitch', |
| 14 | + icon: 'file:twitch.svg', |
| 15 | + group: ['transform'], |
| 16 | + version: 1, |
| 17 | + description: 'Interact with Twitch', |
| 18 | + defaults: { |
| 19 | + name: 'Twitch', |
| 20 | + }, |
| 21 | + inputs: ['main'], |
| 22 | + outputs: ['main'], |
| 23 | + credentials: [ |
| 24 | + { |
| 25 | + name: 'twitchApi', |
| 26 | + required: true, |
| 27 | + }, |
| 28 | + ], |
| 29 | + properties: [ |
| 30 | + { |
| 31 | + displayName: 'Operation', |
| 32 | + name: 'operation', |
| 33 | + type: 'options', |
| 34 | + noDataExpression: true, |
| 35 | + default: 'getChannelStreams', |
| 36 | + options: [ |
| 37 | + { |
| 38 | + name: 'Get Channel Streams', |
| 39 | + value: 'getChannelStreams', |
| 40 | + action: 'Get channel streams', |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + { |
| 45 | + displayName: 'Channel Name', |
| 46 | + name: 'channel_name', |
| 47 | + type: 'string', |
| 48 | + required: true, |
| 49 | + default: '', |
| 50 | + description: 'Name of the channel whose streams to retrieve', |
| 51 | + }, |
| 52 | + ], |
| 53 | + }; |
| 54 | + |
| 55 | + async execute(this: IExecuteFunctions) { |
| 56 | + const items = this.getInputData(); |
| 57 | + const returnData: IDataObject[] = []; |
| 58 | + |
| 59 | + for (let i = 0; i < items.length; i++) { |
| 60 | + const channelName = this.getNodeParameter('channel_name', i) as string; |
| 61 | + |
| 62 | + const response = await twitchApiRequest.call( |
| 63 | + this, |
| 64 | + 'GET', |
| 65 | + '/streams', |
| 66 | + {}, |
| 67 | + { user_login: channelName }, |
| 68 | + ); |
| 69 | + |
| 70 | + if (Array.isArray(response.data)) { |
| 71 | + returnData.push(...response.data); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return [this.helpers.returnJsonArray(returnData)]; |
| 76 | + } |
| 77 | +} |
0 commit comments