forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeSpec.ts
693 lines (653 loc) · 17.5 KB
/
TypeSpec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
import type {
DecoratorContext,
Enum,
Interface,
Model,
ModelProperty,
Namespace,
Numeric,
Operation,
Scalar,
Type,
Union,
} from "../src/index.js";
/**
* Specify how to encode the target type.
*
* @param encoding Known name of an encoding.
* @param encodedAs What target type is this being encoded as. Default to string.
* @example offsetDateTime encoded with rfc7231
*
* ```tsp
* @encode("rfc7231")
* scalar myDateTime extends offsetDateTime;
* ```
* @example utcDateTime encoded with unixTimestamp
*
* ```tsp
* @encode("unixTimestamp", int32)
* scalar myDateTime extends unixTimestamp;
* ```
*/
export type EncodeDecorator = (
context: DecoratorContext,
target: Scalar | ModelProperty,
encoding: Type,
encodedAs?: Scalar
) => void;
/**
* Attach a documentation string.
*
* @param doc Documentation string
* @param formatArgs Record with key value pair that can be interpolated in the doc.
* @example
* ```typespec
* @doc("Represent a Pet available in the PetStore")
* model Pet {}
* ```
*/
export type DocDecorator = (
context: DecoratorContext,
target: Type,
doc: string,
formatArgs?: Type
) => void;
/**
* Returns the model with required properties removed.
*/
export type WithOptionalPropertiesDecorator = (context: DecoratorContext, target: Model) => void;
/**
* Returns the model with non-updateable properties removed.
*/
export type WithUpdateablePropertiesDecorator = (context: DecoratorContext, target: Model) => void;
/**
* Returns the model with the given properties omitted.
*
* @param omit List of properties to omit
*/
export type WithoutOmittedPropertiesDecorator = (
context: DecoratorContext,
target: Model,
omit: Type
) => void;
/**
* Returns the model with only the given properties included.
*
* @param pick List of properties to include
*/
export type WithPickedPropertiesDecorator = (
context: DecoratorContext,
target: Model,
pick: Type
) => void;
/**
* Returns the model with any default values removed.
*/
export type WithoutDefaultValuesDecorator = (context: DecoratorContext, target: Model) => void;
/**
* Set the visibility of key properties in a model if not already set.
*
* @param visibility The desired default visibility value. If a key property already has a `visibility` decorator then the default visibility is not applied.
*/
export type WithDefaultKeyVisibilityDecorator = (
context: DecoratorContext,
target: Model,
visibility: string
) => void;
/**
* Typically a short, single-line description.
*
* @param summary Summary string.
* @example
* ```typespec
* @summary("This is a pet")
* model Pet {}
* ```
*/
export type SummaryDecorator = (context: DecoratorContext, target: Type, summary: string) => void;
/**
* Attach a documentation string to describe the successful return types of an operation.
* If an operation returns a union of success and errors it only describe the success. See `@errorsDoc` for error documentation.
*
* @param doc Documentation string
* @example
* ```typespec
* @returnsDoc("Returns doc")
* op get(): Pet | NotFound;
* ```
*/
export type ReturnsDocDecorator = (
context: DecoratorContext,
target: Operation,
doc: string
) => void;
/**
* Attach a documentation string to describe the error return types of an operation.
* If an operation returns a union of success and errors it only describe the errors. See `@errorsDoc` for success documentation.
*
* @param doc Documentation string
* @example
* ```typespec
* @errorsDoc("Returns doc")
* op get(): Pet | NotFound;
* ```
*/
export type ErrorsDocDecorator = (
context: DecoratorContext,
target: Operation,
doc: string
) => void;
/**
* Mark this type as deprecated.
*
* NOTE: This decorator **should not** be used, use the `#deprecated` directive instead.
*
* @deprecated Use the `#deprecated` directive instead.
* @param message Deprecation message.
* @example
* Use the `#deprecated` directive instead:
*
* ```typespec
* #deprecated "Use ActionV2"
* op Action<Result>(): Result;
* ```
*/
export type DeprecatedDecorator = (
context: DecoratorContext,
target: Type,
message: string
) => void;
/**
* Mark this namespace as describing a service and configure service properties.
*
* @param options Optional configuration for the service.
* @example
* ```typespec
* @service
* namespace PetStore;
* ```
* @example Setting service title
* ```typespec
* @service({title: "Pet store"})
* namespace PetStore;
* ```
* @example Setting service version
* ```typespec
* @service({version: "1.0"})
* namespace PetStore;
* ```
*/
export type ServiceDecorator = (
context: DecoratorContext,
target: Namespace,
options?: Type
) => void;
/**
* Specify that this model is an error type. Operations return error types when the operation has failed.
*
* @example
* ```typespec
* @error
* model PetStoreError {
* code: string;
* message: string;
* }
* ```
*/
export type ErrorDecorator = (context: DecoratorContext, target: Model) => void;
/**
* Specify a known data format hint for this string type. For example `uuid`, `uri`, etc.
* This differs from the `@pattern` decorator which is meant to specify a regular expression while `@format` accepts a known format name.
* The format names are open ended and are left to emitter to interpret.
*
* @param format format name.
* @example
* ```typespec
* @format("uuid")
* scalar uuid extends string;
* ```
*/
export type FormatDecorator = (
context: DecoratorContext,
target: Scalar | ModelProperty,
format: string
) => void;
/**
* Specify the the pattern this string should respect using simple regular expression syntax.
* The following syntax is allowed: alternations (`|`), quantifiers (`?`, `*`, `+`, and `{ }`), wildcard (`.`), and grouping parentheses.
* Advanced features like look-around, capture groups, and references are not supported.
*
* This decorator may optionally provide a custom validation _message_. Emitters may choose to use the message to provide
* context when pattern validation fails. For the sake of consistency, the message should be a phrase that describes in
* plain language what sort of content the pattern attempts to validate. For example, a complex regular expression that
* validates a GUID string might have a message like "Must be a valid GUID."
*
* @param pattern Regular expression.
* @param validationMessage Optional validation message that may provide context when validation fails.
* @example
* ```typespec
* @pattern("[a-z]+", "Must be a string consisting of only lower case letters and of at least one character.")
* scalar LowerAlpha extends string;
* ```
*/
export type PatternDecorator = (
context: DecoratorContext,
target: Scalar | ModelProperty,
pattern: string,
validationMessage?: string
) => void;
/**
* Specify the minimum length this string type should be.
*
* @param value Minimum length
* @example
* ```typespec
* @minLength(2)
* scalar Username extends string;
* ```
*/
export type MinLengthDecorator = (
context: DecoratorContext,
target: Scalar | ModelProperty,
value: Numeric
) => void;
/**
* Specify the maximum length this string type should be.
*
* @param value Maximum length
* @example
* ```typespec
* @maxLength(20)
* scalar Username extends string;
* ```
*/
export type MaxLengthDecorator = (
context: DecoratorContext,
target: Scalar | ModelProperty,
value: Numeric
) => void;
/**
* Specify the minimum number of items this array should have.
*
* @param value Minimum number
* @example
* ```typespec
* @minItems(1)
* model Endpoints is string[];
* ```
*/
export type MinItemsDecorator = (
context: DecoratorContext,
target: Type | ModelProperty,
value: Numeric
) => void;
/**
* Specify the maximum number of items this array should have.
*
* @param value Maximum number
* @example
* ```typespec
* @maxItems(5)
* model Endpoints is string[];
* ```
*/
export type MaxItemsDecorator = (
context: DecoratorContext,
target: Type | ModelProperty,
value: Numeric
) => void;
/**
* Specify the minimum value this numeric type should be.
*
* @param value Minimum value
* @example
* ```typespec
* @minValue(18)
* scalar Age is int32;
* ```
*/
export type MinValueDecorator = (
context: DecoratorContext,
target: Scalar | ModelProperty,
value: Numeric
) => void;
/**
* Specify the maximum value this numeric type should be.
*
* @param value Maximum value
* @example
* ```typespec
* @maxValue(200)
* scalar Age is int32;
* ```
*/
export type MaxValueDecorator = (
context: DecoratorContext,
target: Scalar | ModelProperty,
value: Numeric
) => void;
/**
* Specify the minimum value this numeric type should be, exclusive of the given
* value.
*
* @param value Minimum value
* @example
* ```typespec
* @minValueExclusive(0)
* scalar distance is float64;
* ```
*/
export type MinValueExclusiveDecorator = (
context: DecoratorContext,
target: Scalar | ModelProperty,
value: Numeric
) => void;
/**
* Specify the maximum value this numeric type should be, exclusive of the given
* value.
*
* @param value Maximum value
* @example
* ```typespec
* @maxValueExclusive(50)
* scalar distance is float64;
* ```
*/
export type MaxValueExclusiveDecorator = (
context: DecoratorContext,
target: Scalar | ModelProperty,
value: Numeric
) => void;
/**
* Mark this string as a secret value that should be treated carefully to avoid exposure
*
* @example
* ```typespec
* @secret
* scalar Password is string;
* ```
*/
export type SecretDecorator = (context: DecoratorContext, target: Scalar | ModelProperty) => void;
/**
* Mark this operation as a `list` operation for resource types.
*
* @deprecated Use the `listsResource` decorator in `@typespec/rest` instead.
* @param listedType Optional type of the items in the list.
*/
export type ListDecorator = (
context: DecoratorContext,
target: Operation,
listedType?: Model
) => void;
/**
* Attaches a tag to an operation, interface, or namespace. Multiple `@tag` decorators can be specified to attach multiple tags to a TypeSpec element.
*
* @param tag Tag value
*/
export type TagDecorator = (
context: DecoratorContext,
target: Namespace | Interface | Operation,
tag: string
) => void;
/**
* Specifies how a templated type should name their instances.
*
* @param name name the template instance should take
* @param formatArgs Model with key value used to interpolate the name
* @example
* ```typespec
* @friendlyName("{name}List", T)
* model List<Item> {
* value: Item[];
* nextLink: string;
* }
* ```
*/
export type FriendlyNameDecorator = (
context: DecoratorContext,
target: Type,
name: string,
formatArgs?: Type
) => void;
/**
* Provide a set of known values to a string type.
*
* @param values Known values enum.
* @example
* ```typespec
* @knownValues(KnownErrorCode)
* scalar ErrorCode extends string;
*
* enum KnownErrorCode {
* NotFound,
* Invalid,
* }
* ```
*/
export type KnownValuesDecorator = (
context: DecoratorContext,
target: Scalar | ModelProperty,
values: Enum
) => void;
/**
* Mark a model property as the key to identify instances of that type
*
* @param altName Name of the property. If not specified, the decorated property name is used.
* @example
* ```typespec
* model Pet {
* @key id: string;
* }
* ```
*/
export type KeyDecorator = (
context: DecoratorContext,
target: ModelProperty,
altName?: string
) => void;
/**
* Specify this operation is an overload of the given operation.
*
* @param overloadbase Base operation that should be a union of all overloads
* @example
* ```typespec
* op upload(data: string | bytes, @header contentType: "text/plain" | "application/octet-stream"): void;
* @overload(upload)
* op uploadString(data: string, @header contentType: "text/plain" ): void;
* @overload(upload)
* op uploadBytes(data: bytes, @header contentType: "application/octet-stream"): void;
* ```
*/
export type OverloadDecorator = (
context: DecoratorContext,
target: Operation,
overloadbase: Operation
) => void;
/**
* DEPRECATED: Use `@encodedName` instead.
*
* Provide an alternative name for this type.
*
* @param targetName Projection target
* @param projectedName Alternative name
* @example
* ```typespec
* model Certificate {
* @projectedName("json", "exp")
* expireAt: int32;
* }
* ```
*/
export type ProjectedNameDecorator = (
context: DecoratorContext,
target: Type,
targetName: string,
projectedName: string
) => void;
/**
* Provide an alternative name for this type when serialized to the given mime type.
*
* @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`)
* @param name Alternative name
* @example
* ```typespec
* model Certificate {
* @encodedName("application/json", "exp")
* @encodedName("application/xml", "expiry")
* expireAt: int32;
* }
* ```
* @example Invalid values
*
* ```typespec
* @encodedName("application/merge-patch+json", "exp")
* ^ error cannot use subtype
* ```
*/
export type EncodedNameDecorator = (
context: DecoratorContext,
target: Type,
mimeType: string,
name: string
) => void;
/**
* Specify the property to be used to discriminate this type.
*
* @param propertyName The property name to use for discrimination
* @example
* ```typespec
* @discriminator("kind")
* union Pet{ cat: Cat, dog: Dog }
*
* model Cat {kind: "cat", meow: boolean}
* model Dog {kind: "dog", bark: boolean}
* ```
*
* ```typespec
* @discriminator("kind")
* model Pet{ kind: string }
*
* model Cat extends Pet {kind: "cat", meow: boolean}
* model Dog extends Pet {kind: "dog", bark: boolean}
* ```
*/
export type DiscriminatorDecorator = (
context: DecoratorContext,
target: Model | Union,
propertyName: string
) => void;
/**
* Indicates that a property is only considered to be present or applicable ("visible") with
* the in the given named contexts ("visibilities"). When a property has no visibilities applied
* to it, it is implicitly visible always.
*
* As far as the TypeSpec core library is concerned, visibilities are open-ended and can be arbitrary
* strings, but the following visibilities are well-known to standard libraries and should be used
* with standard emitters that interpret them as follows:
*
* - "read": output of any operation.
* - "create": input to operations that create an entity..
* - "query": input to operations that read data.
* - "update": input to operations that update data.
* - "delete": input to operations that delete data.
*
* See also: [Automatic visibility](https://typespec.io/docs/libraries/http/operations#automatic-visibility)
*
* @param visibilities List of visibilities which apply to this property.
* @example
* ```typespec
* model Dog {
* // the service will generate an ID, so you don't need to send it.
* @visibility("read") id: int32;
* // the service will store this secret name, but won't ever return it
* @visibility("create", "update") secretName: string;
* // the regular name is always present
* name: string;
* }
* ```
*/
export type VisibilityDecorator = (
context: DecoratorContext,
target: ModelProperty,
...visibilities: string[]
) => void;
/**
* Removes properties that are not considered to be present or applicable
* ("visible") in the given named contexts ("visibilities"). Can be used
* together with spread to effectively spread only visible properties into
* a new model.
*
* See also: [Automatic visibility](https://typespec.io/docs/libraries/http/operations#automatic-visibility)
*
* When using an emitter that applies visibility automatically, it is generally
* not necessary to use this decorator.
*
* @param visibilities List of visibilities which apply to this property.
* @example
* ```typespec
* model Dog {
* @visibility("read") id: int32;
* @visibility("create", "update") secretName: string;
* name: string;
* }
*
* // The spread operator will copy all the properties of Dog into DogRead,
* // and @withVisibility will then remove those that are not visible with
* // create or update visibility.
* //
* // In this case, the id property is removed, and the name and secretName
* // properties are kept.
* @withVisibility("create", "update")
* model DogCreateOrUpdate {
* ...Dog;
* }
*
* // In this case the id and name properties are kept and the secretName property
* // is removed.
* @withVisibility("read")
* model DogRead {
* ...Dog;
* }
* ```
*/
export type WithVisibilityDecorator = (
context: DecoratorContext,
target: Model,
...visibilities: string[]
) => void;
/**
* A debugging decorator used to inspect a type.
*
* @param text Custom text to log
*/
export type InspectTypeDecorator = (context: DecoratorContext, target: Type, text: string) => void;
/**
* A debugging decorator used to inspect a type name.
*
* @param text Custom text to log
*/
export type InspectTypeNameDecorator = (
context: DecoratorContext,
target: Type,
text: string
) => void;
/**
* Sets which visibilities apply to parameters for the given operation.
*
* @param visibilities List of visibility strings which apply to this operation.
*/
export type ParameterVisibilityDecorator = (
context: DecoratorContext,
target: Operation,
...visibilities: string[]
) => void;
/**
* Sets which visibilities apply to the return type for the given operation.
*
* @param visibilities List of visibility strings which apply to this operation.
*/
export type ReturnTypeVisibilityDecorator = (
context: DecoratorContext,
target: Operation,
...visibilities: string[]
) => void;