UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.

94 lines 3.43 kB
import Addon from './addon.js'; import teamsDefinition from './teams-definition.js'; import { serializeDates, } from '../types/index.js'; import { FeatureEventFormatterMd, } from './feature-event-formatter-md.js'; export default class TeamsAddon extends Addon { constructor(args) { super(teamsDefinition, args); this.msgFormatter = new FeatureEventFormatterMd({ unleashUrl: args.unleashUrl, }); this.flagResolver = args.flagResolver; } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types async handleEvent(event, parameters, integrationId) { let state = 'success'; const stateDetails = []; const { url, customHeaders } = parameters; const { createdBy } = event; const { text, url: featureLink } = this.msgFormatter.format(event); const body = { themeColor: '0076D7', summary: 'Message', sections: [ { activityTitle: text, activitySubtitle: 'Unleash notification update', facts: [ { name: 'User', value: createdBy, }, { name: 'Action', value: event.type, }, ], }, ], potentialAction: [ { '@type': 'OpenUri', name: 'Go to feature', targets: [ { os: 'default', uri: featureLink, }, ], }, ], }; let extraHeaders = {}; if (typeof customHeaders === 'string' && customHeaders.length > 1) { try { extraHeaders = JSON.parse(customHeaders); } catch (e) { state = 'successWithErrors'; const badHeadersMessage = 'Could not parse the JSON in the customHeaders parameter.'; stateDetails.push(badHeadersMessage); this.logger.warn(badHeadersMessage); } } const requestOpts = { method: 'POST', headers: { 'Content-Type': 'application/json', ...extraHeaders }, body: JSON.stringify(body), }; const res = await this.fetchRetry(url, requestOpts); this.logger.info(`Handled event "${event.type}".`); if (res.ok) { const successMessage = `Teams webhook request was successful with status code: ${res.status}.`; stateDetails.push(successMessage); this.logger.info(successMessage); } else { state = 'failed'; const failedMessage = `Teams webhook request failed with status code: ${res.status}.`; stateDetails.push(failedMessage); this.logger.warn(failedMessage); } this.registerEvent({ integrationId, state, stateDetails: stateDetails.join('\n'), event: serializeDates(event), details: { url, body, }, }); } } //# sourceMappingURL=teams.js.map