• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gofynd / example-extension-javascript / 10484846596

21 Aug 2024 06:39AM UTC coverage: 38.947% (-2.4%) from 41.379%
10484846596

push

github

web-flow
Add webhook support in boilerplate (#21)

* Add webhook support in boilerplate

* Update webhook path

* Update extension lib version to v0.7.9

1 of 13 branches covered (7.69%)

Branch coverage included in aggregate %.

1 of 8 new or added lines in 1 file covered. (12.5%)

36 of 82 relevant lines covered (43.9%)

4.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.15
/server.js
1
const express = require('express');
9✔
2
const cookieParser = require('cookie-parser');
9✔
3
const bodyParser = require('body-parser');
9✔
4
const path = require("path");
9✔
5
const sqlite3 = require('sqlite3').verbose();
9✔
6
const serveStatic = require("serve-static");
9✔
7
const { readFileSync } = require('fs');
9✔
8
const { setupFdk } = require("fdk-extension-javascript/express");
9✔
9
const { SQLiteStorage } = require("fdk-extension-javascript/express/storage");
9✔
10
const sqliteInstance = new sqlite3.Database('session_storage.db');
9✔
11
const productRouter = express.Router();
9✔
12

13

14
const fdkExtension = setupFdk({
9✔
15
    api_key: process.env.EXTENSION_API_KEY,
16
    api_secret: process.env.EXTENSION_API_SECRET,
17
    base_url: process.env.EXTENSION_BASE_URL,
18
    callbacks: {
19
        auth: async (req) => {
20
            // Write you code here to return initial launch url after auth process complete
21
            if (req.query.application_id)
×
22
                return `${req.extension.base_url}/company/${req.query['company_id']}/application/${req.query.application_id}`;
×
23
            else
24
                return `${req.extension.base_url}/company/${req.query['company_id']}`;
×
25
        },
26
        
27
        uninstall: async (req) => {
28
            // Write your code here to cleanup data related to extension
29
            // If task is time taking then process it async on other process.
30
        }
31
    },
32
    storage: new SQLiteStorage(sqliteInstance,"exapmple-fynd-platform-extension"), // add your prefix
33
    access_mode: "online",
34
    webhook_config: {
35
        api_path: "/api/webhook-events",
36
        notification_email: "dev@fynd.com",
37
        event_map: {
38
            "company/product/delete": {
NEW
39
                "handler": (eventName) => {  console.log(eventName)},
×
40
                "version": '1'
41
            }
42
        }
43
    },
44
});
45

46
const STATIC_PATH = process.env.NODE_ENV === 'production'
9!
47
    ? path.join(process.cwd(), 'frontend', 'dist')
48
    : path.join(process.cwd(), 'frontend');
49
    
50
const app = express();
9✔
51
const platformApiRoutes = fdkExtension.platformApiRoutes;
9✔
52

53
// Middleware to parse cookies with a secret key
54
app.use(cookieParser("ext.session"));
9✔
55

56
// Middleware to parse JSON bodies with a size limit of 2mb
57
app.use(bodyParser.json({
9✔
58
    limit: '2mb'
59
}));
60

61
// Serve static files from the Vue dist directory
62
app.use(serveStatic(STATIC_PATH, { index: false }));
9✔
63

64
// FDK extension handler and API routes (extension launch routes)
65
app.use("/", fdkExtension.fdkHandler);
9✔
66

67
// Route to handle webhook events and process it.
68
app.post('/api/webhook-events', async function(req, res) {
9✔
NEW
69
    try {
×
NEW
70
      console.log(`Webhook Event: ${req.body.event} received`)
×
NEW
71
      await fdkExtension.webhookRegistry.processWebhook(req);
×
NEW
72
      return res.status(200).json({"success": true});
×
73
    } catch(err) {
NEW
74
      console.log(`Error Processing ${req.body.event} Webhook`);
×
NEW
75
      return res.status(500).json({"success": false});
×
76
    }
77
})
78

79
productRouter.get('/', async function view(req, res, next) {
9✔
80
    try {
18✔
81
        const {
82
            platformClient
83
        } = req;
18✔
84
        const data = await platformClient.catalog.getProducts()
18✔
85
        return res.json(data);
9✔
86
    } catch (err) {
87
        next(err);
9✔
88
    }
89
});
90

91
// Get products list for application
92
productRouter.get('/application/:application_id', async function view(req, res, next) {
9✔
93
    try {
9✔
94
        const {
95
            platformClient
96
        } = req;
9✔
97
        const { application_id } = req.params;
9✔
98
        const data = await platformClient.application(application_id).catalog.getAppProducts()
9✔
99
        return res.json(data);
9✔
100
    } catch (err) {
101
        next(err);
×
102
    }
103
});
104

105
// FDK extension api route which has auth middleware and FDK client instance attached to it.
106
platformApiRoutes.use('/products', productRouter);
9✔
107

108
// If you are adding routes outside of the /api path, 
109
// remember to also add a proxy rule for them in /frontend/vite.config.js
110
app.use('/api', platformApiRoutes);
9✔
111

112
// Serve the Vue app for all other routes
113
app.get('*', (req, res) => {
9✔
114
    return res
×
115
    .status(200)
116
    .set("Content-Type", "text/html")
117
    .send(readFileSync(path.join(STATIC_PATH, "index.html")));
118
});
119

120
module.exports = app;
9✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc