@typespec/http-client-js 0.2.0-dev.8 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typespec/http-client-js",
3
- "version": "0.2.0-dev.8",
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.11 || >=0.1.0-alpha.12-dev <0.1.0-alpha.12",
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",
@@ -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
- <EncodingProvider defaults={{ bytes: "base64" }}>
45
- <JsonTransformDeclaration type={type} target="transport" />
46
- <JsonTransformDeclaration type={type} target="application" />
47
- </EncodingProvider>
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
- <ts.ObjectProperty name="name" jsValue={partName} />,
31
- <ts.ObjectProperty
32
- name="body"
33
- value={<JsonTransform itemRef={partRef} target="transport" type={props.part.body.type} />}
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
  }
@@ -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
- describe("Payload.MultiPart", () => {
8
- let __filename;
9
- let __dirname;
10
- let jpegContents: Uint8Array;
11
- let pngContents: Uint8Array;
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.skip("FormDataClient.HttpParts.ContentType", () => {
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: "image.jpg",
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: "image/jpg",
106
- filename: "image.jpg",
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: "image.jpg",
111
+ filename: "hello.jpg",
116
112
  },
117
113
  });
118
114
  });
119
115
  });
120
116
 
121
117
  describe("FormDataClient.HttpParts", () => {
122
- it.skip("should send json array and file array", async () => {
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.skip("should handle non-string float", async () => {
145
+ it("should handle non-string float", async () => {
150
146
  const client = new HttpPartsClient({
151
147
  allowInsecureConnection: true,
152
148
  retryOptions: {
@@ -31,7 +31,7 @@ export async function foo(
31
31
  body: [
32
32
  {
33
33
  name: "temperature",
34
- body: body.temperature,
34
+ body: body.temperature.body,
35
35
  },
36
36
  ],
37
37
  };
@@ -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
+ ```