Skip to content

Commit 57f9660

Browse files
authored
Merge pull request #60 from contentstack/staging
DX | 14-10-2024 | Release
2 parents a256752 + fdcdc21 commit 57f9660

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/core",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"type": "commonjs",
55
"main": "./dist/cjs/src/index.js",
66
"types": "./dist/cjs/src/index.d.ts",

src/lib/request.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ export async function getData(instance: AxiosInstance, url: string, data?: any)
1313
instance.defaults.headers.preview_token = livePreviewParams.preview_token;
1414
instance.defaults.headers.live_preview = livePreviewParams.live_preview;
1515
}
16-
17-
if (livePreviewParams.enable && livePreviewParams.live_preview && livePreviewParams.live_preview !== 'init') {
16+
if (livePreviewParams.enable) {
1817
// adds protocol so host is replaced and not appended
19-
if (livePreviewParams.host.split(0, 8) === 'https://') {
20-
instance.defaults.baseURL = livePreviewParams.host;
21-
} else {
22-
instance.defaults.baseURL = 'https://' + livePreviewParams.host;
18+
if (livePreviewParams.live_preview && livePreviewParams.live_preview !== 'init') {
19+
if (!livePreviewParams.host) {
20+
throw new Error('Host is required for live preview');
21+
}
22+
instance.defaults.baseURL = livePreviewParams.host.startsWith('https://') ? '' : 'https://' + livePreviewParams.host;
2323
}
2424
}
2525
}
@@ -30,7 +30,7 @@ export async function getData(instance: AxiosInstance, url: string, data?: any)
3030
} else {
3131
throw Error(JSON.stringify(response));
3232
}
33-
} catch (err) {
34-
throw Error(JSON.stringify(err));
33+
} catch (err: any) {
34+
throw new Error(`${err.message || JSON.stringify(err)}`);
3535
}
3636
}

test/request.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,16 @@ describe('Request tests', () => {
2424

2525
await expect(getData(client, url)).rejects.toThrowError(errorMessage);
2626
});
27+
28+
it('should throw error when host is required for live preview', async () => {
29+
const client = httpClient({});
30+
const url = '/your-api-endpoint';
31+
client.stackConfig = {
32+
live_preview: {
33+
enable: true,
34+
preview_token: 'someToken',
35+
},
36+
};
37+
await expect(getData(client, url, {})).rejects.toThrowError('Host is required for live preview');
38+
});
2739
});

0 commit comments

Comments
 (0)