Skip to content

Commit

Permalink
chore(core): add awsExpectUnion function (#5367)
Browse files Browse the repository at this point in the history
* chore(core): add awsExpectUnion function

* chore: restructure export
  • Loading branch information
kuhe committed Oct 17, 2023
1 parent 9fa7bfe commit 51f316e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"url": "https://aws.amazon.com/javascript/"
},
"license": "Apache-2.0",
"dependencies": {
"@smithy/smithy-client": "^2.1.11"
},
"devDependencies": {
"@tsconfig/recommended": "1.0.1",
"concurrently": "7.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
export * from "./protocols/index";
1 change: 1 addition & 0 deletions packages/core/src/protocols/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./json/awsExpectUnion";
30 changes: 30 additions & 0 deletions packages/core/src/protocols/json/awsExpectUnion.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { awsExpectUnion } from "./awsExpectUnion";

describe(awsExpectUnion.name, () => {
it("ignores the __type field", () => {
expect(
awsExpectUnion({
K: "V",
__type: "X",
})
).toEqual({
K: "V",
});
});

it("throws when there are extra keys or no keys", () => {
expect(() =>
awsExpectUnion({
__type: "X",
})
).toThrowError();

expect(() =>
awsExpectUnion({
K: "V",
I: "S",
__type: "X",
})
).toThrowError();
});
});
17 changes: 17 additions & 0 deletions packages/core/src/protocols/json/awsExpectUnion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expectUnion } from "@smithy/smithy-client";

/**
* @internal
*
* Forwards to Smithy's expectUnion function, but also ignores
* the `__type` field if it is present.
*/
export const awsExpectUnion = (value: unknown): Record<string, any> | undefined => {
if (value == null) {
return undefined;
}
if (typeof value === "object" && "__type" in value) {
delete value.__type;
}
return expectUnion(value);
};

0 comments on commit 51f316e

Please sign in to comment.