@typespec/rest 0.41.0

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.
@@ -0,0 +1,280 @@
1
+ import "@typespec/http";
2
+ import "../dist/src/index.js";
3
+ import "../dist/src/internal-decorators.js";
4
+
5
+ namespace TypeSpec.Rest.Resource;
6
+
7
+ using TypeSpec.Http;
8
+
9
+ @doc("The default error response for resource operations.")
10
+ model ResourceError {
11
+ @doc("The error code.")
12
+ code: int32;
13
+
14
+ @doc("The error message.")
15
+ message: string;
16
+ }
17
+
18
+ @doc("Dynamically gathers keys of the model type T.")
19
+ @copyResourceKeyParameters
20
+ @friendlyName("{name}Key", T)
21
+ model KeysOf<T> {}
22
+
23
+ @doc("Dynamically gathers parent keys of the model type T.")
24
+ @copyResourceKeyParameters("parent")
25
+ @friendlyName("{name}ParentKey", T)
26
+ model ParentKeysOf<T> {}
27
+
28
+ @doc("Represents operation parameters for resource TResource.")
29
+ model ResourceParameters<TResource extends object> {
30
+ ...KeysOf<TResource>;
31
+ }
32
+
33
+ @doc("Represents collection operation parameters for resource TResource.")
34
+ model ResourceCollectionParameters<TResource extends object> {
35
+ ...ParentKeysOf<TResource>;
36
+ }
37
+
38
+ /**
39
+ * Represent the resource GET operation.
40
+ * @template TResource The resource model.
41
+ * @template TError The error response.
42
+ */
43
+ @Private.validateHasKey(TResource)
44
+ @Private.validateIsError(TError)
45
+ interface ResourceRead<TResource extends object, TError> {
46
+ @autoRoute
47
+ @doc("Gets an instance of the resource.")
48
+ @readsResource(TResource)
49
+ get(...ResourceParameters<TResource>): TResource | TError;
50
+ }
51
+
52
+ @doc("Resource create operation completed successfully.")
53
+ model ResourceCreatedResponse<T> {
54
+ ...CreatedResponse;
55
+ @body body: T;
56
+ }
57
+
58
+ interface ResourceCreateOrReplace<TResource extends object, TError> {
59
+ @autoRoute
60
+ @doc("Creates or replaces a instance of the resource.")
61
+ @createsOrReplacesResource(TResource)
62
+ createOrReplace(
63
+ ...ResourceParameters<TResource>,
64
+ @body resource: ResourceCreateModel<TResource>
65
+ ): TResource | ResourceCreatedResponse<TResource> | TError;
66
+ }
67
+
68
+ @friendlyName("{name}Update", TResource)
69
+ model ResourceCreateOrUpdateModel<TResource extends object>
70
+ is OptionalProperties<UpdateableProperties<DefaultKeyVisibility<TResource, "read">>>;
71
+
72
+ interface ResourceCreateOrUpdate<TResource extends object, TError> {
73
+ @autoRoute
74
+ @doc("Creates or update a instance of the resource.")
75
+ @createsOrUpdatesResource(TResource)
76
+ createOrUpdate(
77
+ ...ResourceParameters<TResource>,
78
+ @body resource: ResourceCreateOrUpdateModel<TResource>
79
+ ): TResource | ResourceCreatedResponse<TResource> | TError;
80
+ }
81
+
82
+ @friendlyName("{name}Create", TResource)
83
+ model ResourceCreateModel<TResource extends object>
84
+ is UpdateableProperties<DefaultKeyVisibility<TResource, "read">>;
85
+
86
+ interface ResourceCreate<TResource extends object, TError> {
87
+ @autoRoute
88
+ @doc("Creates a new instance of the resource.")
89
+ @createsResource(TResource)
90
+ create(
91
+ ...ResourceCollectionParameters<TResource>,
92
+ @body resource: ResourceCreateModel<TResource>
93
+ ): TResource | ResourceCreatedResponse<TResource> | TError;
94
+ }
95
+
96
+ @Private.validateHasKey(TResource)
97
+ @Private.validateIsError(TError)
98
+ interface ResourceUpdate<TResource extends object, TError> {
99
+ @autoRoute
100
+ @doc("Updates an existing instance of the resource.")
101
+ @updatesResource(TResource)
102
+ update(
103
+ ...ResourceParameters<TResource>,
104
+ @body properties: ResourceCreateOrUpdateModel<TResource>
105
+ ): TResource | TError;
106
+ }
107
+
108
+ @doc("Resource deleted successfully.")
109
+ model ResourceDeletedResponse {
110
+ @doc("The status code.")
111
+ @statusCode
112
+ _: 200;
113
+ }
114
+
115
+ @Private.validateHasKey(TResource)
116
+ @Private.validateIsError(TError)
117
+ interface ResourceDelete<TResource extends object, TError> {
118
+ @autoRoute
119
+ @doc("Deletes an existing instance of the resource.")
120
+ @deletesResource(TResource)
121
+ delete(...ResourceParameters<TResource>): ResourceDeletedResponse | TError;
122
+ }
123
+
124
+ /**
125
+ * Structure for a paging response using `value` and `nextLink` to represent pagination.
126
+ * This only provides the model structure and not actual pagination support.
127
+ * See https://github.com/microsoft/typespec/issues/705 for general paging support.
128
+ */
129
+ @doc("Paged response of {name} items", T)
130
+ @friendlyName("{name}CollectionWithNextLink", T)
131
+ model CollectionWithNextLink<T extends object> {
132
+ @doc("The items on this page")
133
+ value: T[];
134
+
135
+ @doc("The link to the next page of items")
136
+ nextLink?: ResourceLocation<T>;
137
+ }
138
+
139
+ interface ResourceList<TResource extends object, TError> {
140
+ @autoRoute
141
+ @doc("Lists all instances of the resource.")
142
+ @listsResource(TResource)
143
+ list(...ResourceCollectionParameters<TResource>): CollectionWithNextLink<TResource> | TError;
144
+ }
145
+
146
+ @Private.validateHasKey(TResource)
147
+ @Private.validateIsError(TError)
148
+ interface ResourceInstanceOperations<TResource extends object, TError>
149
+ extends ResourceRead<TResource, TError>,
150
+ ResourceUpdate<TResource, TError>,
151
+ ResourceDelete<TResource, TError> {}
152
+
153
+ @Private.validateHasKey(TResource)
154
+ @Private.validateIsError(TError)
155
+ interface ResourceCollectionOperations<TResource extends object, TError>
156
+ extends ResourceCreate<TResource, TError>,
157
+ ResourceList<TResource, TError> {}
158
+
159
+ @Private.validateHasKey(TResource)
160
+ @Private.validateIsError(TError)
161
+ interface ResourceOperations<TResource extends object, TError>
162
+ extends ResourceInstanceOperations<TResource, TError>,
163
+ ResourceCollectionOperations<TResource, TError> {}
164
+
165
+ @Private.validateHasKey(TResource)
166
+ @Private.validateIsError(TError)
167
+ interface SingletonResourceRead<TSingleton extends object, TResource extends object, TError> {
168
+ @autoRoute
169
+ @doc("Gets the singleton resource.")
170
+ @segmentOf(TSingleton)
171
+ @readsResource(TSingleton)
172
+ Get(...ResourceParameters<TResource>): TSingleton | TError;
173
+ }
174
+
175
+ @Private.validateHasKey(TResource)
176
+ @Private.validateIsError(TError)
177
+ interface SingletonResourceUpdate<TSingleton extends object, TResource extends object, TError> {
178
+ @autoRoute
179
+ @doc("Updates the singleton resource.")
180
+ @segmentOf(TSingleton)
181
+ @updatesResource(TSingleton)
182
+ Update(
183
+ ...ResourceParameters<TResource>,
184
+
185
+ @body
186
+ properties: ResourceCreateOrUpdateModel<TSingleton>
187
+ ): TSingleton | TError;
188
+ }
189
+
190
+ interface SingletonResourceOperations<TSingleton extends object, TResource extends object, TError>
191
+ extends SingletonResourceRead<TSingleton, TResource, TError>,
192
+ SingletonResourceUpdate<TSingleton, TResource, TError> {}
193
+
194
+ @Private.validateHasKey(TResource)
195
+ @Private.validateIsError(TError)
196
+ interface ExtensionResourceRead<TExtension extends object, TResource extends object, TError> {
197
+ @autoRoute
198
+ @doc("Gets an instance of the extension resource.")
199
+ @readsResource(TExtension)
200
+ Get(...ResourceParameters<TResource>, ...ResourceParameters<TExtension>): TExtension | TError;
201
+ }
202
+
203
+ interface ExtensionResourceCreateOrUpdate<
204
+ TExtension extends object,
205
+ TResource extends object,
206
+ TError
207
+ > {
208
+ @autoRoute
209
+ @doc("Creates or update a instance of the extension resource.")
210
+ @createsOrUpdatesResource(TExtension)
211
+ CreateOrUpdate(
212
+ ...ResourceParameters<TResource>,
213
+ ...ResourceParameters<TExtension>,
214
+ @body resource: ResourceCreateOrUpdateModel<TExtension>
215
+ ): TExtension | ResourceCreatedResponse<TExtension> | TError;
216
+ }
217
+
218
+ interface ExtensionResourceCreate<TExtension extends object, TResource extends object, TError> {
219
+ @autoRoute
220
+ @doc("Creates a new instance of the extension resource.")
221
+ @createsResource(TExtension)
222
+ Create(
223
+ ...ResourceParameters<TResource>,
224
+ @body resource: ResourceCreateModel<TExtension>
225
+ ): TExtension | ResourceCreatedResponse<TExtension> | TError;
226
+ }
227
+
228
+ interface ExtensionResourceUpdate<TExtension extends object, TResource extends object, TError> {
229
+ @autoRoute
230
+ @doc("Updates an existing instance of the extension resource.")
231
+ @updatesResource(TExtension)
232
+ Update(
233
+ ...ResourceParameters<TResource>,
234
+ ...ResourceParameters<TExtension>,
235
+
236
+ @body
237
+ properties: ResourceCreateOrUpdateModel<TExtension>
238
+ ): TExtension | TError;
239
+ }
240
+
241
+ interface ExtensionResourceDelete<TExtension extends object, TResource extends object, TError> {
242
+ @autoRoute
243
+ @doc("Deletes an existing instance of the extension resource.")
244
+ @deletesResource(TExtension)
245
+ Delete(
246
+ ...ResourceParameters<TResource>,
247
+ ...ResourceParameters<TExtension>
248
+ ): ResourceDeletedResponse | TError;
249
+ }
250
+
251
+ interface ExtensionResourceList<TExtension extends object, TResource extends object, TError> {
252
+ @autoRoute
253
+ @doc("Lists all instances of the extension resource.")
254
+ @listsResource(TExtension)
255
+ List(
256
+ ...ResourceParameters<TResource>,
257
+ ...ResourceCollectionParameters<TExtension>
258
+ ): CollectionWithNextLink<TExtension> | TError;
259
+ }
260
+
261
+ interface ExtensionResourceInstanceOperations<
262
+ TExtension extends object,
263
+ TResource extends object,
264
+ TError
265
+ >
266
+ extends ExtensionResourceRead<TExtension, TResource, TError>,
267
+ ExtensionResourceUpdate<TExtension, TResource, TError>,
268
+ ExtensionResourceDelete<TExtension, TResource, TError> {}
269
+
270
+ interface ExtensionResourceCollectionOperations<
271
+ TExtension extends object,
272
+ TResource extends object,
273
+ TError
274
+ >
275
+ extends ExtensionResourceCreate<TExtension, TResource, TError>,
276
+ ExtensionResourceList<TExtension, TResource, TError> {}
277
+
278
+ interface ExtensionResourceOperations<TExtension extends object, TResource extends object, TError>
279
+ extends ExtensionResourceInstanceOperations<TExtension, TResource, TError>,
280
+ ExtensionResourceCollectionOperations<TExtension, TResource, TError> {}
@@ -0,0 +1,112 @@
1
+ namespace TypeSpec.Rest;
2
+
3
+ using TypeSpec.Reflection;
4
+
5
+ /**
6
+ * This interface or operation should resolve its route automatically. To be used with resource types where the route segments area defined on the models.
7
+ *
8
+ * @example
9
+ *
10
+ * ```typespec
11
+ * @autoRoute
12
+ * interface Pets {
13
+ * get(@segment("pets") @path id: string): void; //-> route: /pets/{id}
14
+ * }
15
+ * ```
16
+ */
17
+ extern dec autoRoute(target: Interface | Operation);
18
+
19
+ /**
20
+ * Defines the preceding path segment for a @path parameter in auto-generated routes.
21
+ *
22
+ * @param name Segment that will be inserted into the operation route before the path parameter's name field.
23
+ *
24
+ * @example
25
+ * @autoRoute
26
+ * interface Pets {
27
+ * get(@segment("pets") @path id: string): void; //-> route: /pets/{id}
28
+ * }
29
+ */
30
+ extern dec segment(target: object | ModelProperty | Operation, name: string);
31
+
32
+ /**
33
+ * Returns the URL segment of a given model if it has `@segment` and `@key` decorator.
34
+ */
35
+ extern dec segmentOf(target: Operation, type: object);
36
+
37
+ /**
38
+ * Defines the separator string that is inserted before the action name in auto-generated routes for actions.
39
+ *
40
+ * @param seperator Seperator seperating the action segment from the rest of the url
41
+ */
42
+ extern dec actionSeparator(target: object | ModelProperty | Operation, seperator: "/" | ":" | "/:");
43
+
44
+ /**
45
+ * Mark this model as a resource type with a name.
46
+ *
47
+ * @param collectionName type's collection name
48
+ */
49
+ extern dec resource(target: object, collectionName: string);
50
+
51
+ /**
52
+ * Specify that this is a Read operation for a given resource.
53
+ *
54
+ * @param resource Resource marked with @resource
55
+ */
56
+ extern dec readsResource(target: Operation, resourceType: object);
57
+
58
+ /**
59
+ * Specify that this is a Create operation for a given resource.
60
+ *
61
+ * @param resource Resource marked with @resource
62
+ */
63
+ extern dec createsResource(target: Operation, resourceType: object);
64
+
65
+ /**
66
+ * Specify that this is a CreateOrReplace operation for a given resource.
67
+ *
68
+ * @param resource Resource marked with @resource
69
+ */
70
+ extern dec createsOrReplacesResource(target: Operation, resourceType: object);
71
+
72
+ /**
73
+ * Specify that this is a CreatesOrUpdate operation for a given resource.
74
+ *
75
+ * @param resource Resource marked with @resource
76
+ */
77
+ extern dec createsOrUpdatesResource(target: Operation, resourceType: object);
78
+
79
+ /**
80
+ * Specify that this is a Update operation for a given resource.
81
+ *
82
+ * @param resource Resource marked with @resource
83
+ */
84
+ extern dec updatesResource(target: Operation, resourceType: object);
85
+
86
+ /**
87
+ * Specify that this is a Delete operation for a given resource.
88
+ *
89
+ * @param resource Resource marked with @resource
90
+ */
91
+ extern dec deletesResource(target: Operation, resourceType: object);
92
+
93
+ /**
94
+ * Specify that this is a List operation for a given resource.
95
+ *
96
+ * @param resource Resource marked with @resource
97
+ */
98
+ extern dec listsResource(target: Operation, resourceType: object);
99
+
100
+ /**
101
+ * Specify this operation is an action. (Scopped to a resource item /pets/{petId}/my-action)
102
+ */
103
+ extern dec action(target: Operation, name?: string);
104
+
105
+ /**
106
+ * Specify this operation is a collection action. (Scopped to a resource, /pets/my-action)
107
+ */
108
+ extern dec collectionAction(target: Operation, resourceType: object, name?: string);
109
+
110
+ namespace Private {
111
+ extern dec resourceLocation(target: string, resourceType: object);
112
+ }
package/lib/rest.tsp ADDED
@@ -0,0 +1,9 @@
1
+ import "@typespec/http";
2
+ import "./rest-decorators.tsp";
3
+ import "./resource.tsp";
4
+
5
+ namespace TypeSpec.Rest;
6
+
7
+ @doc("The location of an instance of {name}", TResource)
8
+ @Private.resourceLocation(TResource)
9
+ scalar ResourceLocation<TResource extends object> extends url;
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@typespec/rest",
3
+ "version": "0.41.0",
4
+ "author": "Microsoft Corporation",
5
+ "description": "TypeSpec REST protocol binding",
6
+ "homepage": "https://microsoft.github.io/typespec",
7
+ "readme": "https://github.com/Microsoft/typespec/blob/master/README.md",
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/Microsoft/typespec.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/Microsoft/typespec/issues"
15
+ },
16
+ "keywords": [
17
+ "typespec"
18
+ ],
19
+ "type": "module",
20
+ "main": "dist/src/index.js",
21
+ "tspMain": "lib/rest.tsp",
22
+ "exports": {
23
+ ".": "./dist/src/index.js",
24
+ "./testing": "./dist/src/testing/index.js"
25
+ },
26
+ "typesVersions": {
27
+ "*": {
28
+ "*": [
29
+ "./dist/src/index.d.ts"
30
+ ],
31
+ "testing": [
32
+ "./dist/src/testing/index.d.ts"
33
+ ]
34
+ }
35
+ },
36
+ "engines": {
37
+ "node": ">=16.0.0"
38
+ },
39
+ "files": [
40
+ "lib/*.tsp",
41
+ "dist/**",
42
+ "!dist/test/**"
43
+ ],
44
+ "peerDependencies": {
45
+ "@typespec/compiler": "~0.41.0"
46
+ },
47
+ "devDependencies": {
48
+ "@types/mocha": "~10.0.0",
49
+ "@types/node": "~18.11.9",
50
+ "@typespec/compiler": "~0.41.0",
51
+ "@typespec/http": "~0.41.0",
52
+ "@typespec/eslint-config-typespec": "~0.6.0",
53
+ "@typespec/library-linter": "~0.41.0",
54
+ "@typespec/eslint-plugin": "~0.41.0",
55
+ "eslint": "^8.12.0",
56
+ "mocha": "~10.1.0",
57
+ "mocha-junit-reporter": "~2.2.0",
58
+ "mocha-multi-reporters": "~1.5.1",
59
+ "c8": "~7.12.0",
60
+ "rimraf": "~3.0.2",
61
+ "typescript": "~4.9.3"
62
+ },
63
+ "scripts": {
64
+ "clean": "rimraf ./dist ./temp",
65
+ "build": "tsc -p . && npm run lint-typespec-library",
66
+ "watch": "tsc -p . --watch",
67
+ "lint-typespec-library": "tsp compile . --warn-as-error --import @typespec/library-linter --no-emit",
68
+ "test": "mocha",
69
+ "test-official": "c8 mocha --forbid-only --reporter mocha-multi-reporters",
70
+ "lint": "eslint . --ext .ts --max-warnings=0",
71
+ "lint:fix": "eslint . --fix --ext .ts"
72
+ }
73
+ }