From 7f33ff8c3cc4dc12fde725a630d6336ed69e97d6 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Tue, 13 Oct 2020 05:08:07 -0400 Subject: [PATCH] :zap: Add share operation to OneDrive Node (#1044) --- .../Microsoft/OneDrive/FileDescription.ts | 87 ++++++++++++++++++- .../Microsoft/OneDrive/FolderDescription.ts | 87 ++++++++++++++++++- .../Microsoft/OneDrive/GenericFunctions.ts | 1 - .../OneDrive/MicrosoftOneDrive.node.ts | 24 +++++ 4 files changed, 196 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/nodes/Microsoft/OneDrive/FileDescription.ts b/packages/nodes-base/nodes/Microsoft/OneDrive/FileDescription.ts index 0e9bc26cba..b408f270cd 100644 --- a/packages/nodes-base/nodes/Microsoft/OneDrive/FileDescription.ts +++ b/packages/nodes-base/nodes/Microsoft/OneDrive/FileDescription.ts @@ -1,4 +1,6 @@ -import { INodeProperties } from 'n8n-workflow'; +import { + INodeProperties, +} from 'n8n-workflow'; export const fileOperations = [ { @@ -38,6 +40,11 @@ export const fileOperations = [ value: 'search', description: 'Search a file', }, + { + name: 'Share', + value: 'share', + description: 'Share a file', + }, { name: 'Upload', value: 'upload', @@ -273,6 +280,84 @@ export const fileFields = [ across several fields including filename, metadata, and file content.`, }, /* -------------------------------------------------------------------------- */ +/* file:share */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'File ID', + name: 'fileId', + type: 'string', + displayOptions: { + show: { + operation: [ + 'share', + ], + resource: [ + 'file', + ], + }, + }, + default: '', + description: 'File ID', + }, + { + displayName: 'Type', + name: 'type', + type: 'options', + options: [ + { + name: 'View', + value: 'view', + }, + { + name: 'Edit', + value: 'edit', + }, + { + name: 'Embed', + value: 'embed', + }, + ], + displayOptions: { + show: { + operation: [ + 'share', + ], + resource: [ + 'file', + ], + }, + }, + default: '', + description: 'The type of sharing link to create', + }, + { + displayName: 'Scope', + name: 'scope', + type: 'options', + options: [ + { + name: 'Anonymous', + value: 'anonymous', + }, + { + name: 'Organization', + value: 'organization', + }, + ], + displayOptions: { + show: { + operation: [ + 'share', + ], + resource: [ + 'file', + ], + }, + }, + default: '', + description: 'The type of sharing link to create', + }, +/* -------------------------------------------------------------------------- */ /* file:upload */ /* -------------------------------------------------------------------------- */ { diff --git a/packages/nodes-base/nodes/Microsoft/OneDrive/FolderDescription.ts b/packages/nodes-base/nodes/Microsoft/OneDrive/FolderDescription.ts index 55401bee5f..a33f1302d6 100644 --- a/packages/nodes-base/nodes/Microsoft/OneDrive/FolderDescription.ts +++ b/packages/nodes-base/nodes/Microsoft/OneDrive/FolderDescription.ts @@ -1,4 +1,6 @@ -import { INodeProperties } from 'n8n-workflow'; +import { + INodeProperties, +} from 'n8n-workflow'; export const folderOperations = [ { @@ -28,6 +30,11 @@ export const folderOperations = [ value: 'search', description: 'Search a folder', }, + { + name: 'Share', + value: 'share', + description: 'Share a folder', + }, ], default: 'getChildren', description: 'The operation to perform.', @@ -124,4 +131,82 @@ export const folderFields = [ description: `The query text used to search for items. Values may be matched across several fields including filename, metadata, and file content.`, }, +/* -------------------------------------------------------------------------- */ +/* folder:share */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'Folder ID', + name: 'folderId', + type: 'string', + displayOptions: { + show: { + operation: [ + 'share', + ], + resource: [ + 'folder', + ], + }, + }, + default: '', + description: 'File ID', + }, + { + displayName: 'Type', + name: 'type', + type: 'options', + options: [ + { + name: 'View', + value: 'view', + }, + { + name: 'Edit', + value: 'edit', + }, + { + name: 'Embed', + value: 'embed', + }, + ], + displayOptions: { + show: { + operation: [ + 'share', + ], + resource: [ + 'folder', + ], + }, + }, + default: '', + description: 'The type of sharing link to create', + }, + { + displayName: 'Scope', + name: 'scope', + type: 'options', + options: [ + { + name: 'Anonymous', + value: 'anonymous', + }, + { + name: 'Organization', + value: 'organization', + }, + ], + displayOptions: { + show: { + operation: [ + 'share', + ], + resource: [ + 'folder', + ], + }, + }, + default: '', + description: 'The type of sharing link to create', + }, ] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Microsoft/OneDrive/GenericFunctions.ts b/packages/nodes-base/nodes/Microsoft/OneDrive/GenericFunctions.ts index 7040bb053e..b8cdc89cac 100644 --- a/packages/nodes-base/nodes/Microsoft/OneDrive/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Microsoft/OneDrive/GenericFunctions.ts @@ -33,7 +33,6 @@ export async function microsoftApiRequest(this: IExecuteFunctions | IExecuteSing if (Object.keys(body).length === 0) { delete options.body; } - //@ts-ignore return await this.helpers.requestOAuth2.call(this, 'microsoftOneDriveOAuth2Api', options); } catch (error) { diff --git a/packages/nodes-base/nodes/Microsoft/OneDrive/MicrosoftOneDrive.node.ts b/packages/nodes-base/nodes/Microsoft/OneDrive/MicrosoftOneDrive.node.ts index 8b65b6b99f..a7bacc29fa 100644 --- a/packages/nodes-base/nodes/Microsoft/OneDrive/MicrosoftOneDrive.node.ts +++ b/packages/nodes-base/nodes/Microsoft/OneDrive/MicrosoftOneDrive.node.ts @@ -159,6 +159,18 @@ export class MicrosoftOneDrive implements INodeType { responseData = responseData.filter((item: IDataObject) => item.file); returnData.push.apply(returnData, responseData as IDataObject[]); } + //https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createlink?view=odsp-graph-online + if (operation === 'share') { + const fileId = this.getNodeParameter('fileId', i) as string; + const type = this.getNodeParameter('type', i) as string; + const scope = this.getNodeParameter('scope', i) as string; + const body: IDataObject = { + type, + scope, + }; + responseData = await microsoftApiRequest.call(this, 'POST', `/drive/items/${fileId}/createLink`, body); + returnData.push(responseData); + } //https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content?view=odsp-graph-online#example-upload-a-new-file if (operation === 'upload') { const parentId = this.getNodeParameter('parentId', i) as string; @@ -221,6 +233,18 @@ export class MicrosoftOneDrive implements INodeType { responseData = responseData.filter((item: IDataObject) => item.folder); returnData.push.apply(returnData, responseData as IDataObject[]); } + //https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createlink?view=odsp-graph-online + if (operation === 'share') { + const folderId = this.getNodeParameter('folderId', i) as string; + const type = this.getNodeParameter('type', i) as string; + const scope = this.getNodeParameter('scope', i) as string; + const body: IDataObject = { + type, + scope, + }; + responseData = await microsoftApiRequest.call(this, 'POST', `/drive/items/${folderId}/createLink`, body); + returnData.push(responseData); + } } } if (resource === 'file' && operation === 'download') {