Skip to content

Commit cb76b1f

Browse files
authored
fix(util-endpoints): handle null-ish input for getEndpointHeaders (#1954)
1 parent 52b4789 commit cb76b1f

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

.changeset/fluffy-tigers-swim.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/util-endpoints": patch
3+
---
4+
5+
handle nullish input for getEndpointHeaders function

packages/util-endpoints/src/bdd/BinaryDecisionDiagram.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { EndpointObjectHeaders, ParameterObject } from "@smithy/types";
22

3-
import type { FunctionArgv } from "../types/shared";
3+
import type { Expression, FunctionArgv } from "../types/shared";
44

55
/**
66
* @internal
@@ -10,7 +10,11 @@ type BddCondition = [string, FunctionArgv] | [string, FunctionArgv, string];
1010
/**
1111
* @internal
1212
*/
13-
type BddResult = [-1] | [-1, string] | [string, Record<string, ParameterObject>, EndpointObjectHeaders];
13+
type BddResult =
14+
| [-1]
15+
| [-1, Expression]
16+
| [string, Record<string, ParameterObject>, EndpointObjectHeaders]
17+
| [string, Record<string, ParameterObject>];
1418

1519
/**
1620
* @internal

packages/util-endpoints/src/decideEndpoint.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,20 @@ describe(decideEndpoint.name, () => {
3636
headers: {},
3737
});
3838
});
39+
40+
it("evaluates templates in error messages", () => {
41+
const r = 100_000_000;
42+
const bdd = new Int32Array([0, 0, 0, 0, r + 0, -1]);
43+
const data = BinaryDecisionDiagram.from(
44+
bdd,
45+
2,
46+
[["isSet", [{ ref: "Region" }]]],
47+
[[-1, "Invalid region: {Region}"]]
48+
);
49+
expect(() =>
50+
decideEndpoint(data, {
51+
endpointParams: { Region: "us-west-2" },
52+
})
53+
).toThrow("Invalid region: us-west-2");
54+
});
3955
});

packages/util-endpoints/src/decideEndpoint.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { BinaryDecisionDiagram } from "./bdd/BinaryDecisionDiagram";
44
import type { EndpointResolverOptions } from "./types";
55
import { EndpointError } from "./types";
66
import { evaluateCondition } from "./utils/evaluateCondition";
7+
import { evaluateExpression } from "./utils/evaluateExpression";
78
import { getEndpointHeaders } from "./utils/getEndpointHeaders";
89
import { getEndpointProperties } from "./utils/getEndpointProperties";
910
import { getEndpointUrl } from "./utils/getEndpointUrl";
@@ -40,15 +41,15 @@ export const decideEndpoint = (bdd: BinaryDecisionDiagram, options: EndpointReso
4041
if (ref >= RESULT) {
4142
const result = results[ref - RESULT];
4243
if (result[0] === -1) {
43-
const [, errorMessage] = result;
44-
throw new EndpointError(errorMessage!);
44+
const [, errorExpression] = result;
45+
throw new EndpointError(evaluateExpression(errorExpression!, "Error", closure) as string);
4546
}
4647
const [url, properties, headers] = result;
4748

4849
return {
4950
url: getEndpointUrl(url, closure),
5051
properties: getEndpointProperties(properties, closure),
51-
headers: getEndpointHeaders(headers, closure),
52+
headers: getEndpointHeaders(headers ?? {}, closure),
5253
};
5354
}
5455

packages/util-endpoints/src/utils/getEndpointHeaders.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe(getEndpointHeaders.name, () => {
1616
});
1717

1818
it("should return an empty object if empty headers are provided", () => {
19+
expect(getEndpointHeaders(null as any, mockOptions)).toEqual({});
1920
expect(getEndpointHeaders({}, mockOptions)).toEqual({});
2021
expect(evaluateExpression).not.toHaveBeenCalled();
2122
});

packages/util-endpoints/src/utils/getEndpointHeaders.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { EndpointError } from "../types";
33
import { evaluateExpression } from "./evaluateExpression";
44

55
export const getEndpointHeaders = (headers: EndpointObjectHeaders, options: EvaluateOptions) =>
6-
Object.entries(headers).reduce(
6+
Object.entries(headers ?? {}).reduce(
77
(acc, [headerKey, headerVal]) => ({
88
...acc,
99
[headerKey]: headerVal.map((headerValEntry) => {

0 commit comments

Comments
 (0)