From 798c3dc928a56f3ff775f7780cc12f3596e21968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Mon, 1 Mar 2021 10:00:11 -0300 Subject: [PATCH] :zap: Add node file --- .../InstagramBasicDisplay.node.ts | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 packages/nodes-base/nodes/Instagram/Basic Display/InstagramBasicDisplay.node.ts diff --git a/packages/nodes-base/nodes/Instagram/Basic Display/InstagramBasicDisplay.node.ts b/packages/nodes-base/nodes/Instagram/Basic Display/InstagramBasicDisplay.node.ts new file mode 100644 index 0000000000..ff71fa2500 --- /dev/null +++ b/packages/nodes-base/nodes/Instagram/Basic Display/InstagramBasicDisplay.node.ts @@ -0,0 +1,185 @@ +import { + IExecuteFunctions, +} from 'n8n-core'; + +import { + IDataObject, + INodeExecutionData, + INodeType, + INodeTypeDescription, +} from 'n8n-workflow'; + +import { + instagramBasicDisplayApiRequest, +} from './GenericFunctions'; + +import { + userFields, + userOperations, +} from './UserDescription'; + +import { + mediaFields, + mediaOperations, +} from './MediaDescription'; + +export class InstagramBasicDisplay implements INodeType { + description: INodeTypeDescription = { + displayName: 'Instagram Basic Display', + name: 'instagramBasicDisplay', + icon: 'file:instagram.svg', + group: ['transform'], + version: 1, + subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', + description: 'Consumes Instagram Basic Display API', + defaults: { + name: 'Instagram Basic Display', + color: '#833ab4', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: 'instagramBasicDisplayOAuth2Api', + required: true, + }, + ], + properties: [ + { + displayName: 'Resource', + name: 'resource', + type: 'options', + options: [ + { + name: 'Media', + value: 'media', + }, + { + name: 'User', + value: 'user', + }, + ], + default: 'user', + description: 'Resource to consume', + }, + ...userOperations, + ...userFields, + ...mediaOperations, + ...mediaFields, + ], + }; + + async execute(this: IExecuteFunctions): Promise { + const items = this.getInputData(); + const returnData: IDataObject[] = []; + + const resource = this.getNodeParameter('resource', 0) as string; + const operation = this.getNodeParameter('operation', 0) as string; + + for (let i = 0; i < items.length; i++) { + + let responseData; + + if (resource === 'user') { + + // ********************************************************************* + // user + // ********************************************************************* + + if (operation === 'get') { + + // ---------------------------------- + // user: get + // ---------------------------------- + + const returnSelf = this.getNodeParameter('returnSelf', i); + const fields = this.getNodeParameter('fields', i) as string[]; + const qs: IDataObject = {}; + + if (fields) { + qs.fields = fields.join(','); + } + + if (returnSelf) { + + responseData = await instagramBasicDisplayApiRequest.call(this, 'GET', '/me', {}, qs); + + } else { + + const userId = this.getNodeParameter('userId', i); + responseData = await instagramBasicDisplayApiRequest.call(this, 'GET', `/${userId}`, {}, qs); + + } + + } + + } else if (resource === 'media') { + + // ********************************************************************* + // media + // ********************************************************************* + + if (operation === 'getAll') { + + // ---------------------------------- + // media: getAll + // ---------------------------------- + + const type = this.getNodeParameter('type', i) as string; + + if (type === 'userMedia') { + + const fields = this.getNodeParameter('fields', i) as string[]; + const qs: IDataObject = {}; + + if (fields) { + qs.fields = fields.join(','); + } + + const userId = this.getNodeParameter('userId', i); + const endpoint = `/${userId}/media`; + + responseData = await instagramBasicDisplayApiRequest.call(this, 'GET', endpoint, {}, qs); + responseData = responseData.data; + + } else if (type === 'albumMedia') { + + const mediaId = this.getNodeParameter('mediaId', i); + const endpoint = `/${mediaId}/children`; + + responseData = await instagramBasicDisplayApiRequest.call(this, 'GET', endpoint, {}, {}); + responseData = responseData.data; + + } else if (type === 'mediaFieldsAndEdges') { + + const fields = this.getNodeParameter('fields', i) as string[]; + const qs: IDataObject = {}; + + if (fields) { + qs.fields = fields.join(','); + } + + const mediaId = this.getNodeParameter('mediaId', i); + responseData = await instagramBasicDisplayApiRequest.call(this, 'GET', `/${mediaId}`, {}, qs); + } + } + } + + if (responseData.media && responseData.media.paging) { + delete responseData.media.paging; + } + + if (responseData.paging) { + delete responseData.paging; + } + + Array.isArray(responseData) + ? returnData.push(...responseData) + : returnData.push(responseData); + + } + + return [this.helpers.returnJsonArray(returnData)]; + + } +}