@typespec/http-client-js 0.2.0-dev.1 → 0.2.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/dist/src/components/client-context/client-context-declaration.d.ts.map +1 -1
- package/dist/src/components/client-context/client-context-declaration.js.map +1 -1
- package/dist/src/components/client-context/client-context-factory.d.ts.map +1 -1
- package/dist/src/components/client-context/client-context-factory.js +7 -4
- package/dist/src/components/client-context/client-context-factory.js.map +1 -1
- package/dist/src/components/client-context/client-context-options.d.ts.map +1 -1
- package/dist/src/components/client-context/client-context-options.js +13 -9
- package/dist/src/components/client-context/client-context-options.js.map +1 -1
- package/dist/src/components/client-context/client-context.d.ts.map +1 -1
- package/dist/src/components/client-context/client-context.js +7 -12
- package/dist/src/components/client-context/client-context.js.map +1 -1
- package/dist/src/components/client-context/parametrized-endpoint.d.ts +1 -1
- package/dist/src/components/client-context/parametrized-endpoint.d.ts.map +1 -1
- package/dist/src/components/client-context/parametrized-endpoint.js +22 -13
- package/dist/src/components/client-context/parametrized-endpoint.js.map +1 -1
- package/dist/src/components/client-directory.d.ts +2 -2
- package/dist/src/components/client-directory.d.ts.map +1 -1
- package/dist/src/components/client-directory.js +33 -21
- package/dist/src/components/client-directory.js.map +1 -1
- package/dist/src/components/client-operation.d.ts.map +1 -1
- package/dist/src/components/client-operation.js +6 -6
- package/dist/src/components/client-operation.js.map +1 -1
- package/dist/src/components/client.d.ts +1 -1
- package/dist/src/components/client.d.ts.map +1 -1
- package/dist/src/components/client.js +83 -64
- package/dist/src/components/client.js.map +1 -1
- package/dist/src/components/encoding-provider.d.ts.map +1 -1
- package/dist/src/components/encoding-provider.js +0 -1
- package/dist/src/components/encoding-provider.js.map +1 -1
- package/dist/src/components/http-request-options.d.ts.map +1 -1
- package/dist/src/components/http-request-options.js +3 -3
- package/dist/src/components/http-request-options.js.map +1 -1
- package/dist/src/components/http-request-parameters-expression.d.ts +1 -2
- package/dist/src/components/http-request-parameters-expression.d.ts.map +1 -1
- package/dist/src/components/http-request-parameters-expression.js +37 -33
- package/dist/src/components/http-request-parameters-expression.js.map +1 -1
- package/dist/src/components/http-request.d.ts.map +1 -1
- package/dist/src/components/http-request.js +24 -20
- package/dist/src/components/http-request.js.map +1 -1
- package/dist/src/components/http-response.d.ts +1 -1
- package/dist/src/components/http-response.d.ts.map +1 -1
- package/dist/src/components/http-response.js +36 -33
- package/dist/src/components/http-response.js.map +1 -1
- package/dist/src/components/models.d.ts.map +1 -1
- package/dist/src/components/models.js +21 -18
- package/dist/src/components/models.js.map +1 -1
- package/dist/src/components/operation-options.d.ts.map +1 -1
- package/dist/src/components/operation-options.js +17 -14
- package/dist/src/components/operation-options.js.map +1 -1
- package/dist/src/components/output.d.ts.map +1 -1
- package/dist/src/components/output.js +2 -1
- package/dist/src/components/output.js.map +1 -1
- package/dist/src/components/serializers.d.ts.map +1 -1
- package/dist/src/components/serializers.js +3 -3
- package/dist/src/components/serializers.js.map +1 -1
- package/dist/src/components/static-helpers/bytes-encoding.d.ts.map +1 -1
- package/dist/src/components/static-helpers/bytes-encoding.js +2 -2
- package/dist/src/components/static-helpers/bytes-encoding.js.map +1 -1
- package/dist/src/components/static-helpers/interfaces.d.ts.map +1 -1
- package/dist/src/components/static-helpers/interfaces.js.map +1 -1
- package/dist/src/components/static-helpers/multipart-helpers.d.ts.map +1 -1
- package/dist/src/components/static-helpers/multipart-helpers.js +4 -4
- 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 +66 -57
- package/dist/src/components/static-helpers/rest-error.js.map +1 -1
- package/dist/src/components/transforms/content-type-encoding-provider.d.ts.map +1 -1
- package/dist/src/components/transforms/content-type-encoding-provider.js +0 -1
- package/dist/src/components/transforms/content-type-encoding-provider.js.map +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.map +1 -1
- package/dist/src/components/transforms/json/json-model-additional-properties-transform.d.ts.map +1 -1
- package/dist/src/components/transforms/json/json-model-additional-properties-transform.js +4 -4
- package/dist/src/components/transforms/json/json-model-additional-properties-transform.js.map +1 -1
- package/dist/src/components/transforms/json/json-model-base-transform.d.ts.map +1 -1
- package/dist/src/components/transforms/json/json-model-base-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.map +1 -1
- package/dist/src/components/transforms/json/json-model-transform.d.ts.map +1 -1
- package/dist/src/components/transforms/json/json-model-transform.js +20 -17
- package/dist/src/components/transforms/json/json-model-transform.js.map +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.map +1 -1
- package/dist/src/components/transforms/json/json-transform-discriminator.d.ts.map +1 -1
- package/dist/src/components/transforms/json/json-transform-discriminator.js +3 -3
- package/dist/src/components/transforms/json/json-transform-discriminator.js.map +1 -1
- package/dist/src/components/transforms/json/json-transform.d.ts.map +1 -1
- package/dist/src/components/transforms/json/json-transform.js.map +1 -1
- package/dist/src/components/transforms/json/union-transform.d.ts.map +1 -1
- package/dist/src/components/transforms/json/union-transform.js +2 -2
- package/dist/src/components/transforms/json/union-transform.js.map +1 -1
- package/dist/src/components/transforms/multipart/array-part-transform.d.ts.map +1 -1
- package/dist/src/components/transforms/multipart/array-part-transform.js.map +1 -1
- package/dist/src/components/transforms/multipart/file-part-transform.js +1 -1
- package/dist/src/components/transforms/multipart/file-part-transform.js.map +1 -1
- package/dist/src/components/transforms/multipart/multipart-transform.d.ts.map +1 -1
- package/dist/src/components/transforms/multipart/multipart-transform.js +9 -6
- package/dist/src/components/transforms/multipart/multipart-transform.js.map +1 -1
- package/dist/src/components/transforms/multipart/simple-part-transform.d.ts.map +1 -1
- package/dist/src/components/transforms/multipart/simple-part-transform.js +1 -1
- package/dist/src/components/transforms/multipart/simple-part-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 -2
- package/dist/src/components/transforms/operation-transform-declaration.js.map +1 -1
- package/dist/src/components/transforms/operation-transform-expression.d.ts.map +1 -1
- package/dist/src/components/transforms/operation-transform-expression.js.map +1 -1
- package/dist/src/components/utils/url-template.d.ts.map +1 -1
- package/dist/src/components/utils/url-template.js.map +1 -1
- package/dist/src/emitter.d.ts.map +1 -1
- package/dist/src/emitter.js +6 -6
- package/dist/src/emitter.js.map +1 -1
- package/dist/src/utils/operations.d.ts +13 -1
- package/dist/src/utils/operations.d.ts.map +1 -1
- package/dist/src/utils/operations.js +71 -0
- package/dist/src/utils/operations.js.map +1 -1
- package/dist/src/utils/parameters.d.ts.map +1 -1
- package/dist/src/utils/parameters.js +1 -1
- package/dist/src/utils/parameters.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +12 -12
- package/src/components/client-context/client-context-declaration.tsx +8 -1
- package/src/components/client-context/client-context-factory.tsx +50 -30
- package/src/components/client-context/client-context-options.tsx +13 -6
- package/src/components/client-context/client-context.tsx +5 -3
- package/src/components/client-context/parametrized-endpoint.tsx +26 -15
- package/src/components/client-directory.tsx +26 -16
- package/src/components/client-operation.tsx +24 -12
- package/src/components/client.tsx +77 -39
- package/src/components/encoding-provider.tsx +1 -3
- package/src/components/http-request-options.tsx +24 -14
- package/src/components/http-request-parameters-expression.tsx +34 -37
- package/src/components/http-request.tsx +26 -16
- package/src/components/http-response.tsx +31 -26
- package/src/components/models.tsx +18 -14
- package/src/components/operation-options.tsx +18 -5
- package/src/components/output.tsx +10 -9
- package/src/components/serializers.tsx +9 -5
- package/src/components/static-helpers/bytes-encoding.tsx +26 -8
- package/src/components/static-helpers/interfaces.tsx +14 -9
- package/src/components/static-helpers/multipart-helpers.tsx +34 -13
- package/src/components/static-helpers/rest-error.tsx +56 -18
- package/src/components/transforms/content-type-encoding-provider.tsx +1 -3
- package/src/components/transforms/json/json-array-transform.tsx +6 -4
- package/src/components/transforms/json/json-model-additional-properties-transform.tsx +18 -13
- package/src/components/transforms/json/json-model-base-transform.tsx +6 -1
- package/src/components/transforms/json/json-model-property-transform.tsx +7 -9
- package/src/components/transforms/json/json-model-transform.tsx +44 -16
- package/src/components/transforms/json/json-record-transform.tsx +5 -3
- package/src/components/transforms/json/json-transform-discriminator.tsx +12 -11
- package/src/components/transforms/json/json-transform.tsx +5 -3
- package/src/components/transforms/json/union-transform.tsx +20 -8
- package/src/components/transforms/multipart/array-part-transform.tsx +6 -3
- package/src/components/transforms/multipart/file-part-transform.tsx +1 -1
- package/src/components/transforms/multipart/multipart-transform.tsx +4 -6
- package/src/components/transforms/multipart/part-transform.tsx +1 -1
- package/src/components/transforms/multipart/simple-part-transform.tsx +7 -10
- package/src/components/transforms/operation-transform-declaration.tsx +24 -7
- package/src/components/transforms/operation-transform-expression.tsx +12 -5
- package/src/components/utils/url-template.tsx +10 -6
- package/src/emitter.tsx +4 -3
- package/src/utils/operations.ts +96 -1
- package/src/utils/parameters.tsx +5 -1
- package/test/scenarios/additional-properties/extends.md +0 -4
- package/test/scenarios/additional-properties/is.md +0 -4
- package/test/scenarios/additional-properties/spread.md +0 -4
- package/test/scenarios/auth/basic_auth.md +0 -1
- package/test/scenarios/auth/bearer.md +0 -1
- package/test/scenarios/auth/client_parameters.md +0 -1
- package/test/scenarios/auth/key_credential.md +0 -1
- package/test/scenarios/auth/sub_client_override.md +0 -2
- package/test/scenarios/client/dotted_namespace.md +0 -1
- package/test/scenarios/client/global_namespace.md +0 -5
- package/test/scenarios/client/multiple_top_level_clients.md +0 -2
- package/test/scenarios/client/nested_client.md +0 -2
- package/test/scenarios/encoding/bytes_body.md +0 -4
- package/test/scenarios/encoding/bytes_json_property.md +0 -5
- package/test/scenarios/encoding/bytes_nullable.md +0 -14
- package/test/scenarios/encoding/header_bytes.md +0 -12
- package/test/scenarios/encoding/header_date.md +0 -20
- package/test/scenarios/encoding/model_with_bytes_property.md +0 -5
- package/test/scenarios/encoding/query_bytes.md +0 -4
- package/test/scenarios/encoding/query_date.md +0 -20
- package/test/scenarios/http-operations/basic-request.md +0 -12
- package/test/scenarios/http-operations/basic-response.md +0 -19
- package/test/scenarios/http-operations/basic.md +0 -6
- package/test/scenarios/http-operations/constant-header.md +0 -4
- package/test/scenarios/http-operations/create-operation.md +0 -4
- package/test/scenarios/http-operations/on_response.md +0 -4
- package/test/scenarios/http-operations/optional-request-body.md +0 -5
- package/test/scenarios/http-operations/path-parameter-in-model.md +0 -4
- package/test/scenarios/http-operations/path-parameter.md +0 -4
- package/test/scenarios/http-operations/scalar-payload.md +0 -4
- package/test/scenarios/http-operations/with-parameters.md +0 -5
- package/test/scenarios/models/inheritance_2_discriminators.md +0 -24
- package/test/scenarios/models/inheritance_discriminator.md +0 -4
- package/test/scenarios/models/inline-models.md +1 -4
- package/test/scenarios/models/model_additional_properties.md +0 -4
- package/test/scenarios/models/model_extends.md +0 -4
- package/test/scenarios/models/model_spread.md +0 -2
- package/test/scenarios/models/nested_property.md +0 -4
- package/test/scenarios/models/property_references_property.md +0 -6
- package/test/scenarios/multipart/anonymous_part.md +1 -10
- package/test/scenarios/multipart/file.md +0 -12
- package/test/scenarios/multipart/file_content_type.md +0 -5
- package/test/scenarios/multipart/simple_part.md +0 -4
- package/test/scenarios/operation-parameters/body_root_anonymous.md +2 -17
- package/test/scenarios/operation-parameters/constant_as_optional.md +0 -5
- package/test/scenarios/operation-parameters/default_value_as_optional.md +0 -5
- package/test/scenarios/operation-parameters/no_content_type.md +0 -5
- package/test/scenarios/operation-parameters/no_parameters.md +0 -5
- package/test/scenarios/operation-parameters/only_optional.md +0 -5
- package/test/scenarios/operation-parameters/only_required.md +0 -5
- package/test/scenarios/operation-parameters/reserved_names.md +0 -5
- package/test/scenarios/operation-parameters/spread_body.md +0 -5
- package/test/scenarios/operation-parameters/spread_with_nested.md +0 -5
- package/test/scenarios/operation-parameters/union_body.md +0 -6
- package/test/scenarios/operation-parameters/with_body_property.md +0 -5
- package/test/scenarios/operation-parameters/with_body_root.md +0 -5
- package/test/scenarios/serializers/arrays.md +0 -8
- package/test/scenarios/serializers/basic_model.md +0 -4
- package/test/scenarios/serializers/basic_model_wire_name.md +0 -2
- package/test/scenarios/serializers/discriminated_union_spread.md +0 -4
- package/test/scenarios/serializers/multipart.md +0 -4
- package/test/scenarios/serializers/polymorphic_single_level_inheritance.md +0 -5
- package/test/scenarios/serializers/record.md +0 -4
- package/test/scenarios/serializers/scalars.md +0 -5
- package/test/scenarios/serializers/spread.md +0 -4
- package/test/scenarios/serializers/string_union.md +0 -4
|
@@ -26,7 +26,7 @@ export function JsonArrayTransform(props: JsonArrayTransformProps) {
|
|
|
26
26
|
const _transformedArray = [];
|
|
27
27
|
|
|
28
28
|
for (const item of ${props.itemRef} ?? []) {
|
|
29
|
-
const transformedItem = ${<JsonTransform type={elementType} target={props.target} itemRef="item as any" />};
|
|
29
|
+
const transformedItem = ${(<JsonTransform type={elementType} target={props.target} itemRef="item as any" />)};
|
|
30
30
|
_transformedArray.push(transformedItem);
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -61,7 +61,7 @@ export function JsonArrayTransformDeclaration(props: JsonArrayTransformDeclarati
|
|
|
61
61
|
"function",
|
|
62
62
|
);
|
|
63
63
|
|
|
64
|
-
const itemType = ay.code`Array<${<ef.TypeExpression type={elementType} />}>`;
|
|
64
|
+
const itemType = ay.code`Array<${(<ef.TypeExpression type={elementType} />)}>`;
|
|
65
65
|
const returnType = props.target === "transport" ? "any" : itemType;
|
|
66
66
|
const inputType = props.target === "transport" ? <>{itemType} | null</> : "any";
|
|
67
67
|
const inputRef = ay.refkey();
|
|
@@ -71,7 +71,8 @@ export function JsonArrayTransformDeclaration(props: JsonArrayTransformDeclarati
|
|
|
71
71
|
};
|
|
72
72
|
|
|
73
73
|
const declarationRefkey = getJsonArrayTransformRefkey(props.type, props.target);
|
|
74
|
-
return
|
|
74
|
+
return (
|
|
75
|
+
<ts.FunctionDeclaration
|
|
75
76
|
name={transformName}
|
|
76
77
|
export
|
|
77
78
|
returnType={returnType}
|
|
@@ -79,5 +80,6 @@ export function JsonArrayTransformDeclaration(props: JsonArrayTransformDeclarati
|
|
|
79
80
|
refkey={declarationRefkey}
|
|
80
81
|
>
|
|
81
82
|
<JsonArrayTransform {...props} itemRef={inputRef} />
|
|
82
|
-
</ts.FunctionDeclaration
|
|
83
|
+
</ts.FunctionDeclaration>
|
|
84
|
+
);
|
|
83
85
|
}
|
|
@@ -19,10 +19,14 @@ export function JsonAdditionalPropertiesTransform(props: JsonAdditionalPropertie
|
|
|
19
19
|
|
|
20
20
|
if (props.target === "application") {
|
|
21
21
|
const properties = $.model.getProperties(props.type, { includeExtended: true });
|
|
22
|
-
const destructuredProperties = ay.mapJoin(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
const destructuredProperties = ay.mapJoin(
|
|
23
|
+
() => properties,
|
|
24
|
+
(name) => name,
|
|
25
|
+
{
|
|
26
|
+
joiner: ",",
|
|
27
|
+
ender: ",",
|
|
28
|
+
},
|
|
29
|
+
);
|
|
26
30
|
|
|
27
31
|
// Inline destructuring that extracts the properties and passes the rest to jsonRecordUnknownToApplicationTransform_2
|
|
28
32
|
const inlineDestructure = ay.code`
|
|
@@ -31,17 +35,18 @@ export function JsonAdditionalPropertiesTransform(props: JsonAdditionalPropertie
|
|
|
31
35
|
),
|
|
32
36
|
`;
|
|
33
37
|
|
|
34
|
-
return
|
|
35
|
-
|
|
36
|
-
{inlineDestructure}
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
return (
|
|
39
|
+
<>
|
|
40
|
+
<ts.ObjectProperty name="additionalProperties">{inlineDestructure}</ts.ObjectProperty>
|
|
41
|
+
</>
|
|
42
|
+
);
|
|
39
43
|
}
|
|
40
44
|
|
|
41
45
|
const itemRef = ay.code`${props.itemRef}.additionalProperties`;
|
|
42
46
|
|
|
43
|
-
return
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
return (
|
|
48
|
+
<>
|
|
49
|
+
...({getJsonRecordTransformRefkey(additionalProperties, props.target)}({itemRef}) ),
|
|
50
|
+
</>
|
|
51
|
+
);
|
|
47
52
|
}
|
|
@@ -15,5 +15,10 @@ export function JsonModelBaseTransform(props: JsonModelBaseTransformProps) {
|
|
|
15
15
|
return null;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
return
|
|
18
|
+
return (
|
|
19
|
+
<>
|
|
20
|
+
...
|
|
21
|
+
<JsonTransform {...props} type={baseModel} />,
|
|
22
|
+
</>
|
|
23
|
+
);
|
|
19
24
|
}
|
|
@@ -13,7 +13,7 @@ export interface JsonModelPropertyTransformProps {
|
|
|
13
13
|
target: "transport" | "application";
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export function JsonModelPropertyTransform(props: JsonModelPropertyTransformProps)
|
|
16
|
+
export function JsonModelPropertyTransform(props: JsonModelPropertyTransformProps) {
|
|
17
17
|
const transformNamer = useTransformNamePolicy();
|
|
18
18
|
const propertyValueType = unpackProperty(props.type);
|
|
19
19
|
|
|
@@ -26,15 +26,13 @@ export function JsonModelPropertyTransform(props: JsonModelPropertyTransformProp
|
|
|
26
26
|
let propertyValue: ay.Children;
|
|
27
27
|
|
|
28
28
|
if ($.scalar.is(propertyValueType)) {
|
|
29
|
-
propertyValue =
|
|
30
|
-
<ScalarDataTransform
|
|
31
|
-
|
|
32
|
-
target={props.target}
|
|
33
|
-
itemRef={propertyValueRef}
|
|
34
|
-
/>;
|
|
29
|
+
propertyValue = (
|
|
30
|
+
<ScalarDataTransform type={props.type} target={props.target} itemRef={propertyValueRef} />
|
|
31
|
+
);
|
|
35
32
|
} else {
|
|
36
|
-
propertyValue =
|
|
37
|
-
<JsonTransform type={propertyValueType} target={props.target} itemRef={propertyValueRef}
|
|
33
|
+
propertyValue = (
|
|
34
|
+
<JsonTransform type={propertyValueType} target={props.target} itemRef={propertyValueRef} />
|
|
35
|
+
);
|
|
38
36
|
}
|
|
39
37
|
|
|
40
38
|
return <ts.ObjectProperty name={JSON.stringify(targetName)} value={propertyValue} />;
|
|
@@ -27,13 +27,31 @@ export function JsonModelTransform(props: JsonModelTransformProps) {
|
|
|
27
27
|
|
|
28
28
|
const discriminate = getJsonTransformDiscriminatorRefkey(props.type, props.target);
|
|
29
29
|
|
|
30
|
-
return
|
|
31
|
-
<
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
return (
|
|
31
|
+
<ts.ObjectExpression>
|
|
32
|
+
<JsonAdditionalPropertiesTransform
|
|
33
|
+
itemRef={props.itemRef}
|
|
34
|
+
target={props.target}
|
|
35
|
+
type={props.type}
|
|
36
|
+
/>
|
|
37
|
+
{discriminator ? (
|
|
38
|
+
<>
|
|
39
|
+
...{discriminate}({props.itemRef}),
|
|
40
|
+
</>
|
|
41
|
+
) : null}
|
|
42
|
+
<ay.For each={properties} joiner="," line>
|
|
43
|
+
{(property) => {
|
|
44
|
+
return (
|
|
45
|
+
<JsonModelPropertyTransform
|
|
46
|
+
itemRef={props.itemRef}
|
|
47
|
+
type={property}
|
|
48
|
+
target={props.target}
|
|
49
|
+
/>
|
|
50
|
+
);
|
|
51
|
+
}}
|
|
52
|
+
</ay.For>
|
|
53
|
+
</ts.ObjectExpression>
|
|
54
|
+
);
|
|
37
55
|
}
|
|
38
56
|
|
|
39
57
|
export function getJsonModelTransformRefkey(
|
|
@@ -70,18 +88,28 @@ export function JsonModelTransformDeclaration(
|
|
|
70
88
|
const hasAdditionalProperties = spread && $.model.is(spread) && $.record.is(spread);
|
|
71
89
|
|
|
72
90
|
const declarationRefkey = getJsonModelTransformRefkey(props.type, props.target);
|
|
73
|
-
return
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
{
|
|
91
|
+
return (
|
|
92
|
+
<>
|
|
93
|
+
{hasAdditionalProperties ? (
|
|
94
|
+
<JsonRecordTransformDeclaration target={props.target} type={spread} />
|
|
95
|
+
) : null}
|
|
96
|
+
<JsonTransformDiscriminatorDeclaration type={props.type} target={props.target} />
|
|
97
|
+
<ts.FunctionDeclaration
|
|
98
|
+
name={transformName}
|
|
99
|
+
export
|
|
100
|
+
returnType={returnType}
|
|
101
|
+
parameters={parameters}
|
|
102
|
+
refkey={declarationRefkey}
|
|
103
|
+
>
|
|
104
|
+
{ay.code`
|
|
79
105
|
if(!${inputRef}) {
|
|
80
106
|
return ${inputRef} as any;
|
|
81
107
|
}
|
|
82
108
|
|
|
83
109
|
`}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
110
|
+
return <JsonModelTransform {...props} itemRef={inputRef} />
|
|
111
|
+
!;
|
|
112
|
+
</ts.FunctionDeclaration>
|
|
113
|
+
</>
|
|
114
|
+
);
|
|
87
115
|
}
|
|
@@ -27,7 +27,7 @@ export function JsonRecordTransform(props: JsonRecordTransformProps) {
|
|
|
27
27
|
const _transformedRecord: any = {};
|
|
28
28
|
|
|
29
29
|
for (const [key, value] of Object.entries(${props.itemRef} ?? {})) {
|
|
30
|
-
const transformedItem = ${<JsonTransform type={elementType} target={props.target} itemRef="value as any" />};
|
|
30
|
+
const transformedItem = ${(<JsonTransform type={elementType} target={props.target} itemRef="value as any" />)};
|
|
31
31
|
_transformedRecord[key] = transformedItem;
|
|
32
32
|
}
|
|
33
33
|
|
|
@@ -72,7 +72,8 @@ export function JsonRecordTransformDeclaration(props: JsonRecordTransformDeclara
|
|
|
72
72
|
};
|
|
73
73
|
|
|
74
74
|
const declarationRefkey = getJsonRecordTransformRefkey(props.type, props.target);
|
|
75
|
-
return
|
|
75
|
+
return (
|
|
76
|
+
<ts.FunctionDeclaration
|
|
76
77
|
name={transformName}
|
|
77
78
|
export
|
|
78
79
|
returnType={returnType}
|
|
@@ -80,5 +81,6 @@ export function JsonRecordTransformDeclaration(props: JsonRecordTransformDeclara
|
|
|
80
81
|
refkey={declarationRefkey}
|
|
81
82
|
>
|
|
82
83
|
<JsonRecordTransform {...props} itemRef={inputRef} />
|
|
83
|
-
</ts.FunctionDeclaration
|
|
84
|
+
</ts.FunctionDeclaration>
|
|
85
|
+
);
|
|
84
86
|
}
|
|
@@ -42,24 +42,23 @@ export function JsonTransformDiscriminator(props: JsonTransformDiscriminatorProp
|
|
|
42
42
|
// type in the discriminator are compatible.
|
|
43
43
|
const itemRef = ay.code`${props.itemRef} as any`;
|
|
44
44
|
const discriminatingCases = ay.mapJoin(
|
|
45
|
-
discriminatedUnion.variants,
|
|
45
|
+
() => discriminatedUnion.variants,
|
|
46
46
|
(name, variant) => {
|
|
47
47
|
return ay.code`
|
|
48
48
|
if( discriminatorValue === ${JSON.stringify(name)}) {
|
|
49
|
-
return ${<JsonTransform type={variant} target={props.target} itemRef={itemRef} />}!
|
|
49
|
+
return ${(<JsonTransform type={variant} target={props.target} itemRef={itemRef} />)}!
|
|
50
50
|
}
|
|
51
51
|
`;
|
|
52
52
|
},
|
|
53
53
|
{ joiner: "\n\n" },
|
|
54
54
|
);
|
|
55
55
|
|
|
56
|
-
return
|
|
57
|
-
|
|
58
|
-
{discriminatingCases}
|
|
59
|
-
<>
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
</>;
|
|
56
|
+
return (
|
|
57
|
+
<>
|
|
58
|
+
const discriminatorValue = {discriminatorRef};{discriminatingCases}
|
|
59
|
+
<>console.warn(`Received unknown kind: ` + discriminatorValue); return {itemRef}</>
|
|
60
|
+
</>
|
|
61
|
+
);
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
export function getJsonTransformDiscriminatorRefkey(
|
|
@@ -97,7 +96,8 @@ export function JsonTransformDiscriminatorDeclaration(
|
|
|
97
96
|
input_: { type: inputType, refkey: inputRef, optional: true },
|
|
98
97
|
};
|
|
99
98
|
|
|
100
|
-
return
|
|
99
|
+
return (
|
|
100
|
+
<ts.FunctionDeclaration
|
|
101
101
|
name={transformName}
|
|
102
102
|
export
|
|
103
103
|
returnType={returnType}
|
|
@@ -110,5 +110,6 @@ export function JsonTransformDiscriminatorDeclaration(
|
|
|
110
110
|
}
|
|
111
111
|
`}
|
|
112
112
|
<JsonTransformDiscriminator {...props} itemRef={inputRef} discriminator={discriminator} />
|
|
113
|
-
</ts.FunctionDeclaration
|
|
113
|
+
</ts.FunctionDeclaration>
|
|
114
|
+
);
|
|
114
115
|
}
|
|
@@ -53,10 +53,12 @@ export function JsonTransform(props: JsonTransformProps) {
|
|
|
53
53
|
case "Union":
|
|
54
54
|
return <JsonUnionTransform type={type} itemRef={props.itemRef} target={props.target} />;
|
|
55
55
|
case "ModelProperty": {
|
|
56
|
-
return
|
|
56
|
+
return (
|
|
57
|
+
<JsonModelPropertyTransform type={type} itemRef={props.itemRef} target={props.target} />
|
|
58
|
+
);
|
|
57
59
|
}
|
|
58
60
|
case "Scalar": {
|
|
59
|
-
return <ScalarDataTransform type={type} itemRef={props.itemRef} target={props.target}/>;
|
|
61
|
+
return <ScalarDataTransform type={type} itemRef={props.itemRef} target={props.target} />;
|
|
60
62
|
}
|
|
61
63
|
default:
|
|
62
64
|
return props.itemRef;
|
|
@@ -79,7 +81,7 @@ export function JsonTransformDeclaration(props: JsonTransformDeclarationProps) {
|
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
if ($.record.is(props.type)) {
|
|
82
|
-
return <JsonRecordTransformDeclaration target={props.target} type={props.type}
|
|
84
|
+
return <JsonRecordTransformDeclaration target={props.target} type={props.type} />;
|
|
83
85
|
}
|
|
84
86
|
|
|
85
87
|
return <JsonModelTransformDeclaration type={props.type} target={props.target} />;
|
|
@@ -18,7 +18,11 @@ export function JsonUnionTransform(props: JsonUnionTransformProps) {
|
|
|
18
18
|
const discriminator = $.type.getDiscriminator(props.type);
|
|
19
19
|
if (discriminator) {
|
|
20
20
|
// return <JsonTransformDiscriminator {...props} discriminator={discriminator}/>;
|
|
21
|
-
return
|
|
21
|
+
return (
|
|
22
|
+
<>
|
|
23
|
+
{getJsonTransformDiscriminatorRefkey(props.type, props.target)}({props.itemRef})
|
|
24
|
+
</>
|
|
25
|
+
);
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
const variantType = props.type.variants.values().next().value!.type;
|
|
@@ -59,15 +63,23 @@ export function JsonUnionTransformDeclaration(props: JsonUnionTransformDeclarati
|
|
|
59
63
|
};
|
|
60
64
|
|
|
61
65
|
const declarationRefkey = getJsonUnionTransformRefkey(props.type, props.target);
|
|
62
|
-
return
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
return (
|
|
67
|
+
<>
|
|
68
|
+
<JsonTransformDiscriminatorDeclaration target={props.target} type={props.type} />
|
|
69
|
+
<ts.FunctionDeclaration
|
|
70
|
+
name={transformName}
|
|
71
|
+
export
|
|
72
|
+
returnType={returnType}
|
|
73
|
+
parameters={parameters}
|
|
74
|
+
refkey={declarationRefkey}
|
|
75
|
+
>
|
|
76
|
+
{ay.code`
|
|
66
77
|
if(!${inputRef}) {
|
|
67
78
|
return ${inputRef} as any;
|
|
68
79
|
}
|
|
69
80
|
`}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
81
|
+
return <JsonUnionTransform {...props} itemRef={inputRef} />
|
|
82
|
+
</ts.FunctionDeclaration>
|
|
83
|
+
</>
|
|
84
|
+
);
|
|
73
85
|
}
|
|
@@ -26,9 +26,12 @@ export function ArrayPartTransform(props: ArrayPartTransformProps) {
|
|
|
26
26
|
name: partElementName,
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
return
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
return (
|
|
30
|
+
<>
|
|
31
|
+
{inputExpression}.map({mapCallbackSignature}{" "}
|
|
32
|
+
{ay.code`=> (${(<HttpPartTransform itemRef={null} part={partElement} />)})`} )
|
|
33
|
+
</>
|
|
34
|
+
);
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
function getPartRef(itemRef: ay.Children, partName: string) {
|
|
@@ -17,7 +17,7 @@ export function FilePartTransform(props: FilePartTransformProps) {
|
|
|
17
17
|
if (defaultContentType) {
|
|
18
18
|
args.push(ts.ValueExpression({ jsValue: defaultContentType }));
|
|
19
19
|
}
|
|
20
|
-
return <ts.FunctionCallExpression
|
|
20
|
+
return <ts.FunctionCallExpression target={getCreateFilePartDescriptorReference()} args={args} />;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
function getContentType(part: HttpOperationPart) {
|
|
@@ -20,12 +20,10 @@ export function MultipartTransform(props: MultipartTransformProps) {
|
|
|
20
20
|
|
|
21
21
|
const itemRef = transportNamer.getApplicationName(props.body.property);
|
|
22
22
|
|
|
23
|
-
const partTransform =
|
|
24
|
-
httpParts
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
joiner: ",\n",
|
|
28
|
-
},
|
|
23
|
+
const partTransform = (
|
|
24
|
+
<ay.For each={httpParts} comma line>
|
|
25
|
+
{(part) => <HttpPartTransform part={part} itemRef={itemRef} />}
|
|
26
|
+
</ay.For>
|
|
29
27
|
);
|
|
30
28
|
|
|
31
29
|
return <>[{partTransform}]</>;
|
|
@@ -11,7 +11,7 @@ export interface HttpPartTransformProps {
|
|
|
11
11
|
|
|
12
12
|
export function HttpPartTransform(props: HttpPartTransformProps) {
|
|
13
13
|
if (props.part.multi) {
|
|
14
|
-
return <ArrayPartTransform part={props.part} itemRef={props.itemRef}/>;
|
|
14
|
+
return <ArrayPartTransform part={props.part} itemRef={props.itemRef} />;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
if (props.part.filename) {
|
|
@@ -25,19 +25,16 @@ export function SimplePartTransform(props: SimplePartTransformProps) {
|
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
return
|
|
28
|
+
return (
|
|
29
|
+
<ts.ObjectExpression>
|
|
29
30
|
<ts.ObjectProperty name="name" jsValue={partName} />,
|
|
30
31
|
<ts.ObjectProperty
|
|
31
32
|
name="body"
|
|
32
|
-
value={
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
/>
|
|
38
|
-
}
|
|
39
|
-
/>,
|
|
40
|
-
</ts.ObjectExpression>;
|
|
33
|
+
value={<JsonTransform itemRef={partRef} target="transport" type={props.part.body.type} />}
|
|
34
|
+
/>
|
|
35
|
+
,
|
|
36
|
+
</ts.ObjectExpression>
|
|
37
|
+
);
|
|
41
38
|
}
|
|
42
39
|
|
|
43
40
|
function getPartRef(itemRef: ay.Children, partName: string) {
|
|
@@ -18,9 +18,11 @@ export function getTransformDeclarationRef(operaion: ClientOperation) {
|
|
|
18
18
|
|
|
19
19
|
export function TransformDeclaration(props: TransformDeclarationProps) {
|
|
20
20
|
const refkey = props.refkey ?? getTransformDeclarationRef(props.operation);
|
|
21
|
-
return
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
return (
|
|
22
|
+
<>
|
|
23
|
+
<TransformToTransportDeclaration operation={props.operation} refkey={refkey} />
|
|
24
|
+
</>
|
|
25
|
+
);
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
export interface SingleBodyTransformDeclarationProps {
|
|
@@ -37,9 +39,17 @@ export function SingleBodyTransformDeclaration(props: SingleBodyTransformDeclara
|
|
|
37
39
|
refkey: inputRef,
|
|
38
40
|
};
|
|
39
41
|
|
|
40
|
-
return
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
return (
|
|
43
|
+
<ts.FunctionDeclaration
|
|
44
|
+
export
|
|
45
|
+
name={props.name}
|
|
46
|
+
parameters={{ payload: payloadParameter }}
|
|
47
|
+
refkey={props.refkey}
|
|
48
|
+
>
|
|
49
|
+
return <JsonTransform itemRef={inputRef} target="transport" type={props.payload.type} />
|
|
50
|
+
!;
|
|
51
|
+
</ts.FunctionDeclaration>
|
|
52
|
+
);
|
|
43
53
|
}
|
|
44
54
|
|
|
45
55
|
interface TransformToTransportDeclarationProps {
|
|
@@ -60,5 +70,12 @@ function TransformToTransportDeclaration(props: TransformToTransportDeclarationP
|
|
|
60
70
|
return null;
|
|
61
71
|
}
|
|
62
72
|
|
|
63
|
-
return
|
|
73
|
+
return (
|
|
74
|
+
<SingleBodyTransformDeclaration
|
|
75
|
+
operation={props.operation}
|
|
76
|
+
payload={requestPayload}
|
|
77
|
+
refkey={props.refkey}
|
|
78
|
+
name={name}
|
|
79
|
+
/>
|
|
80
|
+
);
|
|
64
81
|
}
|
|
@@ -30,11 +30,18 @@ export function OperationTransformExpression(props: OperationTransformToTranspor
|
|
|
30
30
|
// And multiple content types
|
|
31
31
|
const contentType = body.contentTypes[0];
|
|
32
32
|
const payloadType = body.type;
|
|
33
|
-
return
|
|
34
|
-
<
|
|
35
|
-
<
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
return (
|
|
34
|
+
<ContentTypeEncodingProvider contentType={contentType}>
|
|
35
|
+
<TransformNamePolicyContext.Provider
|
|
36
|
+
value={{
|
|
37
|
+
getTransportName: defaultTransportNameGetter,
|
|
38
|
+
getApplicationName: payloadApplicationNameGetter,
|
|
39
|
+
}}
|
|
40
|
+
>
|
|
41
|
+
<JsonTransform itemRef={itemRef} target="transport" type={payloadType} />
|
|
42
|
+
</TransformNamePolicyContext.Provider>
|
|
43
|
+
</ContentTypeEncodingProvider>
|
|
44
|
+
);
|
|
38
45
|
}
|
|
39
46
|
|
|
40
47
|
function payloadApplicationNameGetter(type: HasName<Type>) {
|
|
@@ -11,11 +11,15 @@ export interface UrlTemplateProps {
|
|
|
11
11
|
export function UrlTemplate(props: UrlTemplateProps) {
|
|
12
12
|
const namer = useTransformNamePolicy();
|
|
13
13
|
const params = props.parameters.map((p) => {
|
|
14
|
-
return
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
return (
|
|
15
|
+
<ts.ObjectExpression>
|
|
16
|
+
"{namer.getTransportName(p)}": {namer.getApplicationName(p)}
|
|
17
|
+
</ts.ObjectExpression>
|
|
18
|
+
);
|
|
17
19
|
});
|
|
18
|
-
return
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
return (
|
|
21
|
+
<ts.VarDeclaration name="url" type="string">
|
|
22
|
+
{uriTemplateLib.parse}({JSON.stringify(props.template)}).expand({params})
|
|
23
|
+
</ts.VarDeclaration>
|
|
24
|
+
);
|
|
21
25
|
}
|
package/src/emitter.tsx
CHANGED
|
@@ -18,13 +18,13 @@ import { JsClientEmitterOptions } from "./lib.js";
|
|
|
18
18
|
*/
|
|
19
19
|
export async function $onEmit(context: EmitContext<JsClientEmitterOptions>) {
|
|
20
20
|
const packageName = context.options["package-name"] ?? "test-package";
|
|
21
|
-
const output =
|
|
21
|
+
const output = (
|
|
22
22
|
<Output>
|
|
23
23
|
<ts.PackageDirectory
|
|
24
24
|
name={packageName}
|
|
25
25
|
version="1.0.0"
|
|
26
26
|
path="."
|
|
27
|
-
scripts={{
|
|
27
|
+
scripts={{ build: "tsc" }}
|
|
28
28
|
devDependencies={{ "@types/node": "~18.19.75" }}
|
|
29
29
|
>
|
|
30
30
|
<ay.SourceDirectory path="src">
|
|
@@ -47,7 +47,8 @@ export async function $onEmit(context: EmitContext<JsClientEmitterOptions>) {
|
|
|
47
47
|
</ay.SourceDirectory>
|
|
48
48
|
</ay.SourceDirectory>
|
|
49
49
|
</ts.PackageDirectory>
|
|
50
|
-
</Output
|
|
50
|
+
</Output>
|
|
51
|
+
);
|
|
51
52
|
|
|
52
53
|
await writeOutput(output, context.emitterOutputDir);
|
|
53
54
|
}
|
package/src/utils/operations.ts
CHANGED
|
@@ -1,6 +1,101 @@
|
|
|
1
|
-
import { ModelProperty } from "@typespec/compiler";
|
|
1
|
+
import { Model, ModelProperty, Operation, Union } from "@typespec/compiler";
|
|
2
|
+
import {
|
|
3
|
+
unsafe_mutateSubgraph as mutateSubgraph,
|
|
4
|
+
unsafe_Mutator as Mutator,
|
|
5
|
+
unsafe_MutatorFlow as MutatorFlow,
|
|
6
|
+
unsafe_MutatorRecord,
|
|
7
|
+
} from "@typespec/compiler/experimental";
|
|
2
8
|
import { $ } from "@typespec/compiler/experimental/typekit";
|
|
3
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Prepares operation for client representation. This includes adding an options bag for optional parameters.
|
|
12
|
+
* @param operation operation to be prepared
|
|
13
|
+
* @returns the prepared operation
|
|
14
|
+
*/
|
|
15
|
+
export function prepareOperation(operation: Operation): Operation {
|
|
16
|
+
return mutateSubgraph($.program, [httpParamsMutator], operation).type as Operation;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Mutates anonymous types to be named.
|
|
21
|
+
*/
|
|
22
|
+
const anonymousMutatorRecord: unsafe_MutatorRecord<Model | Union> = {
|
|
23
|
+
filter(t) {
|
|
24
|
+
return MutatorFlow.MutateAndRecur;
|
|
25
|
+
},
|
|
26
|
+
mutate(t, clone) {
|
|
27
|
+
if (!clone.name) {
|
|
28
|
+
clone.name = $.type.getPlausibleName(clone);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const anonymousMutator: Mutator = {
|
|
34
|
+
name: "Anonymous types",
|
|
35
|
+
Model: anonymousMutatorRecord,
|
|
36
|
+
Union: anonymousMutatorRecord,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Mutates the operation so that the parameters model is split into required and optional parameters.
|
|
41
|
+
*/
|
|
42
|
+
export const httpParamsMutator: Mutator = {
|
|
43
|
+
name: "Http parameters",
|
|
44
|
+
Operation: {
|
|
45
|
+
filter() {
|
|
46
|
+
return MutatorFlow.DoNotRecur;
|
|
47
|
+
},
|
|
48
|
+
mutate(o, clone, _program, realm) {
|
|
49
|
+
const httpOperation = $.httpOperation.get(o);
|
|
50
|
+
const returnType = $.httpOperation.getReturnType(httpOperation);
|
|
51
|
+
clone.returnType = returnType;
|
|
52
|
+
const params = $.httpRequest.getParameters(httpOperation, [
|
|
53
|
+
"query",
|
|
54
|
+
"header",
|
|
55
|
+
"path",
|
|
56
|
+
"body",
|
|
57
|
+
"contentType",
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
if (!params) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
clone.parameters = params;
|
|
65
|
+
|
|
66
|
+
const optionals = [...clone.parameters.properties.values()]
|
|
67
|
+
.filter((p) => p.optional)
|
|
68
|
+
.reduce(
|
|
69
|
+
(acc, prop) => {
|
|
70
|
+
acc[prop.name] = prop;
|
|
71
|
+
return acc;
|
|
72
|
+
},
|
|
73
|
+
{} as Record<string, ModelProperty>,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const optionsBag = $.model.create({
|
|
77
|
+
properties: optionals,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const optionsProp = $.modelProperty.create({
|
|
81
|
+
name: "options",
|
|
82
|
+
type: optionsBag,
|
|
83
|
+
optional: true,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
for (const [key, prop] of clone.parameters.properties) {
|
|
87
|
+
if (prop.optional || isConstantHeader(prop)) {
|
|
88
|
+
clone.parameters.properties.delete(key);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (Object.keys(optionals).length > 0) {
|
|
93
|
+
clone.parameters.properties.set("options", optionsProp);
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
|
|
4
99
|
export function isConstantHeader(modelProperty: ModelProperty) {
|
|
5
100
|
if (!$.modelProperty.isHttpHeader(modelProperty)) {
|
|
6
101
|
return false;
|