UNPKG

@atomist/automation-client

Version:
42 lines 1.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const NodeCache = require("node-cache"); const RequestProcessor_1 = require("../internal/transport/RequestProcessor"); const logger_1 = require("../internal/util/logger"); const ApolloGraphClient_1 = require("./ApolloGraphClient"); /** * Factory for creating GraphClient instances for incoming commands and events. * * Uses a cache to store GraphClient instances for 5 mins after which new instances will be given out. */ class ApolloGraphClientFactory { constructor(configuration, authCallback) { this.configuration = configuration; this.authCallback = authCallback; this.graphClients = new NodeCache({ stdTTL: 1 * 60, checkperiod: 1 * 30, useClones: false }); } createGraphClient(event) { let workspaceId; if (RequestProcessor_1.isCommandIncoming(event)) { workspaceId = event.team.id; } else if (RequestProcessor_1.isEventIncoming(event)) { workspaceId = event.extensions.team_id; } let graphClient = this.graphClients.get(workspaceId); if (graphClient) { logger_1.logger.debug("Re-using cached graph client for team '%s'", workspaceId); return graphClient; } else if (this.authCallback) { logger_1.logger.debug("Creating new graph client for team '%s'", workspaceId); graphClient = new ApolloGraphClient_1.ApolloGraphClient(`${this.configuration.endpoints.graphql}/${workspaceId}`, { Authorization: this.authCallback() }); this.graphClients.set(workspaceId, graphClient); return graphClient; } logger_1.logger.debug("Unable to create graph client for team '%s'", workspaceId); return null; } } exports.ApolloGraphClientFactory = ApolloGraphClientFactory; //# sourceMappingURL=ApolloGraphClientFactory.js.map