diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a0e2eb..282a865 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,13 @@ ## Change log +### Version: 1.2.0 +#### Date: Jan-24-2025 + - Fix: URL change for Live Preview + +### Version: 1.1.4 +#### Date: Jan-17-2025 + - Fixed defaultAdapters implementation + - Updated the test cases + ### Version: 1.1.3 #### Date: Oct-22-2024 - Fix: getData to receive params and headers both in data diff --git a/package-lock.json b/package-lock.json index 866216c..be01418 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/core", - "version": "1.1.5", + "version": "1.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/core", - "version": "1.1.5", + "version": "1.2.0", "license": "MIT", "dependencies": { "axios": "^1.7.9", diff --git a/package.json b/package.json index 6c590e7..43dfd73 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/core", - "version": "1.1.5", + "version": "1.2.0", "type": "commonjs", "main": "./dist/cjs/src/index.js", "types": "./dist/cjs/src/index.d.ts", diff --git a/src/lib/request.ts b/src/lib/request.ts index c34994a..5e58006 100644 --- a/src/lib/request.ts +++ b/src/lib/request.ts @@ -19,7 +19,7 @@ export async function getData(instance: AxiosInstance, url: string, data?: any) if (!livePreviewParams.host) { throw new Error('Host is required for live preview'); } - instance.defaults.baseURL = livePreviewParams.host.startsWith('https://') ? '' : 'https://' + livePreviewParams.host; + url = (livePreviewParams.host.startsWith('https://') ? '' : 'https://') + livePreviewParams.host + url; } } } diff --git a/test/request.spec.ts b/test/request.spec.ts index d0836d0..5df7ff0 100644 --- a/test/request.spec.ts +++ b/test/request.spec.ts @@ -59,9 +59,12 @@ describe('Request tests', () => { }); it('should set baseURL correctly when host is provided without https://', async () => { - const client = httpClient({}); + const client = httpClient({ + defaultHostname: 'example.com', + }); const mock = new MockAdapter(client as any); const url = '/your-api-endpoint'; + const livePreviewURL = 'https://rest-preview.com' + url; const mockResponse = { data: 'mocked' }; client.stackConfig = { @@ -69,21 +72,26 @@ describe('Request tests', () => { enable: true, preview_token: 'someToken', live_preview: 'someHash', - host: 'example.com', + host: 'rest-preview.com', }, }; - mock.onGet(url).reply(200, mockResponse); + mock.onGet(livePreviewURL).reply(200, mockResponse); const result = await getData(client, url, {}); - expect(client.defaults.baseURL).toBe('https://example.com'); + expect(client.defaults.baseURL).toBe('https://example.com:443/v3'); + expect(client.stackConfig.live_preview.host).toBe('rest-preview.com'); + expect(mock.history.get[0].url).toBe(livePreviewURL); expect(result).toEqual(mockResponse); }); it('should not modify baseURL when host is already prefixed with https://', async () => { - const client = httpClient({}); + const client = httpClient({ + defaultHostname: 'example.com', + }); const mock = new MockAdapter(client as any); const url = '/your-api-endpoint'; + const livePreviewURL = 'https://rest-preview.com' + url; const mockResponse = { data: 'mocked' }; client.stackConfig = { @@ -91,14 +99,16 @@ describe('Request tests', () => { enable: true, preview_token: 'someToken', live_preview: 'someHash', - host: 'https://example.com', + host: 'https://rest-preview.com', }, }; - mock.onGet(url).reply(200, mockResponse); + mock.onGet(livePreviewURL).reply(200, mockResponse); const result = await getData(client, url, {}); - expect(client.stackConfig.live_preview.host).toBe('https://example.com'); + expect(client.defaults.baseURL).toBe('https://example.com:443/v3'); + expect(client.stackConfig.live_preview.host).toBe('https://rest-preview.com'); + expect(mock.history.get[0].url).toBe(livePreviewURL); expect(result).toEqual(mockResponse); }); });