UNPKG

1.92 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const NodeCache = require("node-cache");
4const RequestProcessor_1 = require("../internal/transport/RequestProcessor");
5const logger_1 = require("../internal/util/logger");
6const ApolloGraphClient_1 = require("./ApolloGraphClient");
7/**
8 * Factory for creating GraphClient instances for incoming commands and events.
9 *
10 * Uses a cache to store GraphClient instances for 5 mins after which new instances will be given out.
11 */
12class ApolloGraphClientFactory {
13 constructor(configuration, authCallback) {
14 this.configuration = configuration;
15 this.authCallback = authCallback;
16 this.graphClients = new NodeCache({ stdTTL: 1 * 60, checkperiod: 1 * 30, useClones: false });
17 }
18 createGraphClient(event) {
19 let workspaceId;
20 if (RequestProcessor_1.isCommandIncoming(event)) {
21 workspaceId = event.team.id;
22 }
23 else if (RequestProcessor_1.isEventIncoming(event)) {
24 workspaceId = event.extensions.team_id;
25 }
26 let graphClient = this.graphClients.get(workspaceId);
27 if (graphClient) {
28 logger_1.logger.debug("Re-using cached graph client for team '%s'", workspaceId);
29 return graphClient;
30 }
31 else if (this.authCallback) {
32 logger_1.logger.debug("Creating new graph client for team '%s'", workspaceId);
33 graphClient = new ApolloGraphClient_1.ApolloGraphClient(`${this.configuration.endpoints.graphql}/${workspaceId}`, { Authorization: this.authCallback() });
34 this.graphClients.set(workspaceId, graphClient);
35 return graphClient;
36 }
37 logger_1.logger.debug("Unable to create graph client for team '%s'", workspaceId);
38 return null;
39 }
40}
41exports.ApolloGraphClientFactory = ApolloGraphClientFactory;
42//# sourceMappingURL=ApolloGraphClientFactory.js.map
\No newline at end of file