From 19d1da8fafe36fb3d877000956c9502140e305d0 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Mon, 27 Jun 2022 13:53:41 +0200 Subject: [PATCH] Feat: Add openapi for feature events --- src/lib/openapi/index.ts | 2 ++ src/lib/openapi/nested-schemas.ts | 4 +-- src/lib/openapi/spec/feature-events-schema.ts | 6 ++-- src/lib/routes/admin-api/event.ts | 35 ++++++++++++++++--- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/lib/openapi/index.ts b/src/lib/openapi/index.ts index 03460ca721..b400a75454 100644 --- a/src/lib/openapi/index.ts +++ b/src/lib/openapi/index.ts @@ -84,6 +84,7 @@ import { strategiesSchema } from './spec/strategies-schema'; import { upsertStrategySchema } from './spec/upsert-strategy-schema'; import { eventSchema } from './spec/event-schema'; import { eventsSchema } from './spec/events-schema'; +import { featureEventsSchema } from './spec/feature-events-schema'; import { clientApplicationSchema } from './spec/client-application-schema'; // All schemas in `openapi/spec` should be listed here. @@ -114,6 +115,7 @@ export const schemas = { exportParametersSchema, featureEnvironmentSchema, featureEnvironmentMetricsSchema, + featureEventsSchema, featureSchema, featureMetricsSchema, featureUsageSchema, diff --git a/src/lib/openapi/nested-schemas.ts b/src/lib/openapi/nested-schemas.ts index 4769caeb23..36c94d6184 100644 --- a/src/lib/openapi/nested-schemas.ts +++ b/src/lib/openapi/nested-schemas.ts @@ -1,4 +1,4 @@ -interface ISchemaObject { +export interface ISchemaObject { [k: string]: IComponentSchema; } @@ -8,7 +8,7 @@ interface IComponentSchema { export const includeSchemasRecursively = ( schemas: ISchemaObject, -): ISchemaObject => +): { [key: string]: ISchemaObject } => Object.entries(schemas).reduce(([key, value], acc) => ({ ...acc, [key]: value, diff --git a/src/lib/openapi/spec/feature-events-schema.ts b/src/lib/openapi/spec/feature-events-schema.ts index c60bae58eb..376d9c9ca1 100644 --- a/src/lib/openapi/spec/feature-events-schema.ts +++ b/src/lib/openapi/spec/feature-events-schema.ts @@ -1,4 +1,5 @@ import { FromSchema } from 'json-schema-to-ts'; +import { includeSchemasRecursively } from '../nested-schemas'; import { eventSchema } from './event-schema'; export const featureEventsSchema = { @@ -16,10 +17,9 @@ export const featureEventsSchema = { }, }, components: { - schemas: { + schemas: includeSchemasRecursively({ eventSchema, - ...eventSchema.components.schemas, - }, + }), }, } as const; diff --git a/src/lib/routes/admin-api/event.ts b/src/lib/routes/admin-api/event.ts index 94d86f03b5..44f85b8ae8 100644 --- a/src/lib/routes/admin-api/event.ts +++ b/src/lib/routes/admin-api/event.ts @@ -13,6 +13,10 @@ import { EventsSchema, } from '../../../lib/openapi/spec/events-schema'; import { serializeDates } from '../../../lib/types/serialize-dates'; +import { + featureEventsSchema, + FeatureEventsSchema, +} from '../../../lib/openapi/spec/feature-events-schema'; const version = 1; export default class EventController extends Controller { @@ -50,7 +54,21 @@ export default class EventController extends Controller { ], }); - this.get('/:name', this.getEventsForToggle); + this.route({ + method: 'get', + path: '/:name', + handler: this.getEventsForToggle, + permission: ADMIN, + middleware: [ + openApiService.validPath({ + operationId: 'getEventsForToggle', + tags: ['admin'], + responses: { + 200: createResponseSchema('featureEventsSchema'), + }, + }), + ], + }); } fixEvents(events: IEvent[]): IEvent[] { @@ -89,15 +107,22 @@ export default class EventController extends Controller { async getEventsForToggle( req: Request<{ name: string }>, - res: Response, + res: Response, ): Promise { const toggleName = req.params.name; const events = await this.eventService.getEventsForToggle(toggleName); - res.json({ + const response = { version, toggleName, - events: this.fixEvents(events), - }); + events: serializeDates(this.fixEvents(events)), + }; + + this.openApiService.respondWithValidation( + 200, + res, + featureEventsSchema.$id, + response, + ); } }