@typespec/compiler 0.63.0-dev.1 → 0.63.0-dev.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/generated-defs/TypeSpec.Prototypes.d.ts +1 -1
  2. package/dist/generated-defs/TypeSpec.Prototypes.d.ts.map +1 -1
  3. package/dist/generated-defs/TypeSpec.d.ts +198 -79
  4. package/dist/generated-defs/TypeSpec.d.ts.map +1 -1
  5. package/dist/manifest.js +2 -2
  6. package/dist/src/core/index.d.ts +1 -0
  7. package/dist/src/core/index.d.ts.map +1 -1
  8. package/dist/src/core/index.js +1 -0
  9. package/dist/src/core/index.js.map +1 -1
  10. package/dist/src/core/logger/logger.js +1 -1
  11. package/dist/src/core/logger/logger.js.map +1 -1
  12. package/dist/src/core/messages.d.ts +56 -2
  13. package/dist/src/core/messages.d.ts.map +1 -1
  14. package/dist/src/core/messages.js +20 -0
  15. package/dist/src/core/messages.js.map +1 -1
  16. package/dist/src/core/visibility/core.d.ts +240 -0
  17. package/dist/src/core/visibility/core.d.ts.map +1 -0
  18. package/dist/src/core/visibility/core.js +483 -0
  19. package/dist/src/core/visibility/core.js.map +1 -0
  20. package/dist/src/core/visibility/index.d.ts +3 -0
  21. package/dist/src/core/visibility/index.d.ts.map +1 -0
  22. package/dist/src/core/visibility/index.js +5 -0
  23. package/dist/src/core/visibility/index.js.map +1 -0
  24. package/dist/src/core/visibility/lifecycle.d.ts +28 -0
  25. package/dist/src/core/visibility/lifecycle.d.ts.map +1 -0
  26. package/dist/src/core/visibility/lifecycle.js +68 -0
  27. package/dist/src/core/visibility/lifecycle.js.map +1 -0
  28. package/dist/src/experimental/mutators.d.ts +4 -0
  29. package/dist/src/experimental/mutators.d.ts.map +1 -1
  30. package/dist/src/experimental/mutators.js +17 -0
  31. package/dist/src/experimental/mutators.js.map +1 -1
  32. package/dist/src/experimental/typekit/define-kit.js +3 -3
  33. package/dist/src/experimental/typekit/define-kit.js.map +1 -1
  34. package/dist/src/experimental/typekit/kits/model-property.d.ts +2 -2
  35. package/dist/src/experimental/typekit/kits/model-property.d.ts.map +1 -1
  36. package/dist/src/experimental/typekit/kits/model-property.js +4 -3
  37. package/dist/src/experimental/typekit/kits/model-property.js.map +1 -1
  38. package/dist/src/lib/decorators.d.ts +5 -28
  39. package/dist/src/lib/decorators.d.ts.map +1 -1
  40. package/dist/src/lib/decorators.js +6 -83
  41. package/dist/src/lib/decorators.js.map +1 -1
  42. package/dist/src/lib/key.d.ts +6 -0
  43. package/dist/src/lib/key.d.ts.map +1 -0
  44. package/dist/src/lib/key.js +7 -0
  45. package/dist/src/lib/key.js.map +1 -0
  46. package/dist/src/lib/tsp-index.d.ts.map +1 -1
  47. package/dist/src/lib/tsp-index.js +10 -4
  48. package/dist/src/lib/tsp-index.js.map +1 -1
  49. package/dist/src/lib/utils.d.ts +9 -1
  50. package/dist/src/lib/utils.d.ts.map +1 -1
  51. package/dist/src/lib/utils.js +14 -0
  52. package/dist/src/lib/utils.js.map +1 -1
  53. package/dist/src/lib/visibility.d.ts +33 -0
  54. package/dist/src/lib/visibility.d.ts.map +1 -0
  55. package/dist/src/lib/visibility.js +367 -0
  56. package/dist/src/lib/visibility.js.map +1 -0
  57. package/lib/std/decorators.tsp +1 -100
  58. package/lib/std/main.tsp +1 -0
  59. package/lib/std/visibility.tsp +400 -0
  60. package/package.json +1 -1
