-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
121 lines (105 loc) · 4.1 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const path = require("path");
const sqlite3 = require('sqlite3').verbose();
const serveStatic = require("serve-static");
const { readFileSync } = require('fs');
const { setupFdk } = require("@gofynd/fdk-extension-javascript/express");
const { SQLiteStorage } = require("@gofynd/fdk-extension-javascript/express/storage");
const sqliteInstance = new sqlite3.Database('session_storage.db');
const productRouter = express.Router();
const fdkExtension = setupFdk({
api_key: process.env.EXTENSION_API_KEY,
api_secret: process.env.EXTENSION_API_SECRET,
base_url: process.env.EXTENSION_BASE_URL,
cluster: process.env.FP_API_DOMAIN,
callbacks: {
auth: async (req) => {
// Write you code here to return initial launch url after auth process complete
if (req.query.application_id)
return `${req.extension.base_url}/company/${req.query['company_id']}/application/${req.query.application_id}`;
else
return `${req.extension.base_url}/company/${req.query['company_id']}`;
},
uninstall: async (req) => {
// Write your code here to cleanup data related to extension
// If task is time taking then process it async on other process.
}
},
storage: new SQLiteStorage(sqliteInstance,"exapmple-fynd-platform-extension"), // add your prefix
access_mode: "online",
webhook_config: {
api_path: "/api/webhook-events",
notification_email: "useremail@example.com",
event_map: {
"company/product/delete": {
"handler": (eventName) => { console.log(eventName)},
"version": '1'
}
}
},
});
const STATIC_PATH = process.env.NODE_ENV === 'production'
? path.join(process.cwd(), 'frontend', 'public' , 'dist')
: path.join(process.cwd(), 'frontend');
const app = express();
const platformApiRoutes = fdkExtension.platformApiRoutes;
// Middleware to parse cookies with a secret key
app.use(cookieParser("ext.session"));
// Middleware to parse JSON bodies with a size limit of 2mb
app.use(bodyParser.json({
limit: '2mb'
}));
// Serve static files from the React dist directory
app.use(serveStatic(STATIC_PATH, { index: false }));
// FDK extension handler and API routes (extension launch routes)
app.use("/", fdkExtension.fdkHandler);
// Route to handle webhook events and process it.
app.post('/api/webhook-events', async function(req, res) {
try {
console.log(`Webhook Event: ${req.body.event} received`)
await fdkExtension.webhookRegistry.processWebhook(req);
return res.status(200).json({"success": true});
} catch(err) {
console.log(`Error Processing ${req.body.event} Webhook`);
return res.status(500).json({"success": false});
}
})
productRouter.get('/', async function view(req, res, next) {
try {
const {
platformClient
} = req;
const data = await platformClient.catalog.getProducts()
return res.json(data);
} catch (err) {
next(err);
}
});
// Get products list for application
productRouter.get('/application/:application_id', async function view(req, res, next) {
try {
const {
platformClient
} = req;
const { application_id } = req.params;
const data = await platformClient.application(application_id).catalog.getAppProducts()
return res.json(data);
} catch (err) {
next(err);
}
});
// FDK extension api route which has auth middleware and FDK client instance attached to it.
platformApiRoutes.use('/products', productRouter);
// If you are adding routes outside of the /api path,
// remember to also add a proxy rule for them in /frontend/vite.config.js
app.use('/api', platformApiRoutes);
// Serve the React app for all other routes
app.get('*', (req, res) => {
return res
.status(200)
.set("Content-Type", "text/html")
.send(readFileSync(path.join(STATIC_PATH, "index.html")));
});
module.exports = app;