@typespec/http-client-js 0.4.0-dev.0 → 0.4.0-dev.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/components/client-context/client-context-factory.d.ts.map +1 -1
- package/dist/src/components/client-context/client-context-factory.js +104 -37
- package/dist/src/components/client-context/client-context-factory.js.map +1 -1
- package/dist/src/components/client-operation.js +5 -7
- 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 +5 -3
- package/dist/src/components/client.js.map +1 -1
- package/dist/src/components/external-packages/ts-http-runtime.d.ts +8 -3
- package/dist/src/components/external-packages/ts-http-runtime.d.ts.map +1 -1
- package/dist/src/components/external-packages/ts-http-runtime.js +2 -2
- package/dist/src/components/external-packages/ts-http-runtime.js.map +1 -1
- package/dist/src/components/http-request-options.d.ts +1 -1
- package/dist/src/components/http-request-options.d.ts.map +1 -1
- package/dist/src/components/http-request-options.js +2 -1
- 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.js +1 -0
- package/dist/src/components/http-request.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/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/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/utils/parameters.d.ts +1 -1
- package/dist/src/utils/parameters.d.ts.map +1 -1
- package/dist/src/utils/parameters.js +29 -14
- package/dist/src/utils/parameters.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/components/client-context/client-context-factory.tsx +86 -37
- package/src/components/client-operation.tsx +3 -3
- package/src/components/client.tsx +6 -9
- package/src/components/external-packages/ts-http-runtime.ts +9 -4
- package/src/components/http-request-options.tsx +6 -1
- package/src/components/http-request-parameters-expression.tsx +142 -18
- package/src/components/http-request.tsx +1 -0
- package/src/components/operation-parameters.tsx +9 -8
- 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/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/utils/parameters.tsx +43 -28
- package/test/e2e/http/authentication/oauth2/main.test.ts +6 -16
- package/test/e2e/http/authentication/union/main.test.ts +2 -5
- package/test/scenarios/auth/basic_auth.md +11 -7
- package/test/scenarios/auth/bearer.md +11 -7
- package/test/scenarios/auth/client_parameters.md +19 -6
- package/test/scenarios/auth/client_structure.md +1 -1
- package/test/scenarios/auth/key_credential.md +11 -6
- package/test/scenarios/auth/oauth2.md +91 -0
- package/test/scenarios/auth/sub_client_override.md +11 -7
- 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/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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typespec/http-client-js",
|
|
3
|
-
"version": "0.4.0-dev.
|
|
3
|
+
"version": "0.4.0-dev.2",
|
|
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,8 +31,8 @@
|
|
|
31
31
|
"@typespec/rest": "^0.68.0 || >=0.69.0-dev <0.69.0"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@alloy-js/core": "^0.
|
|
35
|
-
"@alloy-js/typescript": "^0.
|
|
34
|
+
"@alloy-js/core": "^0.10.0",
|
|
35
|
+
"@alloy-js/typescript": "^0.10.0",
|
|
36
36
|
"@typespec/emitter-framework": "^0.4.0 || >=0.5.0-dev <0.5.0",
|
|
37
37
|
"@typespec/http-client": "^0.4.0 || >=0.5.0-dev <0.5.0",
|
|
38
38
|
"prettier": "~3.5.3"
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"@types/yargs": "~17.0.33",
|
|
46
46
|
"@typespec/http": "^1.0.0-rc.0",
|
|
47
47
|
"@typespec/http-specs": "^0.1.0-alpha.17 || >=0.1.0-alpha.18-dev <0.1.0-alpha.18",
|
|
48
|
-
"@typespec/spector": "^0.1.0-alpha.
|
|
49
|
-
"@typespec/ts-http-runtime": "0.1
|
|
48
|
+
"@typespec/spector": "^0.1.0-alpha.11 || >=0.1.0-alpha.12-dev <0.1.0-alpha.12",
|
|
49
|
+
"@typespec/ts-http-runtime": "0.2.1",
|
|
50
50
|
"@typespec/versioning": "^0.68.0 || >=0.69.0-dev <0.69.0",
|
|
51
51
|
"@vitest/ui": "^3.0.9",
|
|
52
52
|
"change-case": "~5.4.4",
|
|
@@ -2,6 +2,7 @@ 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 } from "@typespec/emitter-framework/typescript";
|
|
5
|
+
import { HttpAuth, type OAuth2Flow } from "@typespec/http";
|
|
5
6
|
import * as cl from "@typespec/http-client";
|
|
6
7
|
import { reportDiagnostic } from "../../lib.js";
|
|
7
8
|
import { buildClientParameters } from "../../utils/parameters.jsx";
|
|
@@ -37,7 +38,7 @@ export function ClientContextFactoryDeclaration(props: ClientContextFactoryProps
|
|
|
37
38
|
);
|
|
38
39
|
|
|
39
40
|
let credentialsRef: string | undefined;
|
|
40
|
-
if (
|
|
41
|
+
if (parameters.some((p) => p.name === "credential")) {
|
|
41
42
|
credentialsRef = "credential";
|
|
42
43
|
}
|
|
43
44
|
|
|
@@ -75,66 +76,114 @@ interface ClientFactoryArgumentsProps {
|
|
|
75
76
|
function ClientFactoryArguments(props: ClientFactoryArgumentsProps) {
|
|
76
77
|
const params = [<>{props.endpointRef},</>];
|
|
77
78
|
|
|
78
|
-
if (props.credentialsRef) {
|
|
79
|
-
params.push(<>{props.credentialsRef},</>);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
79
|
return [
|
|
83
80
|
...params,
|
|
84
|
-
|
|
85
|
-
<
|
|
86
|
-
|
|
81
|
+
props.credentialsRef ? (
|
|
82
|
+
<ClientOptionsExpression>
|
|
83
|
+
{props.credentialsRef}
|
|
84
|
+
<AuthSchemeOptions client={props.client} />
|
|
85
|
+
</ClientOptionsExpression>
|
|
86
|
+
) : (
|
|
87
|
+
<ClientOptionsExpression />
|
|
88
|
+
),
|
|
87
89
|
];
|
|
88
90
|
}
|
|
89
91
|
|
|
90
|
-
interface
|
|
92
|
+
interface AuthSchemeOptionsProps {
|
|
91
93
|
client: cl.Client;
|
|
92
94
|
}
|
|
93
95
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return null;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (clientCredential.schemes.length !== 1) {
|
|
102
|
-
reportDiagnostic($.program, {
|
|
103
|
-
code: "multiple-auth-schemes-not-yet-supported",
|
|
104
|
-
target: props.client.type,
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const scheme = clientCredential.schemes[0];
|
|
96
|
+
interface AuthSchemeProps {
|
|
97
|
+
scheme: HttpAuth;
|
|
98
|
+
client: cl.Client;
|
|
99
|
+
}
|
|
109
100
|
|
|
110
|
-
|
|
101
|
+
function AuthScheme(props: AuthSchemeProps) {
|
|
102
|
+
switch (props.scheme.type) {
|
|
111
103
|
case "http":
|
|
112
|
-
// Todo: handle scopes?
|
|
113
104
|
return (
|
|
114
|
-
<ts.
|
|
115
|
-
<
|
|
116
|
-
<ts.ObjectProperty name="
|
|
117
|
-
|
|
118
|
-
|
|
105
|
+
<ts.ObjectExpression>
|
|
106
|
+
<ay.List comma>
|
|
107
|
+
<ts.ObjectProperty name="kind" jsValue="http" />
|
|
108
|
+
<ts.ObjectProperty name="scheme" jsValue={props.scheme.scheme.toLowerCase()} />
|
|
109
|
+
</ay.List>
|
|
110
|
+
</ts.ObjectExpression>
|
|
119
111
|
);
|
|
120
112
|
case "apiKey":
|
|
121
|
-
if (scheme.in !== "header") {
|
|
113
|
+
if (props.scheme.in !== "header") {
|
|
122
114
|
reportDiagnostic($.program, { code: "non-model-parts", target: props.client.service });
|
|
123
115
|
return null;
|
|
124
116
|
}
|
|
125
117
|
|
|
126
118
|
return (
|
|
127
|
-
<ts.
|
|
128
|
-
<
|
|
129
|
-
<ts.ObjectProperty name="
|
|
130
|
-
|
|
131
|
-
|
|
119
|
+
<ts.ObjectExpression>
|
|
120
|
+
<ay.List comma>
|
|
121
|
+
<ts.ObjectProperty name="kind" jsValue="apiKey" />
|
|
122
|
+
<ts.ObjectProperty name="apiKeyLocation" jsValue={props.scheme.in} />
|
|
123
|
+
<ts.ObjectProperty name="name" jsValue={props.scheme.name} />
|
|
124
|
+
</ay.List>
|
|
125
|
+
</ts.ObjectExpression>
|
|
126
|
+
);
|
|
127
|
+
case "oauth2":
|
|
128
|
+
return (
|
|
129
|
+
<ts.ObjectExpression>
|
|
130
|
+
<ay.List comma>
|
|
131
|
+
<ts.ObjectProperty name="kind" jsValue="oauth2" />
|
|
132
|
+
<ts.ObjectProperty name="flows">
|
|
133
|
+
[
|
|
134
|
+
<ay.For each={props.scheme.flows} comma line>
|
|
135
|
+
{(flow) => <OAuth2Flow flow={flow} />}
|
|
136
|
+
</ay.For>
|
|
137
|
+
]
|
|
138
|
+
</ts.ObjectProperty>
|
|
139
|
+
</ay.List>
|
|
140
|
+
</ts.ObjectExpression>
|
|
132
141
|
);
|
|
133
142
|
default:
|
|
134
143
|
return null;
|
|
135
144
|
}
|
|
136
145
|
}
|
|
137
146
|
|
|
147
|
+
interface OAuth2FlowProps {
|
|
148
|
+
flow: OAuth2Flow;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function OAuth2Flow(props: OAuth2FlowProps) {
|
|
152
|
+
// rename type to kind and clean up the scopes
|
|
153
|
+
const { type, ...rewrittenFlow } = {
|
|
154
|
+
kind: props.flow.type,
|
|
155
|
+
...props.flow,
|
|
156
|
+
// scopes are sometimes duplicated so converting to set and back to dedupe
|
|
157
|
+
scopes: [...new Set(props.flow.scopes.map((s) => s.value))],
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
return <ts.ObjectExpression jsValue={rewrittenFlow} />;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function AuthSchemeOptions(props: AuthSchemeOptionsProps) {
|
|
164
|
+
const clientCredential = $.client.getAuth(props.client);
|
|
165
|
+
|
|
166
|
+
if (clientCredential.schemes.length === 0) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Filtering out custom http schemes for "http/custom" spector scenario
|
|
171
|
+
// TODO: Should typekit allow for arbitrary strings on scheme with http auth type?
|
|
172
|
+
const supportedSchemes = clientCredential.schemes.filter(
|
|
173
|
+
(s) => s.type !== "http" || ["Basic", "Bearer"].includes(s.scheme),
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
return (
|
|
177
|
+
<ts.ObjectProperty name="authSchemes">
|
|
178
|
+
[
|
|
179
|
+
<ay.For each={supportedSchemes} comma line>
|
|
180
|
+
{(scheme) => <AuthScheme scheme={scheme} client={props.client} />}
|
|
181
|
+
</ay.For>
|
|
182
|
+
]
|
|
183
|
+
</ts.ObjectProperty>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
138
187
|
interface ClientOptionsExpressionProps {
|
|
139
188
|
children?: ay.Children;
|
|
140
189
|
}
|
|
@@ -40,10 +40,10 @@ export function ClientOperation(props: ClientOperationProps) {
|
|
|
40
40
|
const returnType = $.httpOperation.getReturnType(props.clientOperation.httpOperation);
|
|
41
41
|
const responseRefkey = ay.refkey(props.clientOperation, "http-response");
|
|
42
42
|
const clientContextInterfaceRef = getClientcontextDeclarationRef(client);
|
|
43
|
-
const signatureParams:
|
|
44
|
-
|
|
43
|
+
const signatureParams: ts.ParameterDescriptor[] = [
|
|
44
|
+
{ name: "client", type: clientContextInterfaceRef, refkey: ay.refkey(client, "client") },
|
|
45
45
|
...getOperationParameters(props.clientOperation.httpOperation),
|
|
46
|
-
|
|
46
|
+
];
|
|
47
47
|
return (
|
|
48
48
|
<>
|
|
49
49
|
<OperationOptionsDeclaration operation={props.clientOperation.httpOperation} />
|
|
@@ -68,7 +68,7 @@ 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 =
|
|
71
|
+
const args = parameters.flatMap((p) => p.refkey);
|
|
72
72
|
|
|
73
73
|
return (
|
|
74
74
|
<ClassMethod
|
|
@@ -148,14 +148,11 @@ function ClientConstructor(props: ClientConstructorProps) {
|
|
|
148
148
|
);
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
-
function calculateSubClientArgs(
|
|
152
|
-
subClient
|
|
153
|
-
parentParams
|
|
154
|
-
)
|
|
155
|
-
|
|
156
|
-
return Object.entries(parentParams)
|
|
157
|
-
.filter(([name]) => Object.keys(subClientParams).includes(name))
|
|
158
|
-
.map(([_, p]) => p.refkey);
|
|
151
|
+
function calculateSubClientArgs(subClient: cl.Client, parentParams: ts.ParameterDescriptor[]) {
|
|
152
|
+
const subClientParams = buildClientParameters(subClient).map((p) => p.name);
|
|
153
|
+
return parentParams
|
|
154
|
+
.filter(({ name }) => subClientParams.includes(name))
|
|
155
|
+
.flatMap((p) => (p.refkey ? p.refkey : []));
|
|
159
156
|
}
|
|
160
157
|
|
|
161
158
|
export interface NewClientExpressionProps {
|
|
@@ -2,16 +2,21 @@ import { createPackage } from "@alloy-js/typescript";
|
|
|
2
2
|
|
|
3
3
|
export const httpRuntimeTemplateLib = createPackage({
|
|
4
4
|
name: "@typespec/ts-http-runtime",
|
|
5
|
-
version: "0.1
|
|
5
|
+
version: "0.2.1",
|
|
6
6
|
descriptor: {
|
|
7
7
|
".": {
|
|
8
8
|
named: [
|
|
9
9
|
"Client",
|
|
10
10
|
"ClientOptions",
|
|
11
11
|
"getClient",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
12
|
+
"ApiKeyCredential",
|
|
13
|
+
"BasicCredential",
|
|
14
|
+
"BearerTokenCredential",
|
|
15
|
+
"OAuth2TokenCredential",
|
|
16
|
+
"AuthorizationCodeFlow",
|
|
17
|
+
"ClientCredentialsFlow",
|
|
18
|
+
"ImplicitFlow",
|
|
19
|
+
"PasswordFlow",
|
|
15
20
|
"PathUncheckedResponse",
|
|
16
21
|
"PipelineRequest",
|
|
17
22
|
"HttpResponse",
|
|
@@ -41,7 +41,12 @@ HttpRequestOptions.Headers = function HttpRequestOptionsHeaders(
|
|
|
41
41
|
return (
|
|
42
42
|
<EncodingProvider defaults={{ bytes: "base64", datetime: "rfc7231" }}>
|
|
43
43
|
<ts.ObjectProperty name="headers">
|
|
44
|
-
<HttpRequestParametersExpression
|
|
44
|
+
<HttpRequestParametersExpression
|
|
45
|
+
parameters={headers}
|
|
46
|
+
optionsParameter={optionsParam}
|
|
47
|
+
httpOperation={httpOperation}
|
|
48
|
+
/>
|
|
49
|
+
,
|
|
45
50
|
</ts.ObjectProperty>
|
|
46
51
|
</EncodingProvider>
|
|
47
52
|
);
|
|
@@ -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
|
|
35
|
-
|
|
36
|
-
|
|
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
|
|
40
|
-
const headerValue = (
|
|
41
|
+
const paramValue = (
|
|
41
42
|
<>
|
|
42
|
-
{
|
|
43
|
-
{
|
|
43
|
+
{propertyExpression.fullExpression}
|
|
44
|
+
{` ?? ${defaultValue}`}
|
|
44
45
|
</>
|
|
45
46
|
);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
52
|
-
if (parameter.optional) {
|
|
54
|
+
if (propertyExpression.isNullish) {
|
|
53
55
|
return ay.code`
|
|
54
|
-
...(${
|
|
56
|
+
...(${propertyExpression.fullExpression} && {${(<JsonTransform itemRef={propertyExpression.leadingExpression} type={httpProperty.property} target="transport" />)}})
|
|
55
57
|
`;
|
|
56
58
|
} else {
|
|
57
|
-
return
|
|
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
|
+
}
|
|
@@ -57,6 +57,7 @@ HttpRequest.Url = function HttpUrlDeclaration(props: HttpUrlProps) {
|
|
|
57
57
|
{uriTemplateLib.parse}({JSON.stringify(urlTemplate)}).expand(
|
|
58
58
|
{
|
|
59
59
|
<HttpRequestParametersExpression
|
|
60
|
+
httpOperation={httpOperation}
|
|
60
61
|
optionsParameter={optionsParameter!}
|
|
61
62
|
parameters={urlParameters}
|
|
62
63
|
/>
|
|
@@ -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
|
}
|
|
@@ -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
|
}
|
|
@@ -47,7 +47,10 @@ export function RestError() {
|
|
|
47
47
|
<ay.List hardline>
|
|
48
48
|
<ts.ClassMethod
|
|
49
49
|
name="constructor"
|
|
50
|
-
parameters={
|
|
50
|
+
parameters={[
|
|
51
|
+
{ name: "message", type: "string" },
|
|
52
|
+
{ name: "response", type: httpRuntimeTemplateLib.HttpResponse },
|
|
53
|
+
]}
|
|
51
54
|
returnType={null}
|
|
52
55
|
refkey={constructorRefkey}
|
|
53
56
|
>
|
|
@@ -68,7 +71,7 @@ export function RestError() {
|
|
|
68
71
|
<ts.ClassMethod
|
|
69
72
|
static
|
|
70
73
|
name="fromHttpResponse"
|
|
71
|
-
parameters={{ response: httpRuntimeTemplateLib.HttpResponse }}
|
|
74
|
+
parameters={[{ name: "response", type: httpRuntimeTemplateLib.HttpResponse }]}
|
|
72
75
|
returnType={getRestErrorRefkey()}
|
|
73
76
|
refkey={fromHttpResponseRefkey}
|
|
74
77
|
>
|
|
@@ -85,7 +88,7 @@ export function RestError() {
|
|
|
85
88
|
<ts.FunctionDeclaration
|
|
86
89
|
export
|
|
87
90
|
name="createRestError"
|
|
88
|
-
parameters={{ response: httpRuntimeTemplateLib.HttpResponse }}
|
|
91
|
+
parameters={[{ name: "response", type: httpRuntimeTemplateLib.HttpResponse }]}
|
|
89
92
|
returnType={getRestErrorRefkey()}
|
|
90
93
|
refkey={getCreateRestErrorRefkey()}
|
|
91
94
|
>
|
|
@@ -66,9 +66,9 @@ export function JsonArrayTransformDeclaration(props: JsonArrayTransformDeclarati
|
|
|
66
66
|
const inputType = props.target === "transport" ? <>{itemType} | null</> : "any";
|
|
67
67
|
const inputRef = ay.refkey();
|
|
68
68
|
|
|
69
|
-
const parameters:
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
const parameters: ts.ParameterDescriptor[] = [
|
|
70
|
+
{ name: "items_", type: inputType, refkey: inputRef, optional: true },
|
|
71
|
+
];
|
|
72
72
|
|
|
73
73
|
const declarationRefkey = getJsonArrayTransformRefkey(props.type, props.target);
|
|
74
74
|
return (
|
|
@@ -35,5 +35,17 @@ export function JsonModelPropertyTransform(props: JsonModelPropertyTransformProp
|
|
|
35
35
|
);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
if (props.target === "transport") {
|
|
39
|
+
return (
|
|
40
|
+
<ay.NamePolicyContext.Provider value={{ getName: (n) => n }}>
|
|
41
|
+
<ts.ObjectProperty name={targetName} value={propertyValue} />
|
|
42
|
+
</ay.NamePolicyContext.Provider>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<>
|
|
48
|
+
<ts.ObjectProperty name={JSON.stringify(targetName)} value={propertyValue} />
|
|
49
|
+
</>
|
|
50
|
+
);
|
|
39
51
|
}
|
|
@@ -79,10 +79,10 @@ export function JsonModelTransformDeclaration(
|
|
|
79
79
|
const inputType = props.target === "transport" ? <>{ay.refkey(props.type)} | null</> : "any";
|
|
80
80
|
const inputRef = ay.refkey();
|
|
81
81
|
|
|
82
|
-
const parameters:
|
|
82
|
+
const parameters: ts.ParameterDescriptor[] = [
|
|
83
83
|
// Make the input optional to make the transform more robust and check against null and undefined
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
{ name: "input_", type: inputType, refkey: inputRef, optional: true },
|
|
85
|
+
];
|
|
86
86
|
|
|
87
87
|
const spread = $.model.getSpreadType(props.type);
|
|
88
88
|
const hasAdditionalProperties = spread && $.model.is(spread) && $.record.is(spread);
|
|
@@ -67,9 +67,9 @@ export function JsonRecordTransformDeclaration(props: JsonRecordTransformDeclara
|
|
|
67
67
|
const inputType = props.target === "transport" ? <>{itemType} | null</> : "any";
|
|
68
68
|
const inputRef = ay.refkey();
|
|
69
69
|
|
|
70
|
-
const parameters:
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
const parameters: ts.ParameterDescriptor[] = [
|
|
71
|
+
{ name: "items_", type: inputType, refkey: inputRef, optional: true },
|
|
72
|
+
];
|
|
73
73
|
|
|
74
74
|
const declarationRefkey = getJsonRecordTransformRefkey(props.type, props.target);
|
|
75
75
|
return (
|