Skip to content

Commit 6031f9d

Browse files
committed
fix: reverts the endpoints helper method integration
1 parent 7d8dce1 commit 6031f9d

File tree

10 files changed

+181
-638
lines changed

10 files changed

+181
-638
lines changed

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fileignoreconfig:
22
- filename: package-lock.json
3-
checksum: 46c0d87a82455d4b2bae3347f7361dda71d2b979426b1c95ef707a9166c17778
3+
checksum: f70da9c8a690f7b3a2eb2461f0492eef5c61025e746593161756eefcbc518517
44
- filename: test/unit/contentstack.spec.ts
55
checksum: d5b99c01459ab8bc597baaa9e6cc4aa91ac6d9bf78af08e1d0220d0c5db3d0b3
66
- filename: test/unit/utils.spec.ts

package-lock.json

Lines changed: 155 additions & 262 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/delivery-sdk",
3-
"version": "4.10.2",
3+
"version": "4.10.3",
44
"type": "module",
55
"license": "MIT",
66
"main": "./dist/legacy/index.cjs",
@@ -36,7 +36,7 @@
3636
},
3737
"dependencies": {
3838
"@contentstack/core": "^1.3.3",
39-
"@contentstack/utils": "^1.6.2",
39+
"@contentstack/utils": "1.5.0",
4040
"axios": "^1.13.1",
4141
"humps": "^2.0.1"
4242
},

src/lib/contentstack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { httpClient, retryRequestHandler, retryResponseErrorHandler, retryRespon
22
import { AxiosRequestHeaders } from 'axios';
33
import { handleRequest } from './cache';
44
import { Stack as StackClass } from './stack';
5-
import { Policy, StackConfig, ContentstackPlugin } from './types';
5+
import { Policy, StackConfig, ContentstackPlugin, Region } from './types';
66
import * as Utility from './utils';
77
import * as Utils from '@contentstack/utils';
88
export { Utils };
@@ -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 || "aws_na", config.host);
37+
const DEFAULT_HOST = Utility.getHostforRegion(config.region || Region.US, config.host);
3838

