1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00
unleash.unleash/src/lib/routes/admin-api/archive.ts
Thomas Heartman 9448461aaa
docs: prep to add OpenAPI spec to Unleash docs (#1907)
* Docs: start experimenting with OpenAPI and docusaurus

* Docs: add docusaurus-theme-openapi-docs pkg

* Wip: current status

* Docs: Add 'docusaurus-plugin-api-docs'

* Move openapi into own sidebar; generate from localhost

* Chore: Update docusaurus plugin for OpenAPI

* Add website/yarn.lock to git

* Fix: fix CSS warning by using flex-end instead of end

* docs: make openapi generated code work again

* docs: make tags work properly with openapi sidebar

* Docs/chore: update OpenAPI tag scheme.

Add a whole bunch of new tags to make it easier to understand
available tags in OpenAPI.

* docs: point to new openapi docs from old api docs

* docs: typo

* Docs:  link restructure

* docs: add operation indicators to openapi docs

* docs: change badge color for operations

* docs: update openapi-docs package

It now sorts tags the same as the schema

* docs: pluralize APIs in slug

* docs: update links to generated api docs

* docs: update openapi snapshot tests with new tags

* docs: conditionally load spec from localhost or from file

* docs: Remove changes relating to immediate switchover

* refactor: rename types; extract into separate file

* docs: fix api doc links
2022-08-12 11:37:57 +02:00

154 lines
5.0 KiB
TypeScript

import { Request, Response } from 'express';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types';
import { Logger } from '../../logger';
import Controller from '../controller';
import { extractUsername } from '../../util/extract-user';
import { DELETE_FEATURE, NONE, UPDATE_FEATURE } from '../../types/permissions';
import FeatureToggleService from '../../services/feature-toggle-service';
import { IAuthRequest } from '../unleash-types';
import {
featuresSchema,
FeaturesSchema,
} from '../../openapi/spec/features-schema';
import { serializeDates } from '../../types/serialize-dates';
import { OpenApiService } from '../../services/openapi-service';
import { createResponseSchema } from '../../openapi/util/create-response-schema';
import { emptyResponse } from '../../openapi/util/standard-responses';
export default class ArchiveController extends Controller {
private readonly logger: Logger;
private featureService: FeatureToggleService;
private openApiService: OpenApiService;
constructor(
config: IUnleashConfig,
{
featureToggleServiceV2,
openApiService,
}: Pick<IUnleashServices, 'featureToggleServiceV2' | 'openApiService'>,
) {
super(config);
this.logger = config.getLogger('/admin-api/archive.js');
this.featureService = featureToggleServiceV2;
this.openApiService = openApiService;
this.route({
method: 'get',
path: '/features',
handler: this.getArchivedFeatures,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Archive'],
operationId: 'getArchivedFeatures',
responses: { 200: createResponseSchema('featuresSchema') },
deprecated: true,
}),
],
});
this.route({
method: 'get',
path: '/features/:projectId',
handler: this.getArchivedFeaturesByProjectId,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Archive'],
operationId: 'getArchivedFeaturesByProjectId',
responses: { 200: createResponseSchema('featuresSchema') },
deprecated: true,
}),
],
});
this.route({
method: 'delete',
path: '/:featureName',
acceptAnyContentType: true,
handler: this.deleteFeature,
permission: DELETE_FEATURE,
middleware: [
openApiService.validPath({
tags: ['Archive'],
operationId: 'deleteFeature',
responses: { 200: emptyResponse },
}),
],
});
this.route({
method: 'post',
path: '/revive/:featureName',
acceptAnyContentType: true,
handler: this.reviveFeature,
permission: UPDATE_FEATURE,
middleware: [
openApiService.validPath({
tags: ['Archive'],
operationId: 'reviveFeature',
responses: { 200: emptyResponse },
}),
],
});
}
async getArchivedFeatures(
req: Request,
res: Response<FeaturesSchema>,
): Promise<void> {
const features = await this.featureService.getMetadataForAllFeatures(
true,
);
this.openApiService.respondWithValidation(
200,
res,
featuresSchema.$id,
{ version: 2, features: serializeDates(features) },
);
}
async getArchivedFeaturesByProjectId(
req: Request<{ projectId: string }, any, any, any>,
res: Response<FeaturesSchema>,
): Promise<void> {
const { projectId } = req.params;
const features =
await this.featureService.getMetadataForAllFeaturesByProjectId(
true,
projectId,
);
this.openApiService.respondWithValidation(
200,
res,
featuresSchema.$id,
{ version: 2, features: serializeDates(features) },
);
}
async deleteFeature(
req: IAuthRequest<{ featureName: string }>,
res: Response<void>,
): Promise<void> {
const { featureName } = req.params;
const user = extractUsername(req);
await this.featureService.deleteFeature(featureName, user);
res.status(200).end();
}
async reviveFeature(
req: IAuthRequest<{ featureName: string }>,
res: Response<void>,
): Promise<void> {
const userName = extractUsername(req);
const { featureName } = req.params;
await this.featureService.reviveToggle(featureName, userName);
res.status(200).end();
}
}
module.exports = ArchiveController;