diff --git a/README.md b/README.md index 91555583..f1c14d66 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,6 @@ FDK Extension Helper Library const redis = new Redis(); let extensionHandler = { - setup: async function(data) { - console.log("called setup callback"); - }, - install: async function(data) { console.log("called install callback"); }, @@ -37,22 +33,17 @@ FDK Extension Helper Library let baseUrl = "https://test.extension.com"; - let FDKClient = setupFdk({ - name: "Transformer", + let fdkClient = setupFdk({ + api_key: "", + api_secret: "", base_url: baseUrl, - logo : { - small: `${baseUrl}\icon.png`, - large: `${baseUrl}\icon.png`, - }, scopes: ["company/products"], - prefix_path: "/callback", callbacks: extensionHandler, - contact_email: "xyz@gmail.com", - developed_by_name: "Fynd", - webhooks: [], - storage: new RedisStorage(redis) + storage: new RedisStorage(redis), + access_mode: "offline", + cluster: "https://api.fyndx0.de" // this is optional by default it points to prod. }); - app.use(FDKClient.fdkHandler); + app.use(fdkClient.fdkHandler); app.listen(8080); ``` diff --git a/examples/test.js b/examples/test.js index 50e8e44d..8fcd757b 100644 --- a/examples/test.js +++ b/examples/test.js @@ -23,7 +23,7 @@ console.log(baseUrl); const redis = new Redis(); -let FDKExtension = setupFdk({ +let fdkExtension = setupFdk({ api_key: "000001", api_secret: "tetsjskdjalsjdl", base_url: baseUrl, @@ -34,14 +34,14 @@ let FDKExtension = setupFdk({ cluster: "https://api.fyndx0.de" // this is optional by default it points to prod. }); -app.use(FDKExtension.fdkHandler); +app.use(fdkExtension.fdkHandler); app.use('/_healthz', (req, res, next) => { res.json({ "ok": "ok" }); }); -FDKExtension.apiRoutes.get("/test/routes", async (req, res, next) => { +fdkExtension.apiRoutes.get("/test/routes", async (req, res, next) => { try { let data = await req.platformClient.lead.getTickets(); res.json(data); @@ -52,7 +52,7 @@ FDKExtension.apiRoutes.get("/test/routes", async (req, res, next) => { }); -FDKExtension.applicationProxyRoutes.get("/1234", async (req, res, next) => { +fdkExtension.applicationProxyRoutes.get("/1234", async (req, res, next) => { try { let data = await req.platformClient.lead.getTickets(); res.json(data); @@ -63,25 +63,8 @@ FDKExtension.applicationProxyRoutes.get("/1234", async (req, res, next) => { }); -app.use(FDKExtension.applicationProxyRoutes); -app.use(FDKExtension.apiRoutes); - -// FDKExtension.apiRoutes.post("/:application_id", async (req, res, next) => { -// try { -// const { application_id } = req.params; -// const { platformClient } = req; -// const platformApiUrl = `https://api.fyndx0.de/service/platform/partners/v1.0/company/${req.fdkSession.company_id}/application/${application_id}/proxy/${platformClient.config.apiKey}`; -// response = await APIClient.execute(platformClient.config, 'post', platformApiUrl, {}, { -// attached_path: "chatbot-test", -// proxy_url: `https://chatbots.extensions.fyndx0.de/proxy/application` -// }); -// return res.json(response); -// } -// catch(err) { -// next(err); -// } -// }); - +app.use(fdkExtension.applicationProxyRoutes); +app.use(fdkExtension.apiRoutes); // sample webhook endpoint const webhookRouter = express.Router({ mergeParams: true }); @@ -89,7 +72,7 @@ webhookRouter.get("/webhook", async (req, res, next) => { // fetch company id from query params let companyId = req.query.companyId; let cluster = "https://api.fyndx0.de"; // either take it from some env variables like "https://api.fyndx0.de" - let client = await FDKExtension.getPlatformClient(cluster, companyId); + let client = await fdkExtension.getPlatformClient(cluster, companyId); res.json({"success": true}); }); diff --git a/express/api_routes.js b/express/api_routes.js index c1df43ec..55acf630 100644 --- a/express/api_routes.js +++ b/express/api_routes.js @@ -31,7 +31,7 @@ function setupProxyRoutes() { apiRoutes.use(sessionMiddleware(true), async (req, res, next) => { try { - const client = extension.getPlatformClient(req.fdkSession.company_id, req.fdkSession); + const client = await extension.getPlatformClient(req.fdkSession.company_id, req.fdkSession); req.platformClient = client; req.extension = extension; next(); diff --git a/express/extension.js b/express/extension.js index 9f4016cb..142c41b1 100644 --- a/express/extension.js +++ b/express/extension.js @@ -75,9 +75,10 @@ class Extension { return platformConfig; } - getPlatformClient(companyId, session) { + async getPlatformClient(companyId, session) { let platformConfig = this.getPlatformConfig(companyId); platformConfig.oauthClient.setToken(session); + await platformConfig.oauthClient.renewAccessToken(); return new PlatformClient(platformConfig); } } diff --git a/express/index.js b/express/index.js index 72202af0..9cef68e9 100644 --- a/express/index.js +++ b/express/index.js @@ -22,7 +22,7 @@ function setupFdk(data) { companyId: companyId }); let session = await SessionStorage.getSession(sid); - client = extension.getPlatformClient(companyId, session); + client = await extension.getPlatformClient(companyId, session); } return client; } diff --git a/express/routes.js b/express/routes.js index db7e3030..ebb2a9ec 100644 --- a/express/routes.js +++ b/express/routes.js @@ -155,7 +155,7 @@ function setupRoutes(ext) { companyId: company_id }); let session = await SessionStorage.getSession(sid); - const client = ext.getPlatformClient(company_id, session); + const client = await ext.getPlatformClient(company_id, session); req.platformClient = client; } req.extension = ext;