Small changes to FacebookGraphAPI-Node

This commit is contained in:
Jan Oberhauser 2020-05-02 16:54:21 +02:00
parent c0c281995a
commit 4f83d29afe
3 changed files with 31 additions and 44 deletions

View file

@ -4,9 +4,9 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
export class GraphApi implements ICredentialType { export class FacebookGraphApi implements ICredentialType {
name = 'graphApi'; name = 'facebookGraphApi';
displayName = 'Graph API'; displayName = 'Facebook Graph API';
properties = [ properties = [
{ {
displayName: 'Access Token', displayName: 'Access Token',

View file

@ -12,24 +12,23 @@ import {
import { OptionsWithUri } from 'request'; import { OptionsWithUri } from 'request';
export class GraphApi implements INodeType { export class FacebookGraphApi implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Graph API', displayName: 'Facebook Graph API',
name: 'graphApi', name: 'facebookGraphApi',
icon: 'file:facebook.png', icon: 'file:facebook.png',
group: ['transform'], group: ['transform'],
version: 1, version: 1,
description: 'Interacts with Facebook using the Graph API', description: 'Interacts with Facebook using the Graph API',
defaults: { defaults: {
name: 'Graph API', name: 'Facebook Graph API',
color: '#772244', color: '#772244',
}, },
inputs: ['main'], inputs: ['main'],
outputs: ['main', 'main'], outputs: ['main'],
outputNames: ['success', 'failure'],
credentials: [ credentials: [
{ {
name: 'graphApi', name: 'facebookGraphApi',
required: true, required: true,
}, },
], ],
@ -85,35 +84,35 @@ export class GraphApi implements INodeType {
}, },
{ {
name: 'v6.0', name: 'v6.0',
value: 'v6.0/', value: 'v6.0',
}, },
{ {
name: 'v5.0', name: 'v5.0',
value: 'v5.0/', value: 'v5.0',
}, },
{ {
name: 'v4.0', name: 'v4.0',
value: 'v4.0/', value: 'v4.0',
}, },
{ {
name: 'v3.3', name: 'v3.3',
value: 'v3.3/', value: 'v3.3',
}, },
{ {
name: 'v3.2', name: 'v3.2',
value: 'v3.2/', value: 'v3.2',
}, },
{ {
name: 'v3.1', name: 'v3.1',
value: 'v3.1/', value: 'v3.1',
}, },
{ {
name: 'v3.0', name: 'v3.0',
value: 'v3.0/', value: 'v3.0',
}, },
{ {
name: 'v2.12', name: 'v2.12',
value: 'v2.12/', value: 'v2.12',
}, },
], ],
default: '', default: '',
@ -270,19 +269,22 @@ export class GraphApi implements INodeType {
const items = this.getInputData(); const items = this.getInputData();
let response: any; // tslint:disable-line:no-any let response: any; // tslint:disable-line:no-any
const successItems: INodeExecutionData[] = []; const returnItems: INodeExecutionData[] = [];
const failureItems: INodeExecutionData[] = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
const graphApiCredentials = this.getCredentials('graphApi'); const graphApiCredentials = this.getCredentials('facebookGraphApi');
const hostUrl = this.getNodeParameter('hostUrl', itemIndex) as string; const hostUrl = this.getNodeParameter('hostUrl', itemIndex) as string;
const httpRequestMethod = this.getNodeParameter('httpRequestMethod', itemIndex) as string; const httpRequestMethod = this.getNodeParameter('httpRequestMethod', itemIndex) as string;
const graphApiVersion = this.getNodeParameter('graphApiVersion', itemIndex) as string; let graphApiVersion = this.getNodeParameter('graphApiVersion', itemIndex) as string;
const node = this.getNodeParameter('node', itemIndex) as string; const node = this.getNodeParameter('node', itemIndex) as string;
const edge = this.getNodeParameter('edge', itemIndex) as string; const edge = this.getNodeParameter('edge', itemIndex) as string;
const options = this.getNodeParameter('options', itemIndex, {}) as IDataObject; const options = this.getNodeParameter('options', itemIndex, {}) as IDataObject;
if (graphApiVersion !== '') {
graphApiVersion += '/';
}
let uri = `https://${hostUrl}/${graphApiVersion}${node}`; let uri = `https://${hostUrl}/${graphApiVersion}${node}`;
if (edge) { if (edge) {
uri = `${uri}/${edge}`; uri = `${uri}/${edge}`;
@ -315,7 +317,7 @@ export class GraphApi implements INodeType {
if (options.queryParameters !== undefined) { if (options.queryParameters !== undefined) {
const queryParameters = options.queryParameters as IDataObject; const queryParameters = options.queryParameters as IDataObject;
if (queryParameters.parameter != undefined) { if (queryParameters.parameter !== undefined) {
for (const queryParameter of queryParameters.parameter as IDataObject[]) { for (const queryParameter of queryParameters.parameter as IDataObject[]) {
requestOptions.qs[queryParameter.name as string] = queryParameter.value; requestOptions.qs[queryParameter.name as string] = queryParameter.value;
} }
@ -379,22 +381,7 @@ export class GraphApi implements INodeType {
throw error; throw error;
} }
let errorItem; returnItems.push(items[itemIndex]);
if (error.response !== undefined) {
// Since this is a Graph API node and we already know the request was
// not successful, we'll go straight to the error details.
const graphApiErrors = error.response.body?.error ?? {};
errorItem = {
statusCode: error.statusCode,
...graphApiErrors,
headers: error.response.headers,
};
} else {
// Unknown Graph API response, we'll dump everything in the response item
errorItem = error;
}
failureItems.push({ json: { ...errorItem }});
continue; continue;
} }
@ -404,13 +391,13 @@ export class GraphApi implements INodeType {
throw new Error('Response body is not valid JSON.'); throw new Error('Response body is not valid JSON.');
} }
failureItems.push({json: {message: response}}); returnItems.push(items[itemIndex]);
continue; continue;
} }
successItems.push({json: response}); returnItems.push({json: response});
} }
return [successItems, failureItems]; return [returnItems];
} }
} }

View file

@ -48,13 +48,13 @@
"dist/credentials/DriftApi.credentials.js", "dist/credentials/DriftApi.credentials.js",
"dist/credentials/DropboxApi.credentials.js", "dist/credentials/DropboxApi.credentials.js",
"dist/credentials/EventbriteApi.credentials.js", "dist/credentials/EventbriteApi.credentials.js",
"dist/credentials/FacebookGraphApi.credentials.js",
"dist/credentials/FreshdeskApi.credentials.js", "dist/credentials/FreshdeskApi.credentials.js",
"dist/credentials/FileMaker.credentials.js", "dist/credentials/FileMaker.credentials.js",
"dist/credentials/FlowApi.credentials.js", "dist/credentials/FlowApi.credentials.js",
"dist/credentials/GithubApi.credentials.js", "dist/credentials/GithubApi.credentials.js",
"dist/credentials/GitlabApi.credentials.js", "dist/credentials/GitlabApi.credentials.js",
"dist/credentials/GoogleApi.credentials.js", "dist/credentials/GoogleApi.credentials.js",
"dist/credentials/GraphApi.credentials.js",
"dist/credentials/GumroadApi.credentials.js", "dist/credentials/GumroadApi.credentials.js",
"dist/credentials/HarvestApi.credentials.js", "dist/credentials/HarvestApi.credentials.js",
"dist/credentials/HttpBasicAuth.credentials.js", "dist/credentials/HttpBasicAuth.credentials.js",
@ -155,6 +155,7 @@
"dist/nodes/Eventbrite/EventbriteTrigger.node.js", "dist/nodes/Eventbrite/EventbriteTrigger.node.js",
"dist/nodes/ExecuteCommand.node.js", "dist/nodes/ExecuteCommand.node.js",
"dist/nodes/ExecuteWorkflow.node.js", "dist/nodes/ExecuteWorkflow.node.js",
"dist/nodes/Facebook/FacebookGraphApi.node.js",
"dist/nodes/FileMaker/FileMaker.node.js", "dist/nodes/FileMaker/FileMaker.node.js",
"dist/nodes/Freshdesk/Freshdesk.node.js", "dist/nodes/Freshdesk/Freshdesk.node.js",
"dist/nodes/Flow/Flow.node.js", "dist/nodes/Flow/Flow.node.js",
@ -167,7 +168,6 @@
"dist/nodes/Gitlab/GitlabTrigger.node.js", "dist/nodes/Gitlab/GitlabTrigger.node.js",
"dist/nodes/Google/GoogleDrive.node.js", "dist/nodes/Google/GoogleDrive.node.js",
"dist/nodes/Google/GoogleSheets.node.js", "dist/nodes/Google/GoogleSheets.node.js",
"dist/nodes/Facebook/GraphApi.node.js",
"dist/nodes/GraphQL/GraphQL.node.js", "dist/nodes/GraphQL/GraphQL.node.js",
"dist/nodes/Gumroad/GumroadTrigger.node.js", "dist/nodes/Gumroad/GumroadTrigger.node.js",
"dist/nodes/Harvest/Harvest.node.js", "dist/nodes/Harvest/Harvest.node.js",