@typespec/http 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.
- package/LICENSE +21 -0
- package/README.md +106 -0
- package/dist/src/content-types.d.ts +15 -0
- package/dist/src/content-types.d.ts.map +1 -0
- package/dist/src/content-types.js +42 -0
- package/dist/src/content-types.js.map +1 -0
- package/dist/src/decorators.d.ts +82 -0
- package/dist/src/decorators.d.ts.map +1 -0
- package/dist/src/decorators.js +513 -0
- package/dist/src/decorators.js.map +1 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +11 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lib.d.ts +294 -0
- package/dist/src/lib.d.ts.map +1 -0
- package/dist/src/lib.js +121 -0
- package/dist/src/lib.js.map +1 -0
- package/dist/src/metadata.d.ts +129 -0
- package/dist/src/metadata.d.ts.map +1 -0
- package/dist/src/metadata.js +323 -0
- package/dist/src/metadata.js.map +1 -0
- package/dist/src/operations.d.ts +31 -0
- package/dist/src/operations.d.ts.map +1 -0
- package/dist/src/operations.js +162 -0
- package/dist/src/operations.js.map +1 -0
- package/dist/src/parameters.d.ts +4 -0
- package/dist/src/parameters.d.ts.map +1 -0
- package/dist/src/parameters.js +142 -0
- package/dist/src/parameters.js.map +1 -0
- package/dist/src/responses.d.ts +7 -0
- package/dist/src/responses.d.ts.map +1 -0
- package/dist/src/responses.js +186 -0
- package/dist/src/responses.js.map +1 -0
- package/dist/src/route.d.ts +23 -0
- package/dist/src/route.d.ts.map +1 -0
- package/dist/src/route.js +149 -0
- package/dist/src/route.js.map +1 -0
- package/dist/src/testing/index.d.ts +3 -0
- package/dist/src/testing/index.d.ts.map +1 -0
- package/dist/src/testing/index.js +8 -0
- package/dist/src/testing/index.js.map +1 -0
- package/dist/src/types.d.ts +254 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils.d.ts +8 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +11 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/src/validate.d.ts +3 -0
- package/dist/src/validate.d.ts.map +1 -0
- package/dist/src/validate.js +9 -0
- package/dist/src/validate.js.map +1 -0
- package/lib/auth.tsp +185 -0
- package/lib/http-decorators.tsp +206 -0
- package/lib/http.tsp +57 -0
- package/package.json +72 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
namespace TypeSpec.Http;
|
|
2
|
+
|
|
3
|
+
using TypeSpec.Reflection;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Header options.
|
|
7
|
+
*/
|
|
8
|
+
model HeaderOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Name of the header when sent over http.
|
|
11
|
+
*/
|
|
12
|
+
name?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Determines the format of the array if type array is used.
|
|
15
|
+
*/
|
|
16
|
+
format?: "csv";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Specify this property is to be sent or received as an http header.
|
|
21
|
+
*
|
|
22
|
+
* @param headerNameOrOptions Optional name of the header when sent over http or header options.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
*
|
|
26
|
+
* ```typespec
|
|
27
|
+
* op read(@header accept: string): {@header("E-Tag") eTag: string};
|
|
28
|
+
* op create(@header({name: "X-Color", format: "csv"}) colors: string[]): void;
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
extern dec header(target: ModelProperty, headerNameOrOptions?: string | HeaderOptions);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Query parameter options.
|
|
35
|
+
*/
|
|
36
|
+
model QueryOptions {
|
|
37
|
+
/**
|
|
38
|
+
* Name of the query when included in the url.
|
|
39
|
+
*/
|
|
40
|
+
name?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Determines the format of the array if type array is used.
|
|
43
|
+
*/
|
|
44
|
+
format?: "multi" | "csv";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Specify this property is to be sent as a query parameter.
|
|
49
|
+
*
|
|
50
|
+
* @param queryNameOrOptions Optional name of the query when included in the url or query parameter options.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
*
|
|
54
|
+
* ```typespec
|
|
55
|
+
* op read(@query select: string, @query("order-by") orderBy: string): void;
|
|
56
|
+
* op list(@query({name: "id", format: "multi"}) ids: string[]): void;
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
extern dec query(target: ModelProperty, queryNameOrOptions?: string | QueryOptions);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Explicitly specify that this property is to be interpolated as a path parameter.
|
|
63
|
+
*
|
|
64
|
+
* @param paramName Optional name of the parameter in the url template.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* ```typespec
|
|
69
|
+
* @route("/read/{explicit}/things/{implicit}")
|
|
70
|
+
* op read(@path explicit: string, implicit: string): void;
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
extern dec path(target: ModelProperty, paramName?: string);
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Explicitly specify that this property is to be set as the body
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
*
|
|
80
|
+
* ```typespec
|
|
81
|
+
* op upload(@body image: bytes): void;
|
|
82
|
+
* op download(): {@body image: bytes};
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
extern dec body(target: ModelProperty);
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Specify the status code for this response. Property type must be a status code integer or a union of status code integer.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
*
|
|
92
|
+
* ```typespec
|
|
93
|
+
* op read(): {@statusCode: 200, @body pet: Pet}
|
|
94
|
+
* op create(): {@statusCode: 201 | 202}
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
extern dec statusCode(target: ModelProperty);
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Specify the http verb for the target operation to be `GET`.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
*
|
|
104
|
+
* ```typespec
|
|
105
|
+
* @get op read(): string
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
extern dec get(target: Operation);
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Specify the http verb for the target operation to be `PUT`.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
*
|
|
115
|
+
* ```typespec
|
|
116
|
+
* @put op set(pet: Pet): void
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
extern dec put(target: Operation);
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Specify the http verb for the target operation to be `POST`.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
*
|
|
126
|
+
* ```typespec
|
|
127
|
+
* @post op create(pet: Pet): void
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
extern dec post(target: Operation);
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Specify the http verb for the target operation to be `PATCH`.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
*
|
|
137
|
+
* ```typespec
|
|
138
|
+
* @patch op update(pet: Pet): void
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
extern dec patch(target: Operation);
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Specify the http verb for the target operation to be `DELETE`.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
*
|
|
148
|
+
* ```typespec
|
|
149
|
+
* @delete op set(petId: string): void
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
extern dec delete(target: Operation);
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Specify the http verb for the target operation to be `HEAD`.
|
|
156
|
+
* @example
|
|
157
|
+
*
|
|
158
|
+
* ```typespec
|
|
159
|
+
* @head op ping(petId: string): void
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
extern dec head(target: Operation);
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Specify the endpoint for this service.
|
|
166
|
+
* @param url Server endpoint
|
|
167
|
+
* @param url Description of the endpoint
|
|
168
|
+
* @param parameters Optional set of parameters used to interpolate the url.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typespec
|
|
172
|
+
* @service
|
|
173
|
+
* @server("https://example.com", "Single server endpoint")
|
|
174
|
+
* namespace PetStore;
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* @example parameterized
|
|
178
|
+
*
|
|
179
|
+
* ```typespec
|
|
180
|
+
* @server("https://{region}.foo.com", "Regional endpoint", {
|
|
181
|
+
* @doc("Region name")
|
|
182
|
+
* region?: string = "westus",
|
|
183
|
+
* })
|
|
184
|
+
* ```
|
|
185
|
+
*
|
|
186
|
+
*/
|
|
187
|
+
extern dec server(target: Namespace, url: string, description: string, parameters?: object);
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Specify this service authentication. See the [documentation in the Http library][https://microsoft.github.io/typespec/standard-library/rest/authentication] for full details.
|
|
191
|
+
*
|
|
192
|
+
* @param auth Authentication configuration. Can be a single security scheme, a union(either option is valid authentication) or a tuple(Must use all authentication together)
|
|
193
|
+
* @example
|
|
194
|
+
*
|
|
195
|
+
* ```typespec
|
|
196
|
+
* @service
|
|
197
|
+
* @useAuth(BasicAuth)
|
|
198
|
+
* namespace PetStore;
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
extern dec useAuth(target: Namespace, auth: object | Union | object[]);
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Specify if inapplicable metadata should be included in the payload for the given entity.
|
|
205
|
+
*/
|
|
206
|
+
extern dec includeInapplicableMetadataInPayload(target: unknown, value: boolean);
|
package/lib/http.tsp
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import "../dist/src/index.js";
|
|
2
|
+
import "./http-decorators.tsp";
|
|
3
|
+
import "./auth.tsp";
|
|
4
|
+
|
|
5
|
+
namespace TypeSpec.Http;
|
|
6
|
+
|
|
7
|
+
using Private;
|
|
8
|
+
|
|
9
|
+
model Response<Status> {
|
|
10
|
+
@doc("The status code.")
|
|
11
|
+
@statusCode
|
|
12
|
+
statusCode: Status;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Defines a model with a single property of the given type, marked with `@body`.
|
|
17
|
+
*
|
|
18
|
+
* This can be useful in situations where you cannot use a bare T as the body
|
|
19
|
+
* and it is awkward to add a property.
|
|
20
|
+
*/
|
|
21
|
+
model Body<T> {
|
|
22
|
+
@body
|
|
23
|
+
body: T;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
model LocationHeader {
|
|
27
|
+
@doc("The Location header contains the URL where the status of the long running operation can be checked.")
|
|
28
|
+
@header
|
|
29
|
+
location: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Don't put @doc on these, change `getStatusCodeDescription` implementation
|
|
33
|
+
// to update the default descriptions for these status codes. This ensures
|
|
34
|
+
// that we get consistent emit between different ways to spell the same
|
|
35
|
+
// responses in TypeSpec.
|
|
36
|
+
model OkResponse is Response<200>;
|
|
37
|
+
model CreatedResponse is Response<201>;
|
|
38
|
+
model AcceptedResponse is Response<202>;
|
|
39
|
+
model NoContentResponse is Response<204>;
|
|
40
|
+
model MovedResponse is Response<301> {
|
|
41
|
+
...LocationHeader;
|
|
42
|
+
}
|
|
43
|
+
model NotModifiedResponse is Response<304>;
|
|
44
|
+
model BadRequestResponse is Response<400>;
|
|
45
|
+
model UnauthorizedResponse is Response<401>;
|
|
46
|
+
model ForbiddenResponse is Response<403>;
|
|
47
|
+
model NotFoundResponse is Response<404>;
|
|
48
|
+
model ConflictResponse is Response<409>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Produces a new model with the same properties as T, but with `@query`,
|
|
52
|
+
*`@header`, `@body`, and `@path` decorators removed from all properties.
|
|
53
|
+
*/
|
|
54
|
+
@plainData
|
|
55
|
+
model PlainData<T> {
|
|
56
|
+
...T;
|
|
57
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@typespec/http",
|
|
3
|
+
"version": "0.41.0",
|
|
4
|
+
"author": "Microsoft Corporation",
|
|
5
|
+
"description": "TypeSpec HTTP protocol binding",
|
|
6
|
+
"homepage": "https://github.com/Microsoft/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/http.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/eslint-config-typespec": "~0.6.0",
|
|
52
|
+
"@typespec/library-linter": "~0.41.0",
|
|
53
|
+
"@typespec/eslint-plugin": "~0.41.0",
|
|
54
|
+
"eslint": "^8.12.0",
|
|
55
|
+
"mocha": "~10.1.0",
|
|
56
|
+
"mocha-junit-reporter": "~2.2.0",
|
|
57
|
+
"mocha-multi-reporters": "~1.5.1",
|
|
58
|
+
"c8": "~7.12.0",
|
|
59
|
+
"rimraf": "~3.0.2",
|
|
60
|
+
"typescript": "~4.9.3"
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"clean": "rimraf ./dist ./temp",
|
|
64
|
+
"build": "tsc -p . && npm run lint-typespec-library",
|
|
65
|
+
"watch": "tsc -p . --watch",
|
|
66
|
+
"lint-typespec-library": "tsp compile . --warn-as-error --import @typespec/library-linter --no-emit",
|
|
67
|
+
"test": "mocha",
|
|
68
|
+
"test-official": "c8 mocha --forbid-only --reporter mocha-multi-reporters",
|
|
69
|
+
"lint": "eslint . --ext .ts --max-warnings=0",
|
|
70
|
+
"lint:fix": "eslint . --fix --ext .ts"
|
|
71
|
+
}
|
|
72
|
+
}
|