microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/events/generated-defs/TypeSpec.Events.ts
75lines · modecode
| 1 | import type { |
| 2 | DecoratorContext, |
| 3 | DecoratorValidatorCallbacks, |
| 4 | ModelProperty, |
| 5 | Union, |
| 6 | UnionVariant, |
| 7 | } from "@typespec/compiler"; |
| 8 | |
| 9 | /** |
| 10 | * Specify that this union describes a set of events. |
| 11 | * |
| 12 | * @example |
| 13 | * ```typespec |
| 14 | * @events |
| 15 | * union MixedEvents { |
| 16 | * pingEvent: string; |
| 17 | * |
| 18 | * doneEvent: "done"; |
| 19 | * } |
| 20 | * ``` |
| 21 | */ |
| 22 | export type EventsDecorator = ( |
| 23 | context: DecoratorContext, |
| 24 | target: Union, |
| 25 | ) => DecoratorValidatorCallbacks | void; |
| 26 | |
| 27 | /** |
| 28 | * Specifies the content type of the event envelope, event body, or event payload. |
| 29 | * When applied to an event payload, that field must also have a corresponding `@data` |
| 30 | * decorator. |
| 31 | * |
| 32 | * @example |
| 33 | * ```typespec |
| 34 | * @events union MixedEvents { |
| 35 | * @contentType("application/json") |
| 36 | * message: { id: string, text: string, } |
| 37 | * } |
| 38 | * ``` |
| 39 | * @example Specify the content type of the event payload. |
| 40 | * |
| 41 | * ```typespec |
| 42 | * @events union MixedEvents { |
| 43 | * { done: true }, |
| 44 | * |
| 45 | * { done: false, @data @contentType("text/plain") value: string,} |
| 46 | * } |
| 47 | * ``` |
| 48 | */ |
| 49 | export type ContentTypeDecorator = ( |
| 50 | context: DecoratorContext, |
| 51 | target: UnionVariant | ModelProperty, |
| 52 | contentType: string, |
| 53 | ) => DecoratorValidatorCallbacks | void; |
| 54 | |
| 55 | /** |
| 56 | * Identifies the payload of an event. |
| 57 | * Only one field in an event can be marked as the payload. |
| 58 | * |
| 59 | * @example |
| 60 | * ```typespec |
| 61 | * @events union MixedEvents { |
| 62 | * { metadata: Record<string>, @data payload: string,} |
| 63 | * } |
| 64 | * ``` |
| 65 | */ |
| 66 | export type DataDecorator = ( |
| 67 | context: DecoratorContext, |
| 68 | target: ModelProperty, |
| 69 | ) => DecoratorValidatorCallbacks | void; |
| 70 | |
| 71 | export type TypeSpecEventsDecorators = { |
| 72 | events: EventsDecorator; |
| 73 | contentType: ContentTypeDecorator; |
| 74 | data: DataDecorator; |
| 75 | }; |
| 76 | |