-
Notifications
You must be signed in to change notification settings - Fork 4
[WIP] Add multi token SDK setup support #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6d53b63
44b45f1
f773b90
959d012
bd68dcc
fd0fc24
546e0a2
55d36d9
5134791
d9d3a97
07576db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,20 @@ | ||
| 'use strict'; | ||
| const { extension } = require('./extension'); | ||
| const express = require('express'); | ||
| const { sessionMiddleware } = require('./middleware/session_middleware'); | ||
| const { ApplicationConfig, ApplicationClient } = require("@gofynd/fdk-client-javascript"); | ||
| const { ExtensionFactory } = require('./extension_factory'); | ||
|
|
||
|
|
||
| function setupProxyRoutes() { | ||
| function setupProxyRoutes(extension) { | ||
| const apiRoutes = express.Router({ mergeParams: true }); | ||
| const applicationProxyRoutes = express.Router({ mergeParams: true }); | ||
|
|
||
| applicationProxyRoutes.use(async (req, res, next) => { | ||
| applicationProxyRoutes.use( "/" ,async (req, res, next) => { | ||
| try { | ||
| const clusterId = req.query.cluster_domain || req.query.cluster_url?.replace("https://", "").replace("http://", ""); | ||
| if (clusterId) { | ||
| extension = ExtensionFactory.getExtension(clusterId) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ExtensionFactory import is missing |
||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Injection of extension instance in the request object will be helpful to get the cluster_id |
||
| if(req.headers["x-user-data"]) { | ||
| req.user = JSON.parse(req.headers["x-user-data"]); | ||
| req.user.user_id = req.user._id; | ||
|
|
@@ -30,8 +34,12 @@ function setupProxyRoutes() { | |
| } | ||
| }); | ||
|
|
||
| apiRoutes.use(sessionMiddleware(true), async (req, res, next) => { | ||
| apiRoutes.use( "/" , sessionMiddleware(extension, true), async (req, res, next) => { | ||
| try { | ||
| const clusterId = req.query.cluster_domain || req.query.cluster_url?.replace("https://", "").replace("http://", ""); | ||
| if (clusterId) { | ||
| extension = ExtensionFactory.getExtension(clusterId) | ||
| } | ||
| const client = await extension.getPlatformClient(req.fdkSession.company_id, req.fdkSession); | ||
| req.platformClient = client; | ||
| req.extension = extension; | ||
brijeshgajjarfynd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| const { FdkInvalidCluster } = require("./error_code"); | ||
| const { Extension } = require("./extension") | ||
|
|
||
| class ExtensionFactory { | ||
| static _extensionMap = {}; | ||
| static _defaultExt = null; | ||
|
|
||
| static getExtension(clusterId) { | ||
| const clusterExt = ExtensionFactory._extensionMap[clusterId] | ||
| if (clusterId !== null && !clusterExt) { | ||
| throw new FdkInvalidCluster(`Extension instance not found for clusterId ${clusterId}`); | ||
| } | ||
| return clusterExt; | ||
| } | ||
|
|
||
| static defaultExtInstance() { | ||
| return ExtensionFactory._defaultExt; | ||
| } | ||
|
|
||
| static async initializeExtension(clusterData, clusterId = null) { | ||
| const promises = []; | ||
| for (let extConfig of clusterData) { | ||
| const extInstance = new Extension(); | ||
| if(clusterId != null && clusterId !== extConfig.cluster_id) { | ||
| continue; | ||
| } | ||
| if (!ExtensionFactory._defaultExt) { | ||
| ExtensionFactory._defaultExt = extInstance; | ||
| } | ||
| const cluster_id = clusterId || extConfig.cluster.replace("https://", "").replace("http://", ""); | ||
| ExtensionFactory._extensionMap[cluster_id] = extInstance; | ||
| promises.push(extInstance.initialize(extConfig)) | ||
| } | ||
| return Promise.all(promises); | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| ExtensionFactory | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.