Skip to content

Commit e6cba69

Browse files
authored
test(middleware-sdk-s3): add type transform integration test (#6450)
1 parent c8e8f28 commit e6cba69

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
import { GetObjectCommand, GetObjectCommandInput, S3, S3Client } from "@aws-sdk/client-s3";
2+
import type { AssertiveClient, BrowserClient, NodeJsClient, NoUndefined, UncheckedClient } from "@smithy/types";
3+
4+
describe("S3 client transform integration test", () => {
5+
it("has the expected compilation behavior", async () => {
6+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
7+
async function compileOnly() {
8+
// default, no validation for undefined values
9+
const s3 = new S3({});
10+
11+
await s3.listBuckets();
12+
await s3.listBuckets({});
13+
14+
// @ts-expect-error (missing Bucket)
15+
s3.getObject({
16+
Key: undefined,
17+
});
18+
19+
s3.getObject({
20+
// @ts-expect-error (unrecognized field)
21+
UnknownProperty: undefined,
22+
});
23+
24+
const get = await s3.getObject({
25+
Bucket: undefined,
26+
Key: undefined,
27+
});
28+
29+
// @ts-expect-error (Body may be undefined)
30+
await get.Body.transformToString();
31+
32+
await get.Body?.transformToString();
33+
await get.Body!.transformToString();
34+
}
35+
});
36+
37+
it("assertive client transform", async () => {
38+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
39+
async function compileOnly() {
40+
// the assertive client requires that inputs/outputs are of the right type
41+
// excluding '| undefined'.
42+
const s3_assertive = new S3() as AssertiveClient<S3>;
43+
const s3Client_assertive = new S3Client() as AssertiveClient<S3Client>;
44+
45+
await s3_assertive.listBuckets();
46+
await s3_assertive.listBuckets({});
47+
48+
s3_assertive.getObject({
49+
Bucket: "undefined",
50+
// @ts-expect-error (undefined not assignable to string)
51+
Key: undefined,
52+
});
53+
s3Client_assertive.send(
54+
new GetObjectCommand({
55+
Bucket: "undefined",
56+
// type transform is unable to validate within Command ctor.
57+
Key: undefined,
58+
})
59+
);
60+
61+
{
62+
const getObjectInput: NoUndefined<GetObjectCommandInput> = {
63+
Bucket: "undefined",
64+
// @ts-expect-error (undefined not assignable to string)
65+
Key: undefined,
66+
// optional params can still be undefined.
67+
SSECustomerAlgorithm: undefined,
68+
};
69+
const get = await s3Client_assertive.send(new GetObjectCommand(getObjectInput));
70+
// @ts-expect-error (Body is possibly undefined)
71+
await get.Body.transformToString();
72+
}
73+
74+
// @ts-expect-error (missing Key)
75+
s3_assertive.getObject({
76+
Bucket: "undefined",
77+
});
78+
79+
{
80+
const get = await s3_assertive.getObject({
81+
Bucket: "undefined",
82+
Key: "undefined",
83+
});
84+
85+
// @ts-expect-error (Body is optional)
86+
await get.Body.transformToString();
87+
88+
await get.Body?.transformToString();
89+
await get.Body!.transformToString();
90+
}
91+
92+
{
93+
const get = await s3Client_assertive.send(
94+
new GetObjectCommand({
95+
Bucket: "undefined",
96+
Key: "undefined",
97+
})
98+
);
99+
100+
// @ts-expect-error (Body is optional)
101+
await get.Body.transformToString();
102+
103+
await get.Body?.transformToString();
104+
await get.Body!.transformToString();
105+
}
106+
}
107+
});
108+
109+
it("unchecked client transform", async () => {
110+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
111+
async function compileOnly() {
112+
// unchecked client also removes the possibility
113+
// of optionality '?' in addition to '| undefined'.
114+
const s3_unchecked = new S3({}) as UncheckedClient<S3>;
115+
const s3Client_unchecked = new S3Client() as UncheckedClient<S3Client>;
116+
117+
await s3_unchecked.listBuckets();
118+
await s3_unchecked.listBuckets({});
119+
120+
s3_unchecked.getObject({
121+
Bucket: "undefined",
122+
// @ts-expect-error (undefined not assignable to string)
123+
Key: undefined,
124+
});
125+
s3_unchecked.send(
126+
new GetObjectCommand({
127+
Bucket: "undefined",
128+
// type transform is unable to validate within Command ctor.
129+
Key: undefined,
130+
})
131+
);
132+
133+
{
134+
const getObjectInput: NoUndefined<GetObjectCommandInput> = {
135+
Bucket: "undefined",
136+
// @ts-expect-error (undefined not assignable to string)
137+
Key: undefined,
138+
// optional params can still be undefined.
139+
SSECustomerAlgorithm: undefined,
140+
};
141+
const get = await s3Client_unchecked.send(new GetObjectCommand(getObjectInput));
142+
await get.Body.transformToString();
143+
}
144+
145+
// @ts-expect-error (missing Key)
146+
s3_unchecked.getObject({
147+
Bucket: "undefined",
148+
});
149+
150+
{
151+
const get = await s3_unchecked.getObject({
152+
Bucket: "undefined",
153+
Key: "undefined",
154+
});
155+
156+
await get.Body.transformToString();
157+
}
158+
159+
{
160+
const get = await s3Client_unchecked.send(
161+
new GetObjectCommand({
162+
Bucket: "undefined",
163+
Key: "undefined",
164+
})
165+
);
166+
167+
await get.Body.transformToString();
168+
}
169+
}
170+
});
171+
172+
it("platform-specific client transforms", async () => {
173+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
174+
async function compileOnly() {
175+
// platform specific clients.
176+
const s3_browser = new S3({}) as BrowserClient<S3>;
177+
const s3Client_browser = new S3Client() as BrowserClient<S3Client>;
178+
const s3_node = new S3() as NodeJsClient<S3>;
179+
const s3Client_node = new S3() as NodeJsClient<S3>;
180+
181+
{
182+
const get = await s3Client_browser.send(
183+
new GetObjectCommand({
184+
Bucket: "undefined",
185+
Key: "undefined",
186+
})
187+
);
188+
get.Body!.tee();
189+
}
190+
{
191+
const get = await s3_browser.getObject({ Bucket: "undefined", Key: "undefined" });
192+
get.Body!.tee();
193+
}
194+
195+
{
196+
const get = await s3Client_node.send(
197+
new GetObjectCommand({
198+
Bucket: "undefined",
199+
Key: "undefined",
200+
})
201+
);
202+
get.Body!.pause();
203+
}
204+
{
205+
const get = await s3_node.getObject({ Bucket: "undefined", Key: "undefined" });
206+
get.Body!.pause();
207+
}
208+
}
209+
});
210+
});

0 commit comments

Comments
 (0)