Skip to content

Commit ea7f79a

Browse files
committed
fix: update default region handling in stack function and improve error messages for invalid regions
1 parent f521bd7 commit ea7f79a

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

src/lib/contentstack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let version = '{{VERSION}}';
3434
*/
3535
// eslint-disable-next-line @typescript-eslint/naming-convention
3636
export function stack(config: StackConfig): StackClass {
37-
const DEFAULT_HOST = Utility.getHostforRegion(config.region || "", config.host);
37+
const DEFAULT_HOST = Utility.getHostforRegion(config.region || "aws_na", config.host);
3838

3939
let defaultConfig = {
4040
defaultHostname: DEFAULT_HOST,

test/unit/contentstack.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ describe("Contentstack", () => {
396396

397397
const stackInstance = createStackInstance(config);
398398

399-
expect(getHostforRegionSpy).toHaveBeenCalledWith(undefined, undefined);
399+
// When no region is provided, the default parameter "aws_na" is used
400+
expect(getHostforRegionSpy).toHaveBeenCalledWith("aws_na", undefined);
400401
expect(stackInstance).toBeInstanceOf(Stack);
401402

402403
getHostforRegionSpy.mockRestore();

test/unit/stack.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ describe('Stack class tests', () => {
285285

286286
it('should throw error for invalid region', async () => {
287287
await expect(stack.setHost('invalid_region')).rejects.toThrow(
288-
'Unable to set host using the provided region. Please provide a valid region.'
288+
'Invalid region: invalid_region'
289289
);
290290
});
291291

test/unit/utils.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,19 @@ describe("Utils functions", () => {
118118

119119
it("should throw error for invalid region", () => {
120120
expect(() => getHostforRegion("invalid_region")).toThrow(
121-
"Unable to set host using the provided region. Please provide a valid region."
121+
"Invalid region: invalid_region"
122122
);
123123
});
124124

125125
it("should throw error for empty string region", () => {
126126
expect(() => getHostforRegion("")).toThrow(
127-
"Unable to set host using the provided region. Please provide a valid region."
127+
"Empty region provided. Please put valid region."
128128
);
129129
});
130130

131131
it("should throw error for null region", () => {
132132
expect(() => getHostforRegion(null as any)).toThrow(
133-
"Unable to set host using the provided region. Please provide a valid region."
133+
"Cannot read properties of null (reading 'toLowerCase')"
134134
);
135135
});
136136

@@ -142,15 +142,15 @@ describe("Utils functions", () => {
142142

143143
it("should throw error for non-string region types", () => {
144144
expect(() => getHostforRegion(123 as any)).toThrow(
145-
"Unable to set host using the provided region. Please provide a valid region."
145+
"region.toLowerCase is not a function"
146146
);
147147

148148
expect(() => getHostforRegion({} as any)).toThrow(
149-
"Unable to set host using the provided region. Please provide a valid region."
149+
"region.toLowerCase is not a function"
150150
);
151151

152152
expect(() => getHostforRegion([] as any)).toThrow(
153-
"Unable to set host using the provided region. Please provide a valid region."
153+
"region.toLowerCase is not a function"
154154
);
155155
});
156156

0 commit comments

Comments
 (0)