@typespec/http-client-js 0.4.0-dev.1 → 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.js +1 -1
- 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/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 +13 -12
- package/dist/src/utils/parameters.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/components/client-context/client-context-factory.tsx +1 -1
- package/src/components/client-operation.tsx +3 -3
- package/src/components/client.tsx +6 -9
- 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 +23 -25
- 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"
|
|
@@ -38,7 +38,7 @@ export function ClientContextFactoryDeclaration(props: ClientContextFactoryProps
|
|
|
38
38
|
);
|
|
39
39
|
|
|
40
40
|
let credentialsRef: string | undefined;
|
|
41
|
-
if ("credential"
|
|
41
|
+
if (parameters.some((p) => p.name === "credential")) {
|
|
42
42
|
credentialsRef = "credential";
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -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 {
|
|
@@ -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 (
|
|
@@ -78,9 +78,9 @@ export function JsonTransformDiscriminatorDeclaration(
|
|
|
78
78
|
const inputType = props.target === "transport" ? typeRef : "any";
|
|
79
79
|
const inputRef = ay.refkey();
|
|
80
80
|
|
|
81
|
-
const parameters:
|
|
82
|
-
|
|
83
|
-
|
|
81
|
+
const parameters: ts.ParameterDescriptor[] = [
|
|
82
|
+
{ name: "input_", type: inputType, refkey: inputRef, optional: true },
|
|
83
|
+
];
|
|
84
84
|
|
|
85
85
|
return (
|
|
86
86
|
<ts.FunctionDeclaration
|
|
@@ -58,9 +58,9 @@ export function JsonUnionTransformDeclaration(props: JsonUnionTransformDeclarati
|
|
|
58
58
|
const inputType = props.target === "transport" ? <>{typeRef} | null</> : "any";
|
|
59
59
|
const inputRef = ay.refkey();
|
|
60
60
|
|
|
61
|
-
const parameters:
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
const parameters: ts.ParameterDescriptor[] = [
|
|
62
|
+
{ name: "input_", type: inputType, refkey: inputRef, optional: true },
|
|
63
|
+
];
|
|
64
64
|
|
|
65
65
|
const declarationRefkey = getJsonUnionTransformRefkey(props.type, props.target);
|
|
66
66
|
return (
|
|
@@ -35,6 +35,7 @@ export interface SingleBodyTransformDeclarationProps {
|
|
|
35
35
|
export function SingleBodyTransformDeclaration(props: SingleBodyTransformDeclarationProps) {
|
|
36
36
|
const inputRef = ay.refkey(props.payload, "property");
|
|
37
37
|
const payloadParameter: ts.ParameterDescriptor = {
|
|
38
|
+
name: "payload",
|
|
38
39
|
type: <ef.TypeExpression type={props.payload.type} />,
|
|
39
40
|
refkey: inputRef,
|
|
40
41
|
};
|
|
@@ -43,7 +44,7 @@ export function SingleBodyTransformDeclaration(props: SingleBodyTransformDeclara
|
|
|
43
44
|
<ts.FunctionDeclaration
|
|
44
45
|
export
|
|
45
46
|
name={props.name}
|
|
46
|
-
parameters={
|
|
47
|
+
parameters={[payloadParameter]}
|
|
47
48
|
refkey={props.refkey}
|
|
48
49
|
>
|
|
49
50
|
return <JsonTransform itemRef={inputRef} target="transport" type={props.payload.type} />
|
package/src/utils/parameters.tsx
CHANGED
|
@@ -8,32 +8,32 @@ import * as cl from "@typespec/http-client";
|
|
|
8
8
|
import { getClientContextOptionsRef } from "../components/client-context/client-context-options.jsx";
|
|
9
9
|
import { httpRuntimeTemplateLib } from "../components/external-packages/ts-http-runtime.js";
|
|
10
10
|
|
|
11
|
-
export function buildClientParameters(client: cl.Client):
|
|
11
|
+
export function buildClientParameters(client: cl.Client): ts.ParameterDescriptor[] {
|
|
12
12
|
const clientConstructor = $.client.getConstructor(client);
|
|
13
13
|
const parameters = $.operation.getClientSignature(client, clientConstructor);
|
|
14
|
-
const params = parameters.
|
|
15
|
-
(
|
|
16
|
-
const
|
|
17
|
-
if (!
|
|
18
|
-
return
|
|
14
|
+
const params = parameters.flatMap(
|
|
15
|
+
(param) => {
|
|
16
|
+
const descriptor = buildClientParameterDescriptor(param);
|
|
17
|
+
if (!descriptor) {
|
|
18
|
+
return [];
|
|
19
19
|
}
|
|
20
|
-
const [name, descriptor] = paramsDescriptor;
|
|
21
20
|
|
|
22
21
|
if (!descriptor.optional) {
|
|
23
|
-
|
|
22
|
+
return [descriptor];
|
|
24
23
|
}
|
|
25
24
|
|
|
26
|
-
return
|
|
25
|
+
return [];
|
|
27
26
|
},
|
|
28
27
|
{} as Record<string, ts.ParameterDescriptor>,
|
|
29
28
|
);
|
|
30
29
|
|
|
31
|
-
if (!params
|
|
32
|
-
params
|
|
30
|
+
if (!params.some((p) => p.name === "options")) {
|
|
31
|
+
params.push({
|
|
32
|
+
name: "options",
|
|
33
33
|
refkey: ay.refkey(),
|
|
34
34
|
optional: true,
|
|
35
35
|
type: getClientContextOptionsRef(client),
|
|
36
|
-
};
|
|
36
|
+
});
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
return params;
|
|
@@ -41,7 +41,7 @@ export function buildClientParameters(client: cl.Client): Record<string, ts.Para
|
|
|
41
41
|
|
|
42
42
|
function buildClientParameterDescriptor(
|
|
43
43
|
modelProperty: ModelProperty,
|
|
44
|
-
):
|
|
44
|
+
): ts.ParameterDescriptor | undefined {
|
|
45
45
|
const authSchemes = $.modelProperty.getCredentialAuth(modelProperty);
|
|
46
46
|
|
|
47
47
|
if (authSchemes) {
|
|
@@ -52,18 +52,16 @@ function buildClientParameterDescriptor(
|
|
|
52
52
|
const credentialType = Array.from(
|
|
53
53
|
new Set(authSchemes.filter((s) => s.type !== "noAuth").map((s) => getCredentialType(s))),
|
|
54
54
|
);
|
|
55
|
-
return
|
|
56
|
-
"credential",
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
},
|
|
66
|
-
];
|
|
55
|
+
return {
|
|
56
|
+
name: "credential",
|
|
57
|
+
refkey: ay.refkey(modelProperty),
|
|
58
|
+
optional: modelProperty.optional,
|
|
59
|
+
type: ay.mapJoin(
|
|
60
|
+
() => credentialType,
|
|
61
|
+
(t) => t,
|
|
62
|
+
{ joiner: " | " },
|
|
63
|
+
),
|
|
64
|
+
};
|
|
67
65
|
}
|
|
68
66
|
|
|
69
67
|
return buildParameterDescriptor(modelProperty);
|
|
@@ -68,7 +68,7 @@ export async function defaultEncoding(
|
|
|
68
68
|
const path = parse("/default").expand({});
|
|
69
69
|
const httpRequestOptions = {
|
|
70
70
|
headers: {
|
|
71
|
-
...(options?.value && { value: dateRfc7231Serializer(options
|
|
71
|
+
...(options?.value && { value: dateRfc7231Serializer(options.value) }),
|
|
72
72
|
},
|
|
73
73
|
};
|
|
74
74
|
const response = await client.pathUnchecked(path).get(httpRequestOptions);
|
|
@@ -66,7 +66,7 @@ export async function defaultEncoding(
|
|
|
66
66
|
options?: DefaultEncodingOptions,
|
|
67
67
|
): Promise<void> {
|
|
68
68
|
const path = parse("/default{?value}").expand({
|
|
69
|
-
...(options?.value && { value: dateRfc3339Serializer(options
|
|
69
|
+
...(options?.value && { value: dateRfc3339Serializer(options.value) }),
|
|
70
70
|
});
|
|
71
71
|
const httpRequestOptions = {
|
|
72
72
|
headers: {},
|
|
@@ -24,7 +24,7 @@ export async function headModel(
|
|
|
24
24
|
const path = parse("/").expand({});
|
|
25
25
|
const httpRequestOptions = {
|
|
26
26
|
headers: {
|
|
27
|
-
...(
|
|
27
|
+
...(input.foo && { foo: input.foo }),
|
|
28
28
|
},
|
|
29
29
|
body: jsonVisibilityModelToTransportTransform(input),
|
|
30
30
|
};
|