1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-05 17:53:12 +02:00

Add schema to basic events endpoint

This commit is contained in:
Thomas Heartman 2022-06-27 13:09:52 +02:00
parent 718a1b4906
commit 472e300386
4 changed files with 52 additions and 5 deletions

View File

@ -0,0 +1,16 @@
interface ISchemaObject {
[k: string]: IComponentSchema;
}
interface IComponentSchema {
components: { schemas?: ISchemaObject };
}
export const includeSchemasRecursively = (
schemas: ISchemaObject,
): ISchemaObject =>
Object.entries(schemas).reduce(([key, value], acc) => ({
...acc,
[key]: value,
...includeSchemasRecursively(value.components.schemas),
}));

View File

@ -1,5 +1,6 @@
import { FromSchema } from 'json-schema-to-ts'; import { FromSchema } from 'json-schema-to-ts';
import { eventSchema } from './event-schema'; import { eventSchema } from './event-schema';
import { includeSchemasRecursively } from '../nested-schemas';
export const eventsSchema = { export const eventsSchema = {
$id: '#/components/schemas/eventsSchema', $id: '#/components/schemas/eventsSchema',
@ -17,9 +18,9 @@ export const eventsSchema = {
}, },
}, },
components: { components: {
schemas: { schemas: includeSchemasRecursively({
eventSchema, eventSchema,
}, }),
}, },
} as const; } as const;

View File

@ -0,0 +1,26 @@
import { FromSchema } from 'json-schema-to-ts';
import { eventSchema } from './event-schema';
export const featureEventsSchema = {
$id: '#/components/schemas/featureEventsSchema',
type: 'object',
additionalProperties: false,
required: ['toggleName', 'events'],
properties: {
toggleName: {
type: 'string',
},
events: {
type: 'array',
items: { $ref: eventSchema.$id },
},
},
components: {
schemas: {
eventSchema,
...eventSchema.components.schemas,
},
},
} as const;
export type FeatureEventsSchema = FromSchema<typeof featureEventsSchema>;

View File

@ -7,8 +7,12 @@ import { IEvent } from '../../types/events';
import Controller from '../controller'; import Controller from '../controller';
import { anonymise } from '../../util/anonymise'; import { anonymise } from '../../util/anonymise';
import { OpenApiService } from '../../services/openapi-service'; import { OpenApiService } from '../../services/openapi-service';
import { createResponseSchema } from 'lib/openapi'; import { createResponseSchema } from '../../../lib/openapi';
import { eventsSchema, EventsSchema } from 'lib/openapi/spec/events-schema'; import {
eventsSchema,
EventsSchema,
} from '../../../lib/openapi/spec/events-schema';
import { serializeDates } from '../../../lib/types/serialize-dates';
const version = 1; const version = 1;
export default class EventController extends Controller { export default class EventController extends Controller {
@ -73,7 +77,7 @@ export default class EventController extends Controller {
const response: EventsSchema = { const response: EventsSchema = {
version, version,
events: this.fixEvents(events), events: serializeDates(this.fixEvents(events)),
}; };
this.openApiService.respondWithValidation( this.openApiService.respondWithValidation(
200, 200,