@typespec/http-client-js 0.16.2-dev.1 → 0.16.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 +24 -20
  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,44 +0,0 @@
1
- import { code } from "@alloy-js/core";
2
- import type { Children } from "@alloy-js/core/jsx-runtime";
3
- import * as ts from "@alloy-js/typescript";
4
- import type { HttpOperationPart } from "@typespec/http";
5
- import { HttpPartTransform } from "./part-transform.jsx";
6
-
7
- export interface ArrayPartTransformProps {
8
- part: HttpOperationPart;
9
- itemRef: Children;
10
- }
11
-
12
- export function ArrayPartTransform(props: ArrayPartTransformProps) {
13
- const namePolicy = ts.useTSNamePolicy();
14
- const applicationName = namePolicy.getName(props.part.name!, "variable");
15
- const partElementName = applicationName;
16
- const mapCallbackSignature = <>({partElementName}: any)</>;
17
- const partItemRef = getPartRef(props.itemRef, applicationName);
18
- const inputExpression = props.part.optional ? (
19
- <>...({partItemRef} ?? [])</>
20
- ) : (
21
- <>...{partItemRef}</>
22
- );
23
-
24
- const partElement: HttpOperationPart = {
25
- ...props.part,
26
- multi: false,
27
- name: partElementName,
28
- };
29
-
30
- return (
31
- <>
32
- {inputExpression}.map({mapCallbackSignature}{" "}
33
- {code`=> (${(<HttpPartTransform itemRef={null} part={partElement} />)})`} )
34
- </>
35
- );
36
- }
37
-
38
- function getPartRef(itemRef: Children, partName: string) {
39
- if (itemRef === null) {
40
- return partName;
41
- }
42
-
43
- return code`${itemRef}.${partName}`;
44
- }
@@ -1,45 +0,0 @@
1
- import type { Children } from "@alloy-js/core";
2
- import { code } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import type { HttpOperationPart } from "@typespec/http";
5
- import { getCreateFilePartDescriptorReference } from "../../static-helpers/multipart-helpers.jsx";
6
-
7
- export interface FilePartTransformProps {
8
- part: HttpOperationPart;
9
- itemRef: Children;
10
- }
11
-
12
- export function FilePartTransform(props: FilePartTransformProps) {
13
- const namePolicy = ts.useTSNamePolicy();
14
- const defaultContentType = getContentType(props.part);
15
- const applicationName = namePolicy.getName(props.part.name!, "variable");
16
- const itemRef = getPartRef(props.itemRef, applicationName);
17
- const args: Children = [JSON.stringify(props.part.name), itemRef];
18
- if (defaultContentType) {
19
- args.push(ts.ValueExpression({ jsValue: defaultContentType }));
20
- }
21
- return <ts.FunctionCallExpression target={getCreateFilePartDescriptorReference()} args={args} />;
22
- }
23
-
24
- function getContentType(part: HttpOperationPart) {
25
- const contentTypes = part.body.contentTypes;
26
- if (contentTypes.length !== 1) {
27
- return undefined;
28
- }
29
-
30
- const contentType = contentTypes[0];
31
-
32
- if (!contentType || contentType === "*/*") {
33
- return undefined;
34
- }
35
-
36
- return contentType;
37
- }
38
-
39
- function getPartRef(itemRef: Children, partName: string) {
40
- if (itemRef === null) {
41
- return partName;
42
- }
43
-
44
- return code`${itemRef}.${partName}`;
45
- }
@@ -1,30 +0,0 @@
1
- import { For } from "@alloy-js/core";
2
- import { useTransformNamePolicy, useTsp } from "@typespec/emitter-framework";
3
- import type { HttpOperationMultipartBody } from "@typespec/http";
4
- import { reportDiagnostic } from "../../../lib.js";
5
- import { HttpPartTransform } from "./part-transform.jsx";
6
-
7
- export interface MultipartTransformProps {
8
- body: HttpOperationMultipartBody;
9
- }
10
-
11
- export function MultipartTransform(props: MultipartTransformProps) {
12
- const { $ } = useTsp();
13
- const transportNamer = useTransformNamePolicy();
14
- const httpParts = props.body.parts;
15
-
16
- if (httpParts.length === 0) {
17
- reportDiagnostic($.program, { code: "missing-http-parts", target: props.body.property });
18
- return <>[]</>;
19
- }
20
-
21
- const itemRef = transportNamer.getApplicationName(props.body.property);
22
-
23
- const partTransform = (
24
- <For each={httpParts} comma line>
25
- {(part, _) => <HttpPartTransform part={part} itemRef={itemRef} />}
26
- </For>
27
- );
28
-
29
- return <>[{partTransform}]</>;
30
- }
@@ -1,22 +0,0 @@
1
- import { type Children } from "@alloy-js/core";
2
- import type { HttpOperationPart } from "@typespec/http";
3
- import { ArrayPartTransform } from "./array-part-transform.jsx";
4
- import { FilePartTransform } from "./file-part-transform.jsx";
5
- import { SimplePartTransform } from "./simple-part-transform.jsx";
6
-
7
- export interface HttpPartTransformProps {
8
- part: HttpOperationPart;
9
- itemRef: Children;
10
- }
11
-
12
- export function HttpPartTransform(props: HttpPartTransformProps) {
13
- if (props.part.multi) {
14
- return <ArrayPartTransform part={props.part} itemRef={props.itemRef} />;
15
- }
16
-
17
- if (props.part.filename) {
18
- return <FilePartTransform part={props.part} itemRef={props.itemRef} />;
19
- }
20
-
21
- return <SimplePartTransform part={props.part} itemRef={props.itemRef} />;
22
- }
@@ -1,42 +0,0 @@
1
- import type { Children } from "@alloy-js/core";
2
- import { code, List } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import type { HttpOperationPart } from "@typespec/http";
5
- import { JsonTransform } from "../json/json-transform.jsx";
6
-
7
- export interface SimplePartTransformProps {
8
- part: HttpOperationPart;
9
- itemRef: Children;
10
- }
11
-
12
- export function SimplePartTransform(props: SimplePartTransformProps) {
13
- const namePolicy = ts.useTSNamePolicy();
14
- const applicationName = namePolicy.getName(props.part.name!, "variable");
15
- const partName = ts.ValueExpression({ jsValue: props.part.name });
16
- const partRef = getPartRef(props.itemRef, applicationName);
17
- let bodyRef = code`${partRef}`;
18
-
19
- if (props.part.body.property) {
20
- bodyRef = code`${partRef}.${props.part.body.property.name}`;
21
- }
22
-
23
- return (
24
- <ts.ObjectExpression>
25
- <List comma line>
26
- <ts.ObjectProperty name="name" jsValue={partName} />
27
- <ts.ObjectProperty
28
- name="body"
29
- value={<JsonTransform itemRef={bodyRef} target="transport" type={props.part.body.type} />}
30
- />
31
- </List>
32
- </ts.ObjectExpression>
33
- );
34
- }
35
-
36
- function getPartRef(itemRef: Children, partName: string) {
37
- if (itemRef === null) {
38
- return partName;
39
- }
40
-
41
- return code`${itemRef}.${partName}`;
42
- }
@@ -1,83 +0,0 @@
1
- import type { Refkey } from "@alloy-js/core";
2
- import { refkey } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import * as ef from "@typespec/emitter-framework/typescript";
5
- import type { HttpOperationBody } from "@typespec/http";
6
- import type { ClientOperation } from "@typespec/http-client";
7
- import { JsonTransform } from "./json/json-transform.jsx";
8
-
9
- export interface TransformDeclarationProps {
10
- operation: ClientOperation;
11
- refkey?: Refkey;
12
- }
13
-
14
- export function getTransformDeclarationRef(operation: ClientOperation) {
15
- return refkey(operation, "transform");
16
- }
17
-
18
- export function TransformDeclaration(props: TransformDeclarationProps) {
19
- const refkey = props.refkey ?? getTransformDeclarationRef(props.operation);
20
- return (
21
- <>
22
- <TransformToTransportDeclaration operation={props.operation} refkey={refkey} />
23
- </>
24
- );
25
- }
26
-
27
- export interface SingleBodyTransformDeclarationProps {
28
- name: string;
29
- operation: ClientOperation;
30
- payload: HttpOperationBody;
31
- refkey: Refkey;
32
- }
33
-
34
- export function SingleBodyTransformDeclaration(props: SingleBodyTransformDeclarationProps) {
35
- const inputRef = refkey(props.payload, "property");
36
- const payloadParameter: ts.ParameterDescriptor = {
37
- name: "payload",
38
- type: <ef.TypeExpression type={props.payload.type} />,
39
- refkey: inputRef,
40
- };
41
-
42
- return (
43
- <ts.FunctionDeclaration
44
- export
45
- name={props.name}
46
- parameters={[payloadParameter]}
47
- refkey={props.refkey}
48
- >
49
- return <JsonTransform itemRef={inputRef} target="transport" type={props.payload.type} />
50
- !;
51
- </ts.FunctionDeclaration>
52
- );
53
- }
54
-
55
- interface TransformToTransportDeclarationProps {
56
- operation: ClientOperation;
57
- refkey: Refkey;
58
- }
59
-
60
- function TransformToTransportDeclaration(props: TransformToTransportDeclarationProps) {
61
- const requestPayload = props.operation.httpOperation.parameters.body;
62
- if (!requestPayload || !requestPayload.property) {
63
- return;
64
- }
65
-
66
- if (requestPayload.bodyKind === "file") return;
67
-
68
- const namePolicy = ts.useTSNamePolicy();
69
- const name = namePolicy.getName(`${props.operation.name}_payload_to_transport`, "function");
70
-
71
- if (requestPayload.bodyKind === "multipart") {
72
- return null;
73
- }
74
-
75
- return (
76
- <SingleBodyTransformDeclaration
77
- operation={props.operation}
78
- payload={requestPayload}
79
- refkey={props.refkey}
80
- name={name}
81
- />
82
- );
83
- }
@@ -1,62 +0,0 @@
1
- import * as ts from "@alloy-js/typescript";
2
- import type { Type } from "@typespec/compiler";
3
- import type { HasName } from "@typespec/emitter-framework";
4
- import { TransformNamePolicyContext, useTsp } from "@typespec/emitter-framework";
5
- import type { HttpOperation } from "@typespec/http";
6
- import { reportDiagnostic } from "../../lib.js";
7
- import { ContentTypeEncodingProvider } from "./content-type-encoding-provider.jsx";
8
- import { JsonTransform } from "./json/json-transform.jsx";
9
- import { MultipartTransform } from "./multipart/multipart-transform.jsx";
10
- import { defaultTransportNameGetter } from "./transform-name-policy.js";
11
-
12
- export interface OperationTransformToTransportExpression {
13
- httpOperation: HttpOperation;
14
- }
15
-
16
- export function OperationTransformExpression(props: OperationTransformToTransportExpression) {
17
- const body = props.httpOperation.parameters.body;
18
-
19
- if (!body) {
20
- return;
21
- }
22
-
23
- const itemRef = body.property ? payloadApplicationNameGetter(body.property) : null;
24
-
25
- if (body.bodyKind === "multipart") {
26
- return <MultipartTransform body={body} />;
27
- }
28
-
29
- // TODO: Handle content types other than application/json and multipart
30
- // And multiple content types
31
- const contentType = body.contentTypes[0];
32
- const payloadType = body.type;
33
- return (
34
- <ContentTypeEncodingProvider contentType={contentType}>
35
- <TransformNamePolicyContext.Provider
36
- value={{
37
- getTransportName: defaultTransportNameGetter,
38
- getApplicationName: payloadApplicationNameGetter,
39
- }}
40
- >
41
- <JsonTransform itemRef={itemRef} target="transport" type={payloadType} />
42
- </TransformNamePolicyContext.Provider>
43
- </ContentTypeEncodingProvider>
44
- );
45
- }
46
-
47
- function payloadApplicationNameGetter(type: HasName<Type>) {
48
- const { $ } = useTsp();
49
- if (typeof type.name !== "string") {
50
- reportDiagnostic($.program, { code: "symbol-name-not-supported", target: type });
51
- return "";
52
- }
53
-
54
- const namePolicy = ts.useTSNamePolicy();
55
- let name = namePolicy.getName(type.name, "object-member-data");
56
-
57
- if ($.modelProperty.is(type) && type.optional) {
58
- name = `options?.${name}`;
59
- }
60
-
61
- return name;
62
- }
@@ -1,301 +0,0 @@
1
- import type { Children, Refkey } from "@alloy-js/core";
2
- import { code } from "@alloy-js/core";
3
- import type { BytesKnownEncoding, EncodeData, Scalar } from "@typespec/compiler";
4
- import { NoTarget } from "@typespec/compiler";
5
- import { useTsp } from "@typespec/emitter-framework";
6
- import * as ef from "@typespec/emitter-framework/typescript";
7
- import { useDefaultEncoding } from "../../context/encoding/encoding-context.jsx";
8
- import { reportDiagnostic } from "../../lib.js";
9
- import {
10
- getDecodeUint8ArrayRef,
11
- getEncodeUint8ArrayRef,
12
- } from "../static-helpers/bytes-encoding.jsx";
13
-
14
- // Define the transformer function type.
15
- export type TransformerFn = (itemRef: Refkey | Children, encoding?: EncodeData) => Children;
16
-
17
- // Define a pair that holds both directions.
18
- export interface TransformerPair {
19
- toTransport: TransformerFn;
20
- toApplication: TransformerFn;
21
- }
22
-
23
- /**
24
- * Returns the transformer pair for the given scalar type.
25
- *
26
- * @param type - The scalar type to look up.
27
- * @returns The transformer pair from scalarTransformerMap for the given type.
28
- */
29
- export function getScalarTransformer(type: Scalar): TransformerPair {
30
- return scalarTransformerMap[getScalarTransformKey(type)];
31
- }
32
-
33
- function defineScalarTransformerMap<T extends Record<string, TransformerPair>>(map: T): T {
34
- return map;
35
- }
36
-
37
- /**
38
- * A paired transformer map for scalar types.
39
- *
40
- * Each scalar type is associated with a pair of functions: one for transforming the value for transport
41
- * (serialization) and one for transforming the value for application (deserialization).
42
- */
43
- const scalarTransformerMap = defineScalarTransformerMap({
44
- boolean: {
45
- toTransport: (itemRef) => passthroughTransformer(itemRef),
46
- toApplication: (itemRef) => passthroughTransformer(itemRef),
47
- },
48
-
49
- bytes: {
50
- toTransport: (itemRef, encoding) => {
51
- const { $ } = useTsp();
52
- const bytesEncoding: BytesKnownEncoding | undefined =
53
- (encoding?.encoding as BytesKnownEncoding) ??
54
- (useDefaultEncoding("bytes") as BytesKnownEncoding);
55
- switch (bytesEncoding) {
56
- case "base64":
57
- case "base64url":
58
- const bytesEncodeRef = getEncodeUint8ArrayRef();
59
- return code`${bytesEncodeRef}(${itemRef}, "${bytesEncoding}")!`;
60
- default:
61
- reportDiagnostic($.program, {
62
- code: "unknown-encoding",
63
- target: encoding?.type ?? NoTarget,
64
- });
65
- return passthroughTransformer(itemRef);
66
- }
67
- },
68
- toApplication: (itemRef, encoding) => {
69
- const { $ } = useTsp();
70
- const bytesEncoding: BytesKnownEncoding | undefined =
71
- (encoding?.encoding as BytesKnownEncoding) ??
72
- (useDefaultEncoding("bytes") as BytesKnownEncoding);
73
-
74
- switch (bytesEncoding) {
75
- case "base64":
76
- case "base64url":
77
- const bytesDecodeRef = getDecodeUint8ArrayRef();
78
- return code`${bytesDecodeRef}(${itemRef})!`;
79
- default:
80
- reportDiagnostic($.program, {
81
- code: "unknown-encoding",
82
- target: encoding?.type ?? NoTarget,
83
- });
84
- return passthroughTransformer(itemRef);
85
- }
86
- },
87
- },
88
-
89
- // For numerics that have no transformation, both directions simply passthrough.
90
- decimal: {
91
- toTransport: (itemRef) => passthroughTransformer(itemRef),
92
- toApplication: (itemRef) => passthroughTransformer(itemRef),
93
- },
94
- decimal128: {
95
- toTransport: (itemRef) => passthroughTransformer(itemRef),
96
- toApplication: (itemRef) => passthroughTransformer(itemRef),
97
- },
98
- float: {
99
- toTransport: (itemRef) => passthroughTransformer(itemRef),
100
- toApplication: (itemRef) => passthroughTransformer(itemRef),
101
- },
102
- float32: {
103
- toTransport: (itemRef) => passthroughTransformer(itemRef),
104
- toApplication: (itemRef) => passthroughTransformer(itemRef),
105
- },
106
- float64: {
107
- toTransport: (itemRef) => passthroughTransformer(itemRef),
108
- toApplication: (itemRef) => passthroughTransformer(itemRef),
109
- },
110
- int16: {
111
- toTransport: (itemRef) => passthroughTransformer(itemRef),
112
- toApplication: (itemRef) => passthroughTransformer(itemRef),
113
- },
114
- int32: {
115
- toTransport: (itemRef) => passthroughTransformer(itemRef),
116
- toApplication: (itemRef) => passthroughTransformer(itemRef),
117
- },
118
- int64: {
119
- toTransport: (itemRef) => passthroughTransformer(itemRef),
120
- toApplication: (itemRef) => passthroughTransformer(itemRef),
121
- },
122
- int8: {
123
- toTransport: (itemRef) => passthroughTransformer(itemRef),
124
- toApplication: (itemRef) => passthroughTransformer(itemRef),
125
- },
126
- integer: {
127
- toTransport: (itemRef) => passthroughTransformer(itemRef),
128
- toApplication: (itemRef) => passthroughTransformer(itemRef),
129
- },
130
- numeric: {
131
- toTransport: (itemRef) => passthroughTransformer(itemRef),
132
- toApplication: (itemRef) => passthroughTransformer(itemRef),
133
- },
134
- safeint: {
135
- toTransport: (itemRef) => passthroughTransformer(itemRef),
136
- toApplication: (itemRef) => passthroughTransformer(itemRef),
137
- },
138
- uint16: {
139
- toTransport: (itemRef) => passthroughTransformer(itemRef),
140
- toApplication: (itemRef) => passthroughTransformer(itemRef),
141
- },
142
- uint32: {
143
- toTransport: (itemRef) => passthroughTransformer(itemRef),
144
- toApplication: (itemRef) => passthroughTransformer(itemRef),
145
- },
146
- uint64: {
147
- toTransport: (itemRef) => passthroughTransformer(itemRef),
148
- toApplication: (itemRef) => passthroughTransformer(itemRef),
149
- },
150
- uint8: {
151
- toTransport: (itemRef) => passthroughTransformer(itemRef),
152
- toApplication: (itemRef) => passthroughTransformer(itemRef),
153
- },
154
-
155
- // Duration: We don't apply any transformation so just passthrough.
156
- duration: {
157
- toTransport: (itemRef) => passthroughTransformer(itemRef),
158
- toApplication: (itemRef) => passthroughTransformer(itemRef),
159
- },
160
-
161
- // Date/Time scalars: using a serializer on transport and a deserializer on application.
162
- offsetDateTime: {
163
- toTransport: (itemRef) => passthroughTransformer(itemRef),
164
- toApplication: (itemRef) => passthroughTransformer(itemRef),
165
- },
166
- plainDate: {
167
- toTransport: (itemRef) => passthroughTransformer(itemRef),
168
- toApplication: (itemRef) => passthroughTransformer(itemRef),
169
- },
170
- plainTime: {
171
- toTransport: (itemRef) => passthroughTransformer(itemRef),
172
- toApplication: (itemRef) => passthroughTransformer(itemRef),
173
- },
174
- utcDateTime: {
175
- toTransport: (itemRef, encoding) => {
176
- const { $ } = useTsp();
177
- const dateEncoding = encoding?.encoding ?? useDefaultEncoding("datetime");
178
- let encodingFnRef: Refkey = ef.DateRfc3339SerializerRefkey;
179
- switch (dateEncoding) {
180
- case "unixTimestamp":
181
- encodingFnRef = ef.DateUnixTimestampSerializerRefkey;
182
- break;
183
- case "rfc7231":
184
- encodingFnRef = ef.DateRfc7231SerializerRefkey;
185
- break;
186
- case "rfc3339":
187
- // already defaulted above.
188
- break;
189
- default:
190
- reportDiagnostic($.program, {
191
- code: "unknown-encoding",
192
- target: encoding?.type ?? NoTarget,
193
- });
194
- }
195
- return code`${encodingFnRef}(${itemRef})`;
196
- },
197
- toApplication: (itemRef, encoding) => {
198
- const { $ } = useTsp();
199
- const dateEncoding = encoding?.encoding ?? useDefaultEncoding("datetime");
200
- let decodingFnRef: Refkey = ef.DateDeserializerRefkey;
201
- switch (dateEncoding) {
202
- case "unixTimestamp":
203
- decodingFnRef = ef.DateUnixTimestampDeserializerRefkey;
204
- break;
205
- case "rfc7231":
206
- decodingFnRef = ef.DateRfc7231DeserializerRefkey;
207
- break;
208
- case "rfc3339":
209
- // default already
210
- break;
211
- default:
212
- reportDiagnostic($.program, {
213
- code: "unknown-encoding",
214
- target: encoding?.type ?? NoTarget,
215
- });
216
- }
217
- return code`${decodingFnRef}(${itemRef})!`;
218
- },
219
- },
220
-
221
- unixTimestamp32: {
222
- toTransport: (itemRef) => {
223
- return code`${ef.DateUnixTimestampSerializerRefkey}(${itemRef})`;
224
- },
225
- toApplication: (itemRef) => {
226
- return code`${ef.DateUnixTimestampDeserializerRefkey}(${itemRef})`;
227
- },
228
- },
229
-
230
- // For URL and string, no transformation is needed.
231
- url: {
232
- toTransport: (itemRef) => passthroughTransformer(itemRef),
233
- toApplication: (itemRef) => passthroughTransformer(itemRef),
234
- },
235
- string: {
236
- toTransport: (itemRef) => passthroughTransformer(itemRef),
237
- toApplication: (itemRef) => passthroughTransformer(itemRef),
238
- },
239
- });
240
-
241
- /**
242
- * Tests for determining the scalar transformation key.
243
- */
244
- type ScalarTest = (type: Scalar) => boolean;
245
-
246
- function getScalarTests(): { key: keyof typeof scalarTransformerMap; test: ScalarTest }[] {
247
- const { $ } = useTsp();
248
- return [
249
- { key: "boolean", test: (t) => $.scalar.isBoolean(t) || $.scalar.extendsBoolean(t) },
250
- { key: "bytes", test: (t) => $.scalar.isBytes(t) || $.scalar.extendsBytes(t) },
251
- { key: "decimal", test: (t) => $.scalar.isDecimal(t) || $.scalar.extendsDecimal(t) },
252
- { key: "decimal128", test: (t) => $.scalar.isDecimal128(t) || $.scalar.extendsDecimal128(t) },
253
- { key: "duration", test: (t) => $.scalar.isDuration(t) || $.scalar.extendsDuration(t) },
254
- { key: "float", test: (t) => $.scalar.isFloat(t) || $.scalar.extendsFloat(t) },
255
- { key: "float32", test: (t) => $.scalar.isFloat32(t) || $.scalar.extendsFloat32(t) },
256
- { key: "float64", test: (t) => $.scalar.isFloat64(t) || $.scalar.extendsFloat64(t) },
257
- { key: "int16", test: (t) => $.scalar.isInt16(t) || $.scalar.extendsInt16(t) },
258
- { key: "int32", test: (t) => $.scalar.isInt32(t) || $.scalar.extendsInt32(t) },
259
- { key: "int64", test: (t) => $.scalar.isInt64(t) || $.scalar.extendsInt64(t) },
260
- { key: "int8", test: (t) => $.scalar.isInt8(t) || $.scalar.extendsInt8(t) },
261
- { key: "integer", test: (t) => $.scalar.isInteger(t) || $.scalar.extendsInteger(t) },
262
- { key: "numeric", test: (t) => $.scalar.isNumeric(t) || $.scalar.extendsNumeric(t) },
263
- {
264
- key: "offsetDateTime",
265
- test: (t) => $.scalar.isOffsetDateTime(t) || $.scalar.extendsOffsetDateTime(t),
266
- },
267
- { key: "plainDate", test: (t) => $.scalar.isPlainDate(t) || $.scalar.extendsPlainDate(t) },
268
- { key: "plainTime", test: (t) => $.scalar.isPlainTime(t) || $.scalar.extendsPlainTime(t) },
269
- { key: "safeint", test: (t) => $.scalar.isSafeint(t) || $.scalar.extendsSafeint(t) },
270
- { key: "string", test: (t) => $.scalar.isString(t) || $.scalar.extendsString(t) },
271
- { key: "uint16", test: (t) => $.scalar.isUint16(t) || $.scalar.extendsUint16(t) },
272
- { key: "uint32", test: (t) => $.scalar.isUint32(t) || $.scalar.extendsUint32(t) },
273
- { key: "uint64", test: (t) => $.scalar.isUint64(t) || $.scalar.extendsUint64(t) },
274
- { key: "uint8", test: (t) => $.scalar.isUint8(t) || $.scalar.extendsUint8(t) },
275
- { key: "url", test: (t) => $.scalar.isUrl(t) || $.scalar.extendsUrl(t) },
276
- {
277
- key: "utcDateTime",
278
- test: (t) => $.scalar.isUtcDateTime(t) || $.scalar.extendsUtcDateTime(t),
279
- },
280
- ];
281
- }
282
-
283
- /**
284
- * Determines the key to use in the scalarTransformerMap for the given scalar type.
285
- *
286
- * @param type - The scalar type to check.
287
- * @returns The corresponding key from the scalarTransformerMap.
288
- * @throws Error if the scalar type is unknown.
289
- */
290
- function getScalarTransformKey(type: Scalar): keyof typeof scalarTransformerMap {
291
- for (const { key, test } of getScalarTests()) {
292
- if (test(type)) {
293
- return key;
294
- }
295
- }
296
- throw new Error(`Unknown scalar type: ${type}`);
297
- }
298
-
299
- function passthroughTransformer(itemRef: Refkey | Children) {
300
- return itemRef;
301
- }