@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,218 +0,0 @@
1
- import type { Children, Refkey } from "@alloy-js/core";
2
- import { For, List, refkey } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import { useTsp } from "@typespec/emitter-framework";
5
- import { FunctionDeclaration } from "@typespec/emitter-framework/typescript";
6
- import type { HttpAuth } from "@typespec/http";
7
- import { type OAuth2Flow } from "@typespec/http";
8
- import type * as cl from "@typespec/http-client";
9
- import { reportDiagnostic } from "../../lib.js";
10
- import { buildClientParameters } from "../../utils/parameters.jsx";
11
- import { httpRuntimeTemplateLib } from "../external-packages/ts-http-runtime.js";
12
- import { addClientTestOptions } from "../testing/client-options.jsx";
13
- import { getClientcontextDeclarationRef } from "./client-context-declaration.jsx";
14
- import { ParametrizedEndpoint } from "./parametrized-endpoint.jsx";
15
-
16
- export interface ClientContextFactoryProps {
17
- client: cl.Client;
18
- }
19
-
20
- export function getClientContextFactoryRef(client: cl.Client) {
21
- return refkey(client, "contextFactory");
22
- }
23
-
24
- export function ClientContextFactoryDeclaration(props: ClientContextFactoryProps) {
25
- const { $ } = useTsp();
26
- const ref = getClientContextFactoryRef(props.client);
27
- const contextDeclarationRef = getClientcontextDeclarationRef(props.client);
28
- const namePolicy = ts.useTSNamePolicy();
29
- const factoryFunctionName = namePolicy.getName(`create_${props.client.name}Context`, "function");
30
-
31
- const clientConstructor = $.client.getConstructor(props.client);
32
- const parameters = buildClientParameters(props.client, refkey());
33
- const urlTemplate = $.client.getUrlTemplate(props.client);
34
- const endpointRef = refkey();
35
- const resolvedEndpoint = (
36
- <ParametrizedEndpoint
37
- refkey={endpointRef}
38
- template={urlTemplate.url}
39
- params={urlTemplate.parameters}
40
- />
41
- );
42
-
43
- let credentialsRef: string | undefined;
44
- if (parameters.some((p) => p.name === "credential")) {
45
- credentialsRef = "credential";
46
- }
47
-
48
- // Filter out optional parameters, they will be passed as options
49
- const args = (
50
- <ClientFactoryArguments
51
- client={props.client}
52
- endpointRef={endpointRef}
53
- credentialsRef={credentialsRef}
54
- />
55
- );
56
-
57
- return (
58
- <FunctionDeclaration
59
- export
60
- name={factoryFunctionName}
61
- type={clientConstructor}
62
- returnType={contextDeclarationRef}
63
- refkey={ref}
64
- parametersMode="replace"
65
- parameters={parameters}
66
- >
67
- {resolvedEndpoint}
68
- return <ts.FunctionCallExpression target={httpRuntimeTemplateLib.getClient} args={[args]} />
69
- </FunctionDeclaration>
70
- );
71
- }
72
-
73
- interface ClientFactoryArgumentsProps {
74
- client: cl.Client;
75
- endpointRef: Refkey;
76
- credentialsRef?: string;
77
- }
78
-
79
- function ClientFactoryArguments(props: ClientFactoryArgumentsProps) {
80
- const params = [<>{props.endpointRef},</>];
81
-
82
- return [
83
- ...params,
84
- props.credentialsRef ? (
85
- <ClientOptionsExpression>
86
- {props.credentialsRef}
87
- <AuthSchemeOptions client={props.client} />
88
- </ClientOptionsExpression>
89
- ) : (
90
- <ClientOptionsExpression />
91
- ),
92
- ];
93
- }
94
-
95
- interface AuthSchemeOptionsProps {
96
- client: cl.Client;
97
- }
98
-
99
- interface AuthSchemeProps {
100
- scheme: HttpAuth;
101
- client: cl.Client;
102
- }
103
-
104
- function AuthScheme(props: AuthSchemeProps) {
105
- const { $ } = useTsp();
106
- switch (props.scheme.type) {
107
- case "http":
108
- return (
109
- <ts.ObjectExpression>
110
- <List comma>
111
- <ts.ObjectProperty name="kind" jsValue="http" />
112
- <ts.ObjectProperty name="scheme" jsValue={props.scheme.scheme.toLowerCase()} />
113
- </List>
114
- </ts.ObjectExpression>
115
- );
116
- case "apiKey":
117
- if (props.scheme.in !== "header") {
118
- reportDiagnostic($.program, {
119
- code: "key-credential-non-header-not-implemented",
120
- target: props.client.service,
121
- });
122
- }
123
-
124
- return (
125
- <ts.ObjectExpression>
126
- <List comma>
127
- <ts.ObjectProperty name="kind" jsValue="apiKey" />
128
- <ts.ObjectProperty name="apiKeyLocation" jsValue={props.scheme.in} />
129
- <ts.ObjectProperty name="name" jsValue={props.scheme.name} />
130
- </List>
131
- </ts.ObjectExpression>
132
- );
133
- case "oauth2":
134
- return (
135
- <ts.ObjectExpression>
136
- <List comma>
137
- <ts.ObjectProperty name="kind" jsValue="oauth2" />
138
- <ts.ObjectProperty name="flows">
139
- [
140
- <For each={props.scheme.flows} comma line>
141
- {(flow) => <OAuth2Flow flow={flow} />}
142
- </For>
143
- ]
144
- </ts.ObjectProperty>
145
- </List>
146
- </ts.ObjectExpression>
147
- );
148
- default:
149
- return null;
150
- }
151
- }
152
-
153
- interface OAuth2FlowProps {
154
- flow: OAuth2Flow;
155
- }
156
-
157
- function OAuth2Flow(props: OAuth2FlowProps) {
158
- // rename type to kind and clean up the scopes
159
- const { type, ...rewrittenFlow } = {
160
- kind: props.flow.type,
161
- ...props.flow,
162
- // scopes are sometimes duplicated so converting to set and back to dedupe
163
- scopes: [...new Set(props.flow.scopes.map((s) => s.value))],
164
- };
165
-
166
- return <ts.ObjectExpression jsValue={rewrittenFlow} />;
167
- }
168
-
169
- function AuthSchemeOptions(props: AuthSchemeOptionsProps) {
170
- const { $ } = useTsp();
171
- const clientCredential = $.client.getAuth(props.client);
172
-
173
- if (clientCredential.schemes.length === 0) {
174
- return null;
175
- }
176
-
177
- // Filtering out custom http schemes for "http/custom" spector scenario
178
- // TODO: Should typekit allow for arbitrary strings on scheme with http auth type?
179
- const supportedSchemes = clientCredential.schemes.filter(
180
- (s) => s.type !== "http" || ["Basic", "Bearer"].includes(s.scheme),
181
- );
182
-
183
- return (
184
- <ts.ObjectProperty name="authSchemes">
185
- [
186
- <For each={supportedSchemes} comma line>
187
- {(scheme) => <AuthScheme scheme={scheme} client={props.client} />}
188
- </For>
189
- ]
190
- </ts.ObjectProperty>
191
- );
192
- }
193
-
194
- interface ClientOptionsExpressionProps {
195
- children?: Children;
196
- }
197
-
198
- function ClientOptionsExpression(props: ClientOptionsExpressionProps) {
199
- const options: Children = ["...options"];
200
-
201
- // Conditionally add test options
202
- // based on the environment variable TYPESPEC_JS_EMITTER_TESTING
203
- addClientTestOptions(options);
204
-
205
- const children = Array.isArray(props.children) ? props.children : [props.children];
206
- if (children.length) {
207
- const children = Array.isArray(props.children) ? props.children : [props.children];
208
- options.push(...children);
209
- }
210
-
211
- return (
212
- <ts.ObjectExpression>
213
- <For each={options} joiner="," line>
214
- {(child) => child}
215
- </For>
216
- </ts.ObjectExpression>
217
- );
218
- }
@@ -1,36 +0,0 @@
1
- import type { Children } from "@alloy-js/core";
2
- import { For, refkey } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import type * as cl from "@typespec/http-client";
5
- import { httpRuntimeTemplateLib } from "../external-packages/ts-http-runtime.js";
6
-
7
- export interface ClientContextOptionsDeclarationProps {
8
- client: cl.Client;
9
- }
10
-
11
- export function getClientContextOptionsRef(client: cl.Client) {
12
- return refkey(client, "options");
13
- }
14
-
15
- export function ClientContextOptionsDeclaration(props: ClientContextOptionsDeclarationProps) {
16
- const ref = getClientContextOptionsRef(props.client);
17
- const namePolicy = ts.useTSNamePolicy();
18
- const name = namePolicy.getName(`${props.client.name}Options`, "interface");
19
-
20
- // TODO: Here we will calculate and include all the options that the client can accept
21
- const clientOptions: Map<string, Children> = new Map();
22
-
23
- return (
24
- <ts.InterfaceDeclaration
25
- export
26
- name={name}
27
- refkey={ref}
28
- extends={<ts.Reference refkey={httpRuntimeTemplateLib.ClientOptions} />}
29
- >
30
- <For each={Array.from(clientOptions.entries())} semicolon line>
31
- {([key, value]) => <ts.InterfaceMember optional name={key} type={value} />}
32
- </For>
33
- <ts.InterfaceMember optional name="endpoint" type="string" />;
34
- </ts.InterfaceDeclaration>
35
- );
36
- }
@@ -1,23 +0,0 @@
1
- import { type Children } from "@alloy-js/core";
2
- import * as ts from "@alloy-js/typescript";
3
- import type * as cl from "@typespec/http-client";
4
- import { ClientContextDeclaration } from "./client-context-declaration.jsx";
5
- import { ClientContextFactoryDeclaration } from "./client-context-factory.jsx";
6
- import { ClientContextOptionsDeclaration } from "./client-context-options.jsx";
7
-
8
- export interface ClientContextProps {
9
- client: cl.Client;
10
- children?: Children;
11
- }
12
-
13
- export function ClientContext(props: ClientContextProps) {
14
- const namePolicy = ts.useTSNamePolicy();
15
- const fileName = namePolicy.getName(props.client.name + "Context", "variable");
16
- return (
17
- <ts.SourceFile path={`${fileName}.ts`}>
18
- <ClientContextDeclaration client={props.client} />
19
- <ClientContextOptionsDeclaration client={props.client} />
20
- <ClientContextFactoryDeclaration client={props.client} />
21
- </ts.SourceFile>
22
- );
23
- }
@@ -1,51 +0,0 @@
1
- import type { Refkey } from "@alloy-js/core";
2
- import { code, For, refkey, StatementList } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import type { ModelProperty } from "@typespec/compiler";
5
- import { useTransformNamePolicy } from "@typespec/emitter-framework";
6
- import { getDefaultValue } from "../../utils/parameters.jsx";
7
-
8
- export interface ParametrizedEndpointProps {
9
- refkey: Refkey;
10
- template: string;
11
- params: ModelProperty[];
12
- }
13
-
14
- export function ParametrizedEndpoint(props: ParametrizedEndpointProps) {
15
- const propNamer = useTransformNamePolicy();
16
- const paramsRef = refkey();
17
- const params = (
18
- <ts.VarDeclaration name="params" type={"Record<string, any>"} refkey={paramsRef}>
19
- <ts.ObjectExpression>
20
- <For each={props.params} joiner="," line>
21
- {(p) => {
22
- const applicationName = propNamer.getApplicationName(p);
23
- const transportName = propNamer.getTransportName(p);
24
- const defaultValue = p.defaultValue ? ` ?? ${getDefaultValue(p)}` : "";
25
- const itemRef = p.optional
26
- ? `options?.${applicationName}${defaultValue}`
27
- : applicationName;
28
- return <ts.ObjectProperty name={transportName} value={itemRef} />;
29
- }}
30
- </For>
31
- </ts.ObjectExpression>
32
- </ts.VarDeclaration>
33
- );
34
-
35
- const resolvedEndpoint = (
36
- <ts.VarDeclaration name="resolvedEndpoint" refkey={props.refkey}>
37
- {code`
38
- "${props.template}".replace(/{([^}]+)}/g, (_, key) =>
39
- key in ${paramsRef} ? String(params[key]) : (() => { throw new Error(\`Missing parameter: $\{key}\`); })()
40
- );
41
- `}
42
- </ts.VarDeclaration>
43
- );
44
-
45
- return (
46
- <StatementList>
47
- {params}
48
- {resolvedEndpoint}
49
- </StatementList>
50
- );
51
- }
@@ -1,51 +0,0 @@
1
- import type { Children } from "@alloy-js/core";
2
- import { For, SourceDirectory, StatementList } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import type * as cl from "@typespec/http-client";
5
- import { useClientLibrary } from "@typespec/http-client";
6
- import { ClientContext } from "./client-context/client-context.jsx";
7
- import { ClientOperations } from "./client-operation.jsx";
8
-
9
- export interface OperationsDirectoryProps {
10
- children?: Children;
11
- }
12
-
13
- export function OperationsDirectory(props: OperationsDirectoryProps) {
14
- const { topLevel: clients } = useClientLibrary();
15
- // If it is the root client, we don't need to create a directory
16
- return (
17
- <For each={clients} joiner="," line>
18
- {(client) => (
19
- <StatementList>
20
- <ClientOperations client={client} />
21
- <ClientContext client={client} />
22
- <SubClients client={client} />
23
- </StatementList>
24
- )}
25
- </For>
26
- );
27
- }
28
-
29
- export interface SubClientsProps {
30
- client: cl.Client;
31
- }
32
-
33
- export function SubClients(props: SubClientsProps) {
34
- const subClients = props.client.subClients;
35
-
36
- return (
37
- <For each={subClients}>
38
- {(subClient) => {
39
- const namePolicy = ts.useTSNamePolicy();
40
- const subClientName = namePolicy.getName(subClient.name, "variable");
41
- return (
42
- <SourceDirectory path={subClientName}>
43
- <ClientOperations client={subClient} />
44
- <ClientContext client={subClient} />
45
- <SubClients client={subClient} />
46
- </SourceDirectory>
47
- );
48
- }}
49
- </For>
50
- );
51
- }
@@ -1,102 +0,0 @@
1
- import type { Refkey } from "@alloy-js/core";
2
- import { For, List, refkey } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import { useTsp } from "@typespec/emitter-framework";
5
- import { FunctionDeclaration, TypeExpression } from "@typespec/emitter-framework/typescript";
6
- import type { HttpOperation } from "@typespec/http";
7
- import * as cl from "@typespec/http-client";
8
- import { reportDiagnostic } from "../lib.js";
9
- import { getClientcontextDeclarationRef } from "./client-context/client-context-declaration.jsx";
10
- import { HttpRequest } from "./http-request.jsx";
11
- import { HttpResponse } from "./http-response.jsx";
12
- import { OperationPipeline } from "./operation-handlers/operation-pipeline.jsx";
13
- import { PaginatedOperationHandler } from "./operation-handlers/paging/paginated-operation-handler.jsx";
14
- import { OperationOptionsDeclaration } from "./operation-options.jsx";
15
- import { getOperationParameters } from "./operation-parameters.jsx";
16
-
17
- export interface ClientOperationsProps {
18
- client: cl.Client;
19
- }
20
-
21
- export function ClientOperations(props: ClientOperationsProps) {
22
- const namePolicy = ts.useTSNamePolicy();
23
- const clientOperations = props.client.operations;
24
- const fileName = namePolicy.getName(props.client.name + "Operations", "variable");
25
-
26
- if (clientOperations.length === 0) {
27
- return null;
28
- }
29
-
30
- return (
31
- <ts.SourceFile path={`${fileName}.ts`}>
32
- <For each={clientOperations}>
33
- {(operation) => (
34
- <OperationPipeline
35
- httpOperation={operation.httpOperation}
36
- pipeline={{ handlers: [PaginatedOperationHandler] }}
37
- />
38
- )}
39
- </For>
40
- </ts.SourceFile>
41
- );
42
- }
43
-
44
- export interface ClientOperationProps {
45
- httpOperation: HttpOperation;
46
- refkey: Refkey;
47
- internal?: boolean;
48
- }
49
-
50
- function getClientOperationOptionsParamRef(httpOperation: HttpOperation): Refkey {
51
- return refkey(httpOperation, "client-operation-options-parameter");
52
- }
53
-
54
- export function ClientOperation(props: ClientOperationProps) {
55
- const { $ } = useTsp();
56
- const optionsRefkey = getClientOperationOptionsParamRef(props.httpOperation);
57
- const clientLibrary = cl.useClientLibrary();
58
- const client = clientLibrary.getClientForOperation(props.httpOperation);
59
-
60
- if (!client) {
61
- reportDiagnostic($.program, {
62
- code: "client-not-found",
63
- target: props.httpOperation.operation,
64
- });
65
- return;
66
- }
67
-
68
- const returnType = $.httpOperation.getReturnType(props.httpOperation);
69
- const responseRefkey = refkey(props.httpOperation, "http-response");
70
- const clientContextInterfaceRef = getClientcontextDeclarationRef(client);
71
- const signatureParams: ts.ParameterDescriptor[] = [
72
- {
73
- name: "client",
74
- type: clientContextInterfaceRef,
75
- },
76
- ...getOperationParameters(props.httpOperation, optionsRefkey),
77
- ];
78
- return (
79
- <List hardline>
80
- <OperationOptionsDeclaration operation={props.httpOperation} />
81
- <FunctionDeclaration
82
- export={!props.internal}
83
- async
84
- type={props.httpOperation.operation}
85
- returnType={<TypeExpression type={returnType} />}
86
- parametersMode="replace"
87
- parameters={signatureParams}
88
- refkey={props.refkey}
89
- >
90
- <List hardline>
91
- <HttpRequest
92
- httpOperation={props.httpOperation}
93
- responseRefkey={responseRefkey}
94
- operationOptionsParamRefkey={optionsRefkey}
95
- />
96
- <HttpResponse httpOperation={props.httpOperation} responseRefkey={responseRefkey} />
97
- </List>
98
- </FunctionDeclaration>
99
- ;
100
- </List>
101
- );
102
- }
@@ -1,177 +0,0 @@
1
- import type { Refkey } from "@alloy-js/core";
2
- import { For, List, refkey } from "@alloy-js/core";
3
- import * as ts from "@alloy-js/typescript";
4
- import { useTsp } from "@typespec/emitter-framework";
5
- import { ClassMethod } from "@typespec/emitter-framework/typescript";
6
- import type * as cl from "@typespec/http-client";
7
- import { useClientLibrary } from "@typespec/http-client";
8
- import { flattenClients } from "../utils/client-discovery.js";
9
- import { buildClientParameters } from "../utils/parameters.jsx";
10
- import { getClientcontextDeclarationRef } from "./client-context/client-context-declaration.jsx";
11
- import { getClientContextFactoryRef } from "./client-context/client-context-factory.jsx";
12
- import { getOperationParameters } from "./operation-parameters.jsx";
13
-
14
- export interface ClientProps {}
15
-
16
- export function Client(props: ClientProps) {
17
- const { $ } = useTsp();
18
- const namePolicy = ts.useTSNamePolicy();
19
- const { topLevel } = useClientLibrary();
20
-
21
- return (
22
- <For each={topLevel} hardline>
23
- {(client) => {
24
- const fileName = namePolicy.getName($.client.getName(client), "variable");
25
- const flatClients = flattenClients(client);
26
- return (
27
- <ts.SourceFile path={`${fileName}.ts`}>
28
- <For each={flatClients} hardline>
29
- {(client) => <ClientClass client={client} />}
30
- </For>
31
- </ts.SourceFile>
32
- );
33
- }}
34
- </For>
35
- );
36
- }
37
-
38
- export interface ClientClassProps {
39
- client: cl.Client;
40
- }
41
-
42
- export function getClientClassRef(client: cl.Client) {
43
- return refkey(client.type, "client-class");
44
- }
45
-
46
- function getClientContextFieldRef(client: cl.Client) {
47
- return refkey(client.type, "client-context");
48
- }
49
- export function ClientClass(props: ClientClassProps) {
50
- const { $ } = useTsp();
51
- const namePolicy = ts.useTSNamePolicy();
52
- const clientName = namePolicy.getName($.client.getName(props.client), "class");
53
- const contextMemberRef = getClientContextFieldRef(props.client);
54
- const contextDeclarationRef = getClientcontextDeclarationRef(props.client);
55
- const clientClassRef = getClientClassRef(props.client);
56
- const subClients = props.client.subClients;
57
- const operations = props.client.operations;
58
- return (
59
- <ts.ClassDeclaration export name={clientName} refkey={clientClassRef}>
60
- <List hardline>
61
- <ts.ClassField
62
- name="context"
63
- jsPrivate
64
- refkey={contextMemberRef}
65
- type={contextDeclarationRef}
66
- />
67
- <For each={subClients} hardline semicolon>
68
- {(subClient) => <SubClientClassField client={subClient} />}
69
- </For>
70
- <ClientConstructor client={props.client} />
71
- <For each={operations} hardline semicolon>
72
- {(op) => {
73
- const parameters = getOperationParameters(op.httpOperation, refkey());
74
- const args = parameters.flatMap((p) => p.refkey);
75
- const isPaging = Boolean($.operation.getPagingMetadata(op.httpOperation.operation));
76
-
77
- return (
78
- <ClassMethod
79
- async={!isPaging}
80
- type={op.httpOperation.operation}
81
- parameters={parameters}
82
- returnType={null}
83
- parametersMode="replace"
84
- >
85
- return{" "}
86
- <ts.FunctionCallExpression
87
- target={refkey(op.httpOperation.operation)}
88
- args={[contextMemberRef, ...args]}
89
- />
90
- ;
91
- </ClassMethod>
92
- );
93
- }}
94
- </For>
95
- </List>
96
- </ts.ClassDeclaration>
97
- );
98
- }
99
-
100
- interface SubClientClassFieldProps {
101
- client: cl.Client;
102
- }
103
-
104
- function getSubClientClassFieldRef(client: cl.Client) {
105
- return refkey(client.type, "client-field");
106
- }
107
-
108
- function SubClientClassField(props: SubClientClassFieldProps) {
109
- const { $ } = useTsp();
110
- const parent = props.client.parent;
111
- // If sub client has different parameters than client, don't add it as a subclass field
112
- // Todo: We need to detect the extra parameters and make this field a factory for the subclient
113
- if (parent && !$.client.haveSameConstructor(props.client, parent)) {
114
- return null;
115
- }
116
-
117
- const namePolicy = ts.useTSNamePolicy();
118
- const fieldName = namePolicy.getName($.client.getName(props.client), "class");
119
- const subClientClassRef = getClientClassRef(props.client);
120
- const subClientFieldRef = getSubClientClassFieldRef(props.client);
121
- return <ts.ClassField name={fieldName} type={subClientClassRef} refkey={subClientFieldRef} />;
122
- }
123
-
124
- interface ClientConstructorProps {
125
- client: cl.Client;
126
- }
127
-
128
- function ClientConstructor(props: ClientConstructorProps) {
129
- const { $ } = useTsp();
130
- const subClients = props.client.subClients.filter((sc) =>
131
- $.client.haveSameConstructor(sc, props.client),
132
- );
133
- const clientContextFieldRef = getClientContextFieldRef(props.client);
134
- const clientContextFactoryRef = getClientContextFactoryRef(props.client);
135
- const constructorParameters = buildClientParameters(props.client, refkey());
136
- const args = Object.values(constructorParameters).map((p) => p.refkey);
137
-
138
- return (
139
- <ts.ClassMethod name="constructor" parameters={constructorParameters}>
140
- {clientContextFieldRef} ={" "}
141
- <ts.FunctionCallExpression target={clientContextFactoryRef} args={args} />;<br />
142
- <For each={subClients} joiner=";" hardline>
143
- {(subClient) => {
144
- const subClientFieldRef = getSubClientClassFieldRef(subClient);
145
- const subClientArgs = calculateSubClientArgs(subClient, constructorParameters);
146
- return (
147
- <>
148
- {subClientFieldRef} = <NewClientExpression client={subClient} args={subClientArgs} />;
149
- </>
150
- );
151
- }}
152
- </For>
153
- </ts.ClassMethod>
154
- );
155
- }
156
-
157
- function calculateSubClientArgs(subClient: cl.Client, parentParams: ts.ParameterDescriptor[]) {
158
- const subClientParams = buildClientParameters(subClient, refkey()).map((p) => p.name);
159
- return parentParams
160
- .filter(({ name }) => subClientParams.includes(name))
161
- .flatMap((p) => (p.refkey ? p.refkey : []));
162
- }
163
-
164
- export interface NewClientExpressionProps {
165
- client: cl.Client;
166
- args: Refkey[];
167
- }
168
-
169
- function NewClientExpression(props: NewClientExpressionProps) {
170
- const clientConstructorRef = getClientClassRef(props.client);
171
-
172
- return (
173
- <>
174
- new <ts.FunctionCallExpression target={clientConstructorRef} args={props.args} />
175
- </>
176
- );
177
- }
@@ -1,17 +0,0 @@
1
- import { type Children } from "@alloy-js/core";
2
- import { EncodingContext } from "../context/encoding/encoding-context.jsx";
3
- import type { EncodingDefaults } from "../context/encoding/types.js";
4
- export interface EncodingProviderProps {
5
- defaults?: EncodingDefaults;
6
- children?: Children;
7
- }
8
-
9
- export function EncodingProvider(props: EncodingProviderProps) {
10
- const defaults: EncodingDefaults = {
11
- bytes: "none",
12
- datetime: "rfc3339",
13
- ...props.defaults,
14
- };
15
-
16
- return <EncodingContext.Provider value={defaults}>{props.children}</EncodingContext.Provider>;
17
- }