@typespec/http-client-js 0.4.0-dev.2 → 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 (107) hide show
  1. package/CHANGELOG.md +0 -1
  2. package/dist/src/components/client-operation.d.ts +4 -1
  3. package/dist/src/components/client-operation.d.ts.map +1 -1
  4. package/dist/src/components/client-operation.js +64 -32
  5. package/dist/src/components/client-operation.js.map +1 -1
  6. package/dist/src/components/client.d.ts.map +1 -1
  7. package/dist/src/components/client.js +2 -1
  8. package/dist/src/components/client.js.map +1 -1
  9. package/dist/src/components/http-request-options.d.ts +4 -4
  10. package/dist/src/components/http-request-options.d.ts.map +1 -1
  11. package/dist/src/components/http-request-options.js +10 -11
  12. package/dist/src/components/http-request-options.js.map +1 -1
  13. package/dist/src/components/http-request.d.ts +3 -3
  14. package/dist/src/components/http-request.d.ts.map +1 -1
  15. package/dist/src/components/http-request.js +35 -31
  16. package/dist/src/components/http-request.js.map +1 -1
  17. package/dist/src/components/http-response.d.ts +3 -3
  18. package/dist/src/components/http-response.d.ts.map +1 -1
  19. package/dist/src/components/http-response.js +11 -6
  20. package/dist/src/components/http-response.js.map +1 -1
  21. package/dist/src/components/operation-handlers/index.d.ts +3 -0
  22. package/dist/src/components/operation-handlers/index.d.ts.map +1 -0
  23. package/dist/src/components/operation-handlers/index.js +3 -0
  24. package/dist/src/components/operation-handlers/index.js.map +1 -0
  25. package/dist/src/components/operation-handlers/operation-pipeline.d.ts +15 -0
  26. package/dist/src/components/operation-handlers/operation-pipeline.d.ts.map +1 -0
  27. package/dist/src/components/operation-handlers/operation-pipeline.js +28 -0
  28. package/dist/src/components/operation-handlers/operation-pipeline.js.map +1 -0
  29. package/dist/src/components/operation-handlers/paging/page-item.d.ts +3 -0
  30. package/dist/src/components/operation-handlers/paging/page-item.d.ts.map +1 -0
  31. package/dist/src/components/operation-handlers/paging/page-item.js +17 -0
  32. package/dist/src/components/operation-handlers/paging/page-item.js.map +1 -0
  33. package/dist/src/components/operation-handlers/paging/page-response.d.ts +10 -0
  34. package/dist/src/components/operation-handlers/paging/page-response.d.ts.map +1 -0
  35. package/dist/src/components/operation-handlers/paging/page-response.js +50 -0
  36. package/dist/src/components/operation-handlers/paging/page-response.js.map +1 -0
  37. package/dist/src/components/operation-handlers/paging/page-settings.d.ts +11 -0
  38. package/dist/src/components/operation-handlers/paging/page-settings.d.ts.map +1 -0
  39. package/dist/src/components/operation-handlers/paging/page-settings.js +52 -0
  40. package/dist/src/components/operation-handlers/paging/page-settings.js.map +1 -0
  41. package/dist/src/components/operation-handlers/paging/paginated-operation-handler.d.ts +3 -0
  42. package/dist/src/components/operation-handlers/paging/paginated-operation-handler.d.ts.map +1 -0
  43. package/dist/src/components/operation-handlers/paging/paginated-operation-handler.js +170 -0
  44. package/dist/src/components/operation-handlers/paging/paginated-operation-handler.js.map +1 -0
  45. package/dist/src/components/operation-handlers/paging/request-send.d.ts +12 -0
  46. package/dist/src/components/operation-handlers/paging/request-send.d.ts.map +1 -0
  47. package/dist/src/components/operation-handlers/paging/request-send.js +78 -0
  48. package/dist/src/components/operation-handlers/paging/request-send.js.map +1 -0
  49. package/dist/src/components/operation-handlers/paging/response-deserialize.d.ts +11 -0
  50. package/dist/src/components/operation-handlers/paging/response-deserialize.d.ts.map +1 -0
  51. package/dist/src/components/operation-handlers/paging/response-deserialize.js +51 -0
  52. package/dist/src/components/operation-handlers/paging/response-deserialize.js.map +1 -0
  53. package/dist/src/components/operation-handlers/paging/util.d.ts +17 -0
  54. package/dist/src/components/operation-handlers/paging/util.d.ts.map +1 -0
  55. package/dist/src/components/operation-handlers/paging/util.js +46 -0
  56. package/dist/src/components/operation-handlers/paging/util.js.map +1 -0
  57. package/dist/src/components/operation-handlers/types.d.ts +16 -0
  58. package/dist/src/components/operation-handlers/types.d.ts.map +1 -0
  59. package/dist/src/components/operation-handlers/types.js +1 -0
  60. package/dist/src/components/operation-handlers/types.js.map +1 -0
  61. package/dist/src/components/operation-options.d.ts +2 -0
  62. package/dist/src/components/operation-options.d.ts.map +1 -1
  63. package/dist/src/components/operation-options.js +2 -1
  64. package/dist/src/components/operation-options.js.map +1 -1
  65. package/dist/src/components/output.d.ts.map +1 -1
  66. package/dist/src/components/output.js +4 -0
  67. package/dist/src/components/output.js.map +1 -1
  68. package/dist/src/components/static-helpers/paging-helper.d.ts +5 -0
  69. package/dist/src/components/static-helpers/paging-helper.d.ts.map +1 -0
  70. package/dist/src/components/static-helpers/paging-helper.js +197 -0
  71. package/dist/src/components/static-helpers/paging-helper.js.map +1 -0
  72. package/dist/src/components/transforms/operation-transform-expression.d.ts +2 -2
  73. package/dist/src/components/transforms/operation-transform-expression.d.ts.map +1 -1
  74. package/dist/src/components/transforms/operation-transform-expression.js +1 -1
  75. package/dist/src/components/transforms/operation-transform-expression.js.map +1 -1
  76. package/dist/src/emitter.d.ts.map +1 -1
  77. package/dist/src/emitter.js +2 -1
  78. package/dist/src/emitter.js.map +1 -1
  79. package/dist/src/lib.d.ts +17 -2
  80. package/dist/src/lib.d.ts.map +1 -1
  81. package/dist/src/lib.js +6 -0
  82. package/dist/src/lib.js.map +1 -1
  83. package/dist/tsconfig.tsbuildinfo +1 -1
  84. package/package.json +2 -2
  85. package/src/components/client-operation.tsx +37 -13
  86. package/src/components/client.tsx +2 -1
  87. package/src/components/http-request-options.tsx +11 -12
  88. package/src/components/http-request.tsx +22 -21
  89. package/src/components/http-response.tsx +8 -9
  90. package/src/components/operation-handlers/index.tsx +2 -0
  91. package/src/components/operation-handlers/operation-pipeline.tsx +33 -0
  92. package/src/components/operation-handlers/paging/page-item.tsx +13 -0
  93. package/src/components/operation-handlers/paging/page-response.tsx +55 -0
  94. package/src/components/operation-handlers/paging/page-settings.tsx +53 -0
  95. package/src/components/operation-handlers/paging/paginated-operation-handler.tsx +192 -0
  96. package/src/components/operation-handlers/paging/request-send.tsx +70 -0
  97. package/src/components/operation-handlers/paging/response-deserialize.tsx +54 -0
  98. package/src/components/operation-handlers/paging/util.ts +70 -0
  99. package/src/components/operation-handlers/types.tsx +18 -0
  100. package/src/components/operation-options.tsx +6 -3
  101. package/src/components/output.tsx +2 -1
  102. package/src/components/static-helpers/paging-helper.tsx +195 -0
  103. package/src/components/transforms/operation-transform-expression.tsx +3 -3
  104. package/src/emitter.tsx +2 -0
  105. package/src/lib.ts +6 -0
  106. package/test/e2e/http/payload/pageable/main.test.ts +78 -22
  107. package/test/scenarios/http-operations/paging.md +244 -0
