n8n/packages/nodes-base/nodes/Google/Slides/GoogleSlides.node.ts

300 lines
6.7 KiB
TypeScript
Raw Normal View History

2020-10-19 04:45:33 -07:00
import {
IExecuteFunctions,
} from 'n8n-core';
import {
2021-03-29 01:13:28 -07:00
IDataObject,
2020-10-19 04:45:33 -07:00
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
googleApiRequest,
} from './GenericFunctions';
export interface IGoogleAuthCredentials {
email: string;
privateKey: string;
}
export class GoogleSlides implements INodeType {
description: INodeTypeDescription = {
displayName: 'Google Slides',
name: 'googleSlides',
icon: 'file:googleslides.svg',
group: ['input', 'output'],
version: 1,
2020-10-20 01:13:36 -07:00
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
2021-03-29 01:13:28 -07:00
description: 'Consume the Google Slides API',
2020-10-19 04:45:33 -07:00
defaults: {
name: 'Google Slides',
2020-10-20 01:13:36 -07:00
color: '#edba25',
2020-10-19 04:45:33 -07:00
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'googleApi',
required: true,
displayOptions: {
show: {
authentication: [
'serviceAccount',
],
},
},
},
{
name: 'googleSlidesOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'OAuth2',
value: 'oAuth2',
},
2021-03-29 01:13:28 -07:00
{
name: 'Service Account',
value: 'serviceAccount',
},
2020-10-19 04:45:33 -07:00
],
default: 'serviceAccount',
},
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
2020-10-20 01:13:36 -07:00
name: 'Page',
value: 'page',
2020-10-19 04:45:33 -07:00
},
2021-03-29 01:13:28 -07:00
{
name: 'Presentation',
value: 'presentation',
},
2020-10-19 04:45:33 -07:00
],
2020-10-20 01:13:36 -07:00
default: 'presentation',
2021-03-29 01:13:28 -07:00
description: 'Resource to operate on',
2020-10-19 04:45:33 -07:00
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Create',
value: 'create',
description: 'Create a presentation',
},
{
name: 'Get',
value: 'get',
description: 'Get a presentation',
},
2021-03-29 01:13:28 -07:00
{
name: 'Get Slides',
value: 'getSlides',
description: 'Get presentation slides',
},
2020-10-19 04:45:33 -07:00
],
displayOptions: {
show: {
resource: [
2020-10-20 01:13:36 -07:00
'presentation',
2020-10-19 04:45:33 -07:00
],
},
},
default: 'create',
2021-03-29 01:13:28 -07:00
description: 'Operation to perform',
2020-10-19 04:45:33 -07:00
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Get',
value: 'get',
description: 'Get a page',
},
{
name: 'Get Thumbnail',
value: 'getThumbnail',
description: 'Get a thumbnail',
},
],
displayOptions: {
show: {
resource: [
2020-10-20 01:13:36 -07:00
'page',
2020-10-19 04:45:33 -07:00
],
},
},
default: 'get',
2021-03-29 01:13:28 -07:00
description: 'Operation to perform',
2020-10-19 04:45:33 -07:00
},
{
displayName: 'Title',
name: 'title',
2021-03-29 01:13:28 -07:00
description: 'Title of the presentation to create.',
2020-10-19 04:45:33 -07:00
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
2020-10-20 01:13:36 -07:00
'presentation',
2020-10-19 04:45:33 -07:00
],
2021-03-29 01:13:28 -07:00
operation: [
'create',
],
2020-10-19 04:45:33 -07:00
},
},
},
{
displayName: 'Presentation ID',
name: 'presentationId',
2021-03-29 01:13:28 -07:00
description: 'ID of the presentation to retrieve. Found in the presentation URL:<br><code>https://docs.google.com/presentation/d/PRESENTATION_ID/edit</code>',
placeholder: '1wZtNFZ8MO-WKrxhYrOLMvyiqSgFwdSz5vn8_l_7eNqw',
2020-10-19 04:45:33 -07:00
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
2020-10-20 01:13:36 -07:00
'presentation',
'page',
2020-10-19 04:45:33 -07:00
],
2021-03-29 01:13:28 -07:00
operation: [
'get',
'getThumbnail',
'getSlides',
],
2020-10-19 04:45:33 -07:00
},
},
},
{
displayName: 'Page Object ID',
name: 'pageObjectId',
2021-03-29 01:13:28 -07:00
description: 'ID of the page object to retrieve.',
2020-10-19 04:45:33 -07:00
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
2021-03-29 01:13:28 -07:00
resource: [
'page',
],
2020-10-19 04:45:33 -07:00
operation: [
'get',
'getThumbnail',
],
},
},
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
2021-03-29 01:13:28 -07:00
let responseData;
const returnData: IDataObject[] = [];
2020-10-19 04:45:33 -07:00
2021-03-29 01:13:28 -07:00
for (let i = 0; i < items.length; i++) {
if (resource === 'page') {
// *********************************************************************
// page
// *********************************************************************
2020-10-19 04:45:33 -07:00
if (operation === 'get') {
2021-03-29 01:13:28 -07:00
// ----------------------------------
// page: get
// ----------------------------------
2020-10-19 04:45:33 -07:00
const presentationId = this.getNodeParameter('presentationId', i) as string;
const pageObjectId = this.getNodeParameter('pageObjectId', i) as string;
2021-03-29 01:13:28 -07:00
responseData = await googleApiRequest.call(this, 'GET', `/${presentationId}/pages/${pageObjectId}`);
2020-10-19 04:45:33 -07:00
} else if (operation === 'getThumbnail') {
2021-03-29 01:13:28 -07:00
// ----------------------------------
// page: getThumbnail
// ----------------------------------
2020-10-19 04:45:33 -07:00
const presentationId = this.getNodeParameter('presentationId', i) as string;
const pageObjectId = this.getNodeParameter('pageObjectId', i) as string;
2021-03-29 01:13:28 -07:00
responseData = await googleApiRequest.call(this, 'GET', `/${presentationId}/pages/${pageObjectId}/thumbnail`);
}
} else if (resource === 'presentation') {
// *********************************************************************
// presentation
// *********************************************************************
if (operation === 'create') {
// ----------------------------------
// presentation: create
// ----------------------------------
const body = {
title: this.getNodeParameter('title', i) as string,
};
responseData = await googleApiRequest.call(this, 'POST', '', body);
} else if (operation === 'get') {
// ----------------------------------
// presentation: get
// ----------------------------------
const presentationId = this.getNodeParameter('presentationId', i) as string;
responseData = await googleApiRequest.call(this, 'GET', `/${presentationId}`);
} else if (operation === 'getSlides') {
// ----------------------------------
// presentation: getSlides
// ----------------------------------
const presentationId = this.getNodeParameter('presentationId', i) as string;
responseData = await googleApiRequest.call(this, 'GET', `/${presentationId}`, {}, { fields: 'slides' });
2020-10-19 04:45:33 -07:00
}
2021-03-29 01:13:28 -07:00
2020-10-19 04:45:33 -07:00
}
2021-03-29 01:13:28 -07:00
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
2020-10-19 04:45:33 -07:00
}
return [this.helpers.returnJsonArray(responseData)];
}
}