@typespec/http-client-js 0.16.1 → 0.16.2-dev.2

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 (225) hide show
  1. package/dist/tsconfig.tsbuildinfo +1 -1
  2. package/package.json +17 -13
  3. package/.github/copilot-instructions.md +0 -180
  4. package/.testignore +0 -19
  5. package/.turbo/turbo-build.log +0 -3
  6. package/CHANGELOG.md +0 -159
  7. package/eng/scripts/calculate-coverage.ts +0 -33
  8. package/eng/scripts/emit-e2e.ts +0 -317
  9. package/eng/scripts/run-e2e-tests.ts +0 -166
  10. package/eng/scripts/tspconfig.yaml +0 -6
  11. package/eng/scripts/upload-spector-results.ts +0 -139
  12. package/src/components/client-context/client-context-declaration.tsx +0 -26
  13. package/src/components/client-context/client-context-factory.tsx +0 -218
  14. package/src/components/client-context/client-context-options.tsx +0 -36
  15. package/src/components/client-context/client-context.tsx +0 -23
  16. package/src/components/client-context/parametrized-endpoint.tsx +0 -51
  17. package/src/components/client-directory.tsx +0 -51
  18. package/src/components/client-operation.tsx +0 -102
  19. package/src/components/client.tsx +0 -177
  20. package/src/components/encoding-provider.tsx +0 -17
  21. package/src/components/external-packages/ts-http-runtime.ts +0 -28
  22. package/src/components/external-packages/uri-template.ts +0 -11
  23. package/src/components/http-request-options.tsx +0 -88
  24. package/src/components/http-request-parameters-expression.tsx +0 -191
  25. package/src/components/http-request.tsx +0 -80
  26. package/src/components/http-response.tsx +0 -74
  27. package/src/components/index.ts +0 -12
  28. package/src/components/models.tsx +0 -26
  29. package/src/components/operation-handlers/index.tsx +0 -2
  30. package/src/components/operation-handlers/operation-pipeline.tsx +0 -33
  31. package/src/components/operation-handlers/paging/page-item.tsx +0 -14
  32. package/src/components/operation-handlers/paging/page-response.tsx +0 -57
  33. package/src/components/operation-handlers/paging/page-settings.tsx +0 -53
  34. package/src/components/operation-handlers/paging/paginated-operation-handler.tsx +0 -200
  35. package/src/components/operation-handlers/paging/request-send.tsx +0 -85
  36. package/src/components/operation-handlers/paging/response-deserialize.tsx +0 -60
  37. package/src/components/operation-handlers/paging/util.ts +0 -70
  38. package/src/components/operation-handlers/types.tsx +0 -18
  39. package/src/components/operation-options.tsx +0 -42
  40. package/src/components/operation-parameters.tsx +0 -44
  41. package/src/components/output.tsx +0 -32
  42. package/src/components/serializers.tsx +0 -74
  43. package/src/components/static-helpers/bytes-encoding.tsx +0 -61
  44. package/src/components/static-helpers/interfaces.tsx +0 -31
  45. package/src/components/static-helpers/multipart-helpers.tsx +0 -80
  46. package/src/components/static-helpers/paging-helper.tsx +0 -202
  47. package/src/components/static-helpers/rest-error.tsx +0 -102
  48. package/src/components/testing/client-options.tsx +0 -30
  49. package/src/components/transforms/content-type-encoding-provider.tsx +0 -26
  50. package/src/components/transforms/data-transform.tsx +0 -40
  51. package/src/components/transforms/json/json-array-transform.tsx +0 -87
  52. package/src/components/transforms/json/json-model-additional-properties-transform.tsx +0 -54
  53. package/src/components/transforms/json/json-model-base-transform.tsx +0 -24
  54. package/src/components/transforms/json/json-model-property-transform.tsx +0 -53
  55. package/src/components/transforms/json/json-model-transform.tsx +0 -115
  56. package/src/components/transforms/json/json-record-transform.tsx +0 -88
  57. package/src/components/transforms/json/json-transform-discriminator.tsx +0 -111
  58. package/src/components/transforms/json/json-transform.tsx +0 -120
  59. package/src/components/transforms/json/union-transform.tsx +0 -87
  60. package/src/components/transforms/json/utils/transport-namer.ts +0 -12
  61. package/src/components/transforms/multipart/array-part-transform.tsx +0 -44
  62. package/src/components/transforms/multipart/file-part-transform.tsx +0 -45
  63. package/src/components/transforms/multipart/multipart-transform.tsx +0 -30
  64. package/src/components/transforms/multipart/part-transform.tsx +0 -22
  65. package/src/components/transforms/multipart/simple-part-transform.tsx +0 -42
  66. package/src/components/transforms/operation-transform-declaration.tsx +0 -83
  67. package/src/components/transforms/operation-transform-expression.tsx +0 -62
  68. package/src/components/transforms/scalar-transform.tsx +0 -301
  69. package/src/components/transforms/transform-name-policy.ts +0 -134
  70. package/src/components/utils/unpack-model-property.ts +0 -44
  71. package/src/components/utils/url-template.tsx +0 -25
  72. package/src/context/encoding/encoding-context.tsx +0 -28
  73. package/src/context/encoding/types.ts +0 -6
  74. package/src/emitter.tsx +0 -85
  75. package/src/index.ts +0 -5
  76. package/src/lib.ts +0 -120
  77. package/src/testing/index.ts +0 -11
  78. package/src/utils/client-discovery.ts +0 -28
  79. package/src/utils/multipart.ts +0 -29
  80. package/src/utils/operations.ts +0 -112
  81. package/src/utils/parameters.tsx +0 -155
  82. package/test/e2e/assets/image.jpg +0 -0
  83. package/test/e2e/assets/image.png +0 -0
  84. package/test/e2e/http/authentication/api-key/main.test.ts +0 -38
  85. package/test/e2e/http/authentication/oauth2/main.test.ts +0 -31
  86. package/test/e2e/http/authentication/union/main.test.ts +0 -26
  87. package/test/e2e/http/encode/bytes/main.test.ts +0 -191
  88. package/test/e2e/http/encode/datetime/main.test.ts +0 -176
  89. package/test/e2e/http/encode/duration/main.test.ts +0 -126
  90. package/test/e2e/http/encode/numeric/main.test.ts +0 -26
  91. package/test/e2e/http/parameters/basic/main.test.ts +0 -35
  92. package/test/e2e/http/parameters/body-optionality/main.test.ts +0 -43
  93. package/test/e2e/http/parameters/collection-format/main.test.ts +0 -40
  94. package/test/e2e/http/parameters/spread/main.test.ts +0 -56
  95. package/test/e2e/http/payload/content-negotiation/main.test.ts +0 -40
  96. package/test/e2e/http/payload/json-merge-patch/main.test.ts +0 -122
  97. package/test/e2e/http/payload/media-type/main.test.ts +0 -28
  98. package/test/e2e/http/payload/multipart/main.test.ts +0 -200
  99. package/test/e2e/http/payload/pageable/main.test.ts +0 -78
  100. package/test/e2e/http/payload/xml/main.test.ts +0 -287
  101. package/test/e2e/http/routes/main.test.ts +0 -284
  102. package/test/e2e/http/serialization/encoded-name/json/main.test.ts +0 -20
  103. package/test/e2e/http/server/endpoint/not-defined/main.test.ts +0 -11
  104. package/test/e2e/http/server/path/multiple/main.test.ts +0 -22
  105. package/test/e2e/http/server/path/single/main.test.ts +0 -11
  106. package/test/e2e/http/server/versions/not-versioned/main.test.ts +0 -22
  107. package/test/e2e/http/server/versions/versioned/main.test.ts +0 -28
  108. package/test/e2e/http/special-headers/conditional-request/main.test.ts +0 -35
  109. package/test/e2e/http/special-headers/repeatability/main.test.ts +0 -18
  110. package/test/e2e/http/special-words/main.test.ts +0 -434
  111. package/test/e2e/http/type/array/array.test.ts +0 -256
  112. package/test/e2e/http/type/dictionary/main.test.ts +0 -229
  113. package/test/e2e/http/type/enum/extensible/main.test.ts +0 -34
  114. package/test/e2e/http/type/enum/fixed/main.test.ts +0 -29
  115. package/test/e2e/http/type/model/empty/main.test.ts +0 -21
  116. package/test/e2e/http/type/model/inheritance/enum-discriminator/main.test.ts +0 -51
  117. package/test/e2e/http/type/model/inheritance/nested-discriminator/main.test.ts +0 -168
  118. package/test/e2e/http/type/model/inheritance/not-discriminated/main.test.ts +0 -42
  119. package/test/e2e/http/type/model/inheritance/recursive/main.test.ts +0 -46
  120. package/test/e2e/http/type/model/inheritance/single-discriminator/main.test.ts +0 -99
  121. package/test/e2e/http/type/model/visibility/main.test.ts +0 -64
  122. package/test/e2e/http/type/property/additional-properties/main.test.ts +0 -185
  123. package/test/e2e/http/type/property/additional-properties/spreads.test.ts +0 -448
  124. package/test/e2e/http/type/property/nullable/main.test.ts +0 -275
  125. package/test/e2e/http/type/property/optionality/main.test.ts +0 -256
  126. package/test/e2e/http/type/property/value-types/main.test.ts +0 -193
  127. package/test/e2e/http/type/scalar/main.test.ts +0 -133
  128. package/test/e2e/http/type/scalar/scalar.test.ts +0 -95
  129. package/test/e2e/http/type/union/main.test.ts +0 -207
  130. package/test/e2e/http/versioning/added/main.test.ts +0 -60
  131. package/test/e2e/http/versioning/madeOptional/main.test.ts +0 -34
  132. package/test/e2e/http/versioning/removed/main.test.ts +0 -75
  133. package/test/e2e/http/versioning/renamedFrom/main.test.ts +0 -52
  134. package/test/e2e/http/versioning/returnTypeChangedFrom/main.test.ts +0 -22
  135. package/test/e2e/http/versioning/typeChangedFrom/main.test.ts +0 -31
  136. package/test/scenarios/additional-properties/extends.md +0 -67
  137. package/test/scenarios/additional-properties/is.md +0 -42
  138. package/test/scenarios/additional-properties/spread.md +0 -68
  139. package/test/scenarios/auth/basic_auth.md +0 -68
  140. package/test/scenarios/auth/bearer.md +0 -68
  141. package/test/scenarios/auth/client_parameters.md +0 -78
  142. package/test/scenarios/auth/client_structure.md +0 -36
  143. package/test/scenarios/auth/key_credential.md +0 -69
  144. package/test/scenarios/auth/oauth2.md +0 -90
  145. package/test/scenarios/auth/sub_client_override.md +0 -114
  146. package/test/scenarios/client/client_context.md +0 -34
  147. package/test/scenarios/client/dotted_namespace.md +0 -27
  148. package/test/scenarios/client/global_namespace.md +0 -69
  149. package/test/scenarios/client/multiple_top_level_clients.md +0 -72
  150. package/test/scenarios/client/nested_client.md +0 -102
  151. package/test/scenarios/client/wrapping_namespace.md +0 -41
  152. package/test/scenarios/encoding/bytes_body.md +0 -44
  153. package/test/scenarios/encoding/bytes_json_property.md +0 -55
  154. package/test/scenarios/encoding/bytes_nullable.md +0 -139
  155. package/test/scenarios/encoding/header_bytes.md +0 -130
  156. package/test/scenarios/encoding/header_date.md +0 -216
  157. package/test/scenarios/encoding/model_with_bytes_property.md +0 -45
  158. package/test/scenarios/encoding/query_bytes.md +0 -42
  159. package/test/scenarios/encoding/query_date.md +0 -216
  160. package/test/scenarios/http-operations/basic-request.md +0 -144
  161. package/test/scenarios/http-operations/basic-response.md +0 -219
  162. package/test/scenarios/http-operations/basic.md +0 -128
  163. package/test/scenarios/http-operations/constant-header.md +0 -33
  164. package/test/scenarios/http-operations/create-operation.md +0 -58
  165. package/test/scenarios/http-operations/on_response.md +0 -52
  166. package/test/scenarios/http-operations/optional-request-body.md +0 -50
  167. package/test/scenarios/http-operations/paging.md +0 -242
  168. package/test/scenarios/http-operations/path-parameter-in-model.md +0 -53
  169. package/test/scenarios/http-operations/path-parameter.md +0 -48
  170. package/test/scenarios/http-operations/query-parameter.md +0 -41
  171. package/test/scenarios/http-operations/scalar-payload.md +0 -57
  172. package/test/scenarios/http-operations/with-parameters.md +0 -78
  173. package/test/scenarios/models/array-properties.md +0 -47
  174. package/test/scenarios/models/basic.md +0 -118
  175. package/test/scenarios/models/dictionary-properties.md +0 -44
  176. package/test/scenarios/models/inheritance_2_discriminators.md +0 -346
  177. package/test/scenarios/models/inheritance_discriminator.md +0 -88
  178. package/test/scenarios/models/inline-models.md +0 -29
  179. package/test/scenarios/models/model_additional_properties.md +0 -75
  180. package/test/scenarios/models/model_extends.md +0 -97
  181. package/test/scenarios/models/model_spread.md +0 -63
  182. package/test/scenarios/models/nested_property.md +0 -41
  183. package/test/scenarios/models/property_references_property.md +0 -150
  184. package/test/scenarios/multipart/anonymous_part.md +0 -53
  185. package/test/scenarios/multipart/file.md +0 -154
  186. package/test/scenarios/multipart/file_content_type.md +0 -92
  187. package/test/scenarios/multipart/non-string-float.md +0 -55
  188. package/test/scenarios/multipart/simple_part.md +0 -65
  189. package/test/scenarios/operation-parameters/body_root_anonymous.md +0 -87
  190. package/test/scenarios/operation-parameters/constant_as_optional.md +0 -54
  191. package/test/scenarios/operation-parameters/default_value_as_optional.md +0 -54
  192. package/test/scenarios/operation-parameters/no_content_type.md +0 -62
  193. package/test/scenarios/operation-parameters/no_parameters.md +0 -50
  194. package/test/scenarios/operation-parameters/only_optional.md +0 -62
  195. package/test/scenarios/operation-parameters/only_required.md +0 -58
  196. package/test/scenarios/operation-parameters/reserved_names.md +0 -60
  197. package/test/scenarios/operation-parameters/spread_body.md +0 -70
  198. package/test/scenarios/operation-parameters/spread_with_nested.md +0 -82
  199. package/test/scenarios/operation-parameters/union_body.md +0 -86
  200. package/test/scenarios/operation-parameters/with_body_property.md +0 -67
  201. package/test/scenarios/operation-parameters/with_body_root.md +0 -69
  202. package/test/scenarios/serializers/arrays.md +0 -191
  203. package/test/scenarios/serializers/basic_model.md +0 -111
  204. package/test/scenarios/serializers/basic_model_wire_name.md +0 -48
  205. package/test/scenarios/serializers/discriminated_union.md +0 -86
  206. package/test/scenarios/serializers/discriminated_union_spread.md +0 -115
  207. package/test/scenarios/serializers/file.md +0 -67
  208. package/test/scenarios/serializers/model_date_time.md +0 -65
  209. package/test/scenarios/serializers/multipart.md +0 -62
  210. package/test/scenarios/serializers/polymorphic_single_level_inheritance.md +0 -157
  211. package/test/scenarios/serializers/property_references_property.md +0 -72
  212. package/test/scenarios/serializers/record.md +0 -97
  213. package/test/scenarios/serializers/scalars.md +0 -34
  214. package/test/scenarios/serializers/spread.md +0 -69
  215. package/test/scenarios/serializers/string_union.md +0 -55
  216. package/test/scenarios/server/default_url.md +0 -45
  217. package/test/scenarios/server/multiple-parameters.md +0 -55
  218. package/test/scenarios/server/no-server.md +0 -37
  219. package/test/scenarios/server/parametrized-endpoint.md +0 -51
  220. package/test/scenarios.test.ts +0 -24
  221. package/test/test-host.ts +0 -22
  222. package/tsconfig.json +0 -16
  223. package/tsconfig.test.json +0 -9
  224. package/vitest.config.e2e.ts +0 -16
  225. package/vitest.config.ts +0 -15