@@ -0,0 +1,192 @@
1
+ import * as ay from "@alloy-js/core";
2
+ import { Children } from "@alloy-js/core";
3
+ import * as ts from "@alloy-js/typescript";
4
+ import { $ } from "@typespec/compiler/experimental/typekit";
5
+ import { FunctionDeclaration } from "@typespec/emitter-framework/typescript";
6
+ import { 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 { OperationOptionsDeclaration } from "../../operation-options.jsx";
11
+ import { getOperationParameters } from "../../operation-parameters.jsx";
12
+ import {
13
+ getBuildPagedAsyncIteratorRefkey,
14
+ getPagedAsyncIterableIteratorRefkey,
15
+ } from "../../static-helpers/paging-helper.jsx";
16
+ import { getPageItemTypeName } from "../paging/page-item.jsx";
17
+ import { getPageResponseTypeRefkey, PageResponseDeclaration } from "../paging/page-response.jsx";
18
+ import {
19
+ getPageSettingProperties,
20
+ getPageSettingsTypeRefkey,
21
+ PageSettingsDeclaration,
22
+ } from "../paging/page-settings.jsx";
23
+ import { HttpRequestSend } from "../paging/request-send.jsx";
24
+ import {
25
+ getHttpRequestDeserializeRefkey,
26
+ HttpResponseDeserialize,
27
+ } from "../paging/response-deserialize.jsx";
28
+ import { extractPagingDetail, PagingDetail } from "../paging/util.js";
29
+ import { OperationHandler } from "../types.js";
30
+
31
+ export const PaginatedOperationHandler: OperationHandler = {
32
+ canHandle(httpOperation: HttpOperation): boolean {
33
+ const pagingMetadata = $.operation.getPagingMetadata(httpOperation.operation);
34
+ return pagingMetadata !== undefined;
35
+ },
36
+ handle(httpOperation: HttpOperation): Children {
37
+ const clientLibrary = cl.useClientLibrary();
38
+ const client = clientLibrary.getClientForOperation(httpOperation);
39
+
40
+ if (!client) {
41
+ reportDiagnostic($.program, {
42
+ code: "client-not-found",
43
+ target: httpOperation.operation,
44
+ });
45
+ return;
46
+ }
47
+ const pagingOperation = $.operation.getPagingMetadata(httpOperation.operation)!;
48
+ const responseRefkey = ay.refkey(httpOperation, "http-response");
49
+ const operationRefkey = ay.refkey(httpOperation.operation);
50
+ const returnType = ay.code`${getPagedAsyncIterableIteratorRefkey()}<${getPageItemTypeName(pagingOperation)},${getPageResponseTypeRefkey(httpOperation)},${getPageSettingsTypeRefkey(httpOperation)}>`;
51
+ const clientContextInterfaceRef = getClientcontextDeclarationRef(client);
52
+ const signatureParams: ts.ParameterDescriptor[] = [
53
+ { name: "client", type: clientContextInterfaceRef, refkey: ay.refkey(client, "client") },
54
+ ...getOperationParameters(httpOperation),
55
+ ];
56
+ const pagingDetail = extractPagingDetail(httpOperation, pagingOperation);
57
+ // Exclude from operation options and include them into PageSettings
58
+ const excludes = getPageSettingProperties(pagingOperation).map((p) => p.property);
59
+ return (
60
+ <ay.List>
61
+ <OperationOptionsDeclaration operation={httpOperation} excludes={excludes} />
62
+ <PageSettingsDeclaration operation={httpOperation} pagingOperation={pagingOperation} />
63
+ <PageResponseDeclaration operation={httpOperation} pagingOperation={pagingOperation} />
64
+ <HttpRequestSend httpOperation={httpOperation} responseRefkey={responseRefkey} />
65
+ <HttpResponseDeserialize httpOperation={httpOperation} responseRefkey={responseRefkey} />
66
+ <FunctionDeclaration
67
+ export
68
+ name={httpOperation.operation.name}
69
+ returnType={returnType}
70
+ refkey={operationRefkey}
71
+ parametersMode="replace"
72
+ parameters={signatureParams}
73
+ >
74
+ <ay.List>
75
+ <GetElements httpOperation={httpOperation} pageDetail={pagingDetail} />
76
+ <GetPagedResponse
77
+ httpOperation={httpOperation}
78
+ pageDetail={pagingDetail}
79
+ operationParams={signatureParams}
80
+ />
81
+ <>
82
+ {ay.code`
83
+ return ${getBuildPagedAsyncIteratorRefkey}<${getPageItemTypeName(pagingOperation)}, ${getPageResponseTypeRefkey(httpOperation)}, ${getPageSettingsTypeRefkey(httpOperation)}>({${getElementsRefkey(httpOperation)}, ${getPagedResponseFunctionRefkey(httpOperation)}});`}
84
+ </>
85
+ </ay.List>
86
+ </FunctionDeclaration>
87
+ </ay.List>
88
+ );
89
+ },
90
+ };
91
+
92
+ interface GetPagedResponseProps {
93
+ httpOperation: HttpOperation;
94
+ pageDetail: PagingDetail;
95
+ operationParams: ts.ParameterDescriptor[];
96
+ }
97
+ function getPagedResponseFunctionRefkey(httpOperation: HttpOperation) {
98
+ return ay.refkey(httpOperation, "get-paged-response");
99
+ }
100
+
101
+ function GetPagedResponse(props: GetPagedResponseProps) {
102
+ const pagingDetail = props.pageDetail;
103
+ const httpOperation = props.httpOperation;
104
+ const params: ts.ParameterDescriptor[] = [
105
+ { name: "nextToken", type: "string", optional: true, refkey: ay.refkey() },
106
+ {
107
+ name: "settings",
108
+ type: getPageSettingsTypeRefkey(httpOperation),
109
+ optional: true,
110
+ refkey: ay.refkey(),
111
+ },
112
+ ];
113
+ // TODO: template can't resolve the ref key so fallback to useTSNamePolicy
114
+ const namePolicy = ts.useTSNamePolicy();
115
+ const functionName = namePolicy.getName(httpOperation.operation.name + "Send", "function");
116
+ const sendParamStr = props.operationParams
117
+ .map((param) => param.name)
118
+ .slice(0, -1)
119
+ .join(", ");
120
+ return (
121
+ <FunctionDeclaration
122
+ name="getPagedResponse"
123
+ async
124
+ refkey={getPagedResponseFunctionRefkey(httpOperation)}
125
+ parameters={params}
126
+ >
127
+ {ay.code`
128
+ ${
129
+ props.pageDetail.pattern === "nextLink"
130
+ ? `
131
+ let response: PathUncheckedResponse;
132
+ if (nextToken) {
133
+ response = await client.pathUnchecked(nextToken).get();
134
+ } else {
135
+ const combinedOptions = { ...options, ...settings };
136
+ response = await ${functionName}(${sendParamStr}, combinedOptions);
137
+ } `
138
+ : `
139
+ const combinedOptions = {...options, ...settings};
140
+ ${
141
+ pagingDetail.input?.nextToken
142
+ ? `
143
+ if (nextToken) {
144
+ combinedOptions.${pagingDetail.input?.nextToken} = nextToken;
145
+ }`
146
+ : ``
147
+ }
148
+ const response = await ${functionName}(${sendParamStr}, combinedOptions);
149
+ `
150
+ }
151
+ return {
152
+ pagedResponse: await ${getHttpRequestDeserializeRefkey(httpOperation)}(response, options),
153
+ nextToken: ${
154
+ pagingDetail.output.nextToken
155
+ ? `response.${pagingDetail.output.nextToken.position}["${pagingDetail.output.nextToken.name}"]`
156
+ : `undefined`
157
+ },
158
+ };
159
+ `}
160
+ </FunctionDeclaration>
161
+ );
162
+ }
163
+
164
+ interface GetElementsProps {
165
+ httpOperation: HttpOperation;
166
+ pageDetail: PagingDetail;
167
+ }
168
+ function getElementsRefkey(httpOperation: HttpOperation) {
169
+ return ay.refkey(httpOperation, "get-elements");
170
+ }
171
+ function GetElements(props: GetElementsProps) {
172
+ const pagingDetail = props.pageDetail;
173
+ const httpOperation = props.httpOperation;
174
+ const params = [
175
+ {
176
+ name: "response",
177
+ type: getPageResponseTypeRefkey(httpOperation),
178
+ refkey: ay.refkey(),
179
+ },
180
+ ];
181
+ return (
182
+ <FunctionDeclaration
183
+ name="getElements"
184
+ refkey={getElementsRefkey(httpOperation)}
185
+ parameters={params}
186
+ >
187
+ {ay.code`
188
+ return response.${pagingDetail.output.items};
189
+ `}
190
+ </FunctionDeclaration>
191
+ );
192
+ }
@@ -0,0 +1,70 @@
1
+ import * as ay from "@alloy-js/core";
2
+ import { List, Refkey, StatementList, code, refkey } from "@alloy-js/core";
3
+ import * as ts from "@alloy-js/typescript";
4
+ import { Reference } from "@alloy-js/typescript";
5
+ import { FunctionDeclaration } from "@typespec/emitter-framework/typescript";
6
+ import { HttpOperation } from "@typespec/http";
7
+ import * as cl from "@typespec/http-client";
8
+ import { getClientcontextDeclarationRef } from "../../client-context/client-context-declaration.jsx";
9
+ import { HttpRequestOptions } from "../../http-request-options.jsx";
10
+ import { HttpRequest } from "../../http-request.jsx";
11
+ import {
12
+ getOperationOptionsParameterRefkey,
13
+ getOperationParameters,
14
+ } from "../../operation-parameters.jsx";
15
+
16
+ export interface HttpRequestSendProps {
17
+ httpOperation: HttpOperation;
18
+ responseRefkey?: Refkey;
19
+ }
20
+
21
+ export function getHttpRequestSendRefkey(httpOperation: HttpOperation) {
22
+ return refkey(httpOperation, "http-request-send");
23
+ }
24
+
25
+ export function getHttpRequestSendParams(httpOperation: HttpOperation) {
26
+ const clientLibrary = cl.useClientLibrary();
27
+ const client = clientLibrary.getClientForOperation(httpOperation);
28
+ const clientContextInterfaceRef = getClientcontextDeclarationRef(client!);
29
+ const signatureParams: ts.ParameterDescriptor[] = [
30
+ { name: "client", type: clientContextInterfaceRef, refkey: ay.refkey(client, "client") },
31
+ ...getOperationParameters(httpOperation),
32
+ ];
33
+ const parameters: ts.ParameterDescriptor[] = [...signatureParams];
34
+ // re-correct the `options` parameter to Record<string, any> to accept both paging and non-paging options
35
+ parameters[parameters.length - 1] = {
36
+ name: "options",
37
+ type: "Record<string, any>",
38
+ refkey: getOperationOptionsParameterRefkey(httpOperation),
39
+ optional: true,
40
+ };
41
+ return parameters;
42
+ }
43
+
44
+ export function HttpRequestSend(props: HttpRequestSendProps) {
45
+ const httpOperation = props.httpOperation;
46
+ const operationUrlRefkey = refkey();
47
+ const requestOptionsRefkey = refkey();
48
+ const verb = props.httpOperation.verb;
49
+ const namePolicy = ts.useTSNamePolicy();
50
+ const functionName = namePolicy.getName(httpOperation.operation.name + "Send", "function");
51
+ const parameters = getHttpRequestSendParams(httpOperation);
52
+ return (
53
+ <FunctionDeclaration
54
+ name={functionName}
55
+ async
56
+ refkey={getHttpRequestSendRefkey(httpOperation)}
57
+ parametersMode="replace"
58
+ parameters={parameters}
59
+ >
60
+ <List>
61
+ <StatementList>
62
+ <HttpRequest.Url httpOperation={props.httpOperation} refkey={operationUrlRefkey} />
63
+
64
+ <HttpRequestOptions httpOperation={props.httpOperation} refkey={requestOptionsRefkey} />
65
+ {code`return await client.pathUnchecked(${(<Reference refkey={operationUrlRefkey} />)}).${verb}(${(<Reference refkey={requestOptionsRefkey} />)});`}
66
+ </StatementList>
67
+ </List>
68
+ </FunctionDeclaration>
69
+ );
70
+ }
@@ -0,0 +1,54 @@
1
+ import { Children, code, List, Refkey, refkey } from "@alloy-js/core";
2
+ import * as ts from "@alloy-js/typescript";
3
+ import { FunctionDeclaration } from "@typespec/emitter-framework/typescript";
4
+ import { HttpOperation } from "@typespec/http";
5
+ import { httpRuntimeTemplateLib } from "../../external-packages/ts-http-runtime.js";
6
+ import { HttpResponseProps, HttpResponses } from "../../http-response.jsx";
7
+ import { getOperationOptionsTypeRefkey } from "../../operation-options.jsx";
8
+ import { getOperationOptionsParameterRefkey } from "../../operation-parameters.jsx";
9
+ import { getCreateRestErrorRefkey } from "../../static-helpers/rest-error.jsx";
10
+
11
+ export function getHttpRequestDeserializeRefkey(httpOperation: HttpOperation) {
12
+ return refkey(httpOperation, "http-request-deserialize");
13
+ }
14
+
15
+ export interface HttpResponseDeserializeProps {
16
+ httpOperation: HttpOperation;
17
+ responseRefkey: Refkey;
18
+ children?: Children;
19
+ }
20
+
21
+ export function HttpResponseDeserialize(props: HttpResponseProps) {
22
+ const httpOperation = props.httpOperation;
23
+ const namePolicy = ts.useTSNamePolicy();
24
+ const functionName = namePolicy.getName(httpOperation.operation.name + "Deserialize", "function");
25
+ const params = [
26
+ {
27
+ name: "response",
28
+ type: httpRuntimeTemplateLib.PathUncheckedResponse,
29
+ },
30
+ {
31
+ name: "options",
32
+ refkey: getOperationOptionsParameterRefkey(httpOperation),
33
+ type: getOperationOptionsTypeRefkey(httpOperation),
34
+ optional: true,
35
+ },
36
+ ];
37
+ return (
38
+ <FunctionDeclaration
39
+ name={functionName}
40
+ refkey={getHttpRequestDeserializeRefkey(httpOperation)}
41
+ parametersMode="replace"
42
+ parameters={params}
43
+ >
44
+ {code`
45
+ if (typeof options?.operationOptions?.onResponse === "function") {
46
+ options?.operationOptions?.onResponse(response);
47
+ }`}
48
+ <List hardline>
49
+ <HttpResponses httpOperation={props.httpOperation} />
50
+ {code`throw ${getCreateRestErrorRefkey()}(response);`}
51
+ </List>
52
+ </FunctionDeclaration>
53
+ );
54
+ }
@@ -0,0 +1,70 @@
1
+ import { ModelProperty, PagingOperation } from "@typespec/compiler";
2
+ import { HttpOperation } from "@typespec/http";
3
+
4
+ export interface PagingDetail {
5
+ // Only support the nextLink or continuationToken
6
+ pattern: "nextLink" | "continuationToken";
7
+ input?: {
8
+ nextToken: string;
9
+ };
10
+ output: {
11
+ items: string;
12
+ nextToken?: {
13
+ name: string;
14
+ position: "headers" | "body";
15
+ };
16
+ };
17
+ }
18
+
19
+ export function extractPagingDetail(
20
+ httpOperation: HttpOperation,
21
+ pagingOperation: PagingOperation,
22
+ ): PagingDetail {
23
+ const ret: PagingDetail = {
24
+ pattern: "nextLink",
25
+ output: {
26
+ items: pagingOperation.output.pageItems.property.name,
27
+ },
28
+ };
29
+ const returnedNextToken =
30
+ pagingOperation.output.nextLink?.property ?? pagingOperation.output.continuationToken?.property;
31
+ const isHeaderToken = getResponseHeader(httpOperation, returnedNextToken);
32
+ const returnedTokenPosition = isHeaderToken ? "headers" : "body";
33
+ const returnedTokenName = isHeaderToken ? isHeaderToken : returnedNextToken?.name;
34
+ if (returnedNextToken && returnedTokenName) {
35
+ ret.output.nextToken = {
36
+ name: returnedTokenName,
37
+ position: returnedTokenPosition,
38
+ };
39
+ }
40
+ if (pagingOperation.output.continuationToken) {
41
+ ret.pattern = "continuationToken";
42
+ if (pagingOperation.input.continuationToken) {
43
+ ret.input = {
44
+ nextToken: pagingOperation.input.continuationToken?.property.name,
45
+ };
46
+ }
47
+ return ret;
48
+ }
49
+ return ret;
50
+ }
51
+
52
+ function getResponseHeader(httpOperation: HttpOperation, prop?: ModelProperty): string | undefined {
53
+ if (!prop) {
54
+ return undefined;
55
+ }
56
+ const headers = httpOperation.responses
57
+ .flatMap((resp) => resp.responses)
58
+ .map((resp) => resp.headers);
59
+ for (const header of headers) {
60
+ if (!header) {
61
+ continue;
62
+ }
63
+ for (const key in header) {
64
+ if (header[key] === prop) {
65
+ return key;
66
+ }
67
+ }
68
+ }
69
+ return undefined;
70
+ }
@@ -0,0 +1,18 @@
1
+ import { Children } from "@alloy-js/core";
2
+ import { HttpOperation } from "@typespec/http";
3
+
4
+ export interface OperationHandler {
5
+ /**
6
+ * Check if this handler can handle the given operation
7
+ */
8
+ canHandle(operation: HttpOperation): boolean;
9
+
10
+ /**
11
+ * Render the operation using this handler
12
+ */
13
+ handle(operation: HttpOperation): Children;
14
+ }
15
+
16
+ export interface OperationHandlerPipeline {
17
+ handlers: OperationHandler[];
18
+ }
@@ -1,11 +1,13 @@
1
1
  import * as ay from "@alloy-js/core";
2
2
  import * as ts from "@alloy-js/typescript";
3
+ import { ModelProperty } from "@typespec/compiler";
3
4
  import * as ef from "@typespec/emitter-framework/typescript";
4
5
  import { HttpOperation } from "@typespec/http";
5
6
  import { hasDefaultValue } from "../utils/parameters.jsx";
6
7
  import { getOperationOptionsInterfaceRefkey } from "./static-helpers/interfaces.jsx";
7
8
  export interface OperationOptionsProps {
8
9
  operation: HttpOperation;
10
+ excludes?: ModelProperty[];
9
11
  }
10
12
 
11
13
  export function getOperationOptionsTypeRefkey(operation: HttpOperation) {
@@ -13,10 +15,11 @@ export function getOperationOptionsTypeRefkey(operation: HttpOperation) {
13
15
  }
14
16
  export function OperationOptionsDeclaration(props: OperationOptionsProps) {
15
17
  const namePolicy = ts.useTSNamePolicy();
18
+ const excludes = (props.excludes ?? []).map((p) => p.name);
16
19
  const interfaceName = namePolicy.getName(props.operation.operation.name + "Options", "interface");
17
- const optionalParameters = props.operation.parameters.properties.filter(
18
- (p) => p.property.optional || hasDefaultValue(p),
19
- );
20
+ const optionalParameters = props.operation.parameters.properties
21
+ .filter((p) => !excludes.includes(p.property.name))
22
+ .filter((p) => p.property.optional || hasDefaultValue(p));
20
23
 
21
24
  return (
22
25
  <ts.InterfaceDeclaration
@@ -1,5 +1,6 @@
1
1
  import * as ay from "@alloy-js/core";
2
2
  import * as ts from "@alloy-js/typescript";
3
+ import { $ } from "@typespec/compiler/experimental/typekit";
3
4
  import { TransformNamePolicyContext } from "@typespec/emitter-framework";
4
5
  import { ClientLibrary } from "@typespec/http-client/components";
5
6
  import { EncodingProvider } from "./encoding-provider.jsx";
@@ -16,7 +17,7 @@ export function Output(props: OutputProps) {
16
17
  const defaultTransformNamePolicy = createTransformNamePolicy();
17
18
  return (
18
19
  <ay.Output namePolicy={tsNamePolicy} externals={[uriTemplateLib, httpRuntimeTemplateLib]}>
19
- <ClientLibrary>
20
+ <ClientLibrary program={$.program}>
20
21
  <TransformNamePolicyContext.Provider value={defaultTransformNamePolicy}>
21
22
  <EncodingProvider>{props.children}</EncodingProvider>
22
23
  </TransformNamePolicyContext.Provider>
@@ -0,0 +1,195 @@
1
+ import * as ay from "@alloy-js/core";
2
+ import * as ts from "@alloy-js/typescript";
3
+
4
+ export function getPagedAsyncIterableIteratorRefkey() {
5
+ return ay.refkey("PagedAsyncIterableIterator", "interface");
6
+ }
7
+
8
+ export function getBuildPagedAsyncIteratorRefkey() {
9
+ return ay.refkey("buildPagedAsyncIterator", "function");
10
+ }
11
+
12
+ function getPagedResultRefkey() {
13
+ return ay.refkey("PagedResult", "interface");
14
+ }
15
+
16
+ function getBuildPagedAsyncIteratorOptionsRefkey() {
17
+ return ay.refkey("BuildPagedAsyncIteratorOptions", "interface");
18
+ }
19
+
20
+ function PagedAsyncIterableIteratorInterfaceDeclaration() {
21
+ return (
22
+ <ay.Declaration
23
+ name="PagedAsyncIterableIterator"
24
+ refkey={getPagedAsyncIterableIteratorRefkey()}
25
+ >
26
+ {ay.code`
27
+ /**
28
+ * An interface that allows async iterable iteration both to completion and by page.
29
+ */
30
+ export interface PagedAsyncIterableIterator<
31
+ TElement,
32
+ TPageResponse,
33
+ TPageSettings,
34
+ > {
35
+ /**
36
+ * The next method, part of the iteration protocol
37
+ */
38
+ next(): Promise<IteratorResult<TElement>>;
39
+ /**
40
+ * The connection to the async iterator, part of the iteration protocol
41
+ */
42
+ [Symbol.asyncIterator](): PagedAsyncIterableIterator<
43
+ TElement,
44
+ TPageResponse,
45
+ TPageSettings
46
+ >;
47
+ /**
48
+ * Return an AsyncIterableIterator that works a page at a time
49
+ */
50
+ byPage: (
51
+ settings?: TPageSettings,
52
+ ) => AsyncIterableIterator<TPageResponse>;
53
+ } `}
54
+ </ay.Declaration>
55
+ );
56
+ }
57
+
58
+ function PagedResultInterfaceDeclaration() {
59
+ return (
60
+ <ay.Declaration name="PagedResult" refkey={getPagedResultRefkey()}>
61
+ {ay.code`
62
+ /**
63
+ * An interface that describes how to communicate with the service.
64
+ */
65
+ interface PagedResult<
66
+ TElement,
67
+ TPageResponse,
68
+ TPageSettings,
69
+ > {
70
+ /**
71
+ * Extract the paged elements from the response. Only support array of elements.
72
+ * @param response paged response
73
+ * @returns TElement[]
74
+ */
75
+ getElements: (response: TPageResponse) => TElement[];
76
+ /**
77
+ * A method that returns a page of results.
78
+ */
79
+ getPage: (
80
+ nextLinkOrContinuationToken?: string,
81
+ settings?: TPageSettings,
82
+ ) => Promise<{ pagedResponse: TPageResponse; nextToken?: string } | undefined>;
83
+ /**
84
+ * a function to implement the \`byPage\` method on the paged async iterator.
85
+ */
86
+ byPage: (
87
+ settings?: TPageSettings,
88
+ ) => AsyncIterableIterator<TPageResponse>;
89
+ }`}
90
+ </ay.Declaration>
91
+ );
92
+ }
93
+
94
+ function BuildPagedAsyncIteratorOptionsInterfaceDeclaration() {
95
+ return (
96
+ <ay.Declaration
97
+ name="BuildPagedAsyncIteratorOptions"
98
+ refkey={getBuildPagedAsyncIteratorOptionsRefkey()}
99
+ >
100
+ {ay.code`
101
+ /**
102
+ * Options for the paging helper
103
+ */
104
+ export interface BuildPagedAsyncIteratorOptions<
105
+ TElement,
106
+ TPageResponse,
107
+ TPageSettings,
108
+ > {
109
+ getElements: (response: TPageResponse) => TElement[];
110
+ getPagedResponse: (nextToken?: string, settings?: TPageSettings) => Promise<{ pagedResponse: TPageResponse; nextToken?: string } | undefined>;
111
+ }`}
112
+ </ay.Declaration>
113
+ );
114
+ }
115
+
116
+ function BuildPagedAsyncIteratorInterfaceDeclaration() {
117
+ return (
118
+ <ay.Declaration name="buildPagedAsyncIterator" refkey={getBuildPagedAsyncIteratorRefkey()}>
119
+ {ay.code`
120
+ /**
121
+ * Helper to paginate results in a generic way and return a PagedAsyncIterableIterator
122
+ */
123
+ export function buildPagedAsyncIterator<
124
+ TElement,
125
+ TPageResponse,
126
+ TPageSettings
127
+ >(
128
+ options: BuildPagedAsyncIteratorOptions<TElement, TPageResponse, TPageSettings>,
129
+ ): PagedAsyncIterableIterator<TElement, TPageResponse, TPageSettings> {
130
+ const pagedResult: ${getPagedResultRefkey()}<TElement, TPageResponse, TPageSettings> = {
131
+ getElements: options.getElements,
132
+ getPage: options.getPagedResponse,
133
+ byPage: (setting?: TPageSettings) => {
134
+ return getPageAsyncIterator(pagedResult, { setting });
135
+ },
136
+ };
137
+ const iter = getItemAsyncIterator<TElement, TPageResponse, TPageSettings>(pagedResult);
138
+ return {
139
+ next() {
140
+ return iter.next();
141
+ },
142
+ [Symbol.asyncIterator]() {
143
+ return this;
144
+ },
145
+ byPage: pagedResult.byPage,
146
+ };
147
+ }
148
+
149
+ async function* getItemAsyncIterator<TElement, TPage, TPageSettings>(
150
+ pagedResult: ${getPagedResultRefkey()}<TElement, TPage, TPageSettings>,
151
+ ): AsyncIterableIterator<TElement> {
152
+ const pages = getPageAsyncIterator(pagedResult);
153
+ for await (const page of pages) {
154
+ const results = pagedResult.getElements(page);
155
+ yield* results;
156
+ }
157
+ }
158
+
159
+ async function* getPageAsyncIterator<TElement, TPageResponse, TPageSettings>(
160
+ pagedResult: ${getPagedResultRefkey()}<TElement, TPageResponse, TPageSettings>,
161
+ options: {
162
+ setting?: TPageSettings;
163
+ } = {},
164
+ ): AsyncIterableIterator<TPageResponse> {
165
+ let response = await pagedResult.getPage(undefined, options.setting);
166
+ let results = response?.pagedResponse;
167
+ let nextToken = response?.nextToken;
168
+ if (!results) {
169
+ return;
170
+ }
171
+ yield results;
172
+ while (nextToken) {
173
+ response = await pagedResult.getPage(nextToken, options.setting);
174
+ if (!response) {
175
+ return;
176
+ }
177
+ results = response.pagedResponse;
178
+ nextToken = response.nextToken;
179
+ yield results;
180
+ }
181
+ }`}
182
+ </ay.Declaration>
183
+ );
184
+ }
185
+
186
+ export function PagingHelpers() {
187
+ return (
188
+ <ts.SourceFile path="pagingHelpers.ts">
189
+ <PagedAsyncIterableIteratorInterfaceDeclaration />
190
+ <PagedResultInterfaceDeclaration />
191
+ <BuildPagedAsyncIteratorOptionsInterfaceDeclaration />
192
+ <BuildPagedAsyncIteratorInterfaceDeclaration />
193
+ </ts.SourceFile>
194
+ );
195
+ }
@@ -2,7 +2,7 @@ import * as ts from "@alloy-js/typescript";
2
2
  import { Type } from "@typespec/compiler";
3
3
  import { $ } from "@typespec/compiler/experimental/typekit";
4
4
  import { HasName, TransformNamePolicyContext } from "@typespec/emitter-framework";
5
- import { ClientOperation } from "@typespec/http-client";
5
+ import { HttpOperation } from "@typespec/http";
6
6
  import { reportDiagnostic } from "../../lib.js";
7
7
  import { ContentTypeEncodingProvider } from "./content-type-encoding-provider.jsx";
8
8
  import { JsonTransform } from "./json/json-transform.jsx";
@@ -10,11 +10,11 @@ import { MultipartTransform } from "./multipart/multipart-transform.jsx";
10
10
  import { defaultTransportNameGetter } from "./transform-name-policy.js";
11
11
 
12
12
  export interface OperationTransformToTransportExpression {
13
- operation: ClientOperation;
13
+ httpOperation: HttpOperation;
14
14
  }
15
15
 
16
16
  export function OperationTransformExpression(props: OperationTransformToTransportExpression) {
17
- const body = props.operation.httpOperation.parameters.body;
17
+ const body = props.httpOperation.parameters.body;
18
18
 
19
19
  if (!body) {
20
20
  return;
package/src/emitter.tsx CHANGED
@@ -9,6 +9,7 @@ import { Output } from "./components/output.jsx";
9
9
  import { ModelSerializers } from "./components/serializers.js";
10
10
  import { Interfaces } from "./components/static-helpers/interfaces.jsx";
11
11
  import { MultipartHelpers } from "./components/static-helpers/multipart-helpers.jsx";
12
+ import { PagingHelpers } from "./components/static-helpers/paging-helper.jsx";
12
13
  import { RestError } from "./components/static-helpers/rest-error.jsx";
13
14
  import { JsClientEmitterOptions } from "./lib.js";
14
15
 
@@ -41,6 +42,7 @@ export async function $onEmit(context: EmitContext<JsClientEmitterOptions>) {
41
42
  <OperationsDirectory />
42
43
  </ay.SourceDirectory>
43
44
  <ay.SourceDirectory path="helpers">
45
+ <PagingHelpers />
44
46
  <Interfaces />
45
47
  <MultipartHelpers />
46
48
  <ts.SourceFile path="error.ts">