unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
114 lines • 5.01 kB
JavaScript
import { IAuthType } from '../../types/option.js';
import version from '../../util/version.js';
import Controller from '../controller.js';
import { simpleAuthSettingsKey, } from '../../types/settings/simple-auth-settings.js';
import { ADMIN, NONE, UPDATE_CORS } from '../../types/permissions.js';
import { createResponseSchema } from '../../openapi/util/create-response-schema.js';
import { uiConfigSchema, } from '../../openapi/spec/ui-config-schema.js';
import { emptyResponse } from '../../openapi/util/standard-responses.js';
import NotFoundError from '../../error/notfound-error.js';
import { createRequestSchema } from '../../openapi/util/create-request-schema.js';
class ConfigController extends Controller {
constructor(config, { versionService, settingService, emailService, openApiService, frontendApiService, maintenanceService, clientInstanceService, sessionService, }) {
super(config);
this.versionService = versionService;
this.settingService = settingService;
this.emailService = emailService;
this.openApiService = openApiService;
this.frontendApiService = frontendApiService;
this.maintenanceService = maintenanceService;
this.clientInstanceService = clientInstanceService;
this.sessionService = sessionService;
this.flagResolver = config.flagResolver;
this.route({
method: 'get',
path: '',
handler: this.getUiConfig,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Admin UI'],
summary: 'Get UI configuration',
description: 'Retrieves the full configuration used to set up the Unleash Admin UI.',
operationId: 'getUiConfig',
responses: {
200: createResponseSchema('uiConfigSchema'),
},
}),
],
});
this.route({
method: 'post',
path: '/cors',
handler: this.setCors,
permission: [ADMIN, UPDATE_CORS],
middleware: [
openApiService.validPath({
tags: ['Admin UI'],
summary: 'Sets allowed CORS origins',
description: 'Sets Cross-Origin Resource Sharing headers for Frontend SDK API.',
operationId: 'setCors',
requestBody: createRequestSchema('setCorsSchema'),
responses: { 204: emptyResponse },
}),
],
});
}
async getUiConfig(req, res) {
const getMaxSessionsCount = async () => {
if (this.flagResolver.isEnabled('showUserDeviceCount')) {
return this.sessionService.getMaxSessionsCount();
}
return 0;
};
const [frontendSettings, simpleAuthSettings, maintenanceMode, maxSessionsCount,] = await Promise.all([
this.frontendApiService.getFrontendSettings(false),
this.settingService.get(simpleAuthSettingsKey),
this.maintenanceService.isMaintenanceMode(),
getMaxSessionsCount(),
]);
const disablePasswordAuth = simpleAuthSettings?.disabled ||
this.config.authentication.type === IAuthType.NONE;
const expFlags = this.config.flagResolver.getAll({
email: req.user.email,
});
const flags = {
...this.config.ui.flags,
...expFlags,
};
const unleashContext = {
...this.flagResolver.getStaticContext(), //clientId etc.
email: req.user.email,
userId: req.user.id,
};
const response = {
...this.config.ui,
flags,
version,
emailEnabled: this.emailService.isEnabled(),
unleashUrl: this.config.server.unleashUrl,
baseUriPath: this.config.server.baseUriPath,
authenticationType: this.config.authentication?.type,
frontendApiOrigins: frontendSettings.frontendApiOrigins,
versionInfo: await this.versionService.getVersionInfo(),
prometheusAPIAvailable: this.config.prometheusApi !== undefined,
resourceLimits: this.config.resourceLimits,
disablePasswordAuth,
maintenanceMode,
feedbackUriPath: this.config.feedbackUriPath,
maxSessionsCount,
unleashContext: unleashContext,
};
this.openApiService.respondWithValidation(200, res, uiConfigSchema.$id, response);
}
async setCors(req, res) {
if (req.body.frontendApiOrigins) {
await this.frontendApiService.setFrontendCorsSettings(req.body.frontendApiOrigins, req.audit);
res.sendStatus(204);
return;
}
throw new NotFoundError();
}
}
export default ConfigController;
//# sourceMappingURL=config.js.map