diff --git a/.gitignore b/.gitignore index f846c68..a4aee7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules package-lock.json yarn.lock +.env diff --git a/package.json b/package.json index d5799c0..3ec5c61 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "body-parser": "^1.18.3", "cors": "^2.8.4", "express": "^4.16.3", - "http-proxy-middleware": "^0.18.0", "request": "^2.85.0", "webtask-tools": "^3.2.0" }, diff --git a/webtask-zoho.js b/webtask-zoho.js index 8d8c49f..e28767f 100644 --- a/webtask-zoho.js +++ b/webtask-zoho.js @@ -1,7 +1,6 @@ const express = require('express') const Webtask = require('webtask-tools') const cors = require('cors') -const proxy = require('http-proxy-middleware') const bodyParser = require('body-parser') const request = require('request') @@ -11,6 +10,9 @@ server.use(cors()) server.listen(4430) server.use(bodyParser.json()) +// +// Zoho API urls +// const apiUrlZohoCampaigns = 'https://campaigns.zoho.com/api/' const apiUrlZohoCRM = 'https://www.zohoapis.com/crm/v2/' @@ -26,7 +28,7 @@ server.get('/newsletter/:data', (req, res) => { url: `${apiUrlZohoCampaigns}json/listsubscribe?authtoken=${ZOHO_CAMPAIGNS_TOKEN}&scope=CampaignsAPI&resfmt=JSON&listkey=${ZOHO_CAMPAIGNS_LIST_KEY}&contactinfo=${data}` // eslint-disable-line max-len } - request(options, (error, response, body) => { // eslint-disable-line consistent-return + request(options, (error, response, body) => { if (error) { res.send(error) } @@ -36,19 +38,29 @@ server.get('/newsletter/:data', (req, res) => { }) }) -const onProxyReqZohoCRM = (proxyReq, req) => { +// +// Create a new lead via Zoho CRM API +// https://www.zoho.com/crm/help/api/v2/#create-specify-records +// +server.get('/crm/:data', (req, res) => { const { ZOHO_CRM_TOKEN } = req.webtaskContext.secrets + const { data } = req.params - proxyReq.setHeader('Authorization', `Zoho-oauthtoken ${ZOHO_CRM_TOKEN}`) -} + const options = { + url: `${apiUrlZohoCRM}Leads`, // eslint-disable-line max-len + headers: { 'Authorization': `Zoho-oauthtoken ${ZOHO_CRM_TOKEN}` }, + method: 'POST', + formData: data + } -const configZohoCRM = { - target: apiUrlZohoCRM, - pathRewrite: { '^/zoho/crm/': '/' }, - changeOrigin: true, - onProxyReq: onProxyReqZohoCRM -} + request(options, (error, response, body) => { + if (error) { + res.send(error) + } -server.use(proxy('/zoho/crm/**', configZohoCRM)) + res.send(body) + res.sendStatus(200) + }) +}) module.exports = Webtask.fromExpress(server)