UNPKG

unleash-server

Version:

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

159 lines • 7.7 kB
import Controller from '../../routes/controller.js'; import { NONE, } from '../../types/index.js'; import { createRequestSchema, createResponseSchema, emptyResponse, frontendApiFeaturesSchema, getStandardResponses, } from '../../openapi/index.js'; import { enrichContextWithIp } from './index.js'; import NotImplementedError from '../../error/not-implemented-error.js'; import rateLimit from 'express-rate-limit'; import { minutesToMilliseconds } from 'date-fns'; import metricsHelper from '../../util/metrics-helper.js'; import { FUNCTION_TIME } from '../../metric-events.js'; export default class FrontendAPIController extends Controller { constructor(config, services) { super(config); this.logger = config.getLogger('frontend-api-controller.ts'); this.services = services; this.flagResolver = config.flagResolver; this.timer = (functionName) => metricsHelper.wrapTimer(config.eventBus, FUNCTION_TIME, { className: 'FrontendAPIController', functionName, }); this.route({ method: 'get', path: '', handler: this.getFrontendApiFeatures, permission: NONE, middleware: [ this.services.openApiService.validPath({ tags: ['Frontend API'], operationId: 'getFrontendFeatures', responses: { 200: createResponseSchema('frontendApiFeaturesSchema'), ...getStandardResponses(401, 404), }, summary: 'Retrieve enabled feature flags for the provided context.', description: 'This endpoint returns the list of feature flags that the frontend API evaluates to enabled for the given context. Context values are provided as query parameters. If the Frontend API is disabled 404 is returned.', }), ], }); this.route({ method: 'post', path: '', handler: this.getFrontendApiFeatures, permission: NONE, middleware: [ this.services.openApiService.validPath({ tags: ['Frontend API'], operationId: 'getFrontendApiFeaturesWithPost', requestBody: createRequestSchema('frontendApiFeaturesPostSchema'), responses: { 200: createResponseSchema('frontendApiFeaturesSchema'), ...getStandardResponses(401, 404), }, summary: 'Retrieve enabled feature flags for the provided context, using POST.', description: 'This endpoint returns the list of feature flags that the frontend API evaluates to enabled for the given context, using POST. Context values are provided as a `context` property in the request body. If the Frontend API is disabled 404 is returned.', }), ], }); this.route({ method: 'get', path: '/client/features', handler: FrontendAPIController.endpointNotImplemented, permission: NONE, }); this.route({ method: 'post', path: '/client/metrics', handler: this.registerFrontendApiMetrics, permission: NONE, middleware: [ this.services.openApiService.validPath({ tags: ['Frontend API'], summary: 'Register client usage metrics', description: `Registers usage metrics. Stores information about how many times each flag was evaluated to enabled and disabled within a time frame. If provided, this operation will also store data on how many times each feature flag's variants were displayed to the end user. If the Frontend API is disabled 404 is returned.`, operationId: 'registerFrontendMetrics', requestBody: createRequestSchema('clientMetricsSchema'), responses: { 200: emptyResponse, 204: emptyResponse, ...getStandardResponses(400, 401, 404), }, }), rateLimit({ windowMs: minutesToMilliseconds(1), max: config.metricsRateLimiting.frontendMetricsMaxPerMinute, validate: false, standardHeaders: true, legacyHeaders: false, }), ], }); this.route({ method: 'post', path: '/client/register', handler: this.registerFrontendApiClient, permission: NONE, middleware: [ this.services.openApiService.validPath({ tags: ['Frontend API'], summary: 'Register a client SDK', description: 'This is for future use. Currently Frontend client registration is not supported. Returning 200 for clients that expect this status code. If the Frontend API is disabled 404 is returned.', operationId: 'registerFrontendClient', requestBody: createRequestSchema('frontendApiClientSchema'), responses: { 200: emptyResponse, ...getStandardResponses(400, 401, 404), }, }), rateLimit({ windowMs: minutesToMilliseconds(1), max: config.metricsRateLimiting .frontendRegisterMaxPerMinute, validate: false, standardHeaders: true, legacyHeaders: false, }), ], }); this.route({ method: 'get', path: '/health', handler: FrontendAPIController.endpointNotImplemented, permission: NONE, }); this.route({ method: 'get', path: '/internal-backstage/prometheus', handler: FrontendAPIController.endpointNotImplemented, permission: NONE, }); } static async endpointNotImplemented(req, res) { const error = new NotImplementedError('The frontend API does not support this endpoint.'); res.status(error.statusCode).json(error); } async getFrontendApiFeatures(req, res) { const toggles = await this.services.frontendApiService.getFrontendApiFeatures(req.user, FrontendAPIController.createContext(req)); res.set('Cache-control', 'no-cache'); this.services.openApiService.respondWithValidation(200, res, frontendApiFeaturesSchema.$id, { toggles }); } async registerFrontendApiMetrics(req, res) { if (this.config.flagResolver.isEnabled('disableMetrics')) { res.sendStatus(204); return; } await this.services.frontendApiService.registerFrontendApiMetrics(req.user, req.body, req.ip, req.headers['unleash-sdk']); res.sendStatus(200); } async registerFrontendApiClient(req, res) { // Client registration is not yet supported by @unleash/proxy, // but proxy clients may still expect a 200 from this endpoint. res.sendStatus(200); } static createContext(req) { const { query, body } = req; const bodyContext = body.context ?? {}; const contextData = req.method === 'POST' ? bodyContext : query; return enrichContextWithIp(contextData, req.ip); } } //# sourceMappingURL=frontend-api-controller.js.map