@typespec/http-client-js 0.4.0-dev.1 → 0.4.0-dev.3

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 (171) hide show
  1. package/CHANGELOG.md +0 -1
  2. package/dist/src/components/client-context/client-context-factory.js +1 -1
  3. package/dist/src/components/client-context/client-context-factory.js.map +1 -1
  4. package/dist/src/components/client-operation.d.ts +4 -1
  5. package/dist/src/components/client-operation.d.ts.map +1 -1
  6. package/dist/src/components/client-operation.js +68 -38
  7. package/dist/src/components/client-operation.js.map +1 -1
  8. package/dist/src/components/client.d.ts.map +1 -1
  9. package/dist/src/components/client.js +7 -4
  10. package/dist/src/components/client.js.map +1 -1
  11. package/dist/src/components/http-request-options.d.ts +5 -5
  12. package/dist/src/components/http-request-options.d.ts.map +1 -1
  13. package/dist/src/components/http-request-options.js +12 -12
  14. package/dist/src/components/http-request-options.js.map +1 -1
  15. package/dist/src/components/http-request-parameters-expression.d.ts +2 -1
  16. package/dist/src/components/http-request-parameters-expression.d.ts.map +1 -1
  17. package/dist/src/components/http-request-parameters-expression.js +125 -18
  18. package/dist/src/components/http-request-parameters-expression.js.map +1 -1
  19. package/dist/src/components/http-request.d.ts +3 -3
  20. package/dist/src/components/http-request.d.ts.map +1 -1
  21. package/dist/src/components/http-request.js +35 -30
  22. package/dist/src/components/http-request.js.map +1 -1
  23. package/dist/src/components/http-response.d.ts +3 -3
  24. package/dist/src/components/http-response.d.ts.map +1 -1
  25. package/dist/src/components/http-response.js +11 -6
  26. package/dist/src/components/http-response.js.map +1 -1
  27. package/dist/src/components/operation-handlers/index.d.ts +3 -0
  28. package/dist/src/components/operation-handlers/index.d.ts.map +1 -0
  29. package/dist/src/components/operation-handlers/index.js +3 -0
  30. package/dist/src/components/operation-handlers/index.js.map +1 -0
  31. package/dist/src/components/operation-handlers/operation-pipeline.d.ts +15 -0
  32. package/dist/src/components/operation-handlers/operation-pipeline.d.ts.map +1 -0
  33. package/dist/src/components/operation-handlers/operation-pipeline.js +28 -0
  34. package/dist/src/components/operation-handlers/operation-pipeline.js.map +1 -0
  35. package/dist/src/components/operation-handlers/paging/page-item.d.ts +3 -0
  36. package/dist/src/components/operation-handlers/paging/page-item.d.ts.map +1 -0
  37. package/dist/src/components/operation-handlers/paging/page-item.js +17 -0
  38. package/dist/src/components/operation-handlers/paging/page-item.js.map +1 -0
  39. package/dist/src/components/operation-handlers/paging/page-response.d.ts +10 -0
  40. package/dist/src/components/operation-handlers/paging/page-response.d.ts.map +1 -0
  41. package/dist/src/components/operation-handlers/paging/page-response.js +50 -0
  42. package/dist/src/components/operation-handlers/paging/page-response.js.map +1 -0
  43. package/dist/src/components/operation-handlers/paging/page-settings.d.ts +11 -0
  44. package/dist/src/components/operation-handlers/paging/page-settings.d.ts.map +1 -0
  45. package/dist/src/components/operation-handlers/paging/page-settings.js +52 -0
  46. package/dist/src/components/operation-handlers/paging/page-settings.js.map +1 -0
  47. package/dist/src/components/operation-handlers/paging/paginated-operation-handler.d.ts +3 -0
  48. package/dist/src/components/operation-handlers/paging/paginated-operation-handler.d.ts.map +1 -0
  49. package/dist/src/components/operation-handlers/paging/paginated-operation-handler.js +170 -0
  50. package/dist/src/components/operation-handlers/paging/paginated-operation-handler.js.map +1 -0
  51. package/dist/src/components/operation-handlers/paging/request-send.d.ts +12 -0
  52. package/dist/src/components/operation-handlers/paging/request-send.d.ts.map +1 -0
  53. package/dist/src/components/operation-handlers/paging/request-send.js +78 -0
  54. package/dist/src/components/operation-handlers/paging/request-send.js.map +1 -0
  55. package/dist/src/components/operation-handlers/paging/response-deserialize.d.ts +11 -0
  56. package/dist/src/components/operation-handlers/paging/response-deserialize.d.ts.map +1 -0
  57. package/dist/src/components/operation-handlers/paging/response-deserialize.js +51 -0
  58. package/dist/src/components/operation-handlers/paging/response-deserialize.js.map +1 -0
  59. package/dist/src/components/operation-handlers/paging/util.d.ts +17 -0
  60. package/dist/src/components/operation-handlers/paging/util.d.ts.map +1 -0
  61. package/dist/src/components/operation-handlers/paging/util.js +46 -0
  62. package/dist/src/components/operation-handlers/paging/util.js.map +1 -0
  63. package/dist/src/components/operation-handlers/types.d.ts +16 -0
  64. package/dist/src/components/operation-handlers/types.d.ts.map +1 -0
  65. package/dist/src/components/operation-handlers/types.js +1 -0
  66. package/dist/src/components/operation-handlers/types.js.map +1 -0
  67. package/dist/src/components/operation-options.d.ts +2 -0
  68. package/dist/src/components/operation-options.d.ts.map +1 -1
  69. package/dist/src/components/operation-options.js +2 -1
  70. package/dist/src/components/operation-options.js.map +1 -1
  71. package/dist/src/components/operation-parameters.d.ts +1 -1
  72. package/dist/src/components/operation-parameters.d.ts.map +1 -1
  73. package/dist/src/components/operation-parameters.js +7 -5
  74. package/dist/src/components/operation-parameters.js.map +1 -1
  75. package/dist/src/components/output.d.ts.map +1 -1
  76. package/dist/src/components/output.js +4 -0
  77. package/dist/src/components/output.js.map +1 -1
  78. package/dist/src/components/static-helpers/bytes-encoding.js +14 -16
  79. package/dist/src/components/static-helpers/bytes-encoding.js.map +1 -1
  80. package/dist/src/components/static-helpers/multipart-helpers.js +12 -10
  81. package/dist/src/components/static-helpers/multipart-helpers.js.map +1 -1
  82. package/dist/src/components/static-helpers/paging-helper.d.ts +5 -0
  83. package/dist/src/components/static-helpers/paging-helper.d.ts.map +1 -0
  84. package/dist/src/components/static-helpers/paging-helper.js +197 -0
  85. package/dist/src/components/static-helpers/paging-helper.js.map +1 -0
  86. package/dist/src/components/static-helpers/rest-error.d.ts.map +1 -1
  87. package/dist/src/components/static-helpers/rest-error.js +15 -10
  88. package/dist/src/components/static-helpers/rest-error.js.map +1 -1
  89. package/dist/src/components/testing/client-options.d.ts +1 -1
  90. package/dist/src/components/testing/client-options.d.ts.map +1 -1
  91. package/dist/src/components/transforms/json/json-array-transform.d.ts +1 -1
  92. package/dist/src/components/transforms/json/json-array-transform.d.ts.map +1 -1
  93. package/dist/src/components/transforms/json/json-array-transform.js +6 -7
  94. package/dist/src/components/transforms/json/json-array-transform.js.map +1 -1
  95. package/dist/src/components/transforms/json/json-model-property-transform.d.ts.map +1 -1
  96. package/dist/src/components/transforms/json/json-model-property-transform.js +15 -2
  97. package/dist/src/components/transforms/json/json-model-property-transform.js.map +1 -1
  98. package/dist/src/components/transforms/json/json-model-transform.js +8 -8
  99. package/dist/src/components/transforms/json/json-model-transform.js.map +1 -1
  100. package/dist/src/components/transforms/json/json-record-transform.d.ts +1 -1
  101. package/dist/src/components/transforms/json/json-record-transform.d.ts.map +1 -1
  102. package/dist/src/components/transforms/json/json-record-transform.js +6 -7
  103. package/dist/src/components/transforms/json/json-record-transform.js.map +1 -1
  104. package/dist/src/components/transforms/json/json-transform-discriminator.js +6 -7
  105. package/dist/src/components/transforms/json/json-transform-discriminator.js.map +1 -1
  106. package/dist/src/components/transforms/json/union-transform.js +6 -7
  107. package/dist/src/components/transforms/json/union-transform.js.map +1 -1
  108. package/dist/src/components/transforms/operation-transform-declaration.d.ts.map +1 -1
  109. package/dist/src/components/transforms/operation-transform-declaration.js +2 -3
  110. package/dist/src/components/transforms/operation-transform-declaration.js.map +1 -1
  111. package/dist/src/components/transforms/operation-transform-expression.d.ts +2 -2
  112. package/dist/src/components/transforms/operation-transform-expression.d.ts.map +1 -1
  113. package/dist/src/components/transforms/operation-transform-expression.js +1 -1
  114. package/dist/src/components/transforms/operation-transform-expression.js.map +1 -1
  115. package/dist/src/emitter.d.ts.map +1 -1
  116. package/dist/src/emitter.js +2 -1
  117. package/dist/src/emitter.js.map +1 -1
  118. package/dist/src/lib.d.ts +17 -2
  119. package/dist/src/lib.d.ts.map +1 -1
  120. package/dist/src/lib.js +6 -0
  121. package/dist/src/lib.js.map +1 -1
  122. package/dist/src/utils/parameters.d.ts +1 -1
  123. package/dist/src/utils/parameters.d.ts.map +1 -1
  124. package/dist/src/utils/parameters.js +13 -12
  125. package/dist/src/utils/parameters.js.map +1 -1
  126. package/dist/tsconfig.tsbuildinfo +1 -1
  127. package/package.json +4 -4
  128. package/src/components/client-context/client-context-factory.tsx +1 -1
  129. package/src/components/client-operation.tsx +40 -16
  130. package/src/components/client.tsx +8 -10
  131. package/src/components/http-request-options.tsx +17 -13
  132. package/src/components/http-request-parameters-expression.tsx +142 -18
  133. package/src/components/http-request.tsx +22 -20
  134. package/src/components/http-response.tsx +8 -9
  135. package/src/components/operation-handlers/index.tsx +2 -0
  136. package/src/components/operation-handlers/operation-pipeline.tsx +33 -0
  137. package/src/components/operation-handlers/paging/page-item.tsx +13 -0
  138. package/src/components/operation-handlers/paging/page-response.tsx +55 -0
  139. package/src/components/operation-handlers/paging/page-settings.tsx +53 -0
  140. package/src/components/operation-handlers/paging/paginated-operation-handler.tsx +192 -0
  141. package/src/components/operation-handlers/paging/request-send.tsx +70 -0
  142. package/src/components/operation-handlers/paging/response-deserialize.tsx +54 -0
  143. package/src/components/operation-handlers/paging/util.ts +70 -0
  144. package/src/components/operation-handlers/types.tsx +18 -0
  145. package/src/components/operation-options.tsx +6 -3
  146. package/src/components/operation-parameters.tsx +9 -8
  147. package/src/components/output.tsx +2 -1
  148. package/src/components/static-helpers/bytes-encoding.tsx +5 -5
  149. package/src/components/static-helpers/multipart-helpers.tsx +7 -7
  150. package/src/components/static-helpers/paging-helper.tsx +195 -0
  151. package/src/components/static-helpers/rest-error.tsx +6 -3
  152. package/src/components/transforms/json/json-array-transform.tsx +3 -3
  153. package/src/components/transforms/json/json-model-property-transform.tsx +13 -1
  154. package/src/components/transforms/json/json-model-transform.tsx +3 -3
  155. package/src/components/transforms/json/json-record-transform.tsx +3 -3
  156. package/src/components/transforms/json/json-transform-discriminator.tsx +3 -3
  157. package/src/components/transforms/json/union-transform.tsx +3 -3
  158. package/src/components/transforms/operation-transform-declaration.tsx +2 -1
  159. package/src/components/transforms/operation-transform-expression.tsx +3 -3
  160. package/src/emitter.tsx +2 -0
  161. package/src/lib.ts +6 -0
  162. package/src/utils/parameters.tsx +23 -25
  163. package/test/e2e/http/payload/pageable/main.test.ts +78 -22
  164. package/test/scenarios/encoding/header_bytes.md +1 -1
  165. package/test/scenarios/encoding/header_date.md +1 -1
  166. package/test/scenarios/encoding/query_date.md +1 -1
  167. package/test/scenarios/http-operations/paging.md +244 -0
  168. package/test/scenarios/http-operations/query-parameter.md +1 -1
  169. package/test/scenarios/operation-parameters/body_root_anonymous.md +1 -1
  170. package/test/scenarios/operation-parameters/with_body_property.md +1 -1
  171. package/test/scenarios/operation-parameters/with_body_root.md +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typespec/http-client-js",