@@ -0,0 +1,400 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+
4
+ import "../../dist/src/lib/tsp-index.js";
5
+
6
+ using TypeSpec.Reflection;
7
+
8
+ namespace TypeSpec;
9
+
10
+ /**
11
+ * Indicates that a property is only considered to be present or applicable ("visible") with
12
+ * the in the given named contexts ("visibilities"). When a property has no visibilities applied
13
+ * to it, it is implicitly visible always.
14
+ *
15
+ * As far as the TypeSpec core library is concerned, visibilities are open-ended and can be arbitrary
16
+ * strings, but the following visibilities are well-known to standard libraries and should be used
17
+ * with standard emitters that interpret them as follows:
18
+ *
19
+ * - "read": output of any operation.
20
+ * - "create": input to operations that create an entity..
21
+ * - "query": input to operations that read data.
22
+ * - "update": input to operations that update data.
23
+ * - "delete": input to operations that delete data.
24
+ *
25
+ * See also: [Automatic visibility](https://typespec.io/docs/libraries/http/operations#automatic-visibility)
26
+ *
27
+ * @param visibilities List of visibilities which apply to this property.
28
+ *
29
+ * @example
30
+ *
31
+ * ```typespec
32
+ * model Dog {
33
+ * // the service will generate an ID, so you don't need to send it.
34
+ * @visibility(Lifecycle.Read) id: int32;
35
+ * // the service will store this secret name, but won't ever return it
36
+ * @visibility(Lifecycle.Create, Lifecycle.Update) secretName: string;
37
+ * // the regular name is always present
38
+ * name: string;
39
+ * }
40
+ * ```
41
+ */
42
+ extern dec visibility(target: ModelProperty, ...visibilities: valueof (string | EnumMember)[]);
43
+
44
+ /**
45
+ * Indicates that a property is not visible in the given visibility class.
46
+ *
47
+ * This decorator removes all active visibility modifiers from the property within
48
+ * the given visibility class.
49
+ *
50
+ * @param visibilityClass The visibility class to make the property invisible within.
51
+ *
52
+ * @example
53
+ * ```typespec
54
+ * model Example {
55
+ * @invisible(Lifecycle)
56
+ * hidden_property: string;
57
+ * }
58
+ * ```
59
+ */
60
+ extern dec invisible(target: ModelProperty, visibilityClass: Enum);
61
+
62
+ /**
63
+ * Removes visibility modifiers from a property.
64
+ *
65
+ * If the visibility modifiers for a visibility class have not been initialized,
66
+ * this decorator will use the default visibility modifiers for the visibility
67
+ * class as the default modifier set.
68
+ *
69
+ * @param target The property to remove visibility from.
70
+ * @param visibilities The visibility modifiers to remove from the target property.
71
+ *
72
+ * @example
73
+ * ```typespec
74
+ * model Example {
75
+ * // This property will have the Create and Update visibilities, but not the
76
+ * // Read visibility, since it is removed.
77
+ * @removeVisibility(Lifecycle.Read)
78
+ * secret_property: string;
79
+ * }
80
+ * ```
81
+ */
82
+ extern dec removeVisibility(target: ModelProperty, ...visibilities: valueof EnumMember[]);
83
+
84
+ /**
85
+ * Removes properties that are not considered to be present or applicable
86
+ * ("visible") in the given named contexts ("visibilities"). Can be used
87
+ * together with spread to effectively spread only visible properties into
88
+ * a new model.
89
+ *
90
+ * See also: [Automatic visibility](https://typespec.io/docs/libraries/http/operations#automatic-visibility)
91
+ *
92
+ * When using an emitter that applies visibility automatically, it is generally
93
+ * not necessary to use this decorator.
94
+ *
95
+ * @param visibilities List of visibilities which apply to this property.
96
+ *
97
+ * @example
98
+ * ```typespec
99
+ * model Dog {
100
+ * @visibility("read") id: int32;
101
+ * @visibility("create", "update") secretName: string;
102
+ * name: string;
103
+ * }
104
+ *
105
+ * // The spread operator will copy all the properties of Dog into DogRead,
106
+ * // and @withVisibility will then remove those that are not visible with
107
+ * // create or update visibility.
108
+ * //
109
+ * // In this case, the id property is removed, and the name and secretName
110
+ * // properties are kept.
111
+ * @withVisibility("create", "update")
112
+ * model DogCreateOrUpdate {
113
+ * ...Dog;
114
+ * }
115
+ *
116
+ * // In this case the id and name properties are kept and the secretName property
117
+ * // is removed.
118
+ * @withVisibility("read")
119
+ * model DogRead {
120
+ * ...Dog;
121
+ * }
122
+ * ```
123
+ */
124
+ extern dec withVisibility(target: Model, ...visibilities: valueof (string | EnumMember)[]);
125
+
126
+ /**
127
+ * Set the visibility of key properties in a model if not already set.
128
+ *
129
+ * This will set the visibility modifiers of all key properties in the model if the visibility is not already _explicitly_ set,
130
+ * but will not change the visibility of any properties that have visibility set _explicitly_, even if the visibility
131
+ * is the same as the default visibility.
132
+ *
133
+ * Visibility may be explicitly set using any of the following decorators:
134
+ *
135
+ * - `@visibility`
136
+ * - `@removeVisibility`
137
+ * - `@invisible`
138
+ *
139
+ * @param visibility The desired default visibility value. If a key property already has visibility set, it will not be changed.
140
+ */
141
+ extern dec withDefaultKeyVisibility(target: Model, visibility: valueof string | EnumMember);
142
+
143
+ /**
144
+ * Sets which visibilities apply to parameters for the given operation.
145
+ *
146
+ * @param visibilities List of visibility strings which apply to this operation.
147
+ */
148
+ extern dec parameterVisibility(target: Operation, ...visibilities: valueof (string | EnumMember)[]);
149
+
150
+ /**
151
+ * Sets which visibilities apply to the return type for the given operation.
152
+ * @param visibilities List of visibility strings which apply to this operation.
153
+ */
154
+ extern dec returnTypeVisibility(
155
+ target: Operation,
156
+ ...visibilities: valueof (string | EnumMember)[]
157
+ );
158
+
159
+ /**
160
+ * Returns the model with non-updateable properties removed.
161
+ */
162
+ extern dec withUpdateableProperties(target: Model);
163
+
164
+ /**
165
+ * Declares the default visibility modifiers for a visibility class.
166
+ *
167
+ * The default modifiers are used when a property does not have any visibility decorators
168
+ * applied to it.
169
+ *
170
+ * The modifiers passed to this decorator _MUST_ be members of the target Enum.
171
+ *
172
+ * @param visibilities the list of modifiers to use as the default visibility modifiers.
173
+ */
174
+ extern dec defaultVisibility(target: Enum, ...visibilities: valueof EnumMember[]);
175
+
176
+ /**
177
+ * A visibility class for resource lifecycle phases.
178
+ *
179
+ * These visibilities control whether a property is visible during the create, read, and update phases of a resource's
180
+ * lifecycle.
181
+ *
182
+ * @example
183
+ * ```typespec
184
+ * model Dog {
185
+ * @visibility(Lifecycle.Read) id: int32;
186
+ * @visibility(Lifecycle.Create, Lifecycle.Update) secretName: string;
187
+ * name: string;
188
+ * }
189
+ * ```
190
+ *
191
+ * In this example, the `id` property is only visible during the read phase, and the `secretName` property is only visible
192
+ * during the create and update phases. This means that the server will return the `id` property when returning a `Dog`,
193
+ * but the client will not be able to set or update it. In contrast, the `secretName` property can be set when creating
194
+ * or updating a `Dog`, but the server will never return it. The `name` property has no visibility modifiers and is
195
+ * therefore visible in all phases.
196
+ */
197
+ enum Lifecycle {
198
+ Create,
199
+ Read,
200
+ Update,
201
+ }
202
+
203
+ /**
204
+ * A visibility filter, used to specify which properties should be included when
205
+ * using the `withVisibilityFilter` decorator.
206
+ *
207
+ * The filter matches any property with ALL of the following:
208
+ * - If the `any` key is present, the property must have at least one of the specified visibilities.
209
+ * - If the `all` key is present, the property must have all of the specified visibilities.
210
+ * - If the `none` key is present, the property must have none of the specified visibilities.
211
+ */
212
+ model VisibilityFilter {
213
+ any?: EnumMember[];
214
+ all?: EnumMember[];
215
+ none?: EnumMember[];
216
+ }
217
+
218
+ /**
219
+ * Applies the given visibility filter to the properties of the target model.
220
+ *
221
+ * This transformation is recursive, so it will also apply the filter to any nested
222
+ * or referenced models that are the types of any properties in the `target`.
223
+ *
224
+ * @param target The model to apply the visibility filter to.
225
+ * @param filter The visibility filter to apply to the properties of the target model.
226
+ *
227
+ * @example
228
+ * ```typespec
229
+ * model Dog {
230
+ * @visibility(Lifecycle.Read)
231
+ * id: int32;
232
+ *
233
+ * name: string;
234
+ * }
235
+ *
236
+ * @withVisibilityFilter(#{ all: #[Lifecycle.Read] })
237
+ * model DogRead {
238
+ * ...Dog
239
+ * }
240
+ * ```
241
+ */
242
+ extern dec withVisibilityFilter(target: Model, filter: valueof VisibilityFilter);
243
+
244
+ /**
245
+ * Transforms the `target` model to include only properties that are visible during the
246
+ * "Update" lifecycle phase.
247
+ *
248
+ * Any nested models of optional properties will be transformed into the "CreateOrUpdate"
249
+ * lifecycle phase instead of the "Update" lifecycle phase, so that nested models may be
250
+ * fully updated.
251
+ *
252
+ * @param target The model to apply the transformation to.
253
+ *
254
+ * @example
255
+ * ```typespec
256
+ * model Dog {
257
+ * @visibility(Lifecycle.Read)
258
+ * id: int32;
259
+ *
260
+ * @visibility(Lifecycle.Create, Lifecycle.Update)
261
+ * secretName: string;
262
+ *
263
+ * name: string;
264
+ * }
265
+ *
266
+ * @withLifecycleUpdate
267
+ * model DogUpdate {
268
+ * ...Dog
269
+ * }
270
+ * ```
271
+ */
272
+ extern dec withLifecycleUpdate(target: Model);
273
+
274
+ /**
275
+ * A copy of the input model `T` with only the properties that are visible during the
276
+ * "Create" resource lifecycle phase.
277
+ *
278
+ * This transformation is recursive, and will include only properties that have the
279
+ * `Lifecycle.Create` visibility modifier.
280
+ *
281
+ * If a `NameTemplate` is provided, the new model will be named according to the template.
282
+ * The template uses the same syntax as the `@friendlyName` decorator.
283
+ *
284
+ * @template T The model to transform.
285
+ * @template NameTemplate The name template to use for the new model.
286
+ *
287
+ * * @example
288
+ * ```typespec
289
+ * model Dog {
290
+ * @visibility(Lifecycle.Read)
291
+ * id: int32;
292
+ *
293
+ * name: string;
294
+ * }
295
+ *
296
+ * model CreateDog is Create<Dog>;
297
+ * ```
298
+ */
299
+ @friendlyName(NameTemplate, T)
300
+ @withVisibilityFilter(#{ all: #[Lifecycle.Create] })
301
+ model Create<T extends Reflection.Model, NameTemplate extends valueof string = "Create{name}"> {
302
+ ...T;
303
+ }
304
+
305
+ /**
306
+ * A copy of the input model `T` with only the properties that are visible during the
307
+ * "Read" resource lifecycle phase.
308
+ *
309
+ * This transformation is recursive, and will include only properties that have the
310
+ * `Lifecycle.Read` visibility modifier.
311
+ *
312
+ * If a `NameTemplate` is provided, the new model will be named according to the template.
313
+ * The template uses the same syntax as the `@friendlyName` decorator.
314
+ *
315
+ * @template T The model to transform.
316
+ * @template NameTemplate The name template to use for the new model.
317
+ *
318
+ * * @example
319
+ * ```typespec
320
+ * model Dog {
321
+ * @visibility(Lifecycle.Read)
322
+ * id: int32;
323
+ *
324
+ * name: string;
325
+ * }
326
+ *
327
+ * model ReadDog is Read<Dog>;
328
+ * ```
329
+ */
330
+ @friendlyName(NameTemplate, T)
331
+ @withVisibilityFilter(#{ all: #[Lifecycle.Read] })
332
+ model Read<T extends Reflection.Model, NameTemplate extends valueof string = "Read{name}"> {
333
+ ...T;
334
+ }
335
+
336
+ /**
337
+ * A copy of the input model `T` with only the properties that are visible during the
338
+ * "Update" resource lifecycle phase.
339
+ *
340
+ * This transformation will include only the properties that have the `Lifecycle.Update`
341
+ * visibility modifier, and the types of all properties will be replaced with the
342
+ * equivalent `CreateOrUpdate` transformation.
343
+ *
344
+ * If a `NameTemplate` is provided, the new model will be named according to the template.
345
+ * The template uses the same syntax as the `@friendlyName` decorator.
346
+ *
347
+ * @template T The model to transform.
348
+ * @template NameTemplate The name template to use for the new model.
349
+ *
350
+ * * @example
351
+ * ```typespec
352
+ * model Dog {
353
+ * @visibility(Lifecycle.Read)
354
+ * id: int32;
355
+ *
356
+ * name: string;
357
+ * }
358
+ *
359
+ * model UpdateDog is Update<Dog>;
360
+ * ```
361
+ */
362
+ @friendlyName(NameTemplate, T)
363
+ @withLifecycleUpdate
364
+ model Update<T extends Reflection.Model, NameTemplate extends valueof string = "Update{name}"> {
365
+ ...T;
366
+ }
367
+
368
+ /**
369
+ * A copy of the input model `T` with only the properties that are visible during the
370
+ * "Create" or "Update" resource lifecycle phases.
371
+ *
372
+ * This transformation is recursive, and will include only properties that have the
373
+ * `Lifecycle.Create` or `Lifecycle.Update` visibility modifier.
374
+ *
375
+ * If a `NameTemplate` is provided, the new model will be named according to the template.
376
+ * The template uses the same syntax as the `@friendlyName` decorator.
377
+ *
378
+ * @template T The model to transform.
379
+ * @template NameTemplate The name template to use for the new model.
380
+ *
381
+ * * @example
382
+ * ```typespec
383
+ * model Dog {
384
+ * @visibility(Lifecycle.Read)
385
+ * id: int32;
386
+ *
387
+ * name: string;
388
+ * }
389
+ *
390
+ * model CreateOrUpdateDog is CreateOrUpdate<Dog>;
391
+ * ```
392
+ */
393
+ @friendlyName(NameTemplate, T)
394
+ @withVisibilityFilter(#{ any: #[Lifecycle.Create, Lifecycle.Update] })
395
+ model CreateOrUpdate<
396
+ T extends Reflection.Model,
397
+ NameTemplate extends valueof string = "CreateOrUpdate{name}"
398
+ > {
399
+ ...T;
400
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typespec/compiler",
3
- "version": "0.63.0-dev.1",
3
+ "version": "0.63.0-dev.3",
4
4
  "description": "TypeSpec Compiler Preview",
5
5
  "author": "Microsoft Corporation",
6
6
  "license": "MIT",