Skip to content

Commit 51f316e

Browse files
authored
chore(core): add awsExpectUnion function (#5367)
* chore(core): add awsExpectUnion function * chore: restructure export
1 parent 9fa7bfe commit 51f316e

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

packages/core/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"url": "https://aws.amazon.com/javascript/"
2222
},
2323
"license": "Apache-2.0",
24+
"dependencies": {
25+
"@smithy/smithy-client": "^2.1.11"
26+
},
2427
"devDependencies": {
2528
"@tsconfig/recommended": "1.0.1",
2629
"concurrently": "7.0.0",

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export {};
1+
export * from "./protocols/index";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./json/awsExpectUnion";
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { awsExpectUnion } from "./awsExpectUnion";
2+
3+
describe(awsExpectUnion.name, () => {
4+
it("ignores the __type field", () => {
5+
expect(
6+
awsExpectUnion({
7+
K: "V",
8+
__type: "X",
9+
})
10+
).toEqual({
11+
K: "V",
12+
});
13+
});
14+
15+
it("throws when there are extra keys or no keys", () => {
16+
expect(() =>
17+
awsExpectUnion({
18+
__type: "X",
19+
})
20+
).toThrowError();
21+
22+
expect(() =>
23+
awsExpectUnion({
24+
K: "V",
25+
I: "S",
26+
__type: "X",
27+
})
28+
).toThrowError();
29+
});
30+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expectUnion } from "@smithy/smithy-client";
2+
3+
/**
4+
* @internal
5+
*
6+
* Forwards to Smithy's expectUnion function, but also ignores
7+
* the `__type` field if it is present.
8+
*/
9+
export const awsExpectUnion = (value: unknown): Record<string, any> | undefined => {
10+
if (value == null) {
11+
return undefined;
12+
}
13+
if (typeof value === "object" && "__type" in value) {
14+
delete value.__type;
15+
}
16+
return expectUnion(value);
17+
};

0 commit comments

Comments
 (0)