3
- "version": "0.4.0-dev.1",
3
+ "version": "0.4.0-dev.3",
4
4
  "type": "module",
5
5
  "homepage": "https://typespec.io",
6
6
  "readme": "https://github.com/microsoft/typespec/blob/main/packages/http-client-js/README.md",
@@ -31,9 +31,9 @@
31
31
  "@typespec/rest": "^0.68.0 || >=0.69.0-dev <0.69.0"
32
32
  },
33
33
  "dependencies": {
34
- "@alloy-js/core": "^0.6.0",
35
- "@alloy-js/typescript": "^0.6.0",
36
- "@typespec/emitter-framework": "^0.4.0 || >=0.5.0-dev <0.5.0",
34
+ "@alloy-js/core": "^0.10.0",
35
+ "@alloy-js/typescript": "^0.10.0",
36
+ "@typespec/emitter-framework": "^0.5.0 || >=0.6.0-dev <0.6.0",
37
37
  "@typespec/http-client": "^0.4.0 || >=0.5.0-dev <0.5.0",
38
38
  "prettier": "~3.5.3"
39
39
  },
@@ -38,7 +38,7 @@ export function ClientContextFactoryDeclaration(props: ClientContextFactoryProps
38
38
  );
39
39
 
40
40
  let credentialsRef: string | undefined;
