@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,54 +0,0 @@
1
- import type { Children } from "@alloy-js/core";
2
- import { code, mapJoin } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import type { Model } from "@typespec/compiler";
5
- import { useTsp } from "@typespec/emitter-framework";
6
- import { getJsonRecordTransformRefkey } from "./json-record-transform.jsx";
7
-
8
- export interface JsonAdditionalPropertiesTransformProps {
9
- itemRef: Children;
10
- type: Model;
11
- target: "transport" | "application";
12
- }
13
-
14
- export function JsonAdditionalPropertiesTransform(props: JsonAdditionalPropertiesTransformProps) {
15
- const { $ } = useTsp();
16
- const additionalProperties = $.model.getAdditionalPropertiesRecord(props.type);
17
-
18
- if (!additionalProperties) {
19
- return null;
20
- }
21
-
22
- if (props.target === "application") {
23
- const properties = $.model.getProperties(props.type, { includeExtended: true });
24
- const destructuredProperties = mapJoin(
25
- () => properties,
26
- (name) => name,
27
- {
28
- joiner: ",",
29
- ender: ",",
30
- },
31
- );
32
-
33
- // Inline destructuring that extracts the properties and passes the rest to jsonRecordUnknownToApplicationTransform_2
34
- const inlineDestructure = code`
35
- ${getJsonRecordTransformRefkey(additionalProperties, props.target)}(
36
- (({ ${destructuredProperties} ...rest }) => rest)(${props.itemRef})
37
- ),
38
- `;
39
-
40
- return (
41
- <>
42
- <ts.ObjectProperty name="additionalProperties">{inlineDestructure}</ts.ObjectProperty>
43
- </>
44
- );
45
- }
46
-
47
- const itemRef = code`${props.itemRef}.additionalProperties`;
48
-
49
- return (
50
- <>
51
- ...({getJsonRecordTransformRefkey(additionalProperties, props.target)}({itemRef}) ),
52
- </>
53
- );
54
- }
@@ -1,24 +0,0 @@
1
- import { type Children } from "@alloy-js/core";
2
- import type { Model } from "@typespec/compiler";
3
- import { JsonTransform } from "./json-transform.jsx";
4
-
5
- export interface JsonModelBaseTransformProps {
6
- itemRef: Children;
7
- type: Model;
8
- target: "transport" | "application";
9
- }
10
-
11
- export function JsonModelBaseTransform(props: JsonModelBaseTransformProps) {
12
- const baseModel = props.type.baseModel;
13
-
14
- if (!baseModel) {
15
- return null;
16
- }
17
-
18
- return (
19
- <>
20
- ...
21
- <JsonTransform {...props} type={baseModel} />,
22
- </>
23
- );
24
- }
@@ -1,53 +0,0 @@
1
- import type { Children, Refkey } from "@alloy-js/core";
2
- import { code, NamePolicyContext } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import type { ModelProperty } from "@typespec/compiler";
5
- import { useTsp } from "@typespec/emitter-framework";
6
- import { unpackProperty } from "../../utils/unpack-model-property.js";
7
- import { ScalarDataTransform } from "../data-transform.jsx";
8
- import { useTransformNamePolicy } from "../transform-name-policy.js";
9
- import { JsonTransform } from "./json-transform.jsx";
10
-
11
- export interface JsonModelPropertyTransformProps {
12
- itemRef: Refkey | Children;
13
- type: ModelProperty;
14
- target: "transport" | "application";
15
- }
16
-
17
- export function JsonModelPropertyTransform(props: JsonModelPropertyTransformProps) {
18
- const { $ } = useTsp();
19
- const transformNamer = useTransformNamePolicy();
20
- const propertyValueType = unpackProperty(props.type);
21
-
22
- const transportName = transformNamer.getTransportName(props.type);
23
- const applicationName = transformNamer.getApplicationName(props.type);
24
- const targetName = props.target === "transport" ? transportName : applicationName;
25
- const sourceName = props.target === "transport" ? applicationName : transportName;
26
-
27
- const propertyValueRef = props.itemRef ? code`${props.itemRef}.${sourceName}` : sourceName;
28
- let propertyValue: Children;
29
-
30
- if ($.scalar.is(propertyValueType)) {
31
- propertyValue = (
32
- <ScalarDataTransform type={props.type} target={props.target} itemRef={propertyValueRef} />
33
- );
34
- } else {
35
- propertyValue = (
36
- <JsonTransform type={propertyValueType} target={props.target} itemRef={propertyValueRef} />
37
- );
38
- }
39
-
40
- if (props.target === "transport") {
41
- return (
42
- <NamePolicyContext.Provider value={{ getName: (n) => n, for: () => (n) => n }}>
43
- <ts.ObjectProperty name={targetName} value={propertyValue} />
44
- </NamePolicyContext.Provider>
45
- );
46
- }
47
-
48
- return (
49
- <>
50
- <ts.ObjectProperty name={targetName} value={propertyValue} />
51
- </>
52
- );
53
- }
@@ -1,115 +0,0 @@
1
- import * as ts from "@alloy-js/typescript";
2
-
3
- import { type Children, code, For, type Refkey, refkey } from "@alloy-js/core";
4
- import type { Model } from "@typespec/compiler";
5
- import { useTsp } from "@typespec/emitter-framework";
6
- import { JsonAdditionalPropertiesTransform } from "./json-model-additional-properties-transform.jsx";
7
- import { JsonModelPropertyTransform } from "./json-model-property-transform.jsx";
8
- import { JsonRecordTransformDeclaration } from "./json-record-transform.jsx";
9
- import {
10
- getJsonTransformDiscriminatorRefkey,
11
- JsonTransformDiscriminatorDeclaration,
12
- } from "./json-transform-discriminator.jsx";
13
-
14
- export interface JsonModelTransformProps {
15
- itemRef: Refkey | Children;
16
- type: Model;
17
- target: "transport" | "application";
18
- }
19
-
20
- export function JsonModelTransform(props: JsonModelTransformProps) {
21
- const { $ } = useTsp();
22
- // Need to skip never properties
23
- const properties = Array.from(
24
- $.model.getProperties(props.type, { includeExtended: true }).values(),
25
- ).filter((p) => !$.type.isNever(p.type));
26
-
27
- const discriminator = $.model.getDiscriminatedUnion(props.type);
28
-
29
- const discriminate = getJsonTransformDiscriminatorRefkey(props.type, props.target);
30
-
31
- return (
32
- <ts.ObjectExpression>
33
- <JsonAdditionalPropertiesTransform
34
- itemRef={props.itemRef}
35
- target={props.target}
36
- type={props.type}
37
- />
38
- {discriminator ? (
39
- <>
40
- ...{discriminate}({props.itemRef}),
41
- </>
42
- ) : null}
43
- <For each={properties} joiner="," line>
44
- {(property) => {
45
- return (
46
- <JsonModelPropertyTransform
47
- itemRef={props.itemRef}
48
- type={property}
49
- target={props.target}
50
- />
51
- );
52
- }}
53
- </For>
54
- </ts.ObjectExpression>
55
- );
56
- }
57
-
58
- export function getJsonModelTransformRefkey(
59
- type: Model,
60
- target: "transport" | "application",
61
- ): Refkey {
62
- return refkey(type, "json_model_transform", target);
63
- }
64
-
65
- export interface JsonModelTransformDeclarationProps {
66
- type: Model;
67
- target: "transport" | "application";
68
- }
69
-
70
- export function JsonModelTransformDeclaration(props: JsonModelTransformDeclarationProps): Children {
71
- const { $ } = useTsp();
72
- const namePolicy = ts.useTSNamePolicy();
73
- const transformName = namePolicy.getName(
74
- `json_${props.type.name}_to_${props.target}_transform`,
75
- "function",
76
- );
77
-
78
- const returnType = props.target === "transport" ? "any" : refkey(props.type);
79
- const inputType = props.target === "transport" ? <>{refkey(props.type)} | null</> : "any";
80
- const inputRef = refkey();
81
-
82
- const parameters: ts.ParameterDescriptor[] = [
83
- // Make the input optional to make the transform more robust and check against null and undefined
84
- { name: "input_", type: inputType, refkey: inputRef, optional: true },
85
- ];
86
-
87
- const indexType = $.model.getIndexType(props.type);
88
- const hasAdditionalProperties = indexType && $.record.is(indexType);
89
-
90
- const declarationRefkey = getJsonModelTransformRefkey(props.type, props.target);
91
- return (
92
- <>
93
- {hasAdditionalProperties ? (
94
- <JsonRecordTransformDeclaration target={props.target} type={indexType} />
95
- ) : null}
96
- <JsonTransformDiscriminatorDeclaration type={props.type} target={props.target} />
97
- <ts.FunctionDeclaration
98
- name={transformName}
99
- export
100
- returnType={returnType}
101
- parameters={parameters}
102
- refkey={declarationRefkey}
103
- >
104
- {code`
105
- if(!${inputRef}) {
106
- return ${inputRef} as any;
107
- }
108
-
109
- `}
110
- return <JsonModelTransform {...props} itemRef={inputRef} />
111
- !;
112
- </ts.FunctionDeclaration>
113
- </>
114
- );
115
- }
@@ -1,88 +0,0 @@
1
- import * as ts from "@alloy-js/typescript";
2
-
3
- import { type Children, type Refkey, code, refkey } from "@alloy-js/core";
4
- import type { Model } from "@typespec/compiler";
5
- import { useTsp } from "@typespec/emitter-framework";
6
- import { JsonTransform } from "./json-transform.jsx";
7
-
8
- export interface JsonRecordTransformProps {
9
- itemRef: Refkey | Children;
10
- type: Model;
11
- target: "transport" | "application";
12
- }
13
-
14
- export function JsonRecordTransform(props: JsonRecordTransformProps) {
15
- const { $ } = useTsp();
16
- if (!$.record.is(props.type)) {
17
- return null;
18
- }
19
-
20
- const elementType = $.record.getElementType(props.type);
21
-
22
- // TODO: Do we need to cast?
23
- return code`
24
- if(!${props.itemRef}) {
25
- return ${props.itemRef} as any;
26
- }
27
-
28
- const _transformedRecord: any = {};
29
-
30
- for (const [key, value] of Object.entries(${props.itemRef} ?? {})) {
31
- const transformedItem = ${(<JsonTransform type={elementType} target={props.target} itemRef="value as any" />)};
32
- _transformedRecord[key] = transformedItem;
33
- }
34
-
35
- return _transformedRecord;
36
- `;
37
- }
38
-
39
- export function getJsonRecordTransformRefkey(
40
- type: Model,
41
- target: "transport" | "application",
42
- ): Refkey {
43
- return refkey(type, "json_record_transform", target);
44
- }
45
-
46
- export interface JsonRecordTransformDeclarationProps {
47
- type: Model;
48
- target: "transport" | "application";
49
- }
50
-
51
- export function JsonRecordTransformDeclaration(props: JsonRecordTransformDeclarationProps) {
52
- const { $ } = useTsp();
53
- if (!$.record.is(props.type)) {
54
- return null;
55
- }
56
-
57
- const elementType = $.record.getElementType(props.type);
58
- const elementName =
59
- "name" in elementType && typeof elementType.name === "string" ? elementType.name : "element";
60
-
61
- const namePolicy = ts.useTSNamePolicy();
62
- const transformName = namePolicy.getName(
63
- `json_Record_${elementName}_to_${props.target}_transform`,
64
- "function",
65
- );
66
-
67
- const itemType = code`Record<string, any>`;
68
- const returnType = props.target === "transport" ? "any" : itemType;
69
- const inputType = props.target === "transport" ? <>{itemType} | null</> : "any";
70
- const inputRef = refkey();
71
-
72
- const parameters: ts.ParameterDescriptor[] = [
73
- { name: "items_", type: inputType, refkey: inputRef, optional: true },
74
- ];
75
-
76
- const declarationRefkey = getJsonRecordTransformRefkey(props.type, props.target);
77
- return (
78
- <ts.FunctionDeclaration
79
- name={transformName}
80
- export
81
- returnType={returnType}
82
- parameters={parameters}
83
- refkey={declarationRefkey}
84
- >
85
- <JsonRecordTransform {...props} itemRef={inputRef} />
86
- </ts.FunctionDeclaration>
87
- );
88
- }
@@ -1,111 +0,0 @@
1
- import { code, mapJoin, refkey, useNamePolicy, type Children, type Refkey } from "@alloy-js/core";
2
- import * as ts from "@alloy-js/typescript";
3
- import type { Discriminator, Model, Union } from "@typespec/compiler";
4
- import { useTsp } from "@typespec/emitter-framework";
5
- import { JsonTransform } from "./json-transform.jsx";
6
-
7
- export interface JsonTransformDiscriminatorProps {
8
- itemRef: Refkey | Children;
9
- discriminator: Discriminator;
10
- type: Union | Model;
11
- target: "application" | "transport";
12
- }
13
-
14
- export function JsonTransformDiscriminator(props: JsonTransformDiscriminatorProps) {
15
- const { $ } = useTsp();
16
- const discriminatedUnion = $.model.is(props.type)
17
- ? $.model.getDiscriminatedUnion(props.type)
18
- : $.union.getDiscriminatedUnion(props.type);
19
-
20
- const propertyName = props.discriminator.propertyName;
21
-
22
- if (!discriminatedUnion || !propertyName) {
23
- return code`${props.itemRef}`;
24
- }
25
-
26
- const discriminatorRef = code`${props.itemRef}.${props.discriminator.propertyName}`;
27
-
28
- // Need to cast to make sure that the general types which usually have a broader
29
- // type in the discriminator are compatible.
30
- const itemRef = code`${props.itemRef} as any`;
31
- const discriminatingCases = mapJoin(
32
- () => discriminatedUnion.variants,
33
- (name, variant) => {
34
- return code`
35
- if( discriminatorValue === ${JSON.stringify(name)}) {
36
- return ${(<JsonTransform type={variant} target={props.target} itemRef={itemRef} />)}!
37
- }
38
- `;
39
- },
40
- { joiner: "\n\n" },
41
- );
42
-
43
- return (
44
- <>
45
- const discriminatorValue = {discriminatorRef};{discriminatingCases}
46
- <>console.warn(`Received unknown kind: ` + discriminatorValue); return {itemRef}</>
47
- </>
48
- );
49
- }
50
-
51
- export function getJsonTransformDiscriminatorRefkey(
52
- type: Union | Model,
53
- target: "application" | "transport",
54
- ) {
55
- return refkey(type, "json_transform_discriminator_", target);
56
- }
57
-
58
- export interface JsonTransformDiscriminatorDeclarationProps {
59
- type: Union | Model;
60
- target: "application" | "transport";
61
- }
62
-
63
- export function JsonTransformDiscriminatorDeclaration(
64
- props: JsonTransformDiscriminatorDeclarationProps,
65
- ) {
66
- const { $ } = useTsp();
67
- let discriminator: Discriminator | undefined;
68
- if ($.model.is(props.type)) {
69
- discriminator = $.model.getDiscriminatedUnion(props.type);
70
- } else {
71
- const discriminatedUnion = $.union.getDiscriminatedUnion(props.type);
72
- if (discriminatedUnion?.options.discriminatorPropertyName) {
73
- discriminator = { propertyName: discriminatedUnion.options.discriminatorPropertyName };
74
- }
75
- }
76
- if (!discriminator) {
77
- return null;
78
- }
79
-
80
- const namePolicy = useNamePolicy();
81
- const transformName = namePolicy.getName(
82
- `json_${props.type.name}_to_${props.target}_discriminator`,
83
- "function",
84
- );
85
-
86
- const typeRef = refkey(props.type);
87
- const returnType = props.target === "transport" ? "any" : typeRef;
88
- const inputType = props.target === "transport" ? typeRef : "any";
89
- const inputRef = refkey();
90
-
91
- const parameters: ts.ParameterDescriptor[] = [
92
- { name: "input_", type: inputType, refkey: inputRef, optional: true },
93
- ];
94
-
95
- return (
96
- <ts.FunctionDeclaration
97
- name={transformName}
98
- export
99
- returnType={returnType}
100
- parameters={parameters}
101
- refkey={getJsonTransformDiscriminatorRefkey(props.type, props.target)}
102
- >
103
- {code`
104
- if(!${inputRef}) {
105
- return ${inputRef} as any;
106
- }
107
- `}
108
- <JsonTransformDiscriminator {...props} itemRef={inputRef} discriminator={discriminator} />
109
- </ts.FunctionDeclaration>
110
- );
111
- }
@@ -1,120 +0,0 @@
1
- import type { Refkey } from "@alloy-js/core";
2
- import { code } from "@alloy-js/core";
3
- import type { Children } from "@alloy-js/core/jsx-runtime";
4
- import type { Type } from "@typespec/compiler";
5
- import { useTsp } from "@typespec/emitter-framework";
6
- import { ScalarDataTransform } from "../data-transform.jsx";
7
- import {
8
- getJsonArrayTransformRefkey,
9
- JsonArrayTransform,
10
- JsonArrayTransformDeclaration,
11
- } from "./json-array-transform.jsx";
12
- import { JsonModelPropertyTransform } from "./json-model-property-transform.jsx";
13
- import {
14
- getJsonModelTransformRefkey,
15
- JsonModelTransform,
16
- JsonModelTransformDeclaration,
17
- } from "./json-model-transform.jsx";
18
- import {
19
- getJsonRecordTransformRefkey,
20
- JsonRecordTransform,
21
- JsonRecordTransformDeclaration,
22
- } from "./json-record-transform.jsx";
23
- import {
24
- getJsonUnionTransformRefkey,
25
- JsonUnionTransform,
26
- JsonUnionTransformDeclaration,
27
- } from "./union-transform.jsx";
28
-
29
- export interface JsonTransformProps {
30
- itemRef: Refkey | Children;
31
- type: Type;
32
- target: "transport" | "application";
33
- }
34
-
35
- export function JsonTransform(props: JsonTransformProps) {
36
- const { $ } = useTsp();
37
- const type = $.httpPart.unpack(props.type) ?? props.type;
38
- const declaredTransform = getTransformReference(type, props.target);
39
-
40
- if (declaredTransform) {
41
- return code`${declaredTransform}(${props.itemRef})`;
42
- }
43
-
44
- switch (type.kind) {
45
- case "Model": {
46
- if ($.array.is(type)) {
47
- return <JsonArrayTransform type={type} itemRef={props.itemRef} target={props.target} />;
48
- }
49
-
50
- if ($.record.is(type)) {
51
- return <JsonRecordTransform type={type} itemRef={props.itemRef} target={props.target} />;
52
- }
53
-
54
- return <JsonModelTransform type={type} itemRef={props.itemRef} target={props.target} />;
55
- }
56
- case "Union":
57
- return <JsonUnionTransform type={type} itemRef={props.itemRef} target={props.target} />;
58
- case "ModelProperty": {
59
- return (
60
- <JsonModelPropertyTransform type={type} itemRef={props.itemRef} target={props.target} />
61
- );
62
- }
63
- case "Scalar": {
64
- return <ScalarDataTransform type={type} itemRef={props.itemRef} target={props.target} />;
65
- }
66
- default:
67
- return props.itemRef;
68
- }
69
- }
70
-
71
- export interface JsonTransformDeclarationProps {
72
- type: Type;
73
- target: "transport" | "application";
74
- }
75
-
76
- export function JsonTransformDeclaration(props: JsonTransformDeclarationProps) {
77
- const { $ } = useTsp();
78
- if (!$.model.is(props.type) && !$.union.is(props.type)) {
79
- return null;
80
- }
81
-
82
- if ($.model.is(props.type)) {
83
- if ($.array.is(props.type)) {
84
- return <JsonArrayTransformDeclaration target={props.target} type={props.type} />;
85
- }
86
-
87
- if ($.record.is(props.type)) {
88
- return <JsonRecordTransformDeclaration target={props.target} type={props.type} />;
89
- }
90
-
91
- return <JsonModelTransformDeclaration type={props.type} target={props.target} />;
92
- }
93
-
94
- if ($.union.is(props.type)) {
95
- return <JsonUnionTransformDeclaration target={props.target} type={props.type} />;
96
- }
97
- }
98
-
99
- function getTransformReference(
100
- type: Type,
101
- target: "transport" | "application",
102
- ): Refkey | undefined {
103
- const { $ } = useTsp();
104
- if (type.kind === "Model" && Boolean(type.name)) {
105
- if ($.array.is(type)) {
106
- return getJsonArrayTransformRefkey(type, target);
107
- }
108
-
109
- if ($.record.is(type)) {
110
- return getJsonRecordTransformRefkey(type, target);
111
- }
112
- return getJsonModelTransformRefkey(type, target);
113
- }
114
-
115
- if (type.kind === "Union" && Boolean(type.name)) {
116
- return getJsonUnionTransformRefkey(type, target);
117
- }
118
-
119
- return undefined;
120
- }
@@ -1,87 +0,0 @@
1
- import type { Children, Refkey } from "@alloy-js/core";
2
- import { code, refkey } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import type { Union } from "@typespec/compiler";
5
- import { useTsp } from "@typespec/emitter-framework";
6
- import {
7
- getJsonTransformDiscriminatorRefkey,
8
- JsonTransformDiscriminatorDeclaration,
9
- } from "./json-transform-discriminator.jsx";
10
- import { JsonTransform } from "./json-transform.jsx";
11
-
12
- export interface JsonUnionTransformProps {
13
- itemRef: Refkey | Children;
14
- type: Union;
15
- target: "transport" | "application";
16
- }
17
-
18
- export function JsonUnionTransform(props: JsonUnionTransformProps) {
19
- const { $ } = useTsp();
20
- const discriminator = $.union.getDiscriminatedUnion(props.type);
21
- if (discriminator?.options.discriminatorPropertyName) {
22
- // return <JsonTransformDiscriminator {...props} discriminator={discriminator}/>;
23
- return (
24
- <>
25
- {getJsonTransformDiscriminatorRefkey(props.type, props.target)}({props.itemRef})
26
- </>
27
- );
28
- }
29
-
30
- const variantType = props.type.variants.values().next().value!.type;
31
-
32
- if (!$.union.isExtensible(props.type)) {
33
- return props.itemRef;
34
- }
35
-
36
- // TODO: Handle non-discriminated unions
37
- return <JsonTransform {...props} type={variantType} />;
38
- }
39
-
40
- export function getJsonUnionTransformRefkey(
41
- type: Union,
42
- target: "transport" | "application",
43
- ): Refkey {
44
- return refkey(type, "json_union_transform", target);
45
- }
46
- export interface JsonUnionTransformDeclarationProps {
47
- type: Union;
48
- target: "transport" | "application";
49
- }
50
-
51
- export function JsonUnionTransformDeclaration(props: JsonUnionTransformDeclarationProps) {
52
- const namePolicy = ts.useTSNamePolicy();
53
- const transformName = namePolicy.getName(
54
- `json_${props.type.name}_to_${props.target}_transform`,
55
- "function",
56
- );
57
-
58
- const typeRef = refkey(props.type);
59
- const returnType = props.target === "transport" ? "any" : typeRef;
60
- const inputType = props.target === "transport" ? <>{typeRef} | null</> : "any";
61
- const inputRef = refkey();
62
-
63
- const parameters: ts.ParameterDescriptor[] = [
64
- { name: "input_", type: inputType, refkey: inputRef, optional: true },
65
- ];
66
-
67
- const declarationRefkey = getJsonUnionTransformRefkey(props.type, props.target);
68
- return (
69
- <>
70
- <JsonTransformDiscriminatorDeclaration target={props.target} type={props.type} />
71
- <ts.FunctionDeclaration
72
- name={transformName}
73
- export
74
- returnType={returnType}
75
- parameters={parameters}
76
- refkey={declarationRefkey}
77
- >
78
- {code`
79
- if(!${inputRef}) {
80
- return ${inputRef} as any;
81
- }
82
- `}
83
- return <JsonUnionTransform {...props} itemRef={inputRef} />
84
- </ts.FunctionDeclaration>
85
- </>
86
- );
87
- }
@@ -1,12 +0,0 @@
1
- import type { Type } from "@typespec/compiler";
2
- import { type Typekit } from "@typespec/compiler/typekit";
3
- import { reportDiagnostic } from "../../../../lib.js";
4
-
5
- export function getJsonTransportName($: Typekit, type: Type) {
6
- if (!("name" in type)) {
7
- reportDiagnostic($.program, { code: "no-name-type", target: type });
8
- return "";
9
- }
10
-
11
- return $.type.getEncodedName(type as any, "json") ?? type.name;
12
- }