Skip to content

Commit

Permalink
Merge pull request #93 from contentstack/development
Browse files Browse the repository at this point in the history
Staging | DX-1977
  • Loading branch information
nadeem-cs authored Jan 23, 2025
2 parents bb8cb7c + fc7816d commit 38e1d5d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
26 changes: 18 additions & 8 deletions test/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,46 +59,56 @@ 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 = {
live_preview: {
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 = {
live_preview: {
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);
});
});

0 comments on commit 38e1d5d

Please sign in to comment.