From 056b8082b2e21abfb97f3a820cb8d0b701fb9ef8 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Wed, 29 Jun 2022 13:41:42 +0200 Subject: [PATCH] Feat: start adding detailed event descriptions per event --- src/lib/openapi/index.ts | 12 ++- src/lib/openapi/spec/event-schema.ts | 107 +++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/src/lib/openapi/index.ts b/src/lib/openapi/index.ts index b400a75454..f4da759d66 100644 --- a/src/lib/openapi/index.ts +++ b/src/lib/openapi/index.ts @@ -82,7 +82,13 @@ import { emailSchema } from './spec/email-schema'; import { strategySchema } from './spec/strategy-schema'; import { strategiesSchema } from './spec/strategies-schema'; import { upsertStrategySchema } from './spec/upsert-strategy-schema'; -import { eventSchema } from './spec/event-schema'; +import { + eventSchema, + eventCollectedSchema, + featureCreatedEventSchema, + eventBaseSchema, + featureUpdatedEventSchema, +} from './spec/event-schema'; import { eventsSchema } from './spec/events-schema'; import { featureEventsSchema } from './spec/feature-events-schema'; import { clientApplicationSchema } from './spec/client-application-schema'; @@ -111,6 +117,10 @@ export const schemas = { environmentSchema, environmentsSchema, eventSchema, + eventCollectedSchema, + featureCreatedEventSchema, + featureUpdatedEventSchema, + eventBaseSchema, eventsSchema, exportParametersSchema, featureEnvironmentSchema, diff --git a/src/lib/openapi/spec/event-schema.ts b/src/lib/openapi/spec/event-schema.ts index 056794622b..d5c5b7e222 100644 --- a/src/lib/openapi/spec/event-schema.ts +++ b/src/lib/openapi/spec/event-schema.ts @@ -1,4 +1,5 @@ import { FromSchema } from 'json-schema-to-ts'; +import { FEATURE_CREATED, FEATURE_UPDATED } from '../../../lib/types/events'; import { tagSchema } from './tag-schema'; export const eventSchema = { @@ -51,3 +52,109 @@ export const eventSchema = { } as const; export type EventSchema = FromSchema; + +export const eventBaseSchema = { + $id: '#/components/schemas/eventBaseSchema', + type: 'object', + additionalProperties: false, + required: ['id', 'createdAt', 'type', 'createdBy'], + properties: { + id: { + type: 'integer', + minimum: 1, + description: 'The ID of the event.', + example: 42, + }, + createdAt: { + type: 'string', + format: 'date-time', + description: 'When the event ocurred.', + example: '2022-06-29T13:29:48Z', + }, + createdBy: { + type: 'string', + description: + 'The username or email of the person who triggered the event.', + example: 'user@company.com', + }, + }, + components: { + schemas: { + tagSchema, + }, + }, +} as const; + +const getEventTypeProperty = (eventType: string) => ({ + type: 'string', + enum: [eventType], + description: 'The type of the event.', +}); + +// const createEvent = () => ({ +// $id: '#/components/schemas/featureCreatedEventSchema', +// allOf: [ +// { $ref: eventBaseSchema.$id }, +// { +// type: 'object', +// additionalProperties: false, +// required: ['type', ...requiredProps], +// description: +// 'This event fires when you create a feature. The `data` property contains the details for the new feature.', +// properties: { +// type: getEventTypeProperty(eventType), +// }, +// }, +// ], +// components: {}, +// }) + +export const featureCreatedEventSchema = { + $id: '#/components/schemas/featureCreatedEventSchema', + allOf: [ + { $ref: eventBaseSchema.$id }, + { + type: 'object', + additionalProperties: false, + required: ['type'], + description: + 'This event fires when you create a feature. The `data` property contains the details for the new feature.', + properties: { + type: getEventTypeProperty(FEATURE_CREATED), + }, + }, + ], + components: {}, +}; + +export const featureUpdatedEventSchema = { + $id: '#/components/schemas/featureUpdatedEventSchema', + allOf: [ + { $ref: eventBaseSchema.$id }, + { + type: 'object', + additionalProperties: false, + required: ['type'], + deprecated: true, + description: + 'This event fires when a feature gets updated in some way. The `data` property contains the new state of the toggle. This is a legacy event, so it does not populate `preData` property.\n\nThis event type was replaced by more granular event types in Unleash 4.3.', + properties: { + type: getEventTypeProperty(FEATURE_UPDATED), + }, + }, + ], + components: {}, +}; + +export const eventCollectedSchema = { + $id: '#/components/schemas/eventCollectedSchema', + oneOf: [{ $ref: featureCreatedEventSchema.$id }], + components: { + schemas: { + tagSchema, + featureCreatedEventSchema, + }, + }, +} as const; + +export type EventCollectedSchema = FromSchema;