41
- if ("credential" in parameters) {
41
+ if (parameters.some((p) => p.name === "credential")) {
42
42
  credentialsRef = "credential";
43
43
  }
44
44
 
@@ -2,10 +2,14 @@ import * as ay from "@alloy-js/core";
2
2
  import * as ts from "@alloy-js/typescript";
3
3
  import { $ } from "@typespec/compiler/experimental/typekit";
4
4
  import { FunctionDeclaration, TypeExpression } from "@typespec/emitter-framework/typescript";
5
+ import { HttpOperation } from "@typespec/http";
5
6
  import * as cl from "@typespec/http-client";
7
+ import { reportDiagnostic } from "../lib.js";
6
8
  import { getClientcontextDeclarationRef } from "./client-context/client-context-declaration.jsx";
7
9
  import { HttpRequest } from "./http-request.jsx";
8
10
  import { HttpResponse } from "./http-response.jsx";
11
+ import { OperationPipeline } from "./operation-handlers/operation-pipeline.jsx";
12
+ import { PaginatedOperationHandler } from "./operation-handlers/paging/paginated-operation-handler.jsx";
9
13
  import { OperationOptionsDeclaration } from "./operation-options.jsx";
10
14
  import { getOperationParameters } from "./operation-parameters.jsx";
11
15
 
@@ -25,40 +29,60 @@ export function ClientOperations(props: ClientOperationsProps) {
25
29
  return (
26
30
  <ts.SourceFile path={`${fileName}.ts`}>
27
31
  <ay.For each={clientOperations}>
28
- {(operation) => <ClientOperation clientOperation={operation} />}
32
+ {(operation) => (
33
+ <OperationPipeline
34
+ httpOperation={operation.httpOperation}
35
+ pipeline={{ handlers: [PaginatedOperationHandler] }}
36
+ />
37
+ )}
29
38
  </ay.For>
30
39
  </ts.SourceFile>
31
40
  );
32
41
  }
33
42
 
34
43
  export interface ClientOperationProps {
35
- clientOperation: cl.ClientOperation;
44
+ httpOperation: HttpOperation;
45
+ refkey: ay.Refkey;
46
+ internal?: boolean;
36
47
  }
37
48
 
38
49
  export function ClientOperation(props: ClientOperationProps) {
39
- const client = props.clientOperation.client;
40
- const returnType = $.httpOperation.getReturnType(props.clientOperation.httpOperation);
41
- const responseRefkey = ay.refkey(props.clientOperation, "http-response");
50
+ const clientLibrary = cl.useClientLibrary();
51
+ const client = clientLibrary.getClientForOperation(props.httpOperation);
52
+
53
+ if (!client) {
54
+ reportDiagnostic($.program, {
55
+ code: "client-not-found",
56
+ target: props.httpOperation.operation,
57
+ });
58
+ return;
59
+ }
60
+
61
+ const returnType = $.httpOperation.getReturnType(props.httpOperation);
62
+ const responseRefkey = ay.refkey(props.httpOperation, "http-response");
42
63
  const clientContextInterfaceRef = getClientcontextDeclarationRef(client);
43
- const signatureParams: Record<string, ts.ParameterDescriptor | ay.Children> = {
44
- client: { type: clientContextInterfaceRef, refkey: ay.refkey(client, "client") },
45
- ...getOperationParameters(props.clientOperation.httpOperation),
46
- };
64
+ const signatureParams: ts.ParameterDescriptor[] = [
65
+ { name: "client", type: clientContextInterfaceRef, refkey: ay.refkey(client, "client") },
66
+ ...getOperationParameters(props.httpOperation),
67
+ ];
47
68
  return (
48
- <>
49
- <OperationOptionsDeclaration operation={props.clientOperation.httpOperation} />
69
+ <ay.List hardline>
70
+ <OperationOptionsDeclaration operation={props.httpOperation} />
50
71
  <FunctionDeclaration
51
- export
72
+ export={!props.internal}
52
73
  async
53
- type={props.clientOperation.httpOperation.operation}
74
+ type={props.httpOperation.operation}
54
75
  returnType={<TypeExpression type={returnType} />}
55
76
  parametersMode="replace"
56
77
  parameters={signatureParams}
78
+ refkey={props.refkey}
57
79
  >
58
- <HttpRequest operation={props.clientOperation} responseRefkey={responseRefkey} />
59
- <HttpResponse operation={props.clientOperation} responseRefkey={responseRefkey} />
80
+ <ay.List hardline>
81
+ <HttpRequest httpOperation={props.httpOperation} responseRefkey={responseRefkey} />
82
+ <HttpResponse httpOperation={props.httpOperation} responseRefkey={responseRefkey} />
83
+ </ay.List>
60
84
  </FunctionDeclaration>
61
85
  ;
62
- </>
86
+ </ay.List>
63
87
  );
64
88
  }
@@ -68,11 +68,12 @@ export function ClientClass(props: ClientClassProps) {
68
68
  <ay.For each={operations} hardline semicolon>
69
69
  {(op) => {
70
70
  const parameters = getOperationParameters(op.httpOperation);
71
- const args = Object.keys(parameters).map((p) => p);
71
+ const args = parameters.flatMap((p) => p.refkey);
72
+ const isPaging = Boolean($.operation.getPagingMetadata(op.httpOperation.operation));
72
73
 
73
74
  return (
74
75
  <ClassMethod
75
- async
76
+ async={!isPaging}
76
77
  type={op.httpOperation.operation}
77
78
  parameters={parameters}
78
79
  returnType={null}
@@ -148,14 +149,11 @@ function ClientConstructor(props: ClientConstructorProps) {
148
149
  );
149
150
  }
150
151
 
151
- function calculateSubClientArgs(
152
- subClient: cl.Client,
153
- parentParams: Record<string, ts.ParameterDescriptor>,
154
- ) {
155
- const subClientParams = buildClientParameters(subClient);
156
- return Object.entries(parentParams)
157
- .filter(([name]) => Object.keys(subClientParams).includes(name))
158
- .map(([_, p]) => p.refkey);
152
+ function calculateSubClientArgs(subClient: cl.Client, parentParams: ts.ParameterDescriptor[]) {
153
+ const subClientParams = buildClientParameters(subClient).map((p) => p.name);
154
+ return parentParams
155
+ .filter(({ name }) => subClientParams.includes(name))
156
+ .flatMap((p) => (p.refkey ? p.refkey : []));
159
157
  }
160
158
 
161
159
  export interface NewClientExpressionProps {
@@ -1,13 +1,13 @@
1
1
  import { Children, code, Refkey } from "@alloy-js/core";
2
2
  import * as ts from "@alloy-js/typescript";
3
- import { ClientOperation } from "@typespec/http-client";
3
+ import { HttpOperation } from "@typespec/http";
4
4
  import { EncodingProvider } from "./encoding-provider.jsx";
5
5
  import { HttpRequestParametersExpression } from "./http-request-parameters-expression.jsx";
6
6
  import { getOperationOptionsParameterRefkey } from "./operation-parameters.jsx";
7
7
  import { OperationTransformExpression } from "./transforms/operation-transform-expression.jsx";
8
8
 
9
9
  export interface HttpRequestOptionsProps {
10
- operation: ClientOperation;
10
+ httpOperation: HttpOperation;
11
11
  refkey?: Refkey;
12
12
  children?: Children;
13
13
  }
@@ -16,15 +16,15 @@ export function HttpRequestOptions(props: HttpRequestOptionsProps) {
16
16
  return (
17
17
  <ts.VarDeclaration name="httpRequestOptions" refkey={props.refkey}>
18
18
  <ts.ObjectExpression>
19
- <HttpRequestOptions.Headers operation={props.operation} />
20
- <HttpRequestOptions.Body operation={props.operation} />
19
+ <HttpRequestOptions.Headers httpOperation={props.httpOperation} />
20
+ <HttpRequestOptions.Body httpOperation={props.httpOperation} />
21
21
  </ts.ObjectExpression>
22
22
  </ts.VarDeclaration>
23
23
  );
24
24
  }
25
25
 
26
26
  export interface HttpRequestOptionsHeadersProps {
27
- operation: ClientOperation;
27
+ httpOperation: HttpOperation;
28
28
  children?: Children;
29
29
  }
30
30
 
@@ -32,30 +32,34 @@ HttpRequestOptions.Headers = function HttpRequestOptionsHeaders(
32
32
  props: HttpRequestOptionsHeadersProps,
33
33
  ) {
34
34
  // Extract the header request parameters from the operation
35
- const httpOperation = props.operation.httpOperation;
36
- const headers = httpOperation.parameters.properties.filter(
35
+ const httpOperation = props.httpOperation;
36
+ const headers = props.httpOperation.parameters.properties.filter(
37
37
  (p) => p.kind === "header" || p.kind === "contentType",
38
38
  );
39
39
 
40
- const optionsParam = getOperationOptionsParameterRefkey(props.operation.httpOperation);
40
+ const optionsParam = getOperationOptionsParameterRefkey(props.httpOperation);
41
41
  return (
42
42
  <EncodingProvider defaults={{ bytes: "base64", datetime: "rfc7231" }}>
43
43
  <ts.ObjectProperty name="headers">
44
- <HttpRequestParametersExpression parameters={headers} optionsParameter={optionsParam} />,
44
+ <HttpRequestParametersExpression
45
+ parameters={headers}
46
+ optionsParameter={optionsParam}
47
+ httpOperation={httpOperation}
48
+ />
49
+ ,
45
50
  </ts.ObjectProperty>
46
51
  </EncodingProvider>
47
52
  );
48
53
  };
49
54
 
50
55
  export interface HttpRequestOptionsBodyProps {
51
- operation: ClientOperation;
56
+ httpOperation: HttpOperation;
52
57
  itemName?: string;
53
58
  children?: Children;
54
59
  }
55
60
 
56
61
  HttpRequestOptions.Body = function HttpRequestOptionsBody(props: HttpRequestOptionsBodyProps) {
57
- const httpOperation = props.operation.httpOperation;
58
- const body = httpOperation.parameters.body;
62
+ const body = props.httpOperation.parameters.body;
59
63
 
60
64
  if (!body) {
61
65
  return <></>;
@@ -63,7 +67,7 @@ HttpRequestOptions.Body = function HttpRequestOptionsBody(props: HttpRequestOpti
63
67
  // The transformer to apply to the body.
64
68
  const bodyTransform = (
65
69
  <>
66
- <OperationTransformExpression operation={props.operation} />
70
+ <OperationTransformExpression httpOperation={props.httpOperation} />
67
71
  </>
68
72
  );
69
73
 
@@ -1,10 +1,11 @@
1
1
  import * as ay from "@alloy-js/core";
2
2
  import * as ts from "@alloy-js/typescript";
3
3
  import { useTransformNamePolicy } from "@typespec/emitter-framework";
4
- import { HttpProperty } from "@typespec/http";
5
- import { getDefaultValue } from "../utils/parameters.jsx";
4
+ import { HttpOperation, HttpProperty } from "@typespec/http";
5
+ import { getDefaultValue, hasDefaultValue } from "../utils/parameters.jsx";
6
6
  import { JsonTransform } from "./transforms/json/json-transform.jsx";
7
7
  export interface HttpRequestParametersExpressionProps {
8
+ httpOperation: HttpOperation;
8
9
  optionsParameter: ay.Children;
9
10
  parameters?: HttpProperty[];
10
11
  children?: ay.Children;
@@ -28,33 +29,40 @@ export function HttpRequestParametersExpression(props: HttpRequestParametersExpr
28
29
  const members = (
29
30
  <ay.For each={props.parameters} line comma>
30
31
  {(httpProperty) => {
31
- const parameter = httpProperty.property;
32
-
33
32
  const defaultValue = getDefaultValue(httpProperty);
34
- const paramItemRef: ay.Children = transformNamer.getApplicationName(parameter);
35
-
36
- const paramRef = ay.code`${optionsParamRef}?.${paramItemRef}`;
33
+ const propertyExpression = buildMemberChainExpression(
34
+ props.httpOperation,
35
+ httpProperty,
36
+ optionsParamRef,
37
+ );
38
+ const transportParamName = transformNamer.getTransportName(httpProperty.property);
37
39
 
38
40
  if (defaultValue) {
39
- const defaultAssignment = defaultValue ? ` ?? ${defaultValue}` : "";
40
- const headerValue = (
41
+ const paramValue = (
41
42
  <>
42
- {paramRef}
43
- {defaultAssignment}
43
+ {propertyExpression.fullExpression}
44
+ {` ?? ${defaultValue}`}
44
45
  </>
45
46
  );
46
- const name = transformNamer.getTransportName(parameter);
47
- const paramAssignment = <ts.ObjectProperty name={`"${name}"`} value={headerValue} />;
48
- return paramAssignment;
47
+ return (
48
+ <ay.NamePolicyContext.Provider value={{ getName: (n) => n }}>
49
+ <ts.ObjectProperty name={transportParamName} value={paramValue} />
50
+ </ay.NamePolicyContext.Provider>
51
+ );
49
52
  }
50
53
 
51
- const itemRef: ay.Children = parameter.optional ? ay.code`${optionsParamRef}?` : null;
52
- if (parameter.optional) {
54
+ if (propertyExpression.isNullish) {
53
55
  return ay.code`
54
- ...(${paramRef} && {${(<JsonTransform itemRef={itemRef} type={parameter} target="transport" />)}})
56
+ ...(${propertyExpression.fullExpression} && {${(<JsonTransform itemRef={propertyExpression.leadingExpression} type={httpProperty.property} target="transport" />)}})
55
57
  `;
56
58
  } else {
57
- return <JsonTransform itemRef={itemRef} type={parameter} target="transport" />;
59
+ return (
60
+ <JsonTransform
61
+ itemRef={propertyExpression.leadingExpression}
62
+ type={httpProperty.property}
63
+ target="transport"
64
+ />
65
+ );
58
66
  }
59
67
  }}
60
68
  </ay.For>
@@ -64,3 +72,119 @@ export function HttpRequestParametersExpression(props: HttpRequestParametersExpr
64
72
 
65
73
  return <ts.ObjectExpression>{parameters}</ts.ObjectExpression>;
66
74
  }
75
+
76
+ interface MemberChainExpression {
77
+ propertyName: string;
78
+ isNullish: boolean;
79
+ fullExpression: ay.Children;
80
+ leadingExpression?: ay.Children;
81
+ }
82
+
83
+ /**
84
+ * Builds the member chain expression for potentially nested HTTP parameters.
85
+ * This logic will be simplified and made more robust once a couple of changes land in Alloy.
86
+ * - Support refkeys on interface members
87
+ * - Support for conditional chaining on ts.MemberChainExpression
88
+ * - Eventually we'll have an API like:
89
+ * @example
90
+ * ```tsx
91
+ * <InstancePropertyExpression
92
+ * instance={refkey(parameter)}
93
+ * staticMember={refkey(property)}
94
+ * />
95
+ * ```
96
+ * @param httpOperation - The HttpOperation containing parameters.
97
+ * @param httpProperty - The current HttpProperty to build an access path for.
98
+ * @param optionsParamRef - Reference to the user's main options parameter.
99
+ * @returns The full nested expression, leading expression, nullish flag, and final property name.
100
+ */
101
+ function buildMemberChainExpression(
102
+ httpOperation: HttpOperation,
103
+ httpProperty: HttpProperty,
104
+ optionsParamRef?: ay.Children,
105
+ ): MemberChainExpression {
106
+ const segments: ay.Children[] = [];
107
+ let isNullish = httpProperty.property.optional === true;
108
+ const namePolicy = ay.useNamePolicy();
109
+ const propertyApplicationName = namePolicy.getName(httpProperty.property.name, "property");
110
+
111
+ // If the property is at the top level, return early
112
+ if (httpProperty.path.length === 1) {
113
+ if (httpProperty.property.optional || hasDefaultValue(httpProperty)) {
114
+ return {
115
+ propertyName: propertyApplicationName,
116
+ isNullish,
117
+ fullExpression: ay.code`${optionsParamRef}?.${propertyApplicationName}`,
118
+ leadingExpression: ay.code`${optionsParamRef}`,
119
+ };
120
+ }
121
+ return {
122
+ propertyName: propertyApplicationName,
123
+ isNullish,
124
+ fullExpression: propertyApplicationName,
125
+ };
126
+ }
127
+
128
+ // Build access segments for nested properties.
129
+ let currentPath = httpProperty.path.slice(0, -1);
130
+ while (currentPath.length > 0) {
131
+ const parentProperty = findHttpPropertyByPath(httpOperation, currentPath);
132
+ if (!parentProperty) {
133
+ break;
134
+ }
135
+
136
+ // Use optional chaining if the parent is optional.
137
+ const joiner = parentProperty.property.optional ? "?." : ".";
138
+ if (parentProperty.property.optional) {
139
+ isNullish = true;
140
+ }
141
+
142
+ const parentName = namePolicy.getName(parentProperty.property.name, "property");
143
+
144
+ // Unshift the joiner first
145
+ segments.unshift(joiner);
146
+
147
+ // Use direct property access if possible.
148
+ if (isValidIdentifier(parentName)) {
149
+ segments.unshift(ay.code`${parentName}`);
150
+ } else {
151
+ // For non-valid identifiers, use bracket notation.
152
+ segments.unshift(`[${JSON.stringify(parentName)}]`);
153
+ }
154
+
155
+ // Remove the last element to move one level up.
156
+ currentPath = currentPath.slice(0, -1);
157
+ }
158
+
159
+ const fullExpression = (
160
+ <>
161
+ <ay.List children={segments} />
162
+ {propertyApplicationName}
163
+ </>
164
+ );
165
+ const leadingExpression = <ay.List children={segments.slice(0, -1)} />;
166
+
167
+ return {
168
+ propertyName: propertyApplicationName,
169
+ isNullish,
170
+ fullExpression,
171
+ leadingExpression,
172
+ };
173
+ }
174
+
175
+ function isValidIdentifier(name: string): boolean {
176
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
177
+ }
178
+
179
+ function findHttpPropertyByPath(
180
+ httpOperation: HttpOperation,
181
+ path: (string | number)[],
182
+ ): HttpProperty | undefined {
183
+ for (const property of httpOperation.parameters.properties) {
184
+ // Check if the property path matches the given path
185
+ if (JSON.stringify(property.path) === JSON.stringify(path)) {
186
+ return property;
187
+ }
188
+ }
189
+ return undefined;
190
+ }
@@ -1,7 +1,7 @@
1
- import { Children, code, refkey, Refkey, StatementList } from "@alloy-js/core";
1
+ import { Children, code, List, refkey, Refkey, StatementList } from "@alloy-js/core";
2
2
  import * as ts from "@alloy-js/typescript";
3
3
  import { Reference } from "@alloy-js/typescript";
4
- import { ClientOperation } from "@typespec/http-client";
4
+ import { HttpOperation } from "@typespec/http";
5
5
  import { EncodingProvider } from "./encoding-provider.jsx";
6
6
  import { uriTemplateLib } from "./external-packages/uri-template.js";
7
7
  import { HttpRequestOptions } from "./http-request-options.js";
@@ -9,7 +9,7 @@ import { HttpRequestParametersExpression } from "./http-request-parameters-expre
9
9
  import { getOperationOptionsParameterRefkey } from "./operation-parameters.jsx";
10
10
 
11
11
  export interface HttpRequestProps {
12
- operation: ClientOperation;
12
+ httpOperation: HttpOperation;
13
13
  responseRefkey?: Refkey;
14
14
  }
15
15
 
@@ -17,46 +17,48 @@ export function HttpRequest(props: HttpRequestProps) {
17
17
  const operationUrlRefkey = refkey();
18
18
  const requestOptionsRefkey = refkey();
19
19
  const httpResponseRefkey = props.responseRefkey ?? refkey();
20
- const verb = props.operation.httpOperation.verb;
20
+ const verb = props.httpOperation.verb;
21
21
  return (
22
- <StatementList>
23
- <HttpRequest.Url operation={props.operation} refkey={operationUrlRefkey} />
22
+ <List>
23
+ <StatementList>
24
+ <HttpRequest.Url httpOperation={props.httpOperation} refkey={operationUrlRefkey} />
24
25
 
25
- <HttpRequestOptions operation={props.operation} refkey={requestOptionsRefkey} />
26
+ <HttpRequestOptions httpOperation={props.httpOperation} refkey={requestOptionsRefkey} />
26
27
 
27
- <ts.VarDeclaration name="response" refkey={httpResponseRefkey}>
28
- {code`
28
+ <ts.VarDeclaration name="response" refkey={httpResponseRefkey}>
29
+ {code`
29
30
  await client.pathUnchecked(${(<Reference refkey={operationUrlRefkey} />)}).${verb}(${(<Reference refkey={requestOptionsRefkey} />)})
30
-
31
+ `}
32
+ </ts.VarDeclaration>
33
+ <hbr />
34
+ </StatementList>
35
+ {code`
31
36
  if (typeof options?.operationOptions?.onResponse === "function") {
32
37
  options?.operationOptions?.onResponse(response);
33
- }
34
-
35
- `}
36
- </ts.VarDeclaration>
37
- </StatementList>
38
+ }`}
39
+ </List>
38
40
  );
39
41
  }
40
42
 
41
43
  export interface HttpUrlProps {
42
- operation: ClientOperation;
44
+ httpOperation: HttpOperation;
43
45
  refkey?: Refkey;
44
46
  children?: Children;
45
47
  }
46
48
 
47
49
  HttpRequest.Url = function HttpUrlDeclaration(props: HttpUrlProps) {
48
- const httpOperation = props.operation.httpOperation;
49
- const urlTemplate = httpOperation.uriTemplate;
50
- const urlParameters = httpOperation.parameters.properties.filter(
50
+ const urlTemplate = props.httpOperation.uriTemplate;
51
+ const urlParameters = props.httpOperation.parameters.properties.filter(
51
52
  (p) => p.kind === "path" || p.kind === "query",
52
53
  );
53
- const optionsParameter = getOperationOptionsParameterRefkey(props.operation.httpOperation);
54
+ const optionsParameter = getOperationOptionsParameterRefkey(props.httpOperation);
54
55
  return (
55
56
  <EncodingProvider defaults={{ bytes: "base64url" }}>
56
57
  <ts.VarDeclaration name="path" refkey={props.refkey}>
57
58
  {uriTemplateLib.parse}({JSON.stringify(urlTemplate)}).expand(
58
59
  {
59
60
  <HttpRequestParametersExpression
61
+ httpOperation={props.httpOperation}
60
62
  optionsParameter={optionsParameter!}
61
63
  parameters={urlParameters}
62
64
  />
@@ -1,34 +1,33 @@
1
- import { Children, code, For, Refkey } from "@alloy-js/core";
1
+ import { Children, code, For, List, Refkey } from "@alloy-js/core";
2
2
  import { isVoidType } from "@typespec/compiler";
3
3
  import { $ } from "@typespec/compiler/experimental/typekit";
4
- import { ClientOperation } from "@typespec/http-client";
4
+ import { HttpOperation } from "@typespec/http";
5
5
  import { getCreateRestErrorRefkey } from "./static-helpers/rest-error.jsx";
6
6
  import { ContentTypeEncodingProvider } from "./transforms/content-type-encoding-provider.jsx";
7
7
  import { JsonTransform } from "./transforms/json/json-transform.jsx";
8
8
  export interface HttpResponseProps {
9
- operation: ClientOperation;
9
+ httpOperation: HttpOperation;
10
10
  responseRefkey: Refkey;
11
11
  children?: Children;
12
12
  }
13
13
 
14
14
  export function HttpResponse(props: HttpResponseProps) {
15
15
  return (
16
- <>
17
- <HttpResponses operation={props.operation} />
18
-
16
+ <List hardline>
17
+ <HttpResponses httpOperation={props.httpOperation} />
19
18
  {code`throw ${getCreateRestErrorRefkey()}(response);`}
20
- </>
19
+ </List>
21
20
  );
22
21
  }
23
22
 
24
23
  export interface HttpResponsesProps {
25
- operation: ClientOperation;
24
+ httpOperation: HttpOperation;
26
25
  children?: Children;
27
26
  }
28
27
 
29
28
  export function HttpResponses(props: HttpResponsesProps) {
30
29
  // Handle response by status code and content type
31
- const responses = $.httpOperation.flattenResponses(props.operation.httpOperation);
30
+ const responses = $.httpOperation.flattenResponses(props.httpOperation);
32
31
  return (
33
32
  <For each={responses.filter((r) => !$.httpResponse.isErrorResponse(r))}>
34
33
  {({ statusCode, contentType, responseContent, type }) => {
@@ -0,0 +1,2 @@
1
+ export * from "./operation-pipeline.jsx";
2
+ export * from "./types.jsx";
@@ -0,0 +1,33 @@
1
+ import * as ay from "@alloy-js/core";
2
+ import { HttpOperation } from "@typespec/http";
3
+ import { ClientOperation as DefaultOperationComponent } from "../client-operation.jsx";
4
+ import { OperationHandlerPipeline } from "./types.jsx";
5
+
6
+ export interface OperationPipelineProps {
7
+ httpOperation: HttpOperation;
8
+ pipeline: OperationHandlerPipeline;
9
+ internal?: boolean;
10
+ }
11
+
12
+ /**
13
+ * Component that manages the operation handler pipeline.
14
+ * It will try each handler in order until it finds one that can handle the operation.
15
+ * If no handler is found, it will use the default ClientOperation component.
16
+ */
17
+ export function OperationPipeline({ httpOperation, pipeline, internal }: OperationPipelineProps) {
18
+ for (const handler of pipeline.handlers) {
19
+ if (handler.canHandle(httpOperation)) {
20
+ return handler.handle(httpOperation);
21
+ }
22
+ }
23
+
24
+ // Default to standard client operation if no handler matched
25
+ const defaultOperationRefkey = ay.refkey(httpOperation.operation);
26
+ return (
27
+ <DefaultOperationComponent
28
+ httpOperation={httpOperation}
29
+ internal={internal}
30
+ refkey={defaultOperationRefkey}
31
+ />
32
+ );
33
+ }
@@ -0,0 +1,13 @@
1
+ import { PagingOperation } from "@typespec/compiler";
2
+ import { $ } from "@typespec/compiler/experimental/typekit";
3
+ import { TypeExpression } from "@typespec/emitter-framework/typescript";
4
+
5
+ export function getPageItemTypeName(pagingOperation: PagingOperation) {
6
+ const type = pagingOperation.output.pageItems.property.type;
7
+ // only accept array type
8
+ if (type.kind === "Model" && $.array.is(type)) {
9
+ return <TypeExpression type={$.array.getElementType(type)} />;
10
+ }
11
+ // not supported and throw exceptions
12
+ throw new Error("Not supported");
13
+ }