@typespec/http-client-js 0.16.1 → 0.16.2-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/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -13
- package/.github/copilot-instructions.md +0 -180
- package/.testignore +0 -19
- package/.turbo/turbo-build.log +0 -3
- package/CHANGELOG.md +0 -159
- package/eng/scripts/calculate-coverage.ts +0 -33
- package/eng/scripts/emit-e2e.ts +0 -317
- package/eng/scripts/run-e2e-tests.ts +0 -166
- package/eng/scripts/tspconfig.yaml +0 -6
- package/eng/scripts/upload-spector-results.ts +0 -139
- package/src/components/client-context/client-context-declaration.tsx +0 -26
- package/src/components/client-context/client-context-factory.tsx +0 -218
- package/src/components/client-context/client-context-options.tsx +0 -36
- package/src/components/client-context/client-context.tsx +0 -23
- package/src/components/client-context/parametrized-endpoint.tsx +0 -51
- package/src/components/client-directory.tsx +0 -51
- package/src/components/client-operation.tsx +0 -102
- package/src/components/client.tsx +0 -177
- package/src/components/encoding-provider.tsx +0 -17
- package/src/components/external-packages/ts-http-runtime.ts +0 -28
- package/src/components/external-packages/uri-template.ts +0 -11
- package/src/components/http-request-options.tsx +0 -88
- package/src/components/http-request-parameters-expression.tsx +0 -191
- package/src/components/http-request.tsx +0 -80
- package/src/components/http-response.tsx +0 -74
- package/src/components/index.ts +0 -12
- package/src/components/models.tsx +0 -26
- package/src/components/operation-handlers/index.tsx +0 -2
- package/src/components/operation-handlers/operation-pipeline.tsx +0 -33
- package/src/components/operation-handlers/paging/page-item.tsx +0 -14
- package/src/components/operation-handlers/paging/page-response.tsx +0 -57
- package/src/components/operation-handlers/paging/page-settings.tsx +0 -53
- package/src/components/operation-handlers/paging/paginated-operation-handler.tsx +0 -200
- package/src/components/operation-handlers/paging/request-send.tsx +0 -85
- package/src/components/operation-handlers/paging/response-deserialize.tsx +0 -60
- package/src/components/operation-handlers/paging/util.ts +0 -70
- package/src/components/operation-handlers/types.tsx +0 -18
- package/src/components/operation-options.tsx +0 -42
- package/src/components/operation-parameters.tsx +0 -44
- package/src/components/output.tsx +0 -32
- package/src/components/serializers.tsx +0 -74
- package/src/components/static-helpers/bytes-encoding.tsx +0 -61
- package/src/components/static-helpers/interfaces.tsx +0 -31
- package/src/components/static-helpers/multipart-helpers.tsx +0 -80
- package/src/components/static-helpers/paging-helper.tsx +0 -202
- package/src/components/static-helpers/rest-error.tsx +0 -102
- package/src/components/testing/client-options.tsx +0 -30
- package/src/components/transforms/content-type-encoding-provider.tsx +0 -26
- package/src/components/transforms/data-transform.tsx +0 -40
- package/src/components/transforms/json/json-array-transform.tsx +0 -87
- package/src/components/transforms/json/json-model-additional-properties-transform.tsx +0 -54
- package/src/components/transforms/json/json-model-base-transform.tsx +0 -24
- package/src/components/transforms/json/json-model-property-transform.tsx +0 -53
- package/src/components/transforms/json/json-model-transform.tsx +0 -115
- package/src/components/transforms/json/json-record-transform.tsx +0 -88
- package/src/components/transforms/json/json-transform-discriminator.tsx +0 -111
- package/src/components/transforms/json/json-transform.tsx +0 -120
- package/src/components/transforms/json/union-transform.tsx +0 -87
- package/src/components/transforms/json/utils/transport-namer.ts +0 -12
- package/src/components/transforms/multipart/array-part-transform.tsx +0 -44
- package/src/components/transforms/multipart/file-part-transform.tsx +0 -45
- package/src/components/transforms/multipart/multipart-transform.tsx +0 -30
- package/src/components/transforms/multipart/part-transform.tsx +0 -22
- package/src/components/transforms/multipart/simple-part-transform.tsx +0 -42
- package/src/components/transforms/operation-transform-declaration.tsx +0 -83
- package/src/components/transforms/operation-transform-expression.tsx +0 -62
- package/src/components/transforms/scalar-transform.tsx +0 -301
- package/src/components/transforms/transform-name-policy.ts +0 -134
- package/src/components/utils/unpack-model-property.ts +0 -44
- package/src/components/utils/url-template.tsx +0 -25
- package/src/context/encoding/encoding-context.tsx +0 -28
- package/src/context/encoding/types.ts +0 -6
- package/src/emitter.tsx +0 -85
- package/src/index.ts +0 -5
- package/src/lib.ts +0 -120
- package/src/testing/index.ts +0 -11
- package/src/utils/client-discovery.ts +0 -28
- package/src/utils/multipart.ts +0 -29
- package/src/utils/operations.ts +0 -112
- package/src/utils/parameters.tsx +0 -155
- package/test/e2e/assets/image.jpg +0 -0
- package/test/e2e/assets/image.png +0 -0
- package/test/e2e/http/authentication/api-key/main.test.ts +0 -38
- package/test/e2e/http/authentication/oauth2/main.test.ts +0 -31
- package/test/e2e/http/authentication/union/main.test.ts +0 -26
- package/test/e2e/http/encode/bytes/main.test.ts +0 -191
- package/test/e2e/http/encode/datetime/main.test.ts +0 -176
- package/test/e2e/http/encode/duration/main.test.ts +0 -126
- package/test/e2e/http/encode/numeric/main.test.ts +0 -26
- package/test/e2e/http/parameters/basic/main.test.ts +0 -35
- package/test/e2e/http/parameters/body-optionality/main.test.ts +0 -43
- package/test/e2e/http/parameters/collection-format/main.test.ts +0 -40
- package/test/e2e/http/parameters/spread/main.test.ts +0 -56
- package/test/e2e/http/payload/content-negotiation/main.test.ts +0 -40
- package/test/e2e/http/payload/json-merge-patch/main.test.ts +0 -122
- package/test/e2e/http/payload/media-type/main.test.ts +0 -28
- package/test/e2e/http/payload/multipart/main.test.ts +0 -200
- package/test/e2e/http/payload/pageable/main.test.ts +0 -78
- package/test/e2e/http/payload/xml/main.test.ts +0 -287
- package/test/e2e/http/routes/main.test.ts +0 -284
- package/test/e2e/http/serialization/encoded-name/json/main.test.ts +0 -20
- package/test/e2e/http/server/endpoint/not-defined/main.test.ts +0 -11
- package/test/e2e/http/server/path/multiple/main.test.ts +0 -22
- package/test/e2e/http/server/path/single/main.test.ts +0 -11
- package/test/e2e/http/server/versions/not-versioned/main.test.ts +0 -22
- package/test/e2e/http/server/versions/versioned/main.test.ts +0 -28
- package/test/e2e/http/special-headers/conditional-request/main.test.ts +0 -35
- package/test/e2e/http/special-headers/repeatability/main.test.ts +0 -18
- package/test/e2e/http/special-words/main.test.ts +0 -434
- package/test/e2e/http/type/array/array.test.ts +0 -256
- package/test/e2e/http/type/dictionary/main.test.ts +0 -229
- package/test/e2e/http/type/enum/extensible/main.test.ts +0 -34
- package/test/e2e/http/type/enum/fixed/main.test.ts +0 -29
- package/test/e2e/http/type/model/empty/main.test.ts +0 -21
- package/test/e2e/http/type/model/inheritance/enum-discriminator/main.test.ts +0 -51
- package/test/e2e/http/type/model/inheritance/nested-discriminator/main.test.ts +0 -168
- package/test/e2e/http/type/model/inheritance/not-discriminated/main.test.ts +0 -42
- package/test/e2e/http/type/model/inheritance/recursive/main.test.ts +0 -46
- package/test/e2e/http/type/model/inheritance/single-discriminator/main.test.ts +0 -99
- package/test/e2e/http/type/model/visibility/main.test.ts +0 -64
- package/test/e2e/http/type/property/additional-properties/main.test.ts +0 -185
- package/test/e2e/http/type/property/additional-properties/spreads.test.ts +0 -448
- package/test/e2e/http/type/property/nullable/main.test.ts +0 -275
- package/test/e2e/http/type/property/optionality/main.test.ts +0 -256
- package/test/e2e/http/type/property/value-types/main.test.ts +0 -193
- package/test/e2e/http/type/scalar/main.test.ts +0 -133
- package/test/e2e/http/type/scalar/scalar.test.ts +0 -95
- package/test/e2e/http/type/union/main.test.ts +0 -207
- package/test/e2e/http/versioning/added/main.test.ts +0 -60
- package/test/e2e/http/versioning/madeOptional/main.test.ts +0 -34
- package/test/e2e/http/versioning/removed/main.test.ts +0 -75
- package/test/e2e/http/versioning/renamedFrom/main.test.ts +0 -52
- package/test/e2e/http/versioning/returnTypeChangedFrom/main.test.ts +0 -22
- package/test/e2e/http/versioning/typeChangedFrom/main.test.ts +0 -31
- package/test/scenarios/additional-properties/extends.md +0 -67
- package/test/scenarios/additional-properties/is.md +0 -42
- package/test/scenarios/additional-properties/spread.md +0 -68
- package/test/scenarios/auth/basic_auth.md +0 -68
- package/test/scenarios/auth/bearer.md +0 -68
- package/test/scenarios/auth/client_parameters.md +0 -78
- package/test/scenarios/auth/client_structure.md +0 -36
- package/test/scenarios/auth/key_credential.md +0 -69
- package/test/scenarios/auth/oauth2.md +0 -90
- package/test/scenarios/auth/sub_client_override.md +0 -114
- package/test/scenarios/client/client_context.md +0 -34
- package/test/scenarios/client/dotted_namespace.md +0 -27
- package/test/scenarios/client/global_namespace.md +0 -69
- package/test/scenarios/client/multiple_top_level_clients.md +0 -72
- package/test/scenarios/client/nested_client.md +0 -102
- package/test/scenarios/client/wrapping_namespace.md +0 -41
- package/test/scenarios/encoding/bytes_body.md +0 -44
- package/test/scenarios/encoding/bytes_json_property.md +0 -55
- package/test/scenarios/encoding/bytes_nullable.md +0 -139
- package/test/scenarios/encoding/header_bytes.md +0 -130
- package/test/scenarios/encoding/header_date.md +0 -216
- package/test/scenarios/encoding/model_with_bytes_property.md +0 -45
- package/test/scenarios/encoding/query_bytes.md +0 -42
- package/test/scenarios/encoding/query_date.md +0 -216
- package/test/scenarios/http-operations/basic-request.md +0 -144
- package/test/scenarios/http-operations/basic-response.md +0 -219
- package/test/scenarios/http-operations/basic.md +0 -128
- package/test/scenarios/http-operations/constant-header.md +0 -33
- package/test/scenarios/http-operations/create-operation.md +0 -58
- package/test/scenarios/http-operations/on_response.md +0 -52
- package/test/scenarios/http-operations/optional-request-body.md +0 -50
- package/test/scenarios/http-operations/paging.md +0 -242
- package/test/scenarios/http-operations/path-parameter-in-model.md +0 -53
- package/test/scenarios/http-operations/path-parameter.md +0 -48
- package/test/scenarios/http-operations/query-parameter.md +0 -41
- package/test/scenarios/http-operations/scalar-payload.md +0 -57
- package/test/scenarios/http-operations/with-parameters.md +0 -78
- package/test/scenarios/models/array-properties.md +0 -47
- package/test/scenarios/models/basic.md +0 -118
- package/test/scenarios/models/dictionary-properties.md +0 -44
- package/test/scenarios/models/inheritance_2_discriminators.md +0 -346
- package/test/scenarios/models/inheritance_discriminator.md +0 -88
- package/test/scenarios/models/inline-models.md +0 -29
- package/test/scenarios/models/model_additional_properties.md +0 -75
- package/test/scenarios/models/model_extends.md +0 -97
- package/test/scenarios/models/model_spread.md +0 -63
- package/test/scenarios/models/nested_property.md +0 -41
- package/test/scenarios/models/property_references_property.md +0 -150
- package/test/scenarios/multipart/anonymous_part.md +0 -53
- package/test/scenarios/multipart/file.md +0 -154
- package/test/scenarios/multipart/file_content_type.md +0 -92
- package/test/scenarios/multipart/non-string-float.md +0 -55
- package/test/scenarios/multipart/simple_part.md +0 -65
- package/test/scenarios/operation-parameters/body_root_anonymous.md +0 -87
- package/test/scenarios/operation-parameters/constant_as_optional.md +0 -54
- package/test/scenarios/operation-parameters/default_value_as_optional.md +0 -54
- package/test/scenarios/operation-parameters/no_content_type.md +0 -62
- package/test/scenarios/operation-parameters/no_parameters.md +0 -50
- package/test/scenarios/operation-parameters/only_optional.md +0 -62
- package/test/scenarios/operation-parameters/only_required.md +0 -58
- package/test/scenarios/operation-parameters/reserved_names.md +0 -60
- package/test/scenarios/operation-parameters/spread_body.md +0 -70
- package/test/scenarios/operation-parameters/spread_with_nested.md +0 -82
- package/test/scenarios/operation-parameters/union_body.md +0 -86
- package/test/scenarios/operation-parameters/with_body_property.md +0 -67
- package/test/scenarios/operation-parameters/with_body_root.md +0 -69
- package/test/scenarios/serializers/arrays.md +0 -191
- package/test/scenarios/serializers/basic_model.md +0 -111
- package/test/scenarios/serializers/basic_model_wire_name.md +0 -48
- package/test/scenarios/serializers/discriminated_union.md +0 -86
- package/test/scenarios/serializers/discriminated_union_spread.md +0 -115
- package/test/scenarios/serializers/file.md +0 -67
- package/test/scenarios/serializers/model_date_time.md +0 -65
- package/test/scenarios/serializers/multipart.md +0 -62
- package/test/scenarios/serializers/polymorphic_single_level_inheritance.md +0 -157
- package/test/scenarios/serializers/property_references_property.md +0 -72
- package/test/scenarios/serializers/record.md +0 -97
- package/test/scenarios/serializers/scalars.md +0 -34
- package/test/scenarios/serializers/spread.md +0 -69
- package/test/scenarios/serializers/string_union.md +0 -55
- package/test/scenarios/server/default_url.md +0 -45
- package/test/scenarios/server/multiple-parameters.md +0 -55
- package/test/scenarios/server/no-server.md +0 -37
- package/test/scenarios/server/parametrized-endpoint.md +0 -51
- package/test/scenarios.test.ts +0 -24
- package/test/test-host.ts +0 -22
- package/tsconfig.json +0 -16
- package/tsconfig.test.json +0 -9
- package/vitest.config.e2e.ts +0 -16
- package/vitest.config.ts +0 -15
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import * as ts from "@alloy-js/typescript";
|
|
2
|
-
import type { Type } from "@typespec/compiler";
|
|
3
|
-
import * as ef from "@typespec/emitter-framework";
|
|
4
|
-
import { kebabCase } from "change-case";
|
|
5
|
-
import { reportDiagnostic } from "../../lib.js";
|
|
6
|
-
|
|
7
|
-
export function useTransformNamePolicy(): ef.TransformNamePolicy {
|
|
8
|
-
return ef.useTransformNamePolicy();
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* A type that extends `Type` and includes a `name` property of type `string`.
|
|
13
|
-
* This type is used to represent objects that specifically have a `name` property of type `string`.
|
|
14
|
-
*/
|
|
15
|
-
export type WithStringName = Type & { name: string };
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* A type that extends `Type` and includes a `name` property of type `string | symbol`.
|
|
19
|
-
* This type is used to represent objects that have a `name` property which can either be a `string` or `symbol`.
|
|
20
|
-
*/
|
|
21
|
-
export type WithName = Type & { name: string | symbol };
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* A set of optional namers that can be provided for transforming transport and application names.
|
|
25
|
-
* The `transportNamer` transforms the transport name, while `applicationNamer` transforms the application name.
|
|
26
|
-
*/
|
|
27
|
-
export interface TransformNamers {
|
|
28
|
-
/**
|
|
29
|
-
* Function to generate a transport name from a `Type` object.
|
|
30
|
-
*
|
|
31
|
-
* @param type - The type object to transform.
|
|
32
|
-
* @param encoding - An optional encoding string to modify the transformation.
|
|
33
|
-
* @returns A transformed transport name as a string.
|
|
34
|
-
*/
|
|
35
|
-
transportNamer?: (type: WithName, encoding?: string) => string;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Function to generate an application name from a `Type` object.
|
|
39
|
-
*
|
|
40
|
-
* @param type - The type object to transform.
|
|
41
|
-
* @returns A transformed application name as a string.
|
|
42
|
-
*/
|
|
43
|
-
applicationNamer?: (type: WithName) => string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Factory function to create a `TransformNamePolicy` implementation with custom or default name transformation logic.
|
|
48
|
-
* This function uses the provided namers for transport and application names. If no namers are provided,
|
|
49
|
-
* default implementations are used.
|
|
50
|
-
*
|
|
51
|
-
* @param namers - An optional object containing custom `transportNamer` and `applicationNamer` functions.
|
|
52
|
-
* @returns A `TransformNamePolicy` implementation.
|
|
53
|
-
*/
|
|
54
|
-
export function createTransformNamePolicy(namers: TransformNamers = {}): ef.TransformNamePolicy {
|
|
55
|
-
const transportNamer = namers.transportNamer ?? defaultTransportNameGetter;
|
|
56
|
-
const applicationNamer = namers.applicationNamer ?? defaultApplicationNameGetter;
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
/**
|
|
60
|
-
* Transforms the transport name based on the provided `transportNamer` function or the default.
|
|
61
|
-
* If the `type` does not have a string `name`, it reports a diagnostic.
|
|
62
|
-
*
|
|
63
|
-
* @param type - The object that has a `name` property (either string or symbol).
|
|
64
|
-
* @returns The transformed transport name as a string.
|
|
65
|
-
*/
|
|
66
|
-
getTransportName(type) {
|
|
67
|
-
return transportNamer(type);
|
|
68
|
-
},
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Transforms the application name based on the provided `applicationNamer` function or the default.
|
|
72
|
-
* If the `type` does not have a string `name`, it reports a diagnostic.
|
|
73
|
-
*
|
|
74
|
-
* @param type - The object that has a `name` property (either string or symbol).
|
|
75
|
-
* @returns The transformed application name as a string.
|
|
76
|
-
*/
|
|
77
|
-
getApplicationName(type) {
|
|
78
|
-
return applicationNamer(type);
|
|
79
|
-
},
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Default function for transforming the application name. It assumes that the `type` has a `name` of type `string`.
|
|
85
|
-
*
|
|
86
|
-
* @param type - The type object that must have a `name` property of type `string`.
|
|
87
|
-
* @returns The transformed application name as a string.
|
|
88
|
-
*/
|
|
89
|
-
export function defaultApplicationNameGetter(type: WithName): string {
|
|
90
|
-
const { $ } = ef.useTsp();
|
|
91
|
-
if (!hasStringName(type)) {
|
|
92
|
-
reportDiagnostic($.program, { code: "symbol-name-not-supported", target: type });
|
|
93
|
-
return "";
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const namePolicy = ts.useTSNamePolicy();
|
|
97
|
-
const name = type.name;
|
|
98
|
-
return namePolicy.getName(name, "object-member-data");
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Default function for transforming the transport name. It assumes that the `type` has a `name` of type `string`.
|
|
103
|
-
* Optionally uses an `encoding` string to modify the name.
|
|
104
|
-
*
|
|
105
|
-
* @param type - The type object that must have a `name` property of type `string`.
|
|
106
|
-
* @param encoding - Optional encoding for the transport name.
|
|
107
|
-
* @returns The transformed transport name as a string.
|
|
108
|
-
*/
|
|
109
|
-
export function defaultTransportNameGetter(
|
|
110
|
-
type: WithName,
|
|
111
|
-
encoding: string = "application/json",
|
|
112
|
-
): string {
|
|
113
|
-
const { $ } = ef.useTsp();
|
|
114
|
-
if (!hasStringName(type)) {
|
|
115
|
-
reportDiagnostic($.program, { code: "symbol-name-not-supported", target: type });
|
|
116
|
-
return "";
|
|
117
|
-
}
|
|
118
|
-
let name = encoding ? $.type.getEncodedName(type, encoding) : type.name;
|
|
119
|
-
|
|
120
|
-
if ($.modelProperty.is(type) && $.modelProperty.isHttpHeader(type)) {
|
|
121
|
-
name = kebabCase(name);
|
|
122
|
-
}
|
|
123
|
-
return name;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Helper function to check if the provided `type` has a `name` property of type `string`.
|
|
128
|
-
*
|
|
129
|
-
* @param type - The type object that may have a `name` property of type `string` or `symbol`.
|
|
130
|
-
* @returns A boolean indicating whether the `name` property is a string.
|
|
131
|
-
*/
|
|
132
|
-
export function hasStringName(type: WithName): type is WithStringName {
|
|
133
|
-
return typeof type.name === "string";
|
|
134
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import type { ModelProperty, Type } from "@typespec/compiler";
|
|
2
|
-
import { isNullType } from "@typespec/compiler";
|
|
3
|
-
import { useTsp } from "@typespec/emitter-framework";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Sometimes a model property type would be another ModelProperty in this case we need to keep unpacking until we find a non Model Property
|
|
7
|
-
* type to get to the actual type.
|
|
8
|
-
* This also handles an HttpPart which needs to be unpacked as well.
|
|
9
|
-
*/
|
|
10
|
-
export function unpackProperty(modelProperty: ModelProperty): Type {
|
|
11
|
-
const { $ } = useTsp();
|
|
12
|
-
const type = $.httpPart.unpack(modelProperty.type) ?? modelProperty.type;
|
|
13
|
-
if ($.modelProperty.is(type)) {
|
|
14
|
-
return unpackProperty(type);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Check if it is nullable
|
|
18
|
-
if ($.union.is(type)) {
|
|
19
|
-
const variants = Array.from(type.variants.values());
|
|
20
|
-
const nullVariant = variants.find((v) => isNullType(v.type));
|
|
21
|
-
if (variants.length === 2 && nullVariant) {
|
|
22
|
-
// When the union has only null and another variant unpack the non-null type
|
|
23
|
-
const nonNullVariant = variants.find((v) => v !== nullVariant)!.type;
|
|
24
|
-
// Recursively unpack if the non-null variant is a ModelProperty
|
|
25
|
-
return $.modelProperty.is(nonNullVariant) ? unpackProperty(nonNullVariant) : nonNullVariant;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (variants.length > 2 && nullVariant) {
|
|
29
|
-
const nonNullVariants = variants
|
|
30
|
-
.filter((v) => v !== nullVariant)
|
|
31
|
-
.map((v) => {
|
|
32
|
-
// Recursively unpack each variant if it's a ModelProperty
|
|
33
|
-
const variantType = v.type;
|
|
34
|
-
return {
|
|
35
|
-
...v,
|
|
36
|
-
type: $.modelProperty.is(variantType) ? unpackProperty(variantType) : variantType,
|
|
37
|
-
};
|
|
38
|
-
});
|
|
39
|
-
return $.union.create({ name: type.name, variants: nonNullVariants });
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return type;
|
|
44
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import * as ts from "@alloy-js/typescript";
|
|
2
|
-
import type { ModelProperty } from "@typespec/compiler";
|
|
3
|
-
import { useTransformNamePolicy } from "@typespec/emitter-framework";
|
|
4
|
-
import { uriTemplateLib } from "../external-packages/uri-template.js";
|
|
5
|
-
|
|
6
|
-
export interface UrlTemplateProps {
|
|
7
|
-
template: string;
|
|
8
|
-
parameters: ModelProperty[];
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function UrlTemplate(props: UrlTemplateProps) {
|
|
12
|
-
const namer = useTransformNamePolicy();
|
|
13
|
-
const params = props.parameters.map((p) => {
|
|
14
|
-
return (
|
|
15
|
-
<ts.ObjectExpression>
|
|
16
|
-
"{namer.getTransportName(p)}": {namer.getApplicationName(p)}
|
|
17
|
-
</ts.ObjectExpression>
|
|
18
|
-
);
|
|
19
|
-
});
|
|
20
|
-
return (
|
|
21
|
-
<ts.VarDeclaration name="url" type="string">
|
|
22
|
-
{uriTemplateLib.parse}({JSON.stringify(props.template)}).expand({params})
|
|
23
|
-
</ts.VarDeclaration>
|
|
24
|
-
);
|
|
25
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { ComponentContext } from "@alloy-js/core";
|
|
2
|
-
import { createNamedContext, useContext } from "@alloy-js/core";
|
|
3
|
-
import { NoTarget } from "@typespec/compiler";
|
|
4
|
-
import { useTsp } from "@typespec/emitter-framework";
|
|
5
|
-
import { reportDiagnostic } from "../../lib.js";
|
|
6
|
-
import type { EncodingDefaults } from "./types.js";
|
|
7
|
-
|
|
8
|
-
export const EncodingContext: ComponentContext<EncodingDefaults> =
|
|
9
|
-
createNamedContext<EncodingDefaults>("Encoding");
|
|
10
|
-
|
|
11
|
-
export function useEncoding() {
|
|
12
|
-
const context = useContext(EncodingContext);
|
|
13
|
-
const { $ } = useTsp();
|
|
14
|
-
|
|
15
|
-
if (!context) {
|
|
16
|
-
reportDiagnostic($.program, {
|
|
17
|
-
code: "use-encoding-context-without-provider",
|
|
18
|
-
target: NoTarget,
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return context!;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function useDefaultEncoding(scalarType: keyof EncodingDefaults): string | undefined {
|
|
26
|
-
const defaults = useEncoding();
|
|
27
|
-
return defaults[scalarType];
|
|
28
|
-
}
|
package/src/emitter.tsx
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import type { Children } from "@alloy-js/core";
|
|
2
|
-
import { SourceDirectory } from "@alloy-js/core";
|
|
3
|
-
import * as ts from "@alloy-js/typescript";
|
|
4
|
-
import type { EmitContext } from "@typespec/compiler";
|
|
5
|
-
import {
|
|
6
|
-
Experimental_ComponentOverrides,
|
|
7
|
-
Experimental_ComponentOverridesConfig,
|
|
8
|
-
useTsp,
|
|
9
|
-
writeOutput,
|
|
10
|
-
} from "@typespec/emitter-framework";
|
|
11
|
-
import { TypeExpression } from "@typespec/emitter-framework/typescript";
|
|
12
|
-
import { OperationsDirectory } from "./components/client-directory.jsx";
|
|
13
|
-
import { Client } from "./components/client.jsx";
|
|
14
|
-
import { Models } from "./components/models.js";
|
|
15
|
-
import { Output } from "./components/output.jsx";
|
|
16
|
-
import { ModelSerializers } from "./components/serializers.js";
|
|
17
|
-
import { Interfaces } from "./components/static-helpers/interfaces.jsx";
|
|
18
|
-
import { MultipartHelpers } from "./components/static-helpers/multipart-helpers.jsx";
|
|
19
|
-
import { PagingHelpers } from "./components/static-helpers/paging-helper.jsx";
|
|
20
|
-
import { RestError } from "./components/static-helpers/rest-error.jsx";
|
|
21
|
-
import type { JsClientEmitterOptions } from "./lib.js";
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Main function to handle the emission process.
|
|
25
|
-
* @param context - The context for the emission process.
|
|
26
|
-
*/
|
|
27
|
-
export async function $onEmit(context: EmitContext<JsClientEmitterOptions>) {
|
|
28
|
-
const packageName = context.options["package-name"] ?? "test-package";
|
|
29
|
-
const output = (
|
|
30
|
-
<Output program={context.program}>
|
|
31
|
-
<HttpClientOverrides>
|
|
32
|
-
<ts.PackageDirectory
|
|
33
|
-
name={packageName}
|
|
34
|
-
version="1.0.0"
|
|
35
|
-
path="."
|
|
36
|
-
scripts={{ build: "tsc" }}
|
|
37
|
-
devDependencies={{ "@types/node": "~18.19.75" }}
|
|
38
|
-
>
|
|
39
|
-
<SourceDirectory path="src">
|
|
40
|
-
<ts.BarrelFile export="." />
|
|
41
|
-
<Client />
|
|
42
|
-
<SourceDirectory path="models">
|
|
43
|
-
<ts.BarrelFile export="models" />
|
|
44
|
-
<Models />
|
|
45
|
-
<SourceDirectory path="internal">
|
|
46
|
-
<ModelSerializers />
|
|
47
|
-
</SourceDirectory>
|
|
48
|
-
</SourceDirectory>
|
|
49
|
-
<SourceDirectory path="api">
|
|
50
|
-
<OperationsDirectory />
|
|
51
|
-
</SourceDirectory>
|
|
52
|
-
<SourceDirectory path="helpers">
|
|
53
|
-
<PagingHelpers />
|
|
54
|
-
<Interfaces />
|
|
55
|
-
<MultipartHelpers />
|
|
56
|
-
<ts.SourceFile path="error.ts">
|
|
57
|
-
<RestError />
|
|
58
|
-
</ts.SourceFile>
|
|
59
|
-
</SourceDirectory>
|
|
60
|
-
</SourceDirectory>
|
|
61
|
-
</ts.PackageDirectory>
|
|
62
|
-
</HttpClientOverrides>
|
|
63
|
-
</Output>
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
await writeOutput(context.program, output, context.emitterOutputDir);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function HttpClientOverrides(props: { children?: Children }) {
|
|
70
|
-
const { $ } = useTsp();
|
|
71
|
-
const overrides = Experimental_ComponentOverridesConfig().forTypeKind("Model", {
|
|
72
|
-
reference: (props) => {
|
|
73
|
-
if ($.httpPart.is(props.type)) {
|
|
74
|
-
return <TypeExpression type={$.httpPart.unpack(props.type)} />;
|
|
75
|
-
} else {
|
|
76
|
-
return props.default;
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
return (
|
|
81
|
-
<Experimental_ComponentOverrides overrides={overrides}>
|
|
82
|
-
{props.children}
|
|
83
|
-
</Experimental_ComponentOverrides>
|
|
84
|
-
);
|
|
85
|
-
}
|
package/src/index.ts
DELETED
package/src/lib.ts
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import type { JSONSchemaType } from "@typespec/compiler";
|
|
2
|
-
import { createTypeSpecLibrary } from "@typespec/compiler";
|
|
3
|
-
|
|
4
|
-
export interface JsClientEmitterOptions {
|
|
5
|
-
"package-name"?: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const EmitterOptionsSchema: JSONSchemaType<JsClientEmitterOptions> = {
|
|
9
|
-
type: "object",
|
|
10
|
-
additionalProperties: true,
|
|
11
|
-
properties: {
|
|
12
|
-
"package-name": {
|
|
13
|
-
type: "string",
|
|
14
|
-
nullable: true,
|
|
15
|
-
default: "test-package",
|
|
16
|
-
description: "Name of the package as it will be in package.json",
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
required: [],
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export const $lib = createTypeSpecLibrary({
|
|
23
|
-
name: "@typespec/http-client-js",
|
|
24
|
-
emitter: {
|
|
25
|
-
options: EmitterOptionsSchema,
|
|
26
|
-
},
|
|
27
|
-
diagnostics: {
|
|
28
|
-
"unknown-encoding": {
|
|
29
|
-
severity: "warning",
|
|
30
|
-
messages: {
|
|
31
|
-
default: "Unknown encoding",
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
"mixed-part-nonpart": {
|
|
35
|
-
severity: "warning",
|
|
36
|
-
messages: {
|
|
37
|
-
default: "Mixed part and non-part properties in model",
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
"operation-not-in-client": {
|
|
41
|
-
severity: "error",
|
|
42
|
-
messages: {
|
|
43
|
-
default: "Tried to get a client from an operation that is not in a client",
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
"non-model-parts": {
|
|
47
|
-
severity: "error",
|
|
48
|
-
messages: {
|
|
49
|
-
default: "Non-model parts are not supported",
|
|
50
|
-
},
|
|
51
|
-
description: "Non-model parts are not supported",
|
|
52
|
-
},
|
|
53
|
-
"multiple-auth-schemes-not-yet-supported": {
|
|
54
|
-
severity: "warning",
|
|
55
|
-
messages: {
|
|
56
|
-
default: "Multiple authentication schemes are not yet supported",
|
|
57
|
-
},
|
|
58
|
-
description:
|
|
59
|
-
"Multiple authentication schemes are not yet supported. Falling back to the first one.",
|
|
60
|
-
},
|
|
61
|
-
"key-credential-non-header-not-implemented": {
|
|
62
|
-
severity: "warning",
|
|
63
|
-
messages: {
|
|
64
|
-
default: "Key credential in query or cookie is not implemented",
|
|
65
|
-
},
|
|
66
|
-
description:
|
|
67
|
-
"Key credential in query or cookie is not implemented. Falling back to not sending auth details with the requests",
|
|
68
|
-
},
|
|
69
|
-
"unsupported-nondiscriminated-union": {
|
|
70
|
-
severity: "warning",
|
|
71
|
-
messages: {
|
|
72
|
-
default: "Unsupported non-discriminated union, skipping deserializer",
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
"no-name-type": {
|
|
76
|
-
severity: "warning",
|
|
77
|
-
messages: {
|
|
78
|
-
default: "Trying to get a name from a type that doesn't have a name",
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
"symbol-name-not-supported": {
|
|
82
|
-
severity: "error",
|
|
83
|
-
messages: {
|
|
84
|
-
default: "The transform namer doesn't support symbol names",
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
"unsupported-content-type": {
|
|
88
|
-
severity: "warning",
|
|
89
|
-
messages: {
|
|
90
|
-
default: "Unsupported content type. Falling back to json",
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
"missing-http-parts": {
|
|
94
|
-
severity: "warning",
|
|
95
|
-
messages: {
|
|
96
|
-
default: "The operation is defined as a Multipart operation but has no parts",
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
"use-encoding-context-without-provider": {
|
|
100
|
-
severity: "error",
|
|
101
|
-
messages: {
|
|
102
|
-
default: "Trying to use encoding context without a provider",
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
"unexpected-non-scalar-type": {
|
|
106
|
-
severity: "error",
|
|
107
|
-
messages: {
|
|
108
|
-
default: "Unexpected non-scalar type when trying to extract Scalar data",
|
|
109
|
-
},
|
|
110
|
-
},
|
|
111
|
-
"client-not-found": {
|
|
112
|
-
severity: "error",
|
|
113
|
-
messages: {
|
|
114
|
-
default: "Client for operation not found",
|
|
115
|
-
},
|
|
116
|
-
},
|
|
117
|
-
},
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
export const { reportDiagnostic, createDiagnostic } = $lib;
|
package/src/testing/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { resolvePath } from "@typespec/compiler";
|
|
2
|
-
import type { TypeSpecTestLibrary } from "@typespec/compiler/testing";
|
|
3
|
-
import { createTestLibrary } from "@typespec/compiler/testing";
|
|
4
|
-
import { fileURLToPath } from "url";
|
|
5
|
-
|
|
6
|
-
/** @deprecated Use `createTester` from `@typespec/compiler/testing` instead */
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/no-deprecated
|
|
8
|
-
export const HttpClientJavascriptEmitterTestLibrary: TypeSpecTestLibrary = createTestLibrary({
|
|
9
|
-
name: "@typespec/http-client-js",
|
|
10
|
-
packageRoot: resolvePath(fileURLToPath(import.meta.url), "../../../"),
|
|
11
|
-
});
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type * as cl from "@typespec/http-client";
|
|
2
|
-
|
|
3
|
-
const flattenCache: WeakMap<cl.Client, cl.Client[]> = new WeakMap();
|
|
4
|
-
/**
|
|
5
|
-
* Flatten the client hierarchy into a single-level array,
|
|
6
|
-
* caching the result to avoid recomputing.
|
|
7
|
-
*/
|
|
8
|
-
export function flattenClients(client: cl.Client): cl.Client[] {
|
|
9
|
-
// If we already have a cached value for this client, return it.
|
|
10
|
-
if (flattenCache.has(client)) {
|
|
11
|
-
return flattenCache.get(client)!;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// Otherwise, do a DFS/BFS to gather all subClients.
|
|
15
|
-
const result: cl.Client[] = [];
|
|
16
|
-
const stack: cl.Client[] = [client];
|
|
17
|
-
|
|
18
|
-
while (stack.length > 0) {
|
|
19
|
-
const current = stack.pop()!;
|
|
20
|
-
result.push(current);
|
|
21
|
-
// Add sub-clients to the stack
|
|
22
|
-
stack.push(...current.subClients);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Store the result in the cache before returning
|
|
26
|
-
flattenCache.set(client, result);
|
|
27
|
-
return result;
|
|
28
|
-
}
|
package/src/utils/multipart.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { Type } from "@typespec/compiler";
|
|
2
|
-
import { useTsp } from "@typespec/emitter-framework";
|
|
3
|
-
import { reportDiagnostic } from "../lib.js";
|
|
4
|
-
|
|
5
|
-
export function isMultipart(type: Type): boolean {
|
|
6
|
-
const { $ } = useTsp();
|
|
7
|
-
const body = type;
|
|
8
|
-
|
|
9
|
-
if (!$.model.is(body)) {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
let multipartCount = 0;
|
|
14
|
-
let nonMultipartCount = 0;
|
|
15
|
-
for (const prop of body.properties.values()) {
|
|
16
|
-
if ($.httpPart.is(prop.type)) {
|
|
17
|
-
multipartCount++;
|
|
18
|
-
} else if (!$.array.is(prop.type)) {
|
|
19
|
-
nonMultipartCount++;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (multipartCount > 0 && nonMultipartCount > 0) {
|
|
24
|
-
reportDiagnostic($.program, { code: "mixed-part-nonpart", target: type });
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return multipartCount > 0;
|
|
29
|
-
}
|
package/src/utils/operations.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import type { Model, ModelProperty, Operation, Union } from "@typespec/compiler";
|
|
2
|
-
import type {
|
|
3
|
-
unsafe_Mutator as Mutator,
|
|
4
|
-
unsafe_MutatorRecord,
|
|
5
|
-
} from "@typespec/compiler/experimental";
|
|
6
|
-
import {
|
|
7
|
-
unsafe_mutateSubgraph as mutateSubgraph,
|
|
8
|
-
unsafe_MutatorFlow as MutatorFlow,
|
|
9
|
-
} from "@typespec/compiler/experimental";
|
|
10
|
-
import { $, type Typekit } from "@typespec/compiler/typekit";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Prepares operation for client representation. This includes adding an options bag for optional parameters.
|
|
14
|
-
* @param operation operation to be prepared
|
|
15
|
-
* @returns the prepared operation
|
|
16
|
-
*/
|
|
17
|
-
export function prepareOperation($: Typekit, operation: Operation): Operation {
|
|
18
|
-
return mutateSubgraph($.program, [httpParamsMutator], operation).type as Operation;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Mutates anonymous types to be named.
|
|
23
|
-
*/
|
|
24
|
-
const anonymousMutatorRecord: unsafe_MutatorRecord<Model | Union> = {
|
|
25
|
-
filter(t) {
|
|
26
|
-
return MutatorFlow.MutateAndRecur;
|
|
27
|
-
},
|
|
28
|
-
mutate(t, clone, _program, realm) {
|
|
29
|
-
if (!clone.name) {
|
|
30
|
-
clone.name = $(realm).type.getPlausibleName(clone);
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export const anonymousMutator: Mutator = {
|
|
36
|
-
name: "Anonymous types",
|
|
37
|
-
Model: anonymousMutatorRecord,
|
|
38
|
-
Union: anonymousMutatorRecord,
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Mutates the operation so that the parameters model is split into required and optional parameters.
|
|
43
|
-
*/
|
|
44
|
-
export const httpParamsMutator: Mutator = {
|
|
45
|
-
name: "Http parameters",
|
|
46
|
-
Operation: {
|
|
47
|
-
filter() {
|
|
48
|
-
return MutatorFlow.DoNotRecur;
|
|
49
|
-
},
|
|
50
|
-
mutate(o, clone, _program, realm) {
|
|
51
|
-
const tk = $(realm);
|
|
52
|
-
const httpOperation = tk.httpOperation.get(o);
|
|
53
|
-
const returnType = tk.httpOperation.getReturnType(httpOperation);
|
|
54
|
-
clone.returnType = returnType;
|
|
55
|
-
const params = tk.httpRequest.getParameters(httpOperation, [
|
|
56
|
-
"query",
|
|
57
|
-
"header",
|
|
58
|
-
"path",
|
|
59
|
-
"body",
|
|
60
|
-
"contentType",
|
|
61
|
-
]);
|
|
62
|
-
|
|
63
|
-
if (!params) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
clone.parameters = params;
|
|
68
|
-
|
|
69
|
-
const optionals = [...clone.parameters.properties.values()]
|
|
70
|
-
.filter((p) => p.optional)
|
|
71
|
-
.reduce(
|
|
72
|
-
(acc, prop) => {
|
|
73
|
-
acc[prop.name] = prop;
|
|
74
|
-
return acc;
|
|
75
|
-
},
|
|
76
|
-
{} as Record<string, ModelProperty>,
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
const optionsBag = tk.model.create({
|
|
80
|
-
properties: optionals,
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
const optionsProp = tk.modelProperty.create({
|
|
84
|
-
name: "options",
|
|
85
|
-
type: optionsBag,
|
|
86
|
-
optional: true,
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
for (const [key, prop] of clone.parameters.properties) {
|
|
90
|
-
if (prop.optional || isConstantHeader(tk, prop)) {
|
|
91
|
-
clone.parameters.properties.delete(key);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (Object.keys(optionals).length > 0) {
|
|
96
|
-
clone.parameters.properties.set("options", optionsProp);
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
},
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
export function isConstantHeader($: Typekit, modelProperty: ModelProperty) {
|
|
103
|
-
if (!$.modelProperty.isHttpHeader(modelProperty)) {
|
|
104
|
-
return false;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if ("value" in modelProperty.type && modelProperty.type.value !== undefined) {
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return false;
|
|
112
|
-
}
|