mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-10 17:53:36 +02:00
Add schema to basic events endpoint
This commit is contained in:
parent
718a1b4906
commit
472e300386
16
src/lib/openapi/nested-schemas.ts
Normal file
16
src/lib/openapi/nested-schemas.ts
Normal 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),
|
||||
}));
|
@ -1,5 +1,6 @@
|
||||
import { FromSchema } from 'json-schema-to-ts';
|
||||
import { eventSchema } from './event-schema';
|
||||
import { includeSchemasRecursively } from '../nested-schemas';
|
||||
|
||||
export const eventsSchema = {
|
||||
$id: '#/components/schemas/eventsSchema',
|
||||
@ -17,9 +18,9 @@ export const eventsSchema = {
|
||||
},
|
||||
},
|
||||
components: {
|
||||
schemas: {
|
||||
schemas: includeSchemasRecursively({
|
||||
eventSchema,
|
||||
},
|
||||
}),
|
||||
},
|
||||
} as const;
|
||||
|
||||
|
26
src/lib/openapi/spec/feature-events-schema.ts
Normal file
26
src/lib/openapi/spec/feature-events-schema.ts
Normal 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>;
|
@ -7,8 +7,12 @@ import { IEvent } from '../../types/events';
|
||||
import Controller from '../controller';
|
||||
import { anonymise } from '../../util/anonymise';
|
||||
import { OpenApiService } from '../../services/openapi-service';
|
||||
import { createResponseSchema } from 'lib/openapi';
|
||||
import { eventsSchema, EventsSchema } from 'lib/openapi/spec/events-schema';
|
||||
import { createResponseSchema } from '../../../lib/openapi';
|
||||
import {
|
||||
eventsSchema,
|
||||
EventsSchema,
|
||||
} from '../../../lib/openapi/spec/events-schema';
|
||||
import { serializeDates } from '../../../lib/types/serialize-dates';
|
||||
|
||||
const version = 1;
|
||||
export default class EventController extends Controller {
|
||||
@ -73,7 +77,7 @@ export default class EventController extends Controller {
|
||||
|
||||
const response: EventsSchema = {
|
||||
version,
|
||||
events: this.fixEvents(events),
|
||||
events: serializeDates(this.fixEvents(events)),
|
||||
};
|
||||
this.openApiService.respondWithValidation(
|
||||
200,
|
||||
|
Loading…
Reference in New Issue
Block a user