microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/add-client-method-instrumentation

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

packages/compiler/generated-defs/TypeSpec.ts

1208lines · modecode

1import type {
2 DecoratorContext,
3 DecoratorValidatorCallbacks,
4 Enum,
5 EnumValue,
6 Interface,
7 Model,
8 ModelProperty,
9 Namespace,
10 Numeric,
11 Operation,
12 Scalar,
13 ScalarValue,
14 Type,
15 Union,
16 UnionVariant,
17} from "../src/index.js";
18
19export interface ServiceOptions {
20 readonly title?: string;
21}
22
23export interface DiscriminatedOptions {
24 readonly envelope?: "object" | "none";
25 readonly discriminatorPropertyName?: string;
26 readonly envelopePropertyName?: string;
27}
28
29export interface ExampleOptions {
30 readonly title?: string;
31 readonly description?: string;
32}
33
34export interface OperationExample {
35 readonly parameters?: unknown;
36 readonly returnType?: unknown;
37}
38
39export interface VisibilityFilter {
40 readonly any?: readonly EnumValue[];
41 readonly all?: readonly EnumValue[];
42 readonly none?: readonly EnumValue[];
43}
44
45/**
46 * Applies a media type hint to a TypeSpec type. Emitters and libraries may choose to use this hint to determine how a
47 * type should be serialized. For example, the `@typespec/http` library will use the media type hint of the response
48 * body type as a default `Content-Type` if one is not explicitly specified in the operation.
49 *
50 * Media types (also known as MIME types) are defined by RFC 6838. The media type hint should be a valid media type
51 * string as defined by the RFC, but the decorator does not enforce or validate this constraint.
52 *
53 * Notes: the applied media type is _only_ a hint. It may be overridden or not used at all. Media type hints are
54 * inherited by subtypes. If a media type hint is applied to a model, it will be inherited by all other models that
55 * `extend` it unless they delcare their own media type hint.
56 *
57 * @param mediaType The media type hint to apply to the target type.
58 * @example create a model that serializes as XML by default
59 *
60 * ```tsp
61 * @mediaTypeHint("application/xml")
62 * model Example {
63 * @visibility(Lifecycle.Read)
64 * id: string;
65 *
66 * name: string;
67 * }
68 * ```
69 */
70export type MediaTypeHintDecorator = (
71 context: DecoratorContext,
72 target: Model | Scalar | Enum | Union,
73 mediaType: string,
74) => DecoratorValidatorCallbacks | void;
75
76/**
77 * Specify how to encode the target type.
78 *
79 * @param encodingOrEncodeAs Known name of an encoding or a scalar type to encode as(Only for numeric types to encode as string).
80 * @param encodedAs What target type is this being encoded as. Default to string.
81 * @example offsetDateTime encoded with rfc7231
82 *
83 * ```tsp
84 * @encode("rfc7231")
85 * scalar myDateTime extends offsetDateTime;
86 * ```
87 * @example utcDateTime encoded with unixTimestamp
88 *
89 * ```tsp
90 * @encode("unixTimestamp", int32)
91 * scalar myDateTime extends unixTimestamp;
92 * ```
93 * @example encode numeric type to string
94 *
95 * ```tsp
96 * model Pet {
97 * @encode(string) id: int64;
98 * }
99 * ```
100 */
101export type EncodeDecorator = (
102 context: DecoratorContext,
103 target: Scalar | ModelProperty,
104 encodingOrEncodeAs: Scalar | string | EnumValue,
105 encodedAs?: Scalar,
106) => DecoratorValidatorCallbacks | void;
107
108/**
109 * Attach a documentation string. Content support CommonMark markdown formatting.
110 *
111 * @param doc Documentation string
112 * @param formatArgs Record with key value pair that can be interpolated in the doc.
113 * @example
114 * ```typespec
115 * @doc("Represent a Pet available in the PetStore")
116 * model Pet {}
117 * ```
118 */
119export type DocDecorator = (
120 context: DecoratorContext,
121 target: Type,
122 doc: string,
123 formatArgs?: Type,
124) => DecoratorValidatorCallbacks | void;
125
126/**
127 * Returns the model with required properties removed.
128 */
129export type WithOptionalPropertiesDecorator = (
130 context: DecoratorContext,
131 target: Model,
132) => DecoratorValidatorCallbacks | void;
133
134/**
135 * Returns the model with non-updateable properties removed.
136 */
137export type WithUpdateablePropertiesDecorator = (
138 context: DecoratorContext,
139 target: Model,
140) => DecoratorValidatorCallbacks | void;
141
142/**
143 * Returns the model with the given properties omitted.
144 *
145 * @param omit List of properties to omit
146 */
147export type WithoutOmittedPropertiesDecorator = (
148 context: DecoratorContext,
149 target: Model,
150 omit: Type,
151) => DecoratorValidatorCallbacks | void;
152
153/**
154 * Returns the model with only the given properties included.
155 *
156 * @param pick List of properties to include
157 */
158export type WithPickedPropertiesDecorator = (
159 context: DecoratorContext,
160 target: Model,
161 pick: Type,
162) => DecoratorValidatorCallbacks | void;
163
164/**
165 * Returns the model with any default values removed.
166 */
167export type WithoutDefaultValuesDecorator = (
168 context: DecoratorContext,
169 target: Model,
170) => DecoratorValidatorCallbacks | void;
171
172/**
173 * Set the visibility of key properties in a model if not already set.
174 *
175 * This will set the visibility modifiers of all key properties in the model if the visibility is not already _explicitly_ set,
176 * but will not change the visibility of any properties that have visibility set _explicitly_, even if the visibility
177 * is the same as the default visibility.
178 *
179 * Visibility may be set explicitly using any of the following decorators:
180 *
181 * - `@visibility`
182 * - `@removeVisibility`
183 * - `@invisible`
184 *
185 * @param visibility The desired default visibility value. If a key property already has visibility set, it will not be changed.
186 */
187export type WithDefaultKeyVisibilityDecorator = (
188 context: DecoratorContext,
189 target: Model,
190 visibility: EnumValue,
191) => DecoratorValidatorCallbacks | void;
192
193/**
194 * Typically a short, single-line description.
195 *
196 * @param summary Summary string.
197 * @example
198 * ```typespec
199 * @summary("This is a pet")
200 * model Pet {}
201 * ```
202 */
203export type SummaryDecorator = (
204 context: DecoratorContext,
205 target: Type,
206 summary: string,
207) => DecoratorValidatorCallbacks | void;
208
209/**
210 * Attach a documentation string to describe the successful return types of an operation.
211 * If an operation returns a union of success and errors it only describes the success. See `@errorsDoc` for error documentation.
212 *
213 * @param doc Documentation string
214 * @example
215 * ```typespec
216 * @returnsDoc("Returns doc")
217 * op get(): Pet | NotFound;
218 * ```
219 */
220export type ReturnsDocDecorator = (
221 context: DecoratorContext,
222 target: Operation,
223 doc: string,
224) => DecoratorValidatorCallbacks | void;
225
226/**
227 * Attach a documentation string to describe the error return types of an operation.
228 * If an operation returns a union of success and errors it only describes the errors. See `@returnsDoc` for success documentation.
229 *
230 * @param doc Documentation string
231 * @example
232 * ```typespec
233 * @errorsDoc("Errors doc")
234 * op get(): Pet | NotFound;
235 * ```
236 */
237export type ErrorsDocDecorator = (
238 context: DecoratorContext,
239 target: Operation,
240 doc: string,
241) => DecoratorValidatorCallbacks | void;
242
243/**
244 * Mark this namespace as describing a service and configure service properties.
245 *
246 * @param options Optional configuration for the service.
247 * @example
248 * ```typespec
249 * @service
250 * namespace PetStore;
251 * ```
252 * @example Setting service title
253 * ```typespec
254 * @service(#{title: "Pet store"})
255 * namespace PetStore;
256 * ```
257 */
258export type ServiceDecorator = (
259 context: DecoratorContext,
260 target: Namespace,
261 options?: ServiceOptions,
262) => DecoratorValidatorCallbacks | void;
263
264/**
265 * Specify that this model is an error type. Operations return error types when the operation has failed.
266 *
267 * @example
268 * ```typespec
269 * @error
270 * model PetStoreError {
271 * code: string;
272 * message: string;
273 * }
274 * ```
275 */
276export type ErrorDecorator = (
277 context: DecoratorContext,
278 target: Model,
279) => DecoratorValidatorCallbacks | void;
280
281/**
282 * Specify a known data format hint for this string type. For example `uuid`, `uri`, etc.
283 * This differs from the `@pattern` decorator which is meant to specify a regular expression while `@format` accepts a known format name.
284 * The format names are open ended and are left to emitter to interpret.
285 *
286 * @param format format name.
287 * @example
288 * ```typespec
289 * @format("uuid")
290 * scalar uuid extends string;
291 * ```
292 */
293export type FormatDecorator = (
294 context: DecoratorContext,
295 target: Scalar | ModelProperty,
296 format: string,
297) => DecoratorValidatorCallbacks | void;
298
299/**
300 * Specify the the pattern this string should respect using simple regular expression syntax.
301 * The following syntax is allowed: alternations (`|`), quantifiers (`?`, `*`, `+`, and `{ }`), wildcard (`.`), and grouping parentheses.
302 * Advanced features like look-around, capture groups, and references are not supported.
303 *
304 * This decorator may optionally provide a custom validation _message_. Emitters may choose to use the message to provide
305 * context when pattern validation fails. For the sake of consistency, the message should be a phrase that describes in
306 * plain language what sort of content the pattern attempts to validate. For example, a complex regular expression that
307 * validates a GUID string might have a message like "Must be a valid GUID."
308 *
309 * @param pattern Regular expression.
310 * @param validationMessage Optional validation message that may provide context when validation fails.
311 * @example
312 * ```typespec
313 * @pattern("[a-z]+", "Must be a string consisting of only lower case letters and of at least one character.")
314 * scalar LowerAlpha extends string;
315 * ```
316 */
317export type PatternDecorator = (
318 context: DecoratorContext,
319 target: Scalar | ModelProperty,
320 pattern: string,
321 validationMessage?: string,
322) => DecoratorValidatorCallbacks | void;
323
324/**
325 * Specify the minimum length this string type should be.
326 *
327 * @param value Minimum length
328 * @example
329 * ```typespec
330 * @minLength(2)
331 * scalar Username extends string;
332 * ```
333 */
334export type MinLengthDecorator = (
335 context: DecoratorContext,
336 target: Scalar | ModelProperty,
337 value: Numeric,
338) => DecoratorValidatorCallbacks | void;
339
340/**
341 * Specify the maximum length this string type should be.
342 *
343 * @param value Maximum length
344 * @example
345 * ```typespec
346 * @maxLength(20)
347 * scalar Username extends string;
348 * ```
349 */
350export type MaxLengthDecorator = (
351 context: DecoratorContext,
352 target: Scalar | ModelProperty,
353 value: Numeric,
354) => DecoratorValidatorCallbacks | void;
355
356/**
357 * Specify the minimum number of items this array should have.
358 *
359 * @param value Minimum number
360 * @example
361 * ```typespec
362 * @minItems(1)
363 * model Endpoints is string[];
364 * ```
365 */
366export type MinItemsDecorator = (
367 context: DecoratorContext,
368 target: Type | ModelProperty,
369 value: Numeric,
370) => DecoratorValidatorCallbacks | void;
371
372/**
373 * Specify the maximum number of items this array should have.
374 *
375 * @param value Maximum number
376 * @example
377 * ```typespec
378 * @maxItems(5)
379 * model Endpoints is string[];
380 * ```
381 */
382export type MaxItemsDecorator = (
383 context: DecoratorContext,
384 target: Type | ModelProperty,
385 value: Numeric,
386) => DecoratorValidatorCallbacks | void;
387
388/**
389 * Specify the minimum value this numeric type should be.
390 *
391 * @param value Minimum value
392 * @example
393 * ```typespec
394 * @minValue(18)
395 * scalar Age is int32;
396 * ```
397 */
398export type MinValueDecorator = (
399 context: DecoratorContext,
400 target: Scalar | ModelProperty,
401 value: Numeric | ScalarValue | ScalarValue | ScalarValue | ScalarValue | ScalarValue,
402) => DecoratorValidatorCallbacks | void;
403
404/**
405 * Specify the maximum value this numeric type should be.
406 *
407 * @param value Maximum value
408 * @example
409 * ```typespec
410 * @maxValue(200)
411 * scalar Age is int32;
412 * ```
413 */
414export type MaxValueDecorator = (
415 context: DecoratorContext,
416 target: Scalar | ModelProperty,
417 value: Numeric | ScalarValue | ScalarValue | ScalarValue | ScalarValue | ScalarValue,
418) => DecoratorValidatorCallbacks | void;
419
420/**
421 * Specify the minimum value this numeric type should be, exclusive of the given
422 * value.
423 *
424 * @param value Minimum value
425 * @example
426 * ```typespec
427 * @minValueExclusive(0)
428 * scalar distance is float64;
429 * ```
430 */
431export type MinValueExclusiveDecorator = (
432 context: DecoratorContext,
433 target: Scalar | ModelProperty,
434 value: Numeric | ScalarValue | ScalarValue | ScalarValue | ScalarValue | ScalarValue,
435) => DecoratorValidatorCallbacks | void;
436
437/**
438 * Specify the maximum value this numeric type should be, exclusive of the given
439 * value.
440 *
441 * @param value Maximum value
442 * @example
443 * ```typespec
444 * @maxValueExclusive(50)
445 * scalar distance is float64;
446 * ```
447 */
448export type MaxValueExclusiveDecorator = (
449 context: DecoratorContext,
450 target: Scalar | ModelProperty,
451 value: Numeric | ScalarValue | ScalarValue | ScalarValue | ScalarValue | ScalarValue,
452) => DecoratorValidatorCallbacks | void;
453
454/**
455 * Mark this value as a secret value that should be treated carefully to avoid exposure
456 *
457 * @example
458 * ```typespec
459 * @secret
460 * scalar Password is string;
461 * ```
462 */
463export type SecretDecorator = (
464 context: DecoratorContext,
465 target: Scalar | ModelProperty | Model | Union | Enum,
466) => DecoratorValidatorCallbacks | void;
467
468/**
469 * Attaches a tag to an operation, interface, or namespace. Multiple `@tag` decorators can be specified to attach multiple tags to a TypeSpec element.
470 *
471 * @param tag Tag value
472 */
473export type TagDecorator = (
474 context: DecoratorContext,
475 target: Namespace | Interface | Operation,
476 tag: string,
477) => DecoratorValidatorCallbacks | void;
478
479/**
480 * Specifies how a templated type should name their instances.
481 *
482 * @param name name the template instance should take
483 * @param formatArgs Model with key value used to interpolate the name
484 * @example
485 * ```typespec
486 * @friendlyName("{name}List", T)
487 * model List<Item> {
488 * value: Item[];
489 * nextLink: string;
490 * }
491 * ```
492 */
493export type FriendlyNameDecorator = (
494 context: DecoratorContext,
495 target: Type,
496 name: string,
497 formatArgs?: Type,
498) => DecoratorValidatorCallbacks | void;
499
500/**
501 * Mark a model property as the key to identify instances of that type
502 *
503 * @param altName Name of the property. If not specified, the decorated property name is used.
504 * @example
505 * ```typespec
506 * model Pet {
507 * @key id: string;
508 * }
509 * ```
510 */
511export type KeyDecorator = (
512 context: DecoratorContext,
513 target: ModelProperty,
514 altName?: string,
515) => DecoratorValidatorCallbacks | void;
516
517/**
518 * Specify this operation is an overload of the given operation.
519 *
520 * @param overloadbase Base operation that should be a union of all overloads
521 * @example
522 * ```typespec
523 * op upload(data: string | bytes, @header contentType: "text/plain" | "application/octet-stream"): void;
524 * @overload(upload)
525 * op uploadString(data: string, @header contentType: "text/plain" ): void;
526 * @overload(upload)
527 * op uploadBytes(data: bytes, @header contentType: "application/octet-stream"): void;
528 * ```
529 */
530export type OverloadDecorator = (
531 context: DecoratorContext,
532 target: Operation,
533 overloadbase: Operation,
534) => DecoratorValidatorCallbacks | void;
535
536/**
537 * Provide an alternative name for this type when serialized to the given mime type.
538 *
539 * @param mimeType Mime type this should apply to. The mime type should be a known mime type as described here https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types without any suffix (e.g. `+json`)
540 * @param name Alternative name
541 * @example
542 * ```typespec
543 * model Certificate {
544 * @encodedName("application/json", "exp")
545 * @encodedName("application/xml", "expiry")
546 * expireAt: int32;
547 * }
548 * ```
549 * @example Invalid values
550 *
551 * ```typespec
552 * @encodedName("application/merge-patch+json", "exp")
553 * ^ error cannot use subtype
554 * ```
555 */
556export type EncodedNameDecorator = (
557 context: DecoratorContext,
558 target: Type,
559 mimeType: string,
560 name: string,
561) => DecoratorValidatorCallbacks | void;
562
563/**
564 * Specify that this union is discriminated.
565 *
566 * @param options Options to configure the serialization of the discriminated union.
567 * @example
568 * ```typespec
569 * @discriminated
570 * union Pet{ cat: Cat, dog: Dog }
571 *
572 * model Cat { name: string, meow: boolean }
573 * model Dog { name: string, bark: boolean }
574 * ```
575 * Serialized as:
576 * ```json
577 * {
578 * "kind": "cat",
579 * "value": {
580 * "name": "Whiskers",
581 * "meow": true
582 * }
583 * },
584 * {
585 * "kind": "dog",
586 * "value": {
587 * "name": "Rex",
588 * "bark": false
589 * }
590 * }
591 * ```
592 * @example Custom property names
593 *
594 * ```typespec
595 * @discriminated(#{discriminatorPropertyName: "dataKind", envelopePropertyName: "data"})
596 * union Pet{ cat: Cat, dog: Dog }
597 *
598 * model Cat { name: string, meow: boolean }
599 * model Dog { name: string, bark: boolean }
600 * ```
601 * Serialized as:
602 * ```json
603 * {
604 * "dataKind": "cat",
605 * "data": {
606 * "name": "Whiskers",
607 * "meow": true
608 * }
609 * },
610 * {
611 * "dataKind": "dog",
612 * "data": {
613 * "name": "Rex",
614 * "bark": false
615 * }
616 * }
617 * ```
618 */
619export type DiscriminatedDecorator = (
620 context: DecoratorContext,
621 target: Union,
622 options?: DiscriminatedOptions,
623) => DecoratorValidatorCallbacks | void;
624
625/**
626 * Specify the property to be used to discriminate this type.
627 *
628 * @param propertyName The property name to use for discrimination
629 * @example
630 * ```typespec
631 * @discriminator("kind")
632 * model Pet{ kind: string }
633 *
634 * model Cat extends Pet {kind: "cat", meow: boolean}
635 * model Dog extends Pet {kind: "dog", bark: boolean}
636 * ```
637 */
638export type DiscriminatorDecorator = (
639 context: DecoratorContext,
640 target: Model,
641 propertyName: string,
642) => DecoratorValidatorCallbacks | void;
643
644/**
645 * Provide an example value for a data type.
646 *
647 * @param example Example value.
648 * @param options Optional metadata for the example.
649 * @example
650 * ```tsp
651 * @example(#{name: "Fluffy", age: 2})
652 * model Pet {
653 * name: string;
654 * age: int32;
655 * }
656 * ```
657 */
658export type ExampleDecorator = (
659 context: DecoratorContext,
660 target: Model | Enum | Scalar | Union | ModelProperty | UnionVariant,
661 example: unknown,
662 options?: ExampleOptions,
663) => DecoratorValidatorCallbacks | void;
664
665/**
666 * Provide example values for an operation's parameters and corresponding return type.
667 *
668 * @param example Example value.
669 * @param options Optional metadata for the example.
670 * @example
671 * ```tsp
672 * @opExample(#{parameters: #{name: "Fluffy", age: 2}, returnType: #{name: "Fluffy", age: 2, id: "abc"})
673 * op createPet(pet: Pet): Pet;
674 * ```
675 */
676export type OpExampleDecorator = (
677 context: DecoratorContext,
678 target: Operation,
679 example: OperationExample,
680 options?: ExampleOptions,
681) => DecoratorValidatorCallbacks | void;
682
683/**
684 * Mark this operation as a `list` operation that returns a paginated list of items.
685 */
686export type ListDecorator = (
687 context: DecoratorContext,
688 target: Operation,
689) => DecoratorValidatorCallbacks | void;
690
691/**
692 * Pagination property defining the number of items to skip.
693 *
694 * @example
695 * ```tsp
696 * model Page<T> {
697 * @pageItems items: T[];
698 * }
699 * @list op listPets(@offset skip: int32, @pageSize pageSize: int8): Page<Pet>;
700 * ```
701 */
702export type OffsetDecorator = (
703 context: DecoratorContext,
704 target: ModelProperty,
705) => DecoratorValidatorCallbacks | void;
706
707/**
708 * Pagination property defining the page index.
709 *
710 * @example
711 * ```tsp
712 * model Page<T> {
713 * @pageItems items: T[];
714 * }
715 * @list op listPets(@pageIndex page: int32, @pageSize pageSize: int8): Page<Pet>;
716 * ```
717 */
718export type PageIndexDecorator = (
719 context: DecoratorContext,
720 target: ModelProperty,
721) => DecoratorValidatorCallbacks | void;
722
723/**
724 * Specify the pagination parameter that controls the maximum number of items to include in a page.
725 *
726 * @example
727 * ```tsp
728 * model Page<T> {
729 * @pageItems items: T[];
730 * }
731 * @list op listPets(@pageIndex page: int32, @pageSize pageSize: int8): Page<Pet>;
732 * ```
733 */
734export type PageSizeDecorator = (
735 context: DecoratorContext,
736 target: ModelProperty,
737) => DecoratorValidatorCallbacks | void;
738
739/**
740 * Specify the the property that contains the array of page items.
741 *
742 * @example
743 * ```tsp
744 * model Page<T> {
745 * @pageItems items: T[];
746 * }
747 * @list op listPets(@pageIndex page: int32, @pageSize pageSize: int8): Page<Pet>;
748 * ```
749 */
750export type PageItemsDecorator = (
751 context: DecoratorContext,
752 target: ModelProperty,
753) => DecoratorValidatorCallbacks | void;
754
755/**
756 * Pagination property defining the token to get to the next page.
757 * It MUST be specified both on the request parameter and the response.
758 *
759 * @example
760 * ```tsp
761 * model Page<T> {
762 * @pageItems items: T[];
763 * @continuationToken continuationToken: string;
764 * }
765 * @list op listPets(@continuationToken continuationToken: string): Page<Pet>;
766 * ```
767 */
768export type ContinuationTokenDecorator = (
769 context: DecoratorContext,
770 target: ModelProperty,
771) => DecoratorValidatorCallbacks | void;
772
773/**
774 * Pagination property defining a link to the next page.
775 *
776 * It is expected that navigating to the link will return the same set of responses as the operation that returned the current page.
777 *
778 * @example
779 * ```tsp
780 * model Page<T> {
781 * @pageItems items: T[];
782 * @nextLink next: url;
783 * @prevLink prev: url;
784 * @firstLink first: url;
785 * @lastLink last: url;
786 * }
787 * @list op listPets(): Page<Pet>;
788 * ```
789 */
790export type NextLinkDecorator = (
791 context: DecoratorContext,
792 target: ModelProperty,
793) => DecoratorValidatorCallbacks | void;
794
795/**
796 * Pagination property defining a link to the previous page.
797 *
798 * It is expected that navigating to the link will return the same set of responses as the operation that returned the current page.
799 *
800 * @example
801 * ```tsp
802 * model Page<T> {
803 * @pageItems items: T[];
804 * @nextLink next: url;
805 * @prevLink prev: url;
806 * @firstLink first: url;
807 * @lastLink last: url;
808 * }
809 * @list op listPets(): Page<Pet>;
810 * ```
811 */
812export type PrevLinkDecorator = (
813 context: DecoratorContext,
814 target: ModelProperty,
815) => DecoratorValidatorCallbacks | void;
816
817/**
818 * Pagination property defining a link to the first page.
819 *
820 * It is expected that navigating to the link will return the same set of responses as the operation that returned the current page.
821 *
822 * @example
823 * ```tsp
824 * model Page<T> {
825 * @pageItems items: T[];
826 * @nextLink next: url;
827 * @prevLink prev: url;
828 * @firstLink first: url;
829 * @lastLink last: url;
830 * }
831 * @list op listPets(): Page<Pet>;
832 * ```
833 */
834export type FirstLinkDecorator = (
835 context: DecoratorContext,
836 target: ModelProperty,
837) => DecoratorValidatorCallbacks | void;
838
839/**
840 * Pagination property defining a link to the last page.
841 *
842 * It is expected that navigating to the link will return the same set of responses as the operation that returned the current page.
843 *
844 * @example
845 * ```tsp
846 * model Page<T> {
847 * @pageItems items: T[];
848 * @nextLink next: url;
849 * @prevLink prev: url;
850 * @firstLink first: url;
851 * @lastLink last: url;
852 * }
853 * @list op listPets(): Page<Pet>;
854 * ```
855 */
856export type LastLinkDecorator = (
857 context: DecoratorContext,
858 target: ModelProperty,
859) => DecoratorValidatorCallbacks | void;
860
861/**
862 * A debugging decorator used to inspect a type.
863 *
864 * @param text Custom text to log
865 */
866export type InspectTypeDecorator = (
867 context: DecoratorContext,
868 target: Type,
869 text: string,
870) => DecoratorValidatorCallbacks | void;
871
872/**
873 * A debugging decorator used to inspect a type name.
874 *
875 * @param text Custom text to log
876 */
877export type InspectTypeNameDecorator = (
878 context: DecoratorContext,
879 target: Type,
880 text: string,
881) => DecoratorValidatorCallbacks | void;
882
883/**
884 * Sets the visibility modifiers that are active on a property, indicating that it is only considered to be present
885 * (or "visible") in contexts that select for the given modifiers.
886 *
887 * A property without any visibility settings applied for any visibility class (e.g. `Lifecycle`) is considered to have
888 * the default visibility settings for that class.
889 *
890 * If visibility for the property has already been set for a visibility class (for example, using `@invisible` or
891 * `@removeVisibility`), this decorator will **add** the specified visibility modifiers to the property.
892 *
893 * See: [Visibility](https://typespec.io/docs/language-basics/visibility)
894 *
895 * The `@typespec/http` library uses `Lifecycle` visibility to determine which properties are included in the request or
896 * response bodies of HTTP operations. By default, it uses the following visibility settings:
897 *
898 * - For the return type of operations, properties are included if they have `Lifecycle.Read` visibility.
899 * - For POST operation parameters, properties are included if they have `Lifecycle.Create` visibility.
900 * - For PUT operation parameters, properties are included if they have `Lifecycle.Create` or `Lifecycle.Update` visibility.
901 * - For PATCH operation parameters, properties are included if they have `Lifecycle.Update` visibility.
902 * - For DELETE operation parameters, properties are included if they have `Lifecycle.Delete` visibility.
903 * - For GET or HEAD operation parameters, properties are included if they have `Lifecycle.Query` visibility.
904 *
905 * By default, properties have all five Lifecycle visibility modifiers enabled, so a property is visible in all contexts
906 * by default.
907 *
908 * The default settings may be overridden using the `@returnTypeVisibility` and `@parameterVisibility` decorators.
909 *
910 * See also: [Automatic visibility](https://typespec.io/docs/libraries/http/operations#automatic-visibility)
911 *
912 * @param visibilities List of visibilities which apply to this property.
913 * @example
914 * ```typespec
915 * model Dog {
916 * // The service will generate an ID, so you don't need to send it.
917 * @visibility(Lifecycle.Read)
918 * id: int32;
919 *
920 * // The service will store this secret name, but won't ever return it.
921 * @visibility(Lifecycle.Create, Lifecycle.Update)
922 * secretName: string;
923 *
924 * // The regular name has all vi
925 * name: string;
926 * }
927 * ```
928 */
929export type VisibilityDecorator = (
930 context: DecoratorContext,
931 target: ModelProperty,
932 ...visibilities: EnumValue[]
933) => DecoratorValidatorCallbacks | void;
934
935/**
936 * Indicates that a property is not visible in the given visibility class.
937 *
938 * This decorator removes all active visibility modifiers from the property within
939 * the given visibility class, making it invisible to any context that selects for
940 * visibility modifiers within that class.
941 *
942 * @param visibilityClass The visibility class to make the property invisible within.
943 * @example
944 * ```typespec
945 * model Example {
946 * @invisible(Lifecycle)
947 * hidden_property: string;
948 * }
949 * ```
950 */
951export type InvisibleDecorator = (
952 context: DecoratorContext,
953 target: ModelProperty,
954 visibilityClass: Enum,
955) => DecoratorValidatorCallbacks | void;
956
957/**
958 * Removes visibility modifiers from a property.
959 *
960 * If the visibility modifiers for a visibility class have not been initialized,
961 * this decorator will use the default visibility modifiers for the visibility
962 * class as the default modifier set.
963 *
964 * @param target The property to remove visibility from.
965 * @param visibilities The visibility modifiers to remove from the target property.
966 * @example
967 * ```typespec
968 * model Example {
969 * // This property will have all Lifecycle visibilities except the Read
970 * // visibility, since it is removed.
971 * @removeVisibility(Lifecycle.Read)
972 * secret_property: string;
973 * }
974 * ```
975 */
976export type RemoveVisibilityDecorator = (
977 context: DecoratorContext,
978 target: ModelProperty,
979 ...visibilities: EnumValue[]
980) => DecoratorValidatorCallbacks | void;
981
982/**
983 * Removes properties that do not have at least one of the given visibility modifiers
984 * active.
985 *
986 * If no visibility modifiers are supplied, this decorator has no effect.
987 *
988 * See also: [Automatic visibility](https://typespec.io/docs/libraries/http/operations#automatic-visibility)
989 *
990 * When using an emitter that applies visibility automatically, it is generally
991 * not necessary to use this decorator.
992 *
993 * @param visibilities List of visibilities that apply to this property.
994 * @example
995 * ```typespec
996 * model Dog {
997 * @visibility(Lifecycle.Read)
998 * id: int32;
999 *
1000 * @visibility(Lifecycle.Create, Lifecycle.Update)
1001 * secretName: string;
1002 *
1003 * name: string;
1004 * }
1005 *
1006 * // The spread operator will copy all the properties of Dog into DogRead,
1007 * // and @withVisibility will then remove those that are not visible with
1008 * // create or update visibility.
1009 * //
1010 * // In this case, the id property is removed, and the name and secretName
1011 * // properties are kept.
1012 * @withVisibility(Lifecycle.Create, Lifecycle.Update)
1013 * model DogCreateOrUpdate {
1014 * ...Dog;
1015 * }
1016 *
1017 * // In this case the id and name properties are kept and the secretName property
1018 * // is removed.
1019 * @withVisibility(Lifecycle.Read)
1020 * model DogRead {
1021 * ...Dog;
1022 * }
1023 * ```
1024 */
1025export type WithVisibilityDecorator = (
1026 context: DecoratorContext,
1027 target: Model,
1028 ...visibilities: EnumValue[]
1029) => DecoratorValidatorCallbacks | void;
1030
1031/**
1032 * Declares the visibility constraint of the parameters of a given operation.
1033 *
1034 * A parameter or property nested within a parameter will be visible if it has _any_ of the visibilities
1035 * in the list.
1036 *
1037 * It is invalid to call this decorator with no visibility modifiers.
1038 *
1039 * @param visibilities List of visibility modifiers that apply to the parameters of this operation.
1040 */
1041export type ParameterVisibilityDecorator = (
1042 context: DecoratorContext,
1043 target: Operation,
1044 ...visibilities: EnumValue[]
1045) => DecoratorValidatorCallbacks | void;
1046
1047/**
1048 * Declares the visibility constraint of the return type of a given operation.
1049 *
1050 * A property within the return type of the operation will be visible if it has _any_ of the visibilities
1051 * in the list.
1052 *
1053 * It is invalid to call this decorator with no visibility modifiers.
1054 *
1055 * @param visibilities List of visibility modifiers that apply to the return type of this operation.
1056 */
1057export type ReturnTypeVisibilityDecorator = (
1058 context: DecoratorContext,
1059 target: Operation,
1060 ...visibilities: EnumValue[]
1061) => DecoratorValidatorCallbacks | void;
1062
1063/**
1064 * Declares the default visibility modifiers for a visibility class.
1065 *
1066 * The default modifiers are used when a property does not have any visibility decorators
1067 * applied to it.
1068 *
1069 * The modifiers passed to this decorator _MUST_ be members of the target Enum.
1070 *
1071 * @param visibilities the list of modifiers to use as the default visibility modifiers.
1072 */
1073export type DefaultVisibilityDecorator = (
1074 context: DecoratorContext,
1075 target: Enum,
1076 ...visibilities: EnumValue[]
1077) => DecoratorValidatorCallbacks | void;
1078
1079/**
1080 * Applies the given visibility filter to the properties of the target model.
1081 *
1082 * This transformation is recursive, so it will also apply the filter to any nested
1083 * or referenced models that are the types of any properties in the `target`.
1084 *
1085 * If a `nameTemplate` is provided, newly-created type instances will be named according
1086 * to the template. See the `@friendlyName` decorator for more information on the template
1087 * syntax. The transformed type is provided as the argument to the template.
1088 *
1089 * @param target The model to apply the visibility filter to.
1090 * @param filter The visibility filter to apply to the properties of the target model.
1091 * @param nameTemplate The name template to use when renaming new model instances.
1092 * @example
1093 * ```typespec
1094 * model Dog {
1095 * @visibility(Lifecycle.Read)
1096 * id: int32;
1097 *
1098 * name: string;
1099 * }
1100 *
1101 * @withVisibilityFilter(#{ all: #[Lifecycle.Read] })
1102 * model DogRead {
1103 * ...Dog
1104 * }
1105 * ```
1106 */
1107export type WithVisibilityFilterDecorator = (
1108 context: DecoratorContext,
1109 target: Model,
1110 filter: VisibilityFilter,
1111 nameTemplate?: string,
1112) => DecoratorValidatorCallbacks | void;
1113
1114/**
1115 * Transforms the `target` model to include only properties that are visible during the
1116 * "Update" lifecycle phase.
1117 *
1118 * Any nested models of optional properties will be transformed into the "CreateOrUpdate"
1119 * lifecycle phase instead of the "Update" lifecycle phase, so that nested models may be
1120 * fully updated.
1121 *
1122 * If a `nameTemplate` is provided, newly-created type instances will be named according
1123 * to the template. See the `@friendlyName` decorator for more information on the template
1124 * syntax. The transformed type is provided as the argument to the template.
1125 *
1126 * @param target The model to apply the transformation to.
1127 * @param nameTemplate The name template to use when renaming new model instances.
1128 * @example
1129 * ```typespec
1130 * model Dog {
1131 * @visibility(Lifecycle.Read)
1132 * id: int32;
1133 *
1134 * @visibility(Lifecycle.Create, Lifecycle.Update)
1135 * secretName: string;
1136 *
1137 * name: string;
1138 * }
1139 *
1140 * @withLifecycleUpdate
1141 * model DogUpdate {
1142 * ...Dog
1143 * }
1144 * ```
1145 */
1146export type WithLifecycleUpdateDecorator = (
1147 context: DecoratorContext,
1148 target: Model,
1149 nameTemplate?: string,
1150) => DecoratorValidatorCallbacks | void;
1151
1152export type TypeSpecDecorators = {
1153 mediaTypeHint: MediaTypeHintDecorator;
1154 encode: EncodeDecorator;
1155 doc: DocDecorator;
1156 withOptionalProperties: WithOptionalPropertiesDecorator;
1157 withUpdateableProperties: WithUpdateablePropertiesDecorator;
1158 withoutOmittedProperties: WithoutOmittedPropertiesDecorator;
1159 withPickedProperties: WithPickedPropertiesDecorator;
1160 withoutDefaultValues: WithoutDefaultValuesDecorator;
1161 withDefaultKeyVisibility: WithDefaultKeyVisibilityDecorator;
1162 summary: SummaryDecorator;
1163 returnsDoc: ReturnsDocDecorator;
1164 errorsDoc: ErrorsDocDecorator;
1165 service: ServiceDecorator;
1166 error: ErrorDecorator;
1167 format: FormatDecorator;
1168 pattern: PatternDecorator;
1169 minLength: MinLengthDecorator;
1170 maxLength: MaxLengthDecorator;
1171 minItems: MinItemsDecorator;
1172 maxItems: MaxItemsDecorator;
1173 minValue: MinValueDecorator;
1174 maxValue: MaxValueDecorator;
1175 minValueExclusive: MinValueExclusiveDecorator;
1176 maxValueExclusive: MaxValueExclusiveDecorator;
1177 secret: SecretDecorator;
1178 tag: TagDecorator;
1179 friendlyName: FriendlyNameDecorator;
1180 key: KeyDecorator;
1181 overload: OverloadDecorator;
1182 encodedName: EncodedNameDecorator;
1183 discriminated: DiscriminatedDecorator;
1184 discriminator: DiscriminatorDecorator;
1185 example: ExampleDecorator;
1186 opExample: OpExampleDecorator;
1187 list: ListDecorator;
1188 offset: OffsetDecorator;
1189 pageIndex: PageIndexDecorator;
1190 pageSize: PageSizeDecorator;
1191 pageItems: PageItemsDecorator;
1192 continuationToken: ContinuationTokenDecorator;
1193 nextLink: NextLinkDecorator;
1194 prevLink: PrevLinkDecorator;
1195 firstLink: FirstLinkDecorator;
1196 lastLink: LastLinkDecorator;
1197 inspectType: InspectTypeDecorator;
1198 inspectTypeName: InspectTypeNameDecorator;
1199 visibility: VisibilityDecorator;
1200 invisible: InvisibleDecorator;
1201 removeVisibility: RemoveVisibilityDecorator;
1202 withVisibility: WithVisibilityDecorator;
1203 parameterVisibility: ParameterVisibilityDecorator;
1204 returnTypeVisibility: ReturnTypeVisibilityDecorator;
1205 defaultVisibility: DefaultVisibilityDecorator;
1206 withVisibilityFilter: WithVisibilityFilterDecorator;
1207 withLifecycleUpdate: WithLifecycleUpdateDecorator;
1208};
1209