@typespec/http-client-js 0.2.0-dev.7 → 0.2.0-dev.9
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/README.md +31 -15
- package/dist/src/components/serializers.d.ts.map +1 -1
- package/dist/src/components/serializers.js +20 -13
- package/dist/src/components/serializers.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 +23 -13
- package/dist/src/components/transforms/multipart/simple-part-transform.js.map +1 -1
- package/dist/src/lib.d.ts.map +1 -1
- package/dist/src/lib.js +2 -1
- package/dist/src/lib.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -3
- package/src/components/serializers.tsx +14 -6
- package/src/components/transforms/multipart/simple-part-transform.tsx +12 -6
- package/src/lib.ts +6 -1
- package/test/e2e/http/payload/multipart/main.test.ts +20 -24
- package/test/scenarios/multipart/anonymous_part.md +1 -1
- package/test/scenarios/multipart/file_content_type.md +15 -0
- package/test/scenarios/multipart/non-string-float.md +50 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typespec/http-client-js",
|
|
3
|
-
"version": "0.2.0-dev.
|
|
3
|
+
"version": "0.2.0-dev.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"@babel/core": "^7.26.9",
|
|
42
42
|
"@rollup/plugin-babel": "^6.0.4",
|
|
43
43
|
"@types/yargs": "~17.0.33",
|
|
44
|
-
"@typespec/http-specs": "^0.1.0-alpha.
|
|
44
|
+
"@typespec/http-specs": "^0.1.0-alpha.12 || >=0.1.0-alpha.13-dev <0.1.0-alpha.13",
|
|
45
45
|
"@typespec/spector": "^0.1.0-alpha.8 || >=0.1.0-alpha.9-dev <0.1.0-alpha.9",
|
|
46
46
|
"@typespec/ts-http-runtime": "0.1.0",
|
|
47
47
|
"@typespec/versioning": "^0.66.0 || >=0.67.0-dev <0.67.0",
|
|
@@ -58,7 +58,8 @@
|
|
|
58
58
|
"uri-template": "^2.0.0",
|
|
59
59
|
"vitest": "^3.0.7",
|
|
60
60
|
"yargs": "~17.7.2",
|
|
61
|
-
"change-case": "~5.4.4"
|
|
61
|
+
"change-case": "~5.4.4",
|
|
62
|
+
"@typespec/tspd": "^0.46.0"
|
|
62
63
|
},
|
|
63
64
|
"scripts": {
|
|
64
65
|
"build-src": "babel src -d dist/src --extensions .ts,.tsx",
|
|
@@ -77,6 +78,7 @@
|
|
|
77
78
|
"stop:server": "npx tsp-spector server stop",
|
|
78
79
|
"test:e2e": "npm run emit:e2e && node eng/scripts/run-e2e-tests.js",
|
|
79
80
|
"run:e2e": "node eng/scripts/run-e2e-tests.js",
|
|
81
|
+
"regen-docs": "tspd doc . --enable-experimental --output-dir ../../website/src/content/docs/docs/emitters/clients/http-client-js/reference --skip-js",
|
|
80
82
|
"emit:e2e": "node eng/scripts/emit-e2e.js"
|
|
81
83
|
}
|
|
82
84
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as ts from "@alloy-js/typescript";
|
|
2
|
+
import { $ } from "@typespec/compiler/experimental/typekit";
|
|
2
3
|
import {
|
|
3
4
|
DateDeserializer,
|
|
4
5
|
DateRfc3339Serializer,
|
|
@@ -40,12 +41,19 @@ export function ModelSerializers(props: ModelSerializersProps) {
|
|
|
40
41
|
))}
|
|
41
42
|
{dataTypes
|
|
42
43
|
.filter((m) => m.kind === "Model" || m.kind === "Union")
|
|
43
|
-
.map((type) =>
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
.map((type) => {
|
|
45
|
+
let bytesDefaultEncoding: "base64" | "none" = "base64";
|
|
46
|
+
if ($.model.is(type) && type.baseModel && $.model.isHttpFile(type.baseModel)) {
|
|
47
|
+
bytesDefaultEncoding = "none";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<EncodingProvider defaults={{ bytes: bytesDefaultEncoding }}>
|
|
52
|
+
<JsonTransformDeclaration type={type} target="transport" />
|
|
53
|
+
<JsonTransformDeclaration type={type} target="application" />
|
|
54
|
+
</EncodingProvider>
|
|
55
|
+
);
|
|
56
|
+
})}
|
|
49
57
|
</ts.SourceFile>
|
|
50
58
|
);
|
|
51
59
|
}
|
|
@@ -16,6 +16,11 @@ export function SimplePartTransform(props: SimplePartTransformProps) {
|
|
|
16
16
|
const partName = ts.ValueExpression({ jsValue: props.part.name });
|
|
17
17
|
const partContentType = props.part.body.contentTypes[0] ?? "application/json";
|
|
18
18
|
const partRef = getPartRef(props.itemRef, applicationName);
|
|
19
|
+
let bodyRef = ay.code`${partRef}`;
|
|
20
|
+
|
|
21
|
+
if (props.part.body.property) {
|
|
22
|
+
bodyRef = ay.code`${partRef}.${props.part.body.property.name}`;
|
|
23
|
+
}
|
|
19
24
|
|
|
20
25
|
if (!partContentType.startsWith("application/json")) {
|
|
21
26
|
reportDiagnostic($.program, {
|
|
@@ -27,12 +32,13 @@ export function SimplePartTransform(props: SimplePartTransformProps) {
|
|
|
27
32
|
|
|
28
33
|
return (
|
|
29
34
|
<ts.ObjectExpression>
|
|
30
|
-
<
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
<ay.List comma line>
|
|
36
|
+
<ts.ObjectProperty name="name" jsValue={partName} />
|
|
37
|
+
<ts.ObjectProperty
|
|
38
|
+
name="body"
|
|
39
|
+
value={<JsonTransform itemRef={bodyRef} target="transport" type={props.part.body.type} />}
|
|
40
|
+
/>
|
|
41
|
+
</ay.List>
|
|
36
42
|
</ts.ObjectExpression>
|
|
37
43
|
);
|
|
38
44
|
}
|
package/src/lib.ts
CHANGED
|
@@ -8,7 +8,12 @@ const EmitterOptionsSchema: JSONSchemaType<JsClientEmitterOptions> = {
|
|
|
8
8
|
type: "object",
|
|
9
9
|
additionalProperties: true,
|
|
10
10
|
properties: {
|
|
11
|
-
"package-name": {
|
|
11
|
+
"package-name": {
|
|
12
|
+
type: "string",
|
|
13
|
+
nullable: true,
|
|
14
|
+
default: "test-package",
|
|
15
|
+
description: "Name of the package as it will be in package.json",
|
|
16
|
+
},
|
|
12
17
|
},
|
|
13
18
|
required: [],
|
|
14
19
|
};
|
|
@@ -4,30 +4,26 @@ import { fileURLToPath } from "url";
|
|
|
4
4
|
import { beforeEach, describe, it } from "vitest";
|
|
5
5
|
import { FormDataClient, HttpPartsClient } from "../../../generated/payload/multipart/src/index.js";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = dirname(__filename);
|
|
9
|
+
|
|
10
|
+
const jpegImagePath = resolve(__dirname, "../../../assets/image.jpg");
|
|
11
|
+
const jpegBuffer = await readFile(jpegImagePath);
|
|
12
|
+
const jpegContents = new Uint8Array(jpegBuffer);
|
|
13
|
+
|
|
14
|
+
const pngImagePath = resolve(__dirname, "../../../assets/image.png");
|
|
15
|
+
const pngBuffer = await readFile(pngImagePath);
|
|
16
|
+
const pngContents = new Uint8Array(pngBuffer);
|
|
12
17
|
|
|
18
|
+
describe("Payload.MultiPart", () => {
|
|
19
|
+
// Skipping as implicit multipart is going to be deprecated in TypeSpec
|
|
13
20
|
describe.skip("FormDataClient", () => {
|
|
14
21
|
const client = new FormDataClient({
|
|
15
22
|
allowInsecureConnection: true,
|
|
16
23
|
retryOptions: { maxRetries: 1 },
|
|
17
24
|
});
|
|
18
25
|
|
|
19
|
-
beforeEach(async () => {
|
|
20
|
-
__filename = fileURLToPath(import.meta.url);
|
|
21
|
-
__dirname = dirname(__filename);
|
|
22
|
-
|
|
23
|
-
const jpegImagePath = resolve(__dirname, "../../../assets/image.jpg");
|
|
24
|
-
const jpegBuffer = await readFile(jpegImagePath);
|
|
25
|
-
jpegContents = new Uint8Array(jpegBuffer);
|
|
26
|
-
|
|
27
|
-
const pngImagePath = resolve(__dirname, "../../../assets/image.png");
|
|
28
|
-
const pngBuffer = await readFile(pngImagePath);
|
|
29
|
-
pngContents = new Uint8Array(pngBuffer);
|
|
30
|
-
});
|
|
26
|
+
beforeEach(async () => {});
|
|
31
27
|
|
|
32
28
|
it("should send mixed parts with multipart/form-data", async () => {
|
|
33
29
|
await client.basic({
|
|
@@ -82,7 +78,7 @@ describe("Payload.MultiPart", () => {
|
|
|
82
78
|
});
|
|
83
79
|
});
|
|
84
80
|
|
|
85
|
-
describe
|
|
81
|
+
describe("FormDataClient.HttpParts.ContentType", () => {
|
|
86
82
|
const client = new HttpPartsClient({
|
|
87
83
|
allowInsecureConnection: true,
|
|
88
84
|
retryOptions: { maxRetries: 1 },
|
|
@@ -93,7 +89,7 @@ describe("Payload.MultiPart", () => {
|
|
|
93
89
|
profileImage: {
|
|
94
90
|
contents: jpegContents,
|
|
95
91
|
contentType: "image/jpg",
|
|
96
|
-
filename: "
|
|
92
|
+
filename: "hello.jpg",
|
|
97
93
|
},
|
|
98
94
|
});
|
|
99
95
|
});
|
|
@@ -102,8 +98,8 @@ describe("Payload.MultiPart", () => {
|
|
|
102
98
|
await client.contentTypeClient.requiredContentType({
|
|
103
99
|
profileImage: {
|
|
104
100
|
contents: jpegContents,
|
|
105
|
-
contentType: "
|
|
106
|
-
filename: "
|
|
101
|
+
contentType: "application/octet-stream",
|
|
102
|
+
filename: "hello.jpg",
|
|
107
103
|
},
|
|
108
104
|
});
|
|
109
105
|
});
|
|
@@ -112,14 +108,14 @@ describe("Payload.MultiPart", () => {
|
|
|
112
108
|
await client.contentTypeClient.optionalContentType({
|
|
113
109
|
profileImage: {
|
|
114
110
|
contents: jpegContents,
|
|
115
|
-
filename: "
|
|
111
|
+
filename: "hello.jpg",
|
|
116
112
|
},
|
|
117
113
|
});
|
|
118
114
|
});
|
|
119
115
|
});
|
|
120
116
|
|
|
121
117
|
describe("FormDataClient.HttpParts", () => {
|
|
122
|
-
it
|
|
118
|
+
it("should send json array and file array", async () => {
|
|
123
119
|
const client = new HttpPartsClient({
|
|
124
120
|
allowInsecureConnection: true,
|
|
125
121
|
retryOptions: {
|
|
@@ -146,7 +142,7 @@ describe("Payload.MultiPart", () => {
|
|
|
146
142
|
});
|
|
147
143
|
|
|
148
144
|
describe("FormDataClient.HttpParts.NonString", () => {
|
|
149
|
-
it
|
|
145
|
+
it("should handle non-string float", async () => {
|
|
150
146
|
const client = new HttpPartsClient({
|
|
151
147
|
allowInsecureConnection: true,
|
|
152
148
|
retryOptions: {
|
|
@@ -60,3 +60,18 @@ export function jsonFileWithHttpPartSpecificContentTypeRequestToApplicationTrans
|
|
|
60
60
|
}!;
|
|
61
61
|
}
|
|
62
62
|
```
|
|
63
|
+
|
|
64
|
+
```ts src/models/serializers.ts function jsonFileSpecificContentTypeToApplicationTransform
|
|
65
|
+
export function jsonFileSpecificContentTypeToApplicationTransform(
|
|
66
|
+
input_?: any,
|
|
67
|
+
): FileSpecificContentType {
|
|
68
|
+
if (!input_) {
|
|
69
|
+
return input_ as any;
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
filename: input_.filename,
|
|
73
|
+
contentType: input_.contentType,
|
|
74
|
+
contents: input_.contents,
|
|
75
|
+
}!;
|
|
76
|
+
}
|
|
77
|
+
```
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Tests content-type: multipart/form-data for non string
|
|
2
|
+
|
|
3
|
+
## Spec
|
|
4
|
+
|
|
5
|
+
```tsp
|
|
6
|
+
namespace Test;
|
|
7
|
+
@post
|
|
8
|
+
@route("/non-string-float")
|
|
9
|
+
op float(
|
|
10
|
+
@header contentType: "multipart/form-data",
|
|
11
|
+
@multipartBody body: {
|
|
12
|
+
temperature: HttpPart<{
|
|
13
|
+
@body body: float64;
|
|
14
|
+
@header contentType: "text/plain";
|
|
15
|
+
}>;
|
|
16
|
+
},
|
|
17
|
+
): NoContentResponse;
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Operation
|
|
21
|
+
|
|
22
|
+
```ts src/api/testClientOperations.ts function float
|
|
23
|
+
export async function float(
|
|
24
|
+
client: TestClientContext,
|
|
25
|
+
body: { temperature: { body: number; contentType: "text/plain" } },
|
|
26
|
+
options?: FloatOptions,
|
|
27
|
+
): Promise<void> {
|
|
28
|
+
const path = parse("/non-string-float").expand({});
|
|
29
|
+
const httpRequestOptions = {
|
|
30
|
+
headers: {
|
|
31
|
+
"content-type": options?.contentType ?? "multipart/form-data",
|
|
32
|
+
},
|
|
33
|
+
body: [
|
|
34
|
+
{
|
|
35
|
+
name: "temperature",
|
|
36
|
+
body: body.temperature.body,
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
};
|
|
40
|
+
const response = await client.pathUnchecked(path).post(httpRequestOptions);
|
|
41
|
+
|
|
42
|
+
if (typeof options?.operationOptions?.onResponse === "function") {
|
|
43
|
+
options?.operationOptions?.onResponse(response);
|
|
44
|
+
}
|
|
45
|
+
if (+response.status === 204 && !response.body) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
throw createRestError(response);
|
|
49
|
+
}
|
|
50
|
+
```
|