Configure the Advanced I/O Function

If you initialized the Advanced I/O function in Node.js, its directory, functions/files, contains:

You will be adding code in index.js.You can use any IDE to configure the function.

Install Packages for Node.js

The Node.js Event function requires three packages to be installed: express and axios.

express

We will use the Express framework to manage the routing operations that enable us to fetch and delete files.

To install express, navigate to the Node function’s directory (functions/files) and execute the following command:

copy
$
npm install express --save

This will install the Express module and save the dependencies.

catalyst_workdrive_npm_express

axios

axios is a promise-based HTTP client that we will use to send asynchronous delete requests to the WorkDrive API endpoint.

To install axios, navigate to the Node function’s directory (functions/files) and execute the following command:

copy
$
npm install axios

This will install the modules.

This information will also be updated in the package.json file of the Advanced I/O function.

    
copy
{ "name": "files", "version": "1.0.0", "main": "index.js", "author": "emma@zylker.com", "dependencies": { "axios": "^0.21.4", "express": "^4.17.1", "zcatalyst-sdk-node": "latest" } }

You can now add the code in the function file.

Copy the code below and paste it in index.js located in functions/files directory and save the file.

    
index.js
copy
'use strict'; const express = require('express'); var app = express(); const catalyst = require('zcatalyst-sdk-node'); app.use(express.json()); const FOLDERID = {{YOUR_FILESTORE_FOLDER_ID}}; //Enter your File Store Folder ID const axios = require('axios').default; const credentials = { WorkDriveConnectorz: { client_id: '{{YOUR_CLIENT_ID}}', //Enter your Client ID client_secret: '{{YOUR_CLIENT_SECRET}}', //Enter your Client Secret auth_url: 'https://accounts.zoho.com/oauth/v2/token', refresh_url: 'https://accounts.zoho.com/oauth/v2/token', refresh_token: '{{YOUR_REFRESH TOKEN}}' //Enter your Refresh Token } } app.get('/getFiles', async (req, res) => { try { var catalystApp = catalyst.initialize(req); const query = 'SELECT * FROM WorkDriveFileID limit 1,100'; const queryResult = await catalystApp.zcql().executeZCQLQuery(query); let resData = []; for (var i = 0; i < queryResult.length; i++) { const data = queryResult[i].WorkDriveFileID; resData.push(data); } res.status(200).send(resData); } catch (e) { console.log(e); res.status(500).send({ "error": "Internal server error occurred. Please try again in some time." }); } }); app.delete("/deleteFile", async (req, res) => { try { const FILEID = req.query.fileID; const catalystApp = catalyst.initialize(req); const query = "SELECT * FROM WorkDriveFileID where FileID=" + FILEID; const queryResult = await catalystApp.zcql().executeZCQLQuery(query); const ROWID = queryResult[0].WorkDriveFileID.ROWID; const WorkDriveFileID = queryResult[0].WorkDriveFileID.WorkDriveFileID; const accessToken = await catalystApp .connection(credentials) .getConnector("WorkDriveConnectorz") .getAccessToken(); const config = { method: "PATCH", url: `https://www.zohoapis.com/workdrive/api/v1/files/${WorkDriveFileID}`,headers: {Authorization: `Zoho-oauthtoken ${accessToken}`,Accept: "application/vnd.api+json",},data: JSON.stringify({ data: { attributes: { status: "51", }, type: "files", }, }), }; axios(config) .then(async function (response) { const folder = catalystApp.filestore().folder(FOLDERID); await folder.deleteFile(FILEID); const table = catalystApp.datastore().table("WorkDriveFileID"); await table.deleteRow(ROWID); res.status(200).send({ message: "Deleted Successfully" }); }) .catch(function (error) { console.log(error); res.status(500).send({ message: "Internal server error occurred. Please try again in some time" }); }); } catch (e) { console.log(e); res .status(500) .send({ error: "Internal server error occurred. Please try again in some time.", }); } }); module.exports = app;
View more
Note: After you copy and paste this code in your function file, ensure that you provide the following values in it as indicated by the comments:

The Advanced I/O function is now configured. We will discuss the function and client code, after you configure the client.

Last Updated 2023-12-15 18:54:08 +0530 +0530

RELATED LINKS

Functions