@@ -1,216 +0,0 @@
1
- # Should populate a Date query parameter
2
-
3
- This scenario tests that a Date query parameter is sent correctly to the wire. Without any explicit encoding, the default is to encode as rfc3339
4
-
5
- ## TypeSpec
6
-
7
- ```tsp
8
- @service
9
- namespace Test;
10
-
11
- @route("/default")
12
- op defaultEncoding(
13
- @query
14
- value: utcDateTime,
15
- ): NoContentResponse;
16
- ```
17
-
18
- ## TypeScript
19
-
20
- ```ts src/api/testClientOperations.ts function defaultEncoding
21
- export async function defaultEncoding(
22
- client: TestClientContext,
23
- value: Date,
24
- options?: DefaultEncodingOptions,
25
- ): Promise<void> {
26
- const path = parse("/default{?value}").expand({
27
- value: dateRfc3339Serializer(value),
28
- });
29
- const httpRequestOptions = {
30
- headers: {},
31
- };
32
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
33
-
34
- if (typeof options?.operationOptions?.onResponse === "function") {
35
- options?.operationOptions?.onResponse(response);
36
- }
37
- if (+response.status === 204 && !response.body) {
38
- return;
39
- }
40
- throw createRestError(response);
41
- }
42
- ```
43
-
44
- # Should populate a Date query parameter when the param is optional
45
-
46
- This scenario tests that a Date query parameter which is optional, is sent correctly to the wire. Without any explicit encoding, the default is to encode as rfc3339
47
-
48
- ## TypeSpec
49
-
50
- ```tsp
51
- @service
52
- namespace Test;
53
-
54
- @route("/default")
55
- op defaultEncoding(
56
- @query
57
- value?: utcDateTime,
58
- ): NoContentResponse;
59
- ```
60
-
61
- ## TypeScript
62
-
63
- ```ts src/api/testClientOperations.ts function defaultEncoding
64
- export async function defaultEncoding(
65
- client: TestClientContext,
66
- options?: DefaultEncodingOptions,
67
- ): Promise<void> {
68
- const path = parse("/default{?value}").expand({
69
- ...(options?.value && { value: dateRfc3339Serializer(options.value) }),
70
- });
71
- const httpRequestOptions = {
72
- headers: {},
73
- };
74
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
75
-
76
- if (typeof options?.operationOptions?.onResponse === "function") {
77
- options?.operationOptions?.onResponse(response);
78
- }
79
- if (+response.status === 204 && !response.body) {
80
- return;
81
- }
82
- throw createRestError(response);
83
- }
84
- ```
85
-
86
- # Should populate a Date query parameter with explicit encoding rfc3339
87
-
88
- This scenario tests that a Date query parameter is sent correctly to the wire. Explicitly setting encoding to rfc3339
89
-
90
- ## TypeSpec
91
-
92
- ```tsp
93
- @service
94
- namespace Test;
95
-
96
- @route("/default")
97
- op get(
98
- @query
99
- @encode(DateTimeKnownEncoding.rfc3339)
100
- value: utcDateTime,
101
- ): NoContentResponse;
102
- ```
103
-
104
- ## TypeScript
105
-
106
- ```ts src/api/testClientOperations.ts function get
107
- export async function get(
108
- client: TestClientContext,
109
- value: Date,
110
- options?: GetOptions,
111
- ): Promise<void> {
112
- const path = parse("/default{?value}").expand({
113
- value: dateRfc3339Serializer(value),
114
- });
115
- const httpRequestOptions = {
116
- headers: {},
117
- };
118
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
119
-
120
- if (typeof options?.operationOptions?.onResponse === "function") {
121
- options?.operationOptions?.onResponse(response);
122
- }
123
- if (+response.status === 204 && !response.body) {
124
- return;
125
- }
126
- throw createRestError(response);
127
- }
128
- ```
129
-
130
- # onlyShould populate a Date query parameter with explicit encoding rfc7231
131
-
132
- This scenario tests that a Date query parameter is sent correctly to the wire. Explicit encode set to rfc7231
133
-
134
- ## TypeSpec
135
-
136
- ```tsp
137
- @service
138
- namespace Test;
139
-
140
- @route("/default")
141
- op get(
142
- @query
143
- @encode(DateTimeKnownEncoding.rfc7231)
144
- value: utcDateTime,
145
- ): NoContentResponse;
146
- ```
147
-
148
- ## TypeScript
149
-
150
- ```ts src/api/testClientOperations.ts function get
151
- export async function get(
152
- client: TestClientContext,
153
- value: Date,
154
- options?: GetOptions,
155
- ): Promise<void> {
156
- const path = parse("/default{?value}").expand({
157
- value: dateRfc7231Serializer(value),
158
- });
159
- const httpRequestOptions = {
160
- headers: {},
161
- };
162
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
163
-
164
- if (typeof options?.operationOptions?.onResponse === "function") {
165
- options?.operationOptions?.onResponse(response);
166
- }
167
- if (+response.status === 204 && !response.body) {
168
- return;
169
- }
170
- throw createRestError(response);
171
- }
172
- ```
173
-
174
- # Should populate a Date query parameter with explicit encoding Unix Timestamp
175
-
176
- This scenario tests that a Date query parameter is sent correctly to the wire. Explicit encode set to unix timestamp
177
-
178
- ## TypeSpec
179
-
180
- ```tsp
181
- @service
182
- namespace Test;
183
-
184
- @route("/default")
185
- op get(
186
- @query
187
- @encode("unixTimestamp", int64)
188
- value: utcDateTime,
189
- ): NoContentResponse;
190
- ```
191
-
192
- ## TypeScript
193
-
194
- ```ts src/api/testClientOperations.ts function get
195
- export async function get(
196
- client: TestClientContext,
197
- value: Date,
198
- options?: GetOptions,
199
- ): Promise<void> {
200
- const path = parse("/default{?value}").expand({
201
- value: dateUnixTimestampSerializer(value),
202
- });
203
- const httpRequestOptions = {
204
- headers: {},
205
- };
206
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
207
-
208
- if (typeof options?.operationOptions?.onResponse === "function") {
209
- options?.operationOptions?.onResponse(response);
210
- }
211
- if (+response.status === 204 && !response.body) {
212
- return;
213
- }
214
- throw createRestError(response);
215
- }
216
- ```
@@ -1,144 +0,0 @@
1
- # Should handle a basic request
2
-
3
- This test verifies that a basic `GET` request with no headers, body, or parameters is correctly handled.
4
-
5
- ## TypeSpec
6
-
7
- ```tsp
8
- @service(#{ title: "Widget Service" })
9
- namespace DemoService;
10
-
11
- @route("/widgets")
12
- @tag("Widgets")
13
- interface Widgets {
14
- @test @get read(): void;
15
- }
16
- ```
17
-
18
- ## TypeScript
19
-
20
- ### Request
21
-
22
- ```ts src/api/widgetsClient/widgetsClientOperations.ts function read
23
- export async function read(client: WidgetsClientContext, options?: ReadOptions): Promise<void> {
24
- const path = parse("/widgets").expand({});
25
- const httpRequestOptions = {
26
- headers: {},
27
- };
28
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
29
-
30
- if (typeof options?.operationOptions?.onResponse === "function") {
31
- options?.operationOptions?.onResponse(response);
32
- }
33
- if (+response.status === 204 && !response.body) {
34
- return;
35
- }
36
- throw createRestError(response);
37
- }
38
- ```
39
-
40
- # Should handle a request with headers, body, and query parameters
41
-
42
- This test verifies that a `POST` request with headers, a body, and query parameters is correctly handled.
43
-
44
- ## TypeSpec
45
-
46
- ```tsp
47
- @service(#{ title: "Widget Service" })
48
- namespace DemoService;
49
-
50
- @test
51
- model Widget {
52
- @path id: string;
53
- @header etag: string;
54
- @query foo: string;
55
- name: string;
56
- }
57
-
58
- @route("/widgets")
59
- @tag("Widgets")
60
- interface Widgets {
61
- @test @post read(...Widget): void;
62
- }
63
- ```
64
-
65
- ## TypeScript
66
-
67
- ### Request
68
-
69
- ```ts src/api/widgetsClient/widgetsClientOperations.ts function read
70
- export async function read(
71
- client: WidgetsClientContext,
72
- id: string,
73
- etag: string,
74
- foo: string,
75
- name: string,
76
- options?: ReadOptions,
77
- ): Promise<void> {
78
- const path = parse("/widgets/{id}{?foo}").expand({
79
- id: id,
80
- foo: foo,
81
- });
82
- const httpRequestOptions = {
83
- headers: {
84
- etag: etag,
85
- },
86
- body: {
87
- name: name,
88
- },
89
- };
90
- const response = await client.pathUnchecked(path).post(httpRequestOptions);
91
-
92
- if (typeof options?.operationOptions?.onResponse === "function") {
93
- options?.operationOptions?.onResponse(response);
94
- }
95
- if (+response.status === 204 && !response.body) {
96
- return;
97
- }
98
- throw createRestError(response);
99
- }
100
- ```
101
-
102
- # Should handle a scalar body
103
-
104
- This test verifies that a `GET` request with a scalar body is correctly handled.
105
-
106
- ## TypeSpec
107
-
108
- ```tsp
109
- @service(#{ title: "Widget Service" })
110
- namespace DemoService;
111
-
112
- @route("/widgets")
113
- @tag("Widgets")
114
- interface Widgets {
115
- @test @get read(@body count: int32): void;
116
- }
117
- ```
118
-
119
- ## TypeScript
120
-
121
- ### Request
122
-
123
- ```ts src/api/widgetsClient/widgetsClientOperations.ts function read
124
- export async function read(
125
- client: WidgetsClientContext,
126
- count: number,
127
- options?: ReadOptions,
128
- ): Promise<void> {
129
- const path = parse("/widgets").expand({});
130
- const httpRequestOptions = {
131
- headers: {},
132
- body: count,
133
- };
134
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
135
-
136
- if (typeof options?.operationOptions?.onResponse === "function") {
137
- options?.operationOptions?.onResponse(response);
138
- }
139
- if (+response.status === 204 && !response.body) {
140
- return;
141
- }
142
- throw createRestError(response);
143
- }
144
- ```
@@ -1,219 +0,0 @@
1
- # **Handling a Response with No Body (204 No Content)**
2
-
3
- This test verifies that a response with status `204` and no body is correctly handled. The generated TypeScript function should recognize an empty response and return `void` without errors.
4
-
5
- ## **TypeSpec**
6
-
7
- ```tsp
8
- @service(#{ title: "Widget Service" })
9
- namespace DemoService;
10
-
11
- @route("/widgets")
12
- @tag("Widgets")
13
- interface Widgets {
14
- @test @get read(): void;
15
- }
16
- ```
17
-
18
- ## **TypeScript**
19
-
20
- ### **Response Handling**
21
-
22
- ```ts src/api/widgetsClient/widgetsClientOperations.ts function read
23
- export async function read(client: WidgetsClientContext, options?: ReadOptions): Promise<void> {
24
- const path = parse("/widgets").expand({});
25
- const httpRequestOptions = {
26
- headers: {},
27
- };
28
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
29
-
30
- if (typeof options?.operationOptions?.onResponse === "function") {
31
- options?.operationOptions?.onResponse(response);
32
- }
33
- if (+response.status === 204 && !response.body) {
34
- return;
35
- }
36
- throw createRestError(response);
37
- }
38
- ```
39
-
40
- ---
41
-
42
- # **Handling a Response with a JSON Body**
43
-
44
- This test verifies that a response with a body containing a `Widget` model is correctly handled. The generated TypeScript function should deserialize the response body into a `Widget` instance.
45
-
46
- ## **TypeSpec**
47
-
48
- ```tsp
49
- @service(#{ title: "Widget Service" })
50
- namespace DemoService;
51
-
52
- @test
53
- model Widget {
54
- name: string;
55
- age: int32;
56
- }
57
-
58
- @route("/widgets")
59
- @tag("Widgets")
60
- interface Widgets {
61
- @test @get read(): Widget;
62
- }
63
- ```
64
-
65
- ## **TypeScript**
66
-
67
- ### **Deserializer**
68
-
69
- This function converts the received JSON response into a `Widget` instance.
70
-
71
- ```ts src/models/internal/serializers.ts function jsonWidgetToApplicationTransform
72
- export function jsonWidgetToApplicationTransform(input_?: any): Widget {
73
- if (!input_) {
74
- return input_ as any;
75
- }
76
- return {
77
- name: input_.name,
78
- age: input_.age,
79
- }!;
80
- }
81
- ```
82
-
83
- ### **Response Handling**
84
-
85
- The function reads a `Widget` instance from the response body, ensuring it only processes JSON responses with a `200` status.
86
-
87
- ```ts src/api/widgetsClient/widgetsClientOperations.ts function read
88
- export async function read(client: WidgetsClientContext, options?: ReadOptions): Promise<Widget> {
89
- const path = parse("/widgets").expand({});
90
- const httpRequestOptions = {
91
- headers: {},
92
- };
93
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
94
-
95
- if (typeof options?.operationOptions?.onResponse === "function") {
96
- options?.operationOptions?.onResponse(response);
97
- }
98
- if (+response.status === 200 && response.headers["content-type"]?.includes("application/json")) {
99
- return jsonWidgetToApplicationTransform(response.body)!;
100
- }
101
- throw createRestError(response);
102
- }
103
- ```
104
-
105
- ---
106
-
107
- # **Handling a Response with Multiple Status Codes (200 & 204)**
108
-
109
- This test verifies that a response with multiple status codes (`200` and `204`) is correctly handled. If the response is `200`, it should deserialize a `Widget`; if `204`, it should return `void`.
110
-
111
- ## **TypeSpec**
112
-
113
- ```tsp
114
- @service(#{ title: "Widget Service" })
115
- namespace DemoService;
116
-
117
- @test
118
- model Widget {
119
- name: string;
120
- age: int32;
121
- }
122
-
123
- @route("/widgets")
124
- @tag("Widgets")
125
- interface Widgets {
126
- @test @get read(): Widget | void;
127
- }
128
- ```
129
-
130
- ## **TypeScript**
131
-
132
- ### **Response Handling**
133
-
134
- The function determines the response type based on the status code. If `200`, it deserializes a `Widget`; if `204`, it returns `void`.
135
-
136
- ```ts src/api/widgetsClient/widgetsClientOperations.ts function read
137
- export async function read(
138
- client: WidgetsClientContext,
139
- options?: ReadOptions,
140
- ): Promise<Widget | void> {
141
- const path = parse("/widgets").expand({});
142
- const httpRequestOptions = {
143
- headers: {},
144
- };
145
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
146
-
147
- if (typeof options?.operationOptions?.onResponse === "function") {
148
- options?.operationOptions?.onResponse(response);
149
- }
150
- if (+response.status === 200 && response.headers["content-type"]?.includes("application/json")) {
151
- return jsonWidgetToApplicationTransform(response.body)!;
152
- }
153
- if (+response.status === 204 && !response.body) {
154
- return;
155
- }
156
- throw createRestError(response);
157
- }
158
- ```
159
-
160
- ---
161
-
162
- # **Handling a Response with Multiple Content Types**
163
-
164
- This test verifies that a response with multiple content types is correctly handled. The response can be in JSON or XML format, both containing a `Widget` model.
165
-
166
- ## **TypeSpec**
167
-
168
- ```tsp
169
- @service(#{ title: "Widget Service" })
170
- namespace DemoService;
171
-
172
- model Widget {
173
- name: string;
174
- age: int32;
175
- }
176
-
177
- model JsonResponse {
178
- @body body: Widget;
179
- @header contentType: "application/json";
180
- }
181
-
182
- model XmlResponse {
183
- @body body: Widget;
184
- @header contentType: "application/xml";
185
- }
186
-
187
- @route("/widgets")
188
- interface Widgets {
189
- @get read(): JsonResponse | XmlResponse;
190
- }
191
- ```
192
-
193
- ## **TypeScript**
194
-
195
- ### **Response Handling**
196
-
197
- This function ensures that the response is correctly processed based on its `content-type` header. It supports both JSON and XML responses, deserializing them into `Widget` instances.
198
- TODO: need to implement xml serialization
199
-
200
- ```ts src/api/widgetsClient/widgetsClientOperations.ts function read
201
- export async function read(client: WidgetsClientContext, options?: ReadOptions): Promise<Widget> {
202
- const path = parse("/widgets").expand({});
203
- const httpRequestOptions = {
204
- headers: {},
205
- };
206
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
207
-
208
- if (typeof options?.operationOptions?.onResponse === "function") {
209
- options?.operationOptions?.onResponse(response);
210
- }
211
- if (+response.status === 200 && response.headers["content-type"]?.includes("application/json")) {
212
- return jsonWidgetToApplicationTransform(response.body)!;
213
- }
214
- if (+response.status === 200 && response.headers["content-type"]?.includes("application/xml")) {
215
- return jsonWidgetToApplicationTransform(response.body)!;
216
- }
217
- throw createRestError(response);
218
- }
219
- ```
@@ -1,128 +0,0 @@
1
- # **Generating a Basic HTTP Operation**
2
-
3
- This test verifies that a simple HTTP `GET` operation with no request payload or parameters correctly generates a **client class, model, serializer, context, and operation function**. The operation returns a `Widget` model, ensuring proper TypeScript type generation and serialization.
4
-
5
- ## **TypeSpec**
6
-
7
- ```tsp
8
- @service
9
- namespace Test;
10
- model Widget {
11
- id: string;
12
- total_weight: int32;
13
- color: "red" | "blue";
14
- }
15
-
16
- op foo(): Widget;
17
- ```
18
-
19
- ## **TypeScript**
20
-
21
- ### **Client Generation**
22
-
23
- A class named `TestClient` is generated, encapsulating API operations. It includes a single method, `foo`, which internally calls the corresponding operation function.
24
-
25
- ```ts src/testClient.ts
26
- import {
27
- createTestClientContext,
28
- type TestClientContext,
29
- type TestClientOptions,
30
- } from "./api/testClientContext.js";
31
- import { foo, type FooOptions } from "./api/testClientOperations.js";
32
-
33
- export class TestClient {
34
- #context: TestClientContext;
35
- constructor(endpoint: string, options?: TestClientOptions) {
36
- this.#context = createTestClientContext(endpoint, options);
37
- }
38
- async foo(options?: FooOptions) {
39
- return foo(this.#context, options);
40
- }
41
- }
42
- ```
43
-
44
- ### **Model Definition**
45
-
46
- A TypeScript interface for the `Widget` model is generated in `src/models/models.ts`. The field `total_weight` is renamed to `totalWeight` to align with TypeScript naming conventions.
47
-
48
- ```ts src/models/models.ts interface Widget
49
- export interface Widget {
50
- id: string;
51
- totalWeight: number;
52
- color: "red" | "blue";
53
- }
54
- ```
55
-
56
- ### **Serializer Generation**
57
-
58
- A serializer function, `jsonWidgetToTransportTransform`, is generated to transform the `Widget` model into its transport format. It converts TypeScript-friendly property names (`totalWeight`) back to their wire format (`total_weight`).
59
-
60
- ```ts src/models/internal/serializers.ts function jsonWidgetToTransportTransform
61
- export function jsonWidgetToTransportTransform(input_?: Widget | null): any {
62
- if (!input_) {
63
- return input_ as any;
64
- }
65
- return {
66
- id: input_.id,
67
- total_weight: input_.totalWeight,
68
- color: input_.color,
69
- }!;
70
- }
71
- ```
72
-
73
- ### **Context Generation**
74
-
75
- The generated `createTestClientContext` function initializes the API client context with the required endpoint. Since no authentication or additional parameters are used, only the endpoint is required.
76
-
77
- ```ts src/api/testClientContext.ts function createTestClientContext
78
- export function createTestClientContext(
79
- endpoint: string,
80
- options?: TestClientOptions,
81
- ): TestClientContext {
82
- const params: Record<string, any> = {
83
- endpoint: endpoint,
84
- };
85
- const resolvedEndpoint = "{endpoint}".replace(/{([^}]+)}/g, (_, key) =>
86
- key in params
87
- ? String(params[key])
88
- : (() => {
89
- throw new Error(`Missing parameter: ${key}`);
90
- })(),
91
- );
92
- return getClient(resolvedEndpoint, {
93
- ...options,
94
- });
95
- }
96
- ```
97
-
98
- An interface, `TestClientContext`, is also generated to define the shape of the context.
99
-
100
- ```ts src/api/testClientContext.ts interface TestClientContext
101
- export interface TestClientContext extends Client {}
102
- ```
103
-
104
- ### **Operation Function**
105
-
106
- A function named `foo` is generated to handle the HTTP request. It prepares the request, sends it using the client context, and processes the response.
107
-
108
- - The request has **no query, path, or header parameters**.
109
- - The expected response body is a `Widget`, requiring transformation from wire format using `jsonWidgetToApplication`.
110
- - If the response status code is unexpected, an exception is thrown.
111
-
112
- ```ts src/api/testClientOperations.ts function foo
113
- export async function foo(client: TestClientContext, options?: FooOptions): Promise<Widget> {
114
- const path = parse("/").expand({});
115
- const httpRequestOptions = {
116
- headers: {},
117
- };
118
- const response = await client.pathUnchecked(path).get(httpRequestOptions);
119
-
120
- if (typeof options?.operationOptions?.onResponse === "function") {
121
- options?.operationOptions?.onResponse(response);
122
- }
123
- if (+response.status === 200 && response.headers["content-type"]?.includes("application/json")) {
124
- return jsonWidgetToApplicationTransform(response.body)!;
125
- }
126
- throw createRestError(response);
127
- }
128
- ```