@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.
- package/CHANGELOG.md +0 -1
- package/dist/src/components/client-context/client-context-factory.js +1 -1
- package/dist/src/components/client-context/client-context-factory.js.map +1 -1
- package/dist/src/components/client-operation.d.ts +4 -1
- package/dist/src/components/client-operation.d.ts.map +1 -1
- package/dist/src/components/client-operation.js +68 -38
- package/dist/src/components/client-operation.js.map +1 -1
- package/dist/src/components/client.d.ts.map +1 -1
- package/dist/src/components/client.js +7 -4
- package/dist/src/components/client.js.map +1 -1
- package/dist/src/components/http-request-options.d.ts +5 -5
- package/dist/src/components/http-request-options.d.ts.map +1 -1
- package/dist/src/components/http-request-options.js +12 -12
- package/dist/src/components/http-request-options.js.map +1 -1
- package/dist/src/components/http-request-parameters-expression.d.ts +2 -1
- package/dist/src/components/http-request-parameters-expression.d.ts.map +1 -1
- package/dist/src/components/http-request-parameters-expression.js +125 -18
- package/dist/src/components/http-request-parameters-expression.js.map +1 -1
- package/dist/src/components/http-request.d.ts +3 -3
- package/dist/src/components/http-request.d.ts.map +1 -1
- package/dist/src/components/http-request.js +35 -30
- package/dist/src/components/http-request.js.map +1 -1
- package/dist/src/components/http-response.d.ts +3 -3
- package/dist/src/components/http-response.d.ts.map +1 -1
- package/dist/src/components/http-response.js +11 -6
- package/dist/src/components/http-response.js.map +1 -1
- package/dist/src/components/operation-handlers/index.d.ts +3 -0
- package/dist/src/components/operation-handlers/index.d.ts.map +1 -0
- package/dist/src/components/operation-handlers/index.js +3 -0
- package/dist/src/components/operation-handlers/index.js.map +1 -0
- package/dist/src/components/operation-handlers/operation-pipeline.d.ts +15 -0
- package/dist/src/components/operation-handlers/operation-pipeline.d.ts.map +1 -0
- package/dist/src/components/operation-handlers/operation-pipeline.js +28 -0
- package/dist/src/components/operation-handlers/operation-pipeline.js.map +1 -0
- package/dist/src/components/operation-handlers/paging/page-item.d.ts +3 -0
- package/dist/src/components/operation-handlers/paging/page-item.d.ts.map +1 -0
- package/dist/src/components/operation-handlers/paging/page-item.js +17 -0
- package/dist/src/components/operation-handlers/paging/page-item.js.map +1 -0
- package/dist/src/components/operation-handlers/paging/page-response.d.ts +10 -0
- package/dist/src/components/operation-handlers/paging/page-response.d.ts.map +1 -0
- package/dist/src/components/operation-handlers/paging/page-response.js +50 -0
- package/dist/src/components/operation-handlers/paging/page-response.js.map +1 -0
- package/dist/src/components/operation-handlers/paging/page-settings.d.ts +11 -0
- package/dist/src/components/operation-handlers/paging/page-settings.d.ts.map +1 -0
- package/dist/src/components/operation-handlers/paging/page-settings.js +52 -0
- package/dist/src/components/operation-handlers/paging/page-settings.js.map +1 -0
- package/dist/src/components/operation-handlers/paging/paginated-operation-handler.d.ts +3 -0
- package/dist/src/components/operation-handlers/paging/paginated-operation-handler.d.ts.map +1 -0
- package/dist/src/components/operation-handlers/paging/paginated-operation-handler.js +170 -0
- package/dist/src/components/operation-handlers/paging/paginated-operation-handler.js.map +1 -0
- package/dist/src/components/operation-handlers/paging/request-send.d.ts +12 -0
- package/dist/src/components/operation-handlers/paging/request-send.d.ts.map +1 -0
- package/dist/src/components/operation-handlers/paging/request-send.js +78 -0
- package/dist/src/components/operation-handlers/paging/request-send.js.map +1 -0
- package/dist/src/components/operation-handlers/paging/response-deserialize.d.ts +11 -0
- package/dist/src/components/operation-handlers/paging/response-deserialize.d.ts.map +1 -0
- package/dist/src/components/operation-handlers/paging/response-deserialize.js +51 -0
- package/dist/src/components/operation-handlers/paging/response-deserialize.js.map +1 -0
- package/dist/src/components/operation-handlers/paging/util.d.ts +17 -0
- package/dist/src/components/operation-handlers/paging/util.d.ts.map +1 -0
- package/dist/src/components/operation-handlers/paging/util.js +46 -0
- package/dist/src/components/operation-handlers/paging/util.js.map +1 -0
- package/dist/src/components/operation-handlers/types.d.ts +16 -0
- package/dist/src/components/operation-handlers/types.d.ts.map +1 -0
- package/dist/src/components/operation-handlers/types.js +1 -0
- package/dist/src/components/operation-handlers/types.js.map +1 -0
- package/dist/src/components/operation-options.d.ts +2 -0
- package/dist/src/components/operation-options.d.ts.map +1 -1
- package/dist/src/components/operation-options.js +2 -1
- package/dist/src/components/operation-options.js.map +1 -1
- package/dist/src/components/operation-parameters.d.ts +1 -1
- package/dist/src/components/operation-parameters.d.ts.map +1 -1
- package/dist/src/components/operation-parameters.js +7 -5
- package/dist/src/components/operation-parameters.js.map +1 -1
- package/dist/src/components/output.d.ts.map +1 -1
- package/dist/src/components/output.js +4 -0
- package/dist/src/components/output.js.map +1 -1
- package/dist/src/components/static-helpers/bytes-encoding.js +14 -16
- package/dist/src/components/static-helpers/bytes-encoding.js.map +1 -1
- package/dist/src/components/static-helpers/multipart-helpers.js +12 -10
- package/dist/src/components/static-helpers/multipart-helpers.js.map +1 -1
- package/dist/src/components/static-helpers/paging-helper.d.ts +5 -0
- package/dist/src/components/static-helpers/paging-helper.d.ts.map +1 -0
- package/dist/src/components/static-helpers/paging-helper.js +197 -0
- package/dist/src/components/static-helpers/paging-helper.js.map +1 -0
- package/dist/src/components/static-helpers/rest-error.d.ts.map +1 -1
- package/dist/src/components/static-helpers/rest-error.js +15 -10
- package/dist/src/components/static-helpers/rest-error.js.map +1 -1
- package/dist/src/components/testing/client-options.d.ts +1 -1
- package/dist/src/components/testing/client-options.d.ts.map +1 -1
- package/dist/src/components/transforms/json/json-array-transform.d.ts +1 -1
- package/dist/src/components/transforms/json/json-array-transform.d.ts.map +1 -1
- package/dist/src/components/transforms/json/json-array-transform.js +6 -7
- package/dist/src/components/transforms/json/json-array-transform.js.map +1 -1
- package/dist/src/components/transforms/json/json-model-property-transform.d.ts.map +1 -1
- package/dist/src/components/transforms/json/json-model-property-transform.js +15 -2
- package/dist/src/components/transforms/json/json-model-property-transform.js.map +1 -1
- package/dist/src/components/transforms/json/json-model-transform.js +8 -8
- package/dist/src/components/transforms/json/json-model-transform.js.map +1 -1
- package/dist/src/components/transforms/json/json-record-transform.d.ts +1 -1
- package/dist/src/components/transforms/json/json-record-transform.d.ts.map +1 -1
- package/dist/src/components/transforms/json/json-record-transform.js +6 -7
- package/dist/src/components/transforms/json/json-record-transform.js.map +1 -1
- package/dist/src/components/transforms/json/json-transform-discriminator.js +6 -7
- package/dist/src/components/transforms/json/json-transform-discriminator.js.map +1 -1
- package/dist/src/components/transforms/json/union-transform.js +6 -7
- package/dist/src/components/transforms/json/union-transform.js.map +1 -1
- package/dist/src/components/transforms/operation-transform-declaration.d.ts.map +1 -1
- package/dist/src/components/transforms/operation-transform-declaration.js +2 -3
- package/dist/src/components/transforms/operation-transform-declaration.js.map +1 -1
- package/dist/src/components/transforms/operation-transform-expression.d.ts +2 -2
- package/dist/src/components/transforms/operation-transform-expression.d.ts.map +1 -1
- package/dist/src/components/transforms/operation-transform-expression.js +1 -1
- package/dist/src/components/transforms/operation-transform-expression.js.map +1 -1
- package/dist/src/emitter.d.ts.map +1 -1
- package/dist/src/emitter.js +2 -1
- package/dist/src/emitter.js.map +1 -1
- package/dist/src/lib.d.ts +17 -2
- package/dist/src/lib.d.ts.map +1 -1
- package/dist/src/lib.js +6 -0
- package/dist/src/lib.js.map +1 -1
- package/dist/src/utils/parameters.d.ts +1 -1
- package/dist/src/utils/parameters.d.ts.map +1 -1
- package/dist/src/utils/parameters.js +13 -12
- package/dist/src/utils/parameters.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/src/components/client-context/client-context-factory.tsx +1 -1
- package/src/components/client-operation.tsx +40 -16
- package/src/components/client.tsx +8 -10
- package/src/components/http-request-options.tsx +17 -13
- package/src/components/http-request-parameters-expression.tsx +142 -18
- package/src/components/http-request.tsx +22 -20
- package/src/components/http-response.tsx +8 -9
- package/src/components/operation-handlers/index.tsx +2 -0
- package/src/components/operation-handlers/operation-pipeline.tsx +33 -0
- package/src/components/operation-handlers/paging/page-item.tsx +13 -0
- package/src/components/operation-handlers/paging/page-response.tsx +55 -0
- package/src/components/operation-handlers/paging/page-settings.tsx +53 -0
- package/src/components/operation-handlers/paging/paginated-operation-handler.tsx +192 -0
- package/src/components/operation-handlers/paging/request-send.tsx +70 -0
- package/src/components/operation-handlers/paging/response-deserialize.tsx +54 -0
- package/src/components/operation-handlers/paging/util.ts +70 -0
- package/src/components/operation-handlers/types.tsx +18 -0
- package/src/components/operation-options.tsx +6 -3
- package/src/components/operation-parameters.tsx +9 -8
- package/src/components/output.tsx +2 -1
- package/src/components/static-helpers/bytes-encoding.tsx +5 -5
- package/src/components/static-helpers/multipart-helpers.tsx +7 -7
- package/src/components/static-helpers/paging-helper.tsx +195 -0
- package/src/components/static-helpers/rest-error.tsx +6 -3
- package/src/components/transforms/json/json-array-transform.tsx +3 -3
- package/src/components/transforms/json/json-model-property-transform.tsx +13 -1
- package/src/components/transforms/json/json-model-transform.tsx +3 -3
- package/src/components/transforms/json/json-record-transform.tsx +3 -3
- package/src/components/transforms/json/json-transform-discriminator.tsx +3 -3
- package/src/components/transforms/json/union-transform.tsx +3 -3
- package/src/components/transforms/operation-transform-declaration.tsx +2 -1
- package/src/components/transforms/operation-transform-expression.tsx +3 -3
- package/src/emitter.tsx +2 -0
- package/src/lib.ts +6 -0
- package/src/utils/parameters.tsx +23 -25
- package/test/e2e/http/payload/pageable/main.test.ts +78 -22
- package/test/scenarios/encoding/header_bytes.md +1 -1
- package/test/scenarios/encoding/header_date.md +1 -1
- package/test/scenarios/encoding/query_date.md +1 -1
- package/test/scenarios/http-operations/paging.md +244 -0
- package/test/scenarios/http-operations/query-parameter.md +1 -1
- package/test/scenarios/operation-parameters/body_root_anonymous.md +1 -1
- package/test/scenarios/operation-parameters/with_body_property.md +1 -1
- package/test/scenarios/operation-parameters/with_body_root.md +1 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import * as ay from "@alloy-js/core";
|
|
2
|
+
import * as ts from "@alloy-js/typescript";
|
|
3
|
+
import { PagingOperation, PagingProperty } from "@typespec/compiler";
|
|
4
|
+
import * as ef from "@typespec/emitter-framework/typescript";
|
|
5
|
+
import { HttpOperation } from "@typespec/http";
|
|
6
|
+
export interface PageResponseProps {
|
|
7
|
+
operation: HttpOperation;
|
|
8
|
+
pagingOperation: PagingOperation;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function getPageResponseTypeRefkey(operation: HttpOperation) {
|
|
12
|
+
return ay.refkey(operation, "page-response");
|
|
13
|
+
}
|
|
14
|
+
export function PageResponseDeclaration(props: PageResponseProps) {
|
|
15
|
+
const namePolicy = ts.useTSNamePolicy();
|
|
16
|
+
const interfaceName = namePolicy.getName(
|
|
17
|
+
props.operation.operation.name + "PageResponse",
|
|
18
|
+
"interface",
|
|
19
|
+
);
|
|
20
|
+
const definedResponses = props.pagingOperation.output;
|
|
21
|
+
// Only accept these respose items
|
|
22
|
+
const acceptedColumns = [
|
|
23
|
+
"continuationToken",
|
|
24
|
+
"pageItems",
|
|
25
|
+
"nextLink",
|
|
26
|
+
"prevLink",
|
|
27
|
+
"firstLink",
|
|
28
|
+
"lastLink",
|
|
29
|
+
];
|
|
30
|
+
const responseProperties: PagingProperty[] = [];
|
|
31
|
+
for (const key in definedResponses) {
|
|
32
|
+
const property = definedResponses[key as keyof typeof definedResponses];
|
|
33
|
+
if (acceptedColumns.includes(key) && !!property) {
|
|
34
|
+
responseProperties.push(property);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<ts.InterfaceDeclaration
|
|
40
|
+
export
|
|
41
|
+
name={interfaceName}
|
|
42
|
+
refkey={getPageResponseTypeRefkey(props.operation)}
|
|
43
|
+
>
|
|
44
|
+
<ay.For each={responseProperties} line>
|
|
45
|
+
{(parameter) => (
|
|
46
|
+
<ts.InterfaceMember
|
|
47
|
+
name={parameter.property.name}
|
|
48
|
+
optional={parameter.property.optional}
|
|
49
|
+
type={<ef.TypeExpression type={parameter.property.type} />}
|
|
50
|
+
/>
|
|
51
|
+
)}
|
|
52
|
+
</ay.For>
|
|
53
|
+
</ts.InterfaceDeclaration>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as ay from "@alloy-js/core";
|
|
2
|
+
import * as ts from "@alloy-js/typescript";
|
|
3
|
+
import { PagingOperation, PagingProperty } from "@typespec/compiler";
|
|
4
|
+
import * as ef from "@typespec/emitter-framework/typescript";
|
|
5
|
+
import { HttpOperation } from "@typespec/http";
|
|
6
|
+
export interface PageSettingsProps {
|
|
7
|
+
operation: HttpOperation;
|
|
8
|
+
pagingOperation: PagingOperation;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function getPageSettingsTypeRefkey(operation: HttpOperation) {
|
|
12
|
+
return ay.refkey(operation, "page-settings");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getPageSettingProperties(pagingOperation: PagingOperation) {
|
|
16
|
+
const definedSettings = pagingOperation.input;
|
|
17
|
+
// Only accept these page settings
|
|
18
|
+
const acceptedSettings = ["continuationToken", "offset", "pageSize", "pageIndex"];
|
|
19
|
+
const settingProperties: PagingProperty[] = [];
|
|
20
|
+
for (const key in definedSettings) {
|
|
21
|
+
const property = definedSettings[key as keyof typeof definedSettings];
|
|
22
|
+
if (acceptedSettings.includes(key) && !!property) {
|
|
23
|
+
settingProperties.push(property);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return settingProperties;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function PageSettingsDeclaration(props: PageSettingsProps) {
|
|
30
|
+
const namePolicy = ts.useTSNamePolicy();
|
|
31
|
+
const interfaceName = namePolicy.getName(
|
|
32
|
+
props.operation.operation.name + "PageSettings",
|
|
33
|
+
"interface",
|
|
34
|
+
);
|
|
35
|
+
const settingProperties = getPageSettingProperties(props.pagingOperation);
|
|
36
|
+
return (
|
|
37
|
+
<ts.InterfaceDeclaration
|
|
38
|
+
export
|
|
39
|
+
name={interfaceName}
|
|
40
|
+
refkey={getPageSettingsTypeRefkey(props.operation)}
|
|
41
|
+
>
|
|
42
|
+
<ay.For each={settingProperties} line>
|
|
43
|
+
{(parameter) => (
|
|
44
|
+
<ts.InterfaceMember
|
|
45
|
+
name={parameter.property.name}
|
|
46
|
+
optional
|
|
47
|
+
type={<ef.TypeExpression type={parameter.property.type} />}
|
|
48
|
+
/>
|
|
49
|
+
)}
|
|
50
|
+
</ay.For>
|
|
51
|
+
</ts.InterfaceDeclaration>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
@@ -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
|
|
18
|
-
(p) => p.property.
|
|
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
|
|
@@ -14,30 +14,31 @@ export function getOperationOptionsParameterRefkey(operation: HttpOperation) {
|
|
|
14
14
|
return ay.refkey(operation, "operation-options-parameter");
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
export function getOperationParameters(
|
|
18
|
-
operation: HttpOperation,
|
|
19
|
-
): Record<string, ts.ParameterDescriptor | ay.Children> {
|
|
17
|
+
export function getOperationParameters(operation: HttpOperation): ts.ParameterDescriptor[] {
|
|
20
18
|
const transformNamer = useTransformNamePolicy();
|
|
21
19
|
const requiredParameters = operation.parameters.properties
|
|
22
20
|
.filter((p) => !p.property.optional && !hasDefaultValue(p))
|
|
23
21
|
.filter((p) => p.path.length === 1);
|
|
24
22
|
|
|
25
|
-
const parameters:
|
|
23
|
+
const parameters: ts.ParameterDescriptor[] = [];
|
|
26
24
|
|
|
27
25
|
for (const parameter of requiredParameters) {
|
|
26
|
+
const name = transformNamer.getApplicationName(parameter.property);
|
|
28
27
|
const parameterDescriptor: ts.ParameterDescriptor = {
|
|
28
|
+
name,
|
|
29
29
|
refkey: ay.refkey(parameter.property, "operation-parameter"),
|
|
30
30
|
type: <ef.TypeExpression type={parameter.property.type} />,
|
|
31
31
|
};
|
|
32
|
-
|
|
33
|
-
parameters
|
|
32
|
+
|
|
33
|
+
parameters.push(parameterDescriptor);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
parameters
|
|
36
|
+
parameters.push({
|
|
37
|
+
name: "options",
|
|
37
38
|
refkey: getOperationOptionsParameterRefkey(operation),
|
|
38
39
|
type: getOperationOptionsTypeRefkey(operation),
|
|
39
40
|
optional: true,
|
|
40
|
-
};
|
|
41
|
+
});
|
|
41
42
|
|
|
42
43
|
return parameters;
|
|
43
44
|
}
|
|
@@ -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>
|
|
@@ -14,10 +14,10 @@ export function EncodeUint8Array(): ay.Children {
|
|
|
14
14
|
export
|
|
15
15
|
refkey={refkey}
|
|
16
16
|
name="encodeUint8Array"
|
|
17
|
-
parameters={
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
17
|
+
parameters={[
|
|
18
|
+
{ name: "value", type: "Uint8Array | undefined | null", refkey: valueRef },
|
|
19
|
+
{ name: "encoding", type: "BufferEncoding", refkey: encodingRef },
|
|
20
|
+
]}
|
|
21
21
|
returnType="string | undefined"
|
|
22
22
|
>
|
|
23
23
|
{ay.code`
|
|
@@ -40,7 +40,7 @@ export function DecodeBase64(): ay.Children {
|
|
|
40
40
|
<ts.FunctionDeclaration
|
|
41
41
|
export
|
|
42
42
|
name="decodeBase64"
|
|
43
|
-
parameters={{
|
|
43
|
+
parameters={[{ name: "value", type: "string", refkey: valueRef }]}
|
|
44
44
|
returnType="Uint8Array | undefined"
|
|
45
45
|
refkey={refkey}
|
|
46
46
|
>
|
|
@@ -66,15 +66,15 @@ export function MultipartHelpers(props: MultipartHelpersProps) {
|
|
|
66
66
|
);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
function getCreateFilePartParameters() {
|
|
70
|
-
return
|
|
71
|
-
partName: "string",
|
|
72
|
-
fileInput: "any",
|
|
73
|
-
|
|
69
|
+
function getCreateFilePartParameters(): ts.ParameterDescriptor[] {
|
|
70
|
+
return [
|
|
71
|
+
{ name: "partName", type: "string" },
|
|
72
|
+
{ name: "fileInput", type: "any" },
|
|
73
|
+
{
|
|
74
|
+
name: "defaultContentType",
|
|
74
75
|
optional: true,
|
|
75
|
-
key: "defaultContentType",
|
|
76
76
|
refkey: ay.refkey(),
|
|
77
77
|
type: "string",
|
|
78
78
|
},
|
|
79
|
-
|
|
79
|
+
];
|
|
80
80
|
}
|