3939
let defaultConfig = {
4040
defaultHostname: DEFAULT_HOST,

src/lib/stack.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { synchronization } from './synchronization';
88
import {TaxonomyQuery} from './taxonomy-query';
99
import { GlobalFieldQuery } from './global-field-query';
1010
import { GlobalField } from './global-field';
11-
import { getHostforRegion } from './utils';
1211

1312
export class Stack {
1413
readonly config: StackConfig;
@@ -229,19 +228,4 @@ export class Stack {
229228
return this;
230229
}
231230

232-
/**
233-
* @method setHost
234-
* @memberOf Stack
235-
* @description Sets the host based on cloud region
236-
* @param {String} cloudRegion - Cloud region (e.g., 'aws_na', 'aws_eu')
237-
* @param {String} host - Optional custom host
238-
* @return {Promise<string>} - Returns the host URL
239-
* @instance
240-
* */
241-
async setHost(region: string = "aws_na", host?: string): Promise<void> {
242-
const resolvedHost = getHostforRegion(region, host);
243-
244-
this._client.defaults.baseURL = `https://${resolvedHost}`;
245-
}
246-
247231
}

src/lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export interface StackConfig extends HttpClientParams {
7676
environment: string;
7777
branch?: string;
7878
early_access?: string[];
79-
region?: string;
79+
region?: Region;
8080
locale?: string;
8181
plugins?: ContentstackPlugin[];
8282
logHandler?: (level: string, data: any) => void;

src/lib/utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { Region, params } from './types';
2-
import { getContentstackEndpoint } from '@contentstack/utils';
32

4-
export function getHostforRegion(region: string = "aws_na", host?: string): string {
3+
export function getHostforRegion(region: Region = Region.US, host?: string): string {
54
if (host) return host;
65

7-
return getContentstackEndpoint(region, 'contentDelivery', true) as string;
6+
let url = 'cdn.contentstack.io';
7+
if (region !== Region.US) {
8+
url = region.toString().toLowerCase() + '-cdn.contentstack.com';
9+
}
10+
11+
return url;
812
}
913

1014
export function isBrowser() {

test/unit/contentstack.spec.ts

Lines changed: 0 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -263,147 +263,6 @@ describe("Contentstack", () => {
263263
done();
264264
});
265265

266-
describe('getHostforRegion integration in stack creation', () => {
267-
it('should use getHostforRegion to set default hostname for aws_na region', () => {
268-
const getHostforRegionSpy = jest.spyOn(utils, 'getHostforRegion');
269-
const config = {
270-
apiKey: "apiKey",
271-
deliveryToken: "delivery",
272-
environment: "env",
273-
region: "aws_na",
274-
};
275-
276-
const stackInstance = createStackInstance(config);
277-
278-
expect(getHostforRegionSpy).toHaveBeenCalledWith("aws_na", undefined);
279-
expect(stackInstance).toBeInstanceOf(Stack);
280-
281-
getHostforRegionSpy.mockRestore();
282-
});
283-
284-
it('should use getHostforRegion to set default hostname for eu region', () => {
285-
const getHostforRegionSpy = jest.spyOn(utils, 'getHostforRegion');
286-
const config = {
287-
apiKey: "apiKey",
288-
deliveryToken: "delivery",
289-
environment: "env",
290-
region: "eu",
291-
};
292-
293-
const stackInstance = createStackInstance(config);
294-
295-
expect(getHostforRegionSpy).toHaveBeenCalledWith("eu", undefined);
296-
expect(stackInstance).toBeInstanceOf(Stack);
297-
298-
getHostforRegionSpy.mockRestore();
299-
});
300-
301-
it('should use getHostforRegion with custom host when both region and host are provided', () => {
302-
const getHostforRegionSpy = jest.spyOn(utils, 'getHostforRegion');
303-
const config = {
304-
apiKey: "apiKey",
305-
deliveryToken: "delivery",
306-
environment: "env",
307-
region: "eu",
308-
host: CUSTOM_HOST,
309-
};
310-
311-
const stackInstance = createStackInstance(config);
312-
313-
expect(getHostforRegionSpy).toHaveBeenCalledWith("eu", CUSTOM_HOST);
314-
expect(stackInstance).toBeInstanceOf(Stack);
315-
316-
getHostforRegionSpy.mockRestore();
317-
});
318-
319-
it('should use getHostforRegion for azure-na region', () => {
320-
const getHostforRegionSpy = jest.spyOn(utils, 'getHostforRegion');
321-
const config = {
322-
apiKey: "apiKey",
323-
deliveryToken: "delivery",
324-
environment: "env",
325-
region: "azure-na",
326-
};
327-
328-
const stackInstance = createStackInstance(config);
329-
330-
expect(getHostforRegionSpy).toHaveBeenCalledWith("azure-na", undefined);
331-
expect(stackInstance).toBeInstanceOf(Stack);
332-
333-
getHostforRegionSpy.mockRestore();
334-
});
335-
336-
it('should use getHostforRegion for gcp-na region', () => {
337-
const getHostforRegionSpy = jest.spyOn(utils, 'getHostforRegion');
338-
const config = {
339-
apiKey: "apiKey",
340-
deliveryToken: "delivery",
341-
environment: "env",
342-
region: "gcp-na",
343-
};
344-
345-
const stackInstance = createStackInstance(config);
346-
347-
expect(getHostforRegionSpy).toHaveBeenCalledWith("gcp-na", undefined);
348-
expect(stackInstance).toBeInstanceOf(Stack);
349-
350-
getHostforRegionSpy.mockRestore();
351-
});
352-
353-
it('should use getHostforRegion for gcp-eu region', () => {
354-
const getHostforRegionSpy = jest.spyOn(utils, 'getHostforRegion');
355-
const config = {
356-
apiKey: "apiKey",
357-
deliveryToken: "delivery",
358-
environment: "env",
359-
region: "gcp-eu",
360-
};
361-
362-
const stackInstance = createStackInstance(config);
363-
364-
expect(getHostforRegionSpy).toHaveBeenCalledWith("gcp-eu", undefined);
365-
expect(stackInstance).toBeInstanceOf(Stack);
366-
367-
getHostforRegionSpy.mockRestore();
368-
});
369-
370-
it('should handle getHostforRegion error gracefully', () => {
371-
const getHostforRegionSpy = jest.spyOn(utils, 'getHostforRegion').mockImplementation(() => {
372-
throw new Error('Unable to set host using the provided region. Please provide a valid region.');
373-
});
374-
375-
const config = {
376-
apiKey: "apiKey",
377-
deliveryToken: "delivery",
378-
environment: "env",
379-
region: "invalid_region",
380-
};
381-
382-
expect(() => createStackInstance(config)).toThrow(
383-
'Unable to set host using the provided region. Please provide a valid region.'
384-
);
385-
386-
getHostforRegionSpy.mockRestore();
387-
});
388-
389-
it('should use getHostforRegion with undefined region when no region is provided', () => {
390-
const getHostforRegionSpy = jest.spyOn(utils, 'getHostforRegion');
391-
const config = {
392-
apiKey: "apiKey",
393-
deliveryToken: "delivery",
394-
environment: "env",
395-
};
396-
397-
const stackInstance = createStackInstance(config);
398-
399-
// When no region is provided, the default parameter "aws_na" is used
400-
expect(getHostforRegionSpy).toHaveBeenCalledWith("aws_na", undefined);
401-
expect(stackInstance).toBeInstanceOf(Stack);
402-
403-
getHostforRegionSpy.mockRestore();
404-
});
405-
});
406-
407266
describe('locale configuration', () => {
408267
it('should set locale in params when locale is provided in config', () => {
409268
const config = {

test/unit/stack.spec.ts

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -236,70 +236,5 @@ describe('Stack class tests', () => {
236236
expect(stack.config.debug).toEqual(false);
237237
});
238238

239-
describe('setHost method integration tests', () => {
240-
it('should set baseURL correctly for aws_na region', async () => {
241-
await stack.setHost('aws_na');
242-
expect(client.defaults.baseURL).toBe('https://cdn.contentstack.io');
243-
});
244-
245-
it('should set baseURL correctly for eu region', async () => {
246-
await stack.setHost('eu');
247-
expect(client.defaults.baseURL).toBe('https://eu-cdn.contentstack.com');
248-
});
249-
250-
it('should set baseURL correctly for au region', async () => {
251-
await stack.setHost('au');
252-
expect(client.defaults.baseURL).toBe('https://au-cdn.contentstack.com');
253-
});
254-
255-
it('should set baseURL correctly for azure-na region', async () => {
256-
await stack.setHost('azure-na');
257-
expect(client.defaults.baseURL).toBe('https://azure-na-cdn.contentstack.com');
258-
});
259-
260-
it('should set baseURL correctly for gcp-na region', async () => {
261-
await stack.setHost('gcp-na');
262-
expect(client.defaults.baseURL).toBe('https://gcp-na-cdn.contentstack.com');
263-
});
264-
265-
it('should set baseURL correctly for gcp-eu region', async () => {
266-
await stack.setHost('gcp-eu');
267-
expect(client.defaults.baseURL).toBe('https://gcp-eu-cdn.contentstack.com');
268-
});
269-
270-
it('should prioritize custom host over region', async () => {
271-
const customHost = 'custom.example.com';
272-
await stack.setHost('eu', customHost);
273-
expect(client.defaults.baseURL).toBe(`https://${customHost}`);
274-
});
275-
276-
it('should handle case insensitive regions', async () => {
277-
await stack.setHost('EU');
278-
expect(client.defaults.baseURL).toBe('https://eu-cdn.contentstack.com');
279-
});
280-
281-
it('should use default region when no region provided', async () => {
282-
await stack.setHost();
283-
expect(client.defaults.baseURL).toBe('https://cdn.contentstack.io');
284-
});
285-
286-
it('should throw error for invalid region', async () => {
287-
await expect(stack.setHost('invalid_region')).rejects.toThrow(
288-
'Invalid region: invalid_region'
289-
);
290-
});
291-
292-
it('should handle region aliases correctly', async () => {
293-
await stack.setHost('na');
294-
expect(client.defaults.baseURL).toBe('https://cdn.contentstack.io');
295-
296-
await stack.setHost('us');
297-
expect(client.defaults.baseURL).toBe('https://cdn.contentstack.io');
298-
299-
await stack.setHost('aws-na');
300-
expect(client.defaults.baseURL).toBe('https://cdn.contentstack.io');
301-
});
302-
});
303-
304239
});
305240

0 commit comments

Comments
 (0)