Skip to content

Commit c8492c8

Browse files
doankhoidevrntdependabot-preview[bot]
authored
feature: adding api_base option (#96)
* feature: adding api_base option * chore: optional attribute * chore: update docs of api base prop in provider * test: add cypress test for api base * test: add integration test for api base * chore(deps-dev): bump @testing-library/react from 10.4.9 to 11.0.0 Bumps [@testing-library/react](https://github.com/testing-library/react-testing-library) from 10.4.9 to 11.0.0. - [Release notes](https://github.com/testing-library/react-testing-library/releases) - [Changelog](https://github.com/testing-library/react-testing-library/blob/master/CHANGELOG.md) - [Commits](testing-library/react-testing-library@v10.4.9...v11.0.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * chore(deps-dev): bump @testing-library/react from 11.0.0 to 11.0.2 Bumps [@testing-library/react](https://github.com/testing-library/react-testing-library) from 11.0.0 to 11.0.2. - [Release notes](https://github.com/testing-library/react-testing-library/releases) - [Changelog](https://github.com/testing-library/react-testing-library/blob/master/CHANGELOG.md) - [Commits](testing-library/react-testing-library@v11.0.0...v11.0.2) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * chore(deps-dev): bump husky from 4.2.5 to 4.3.0 Bumps [husky](https://github.com/typicode/husky) from 4.2.5 to 4.3.0. - [Release notes](https://github.com/typicode/husky/releases) - [Changelog](https://github.com/typicode/husky/blob/master/CHANGELOG.md) - [Commits](typicode/husky@v4.2.5...v4.3.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * chore(deps-dev): bump @testing-library/react from 11.0.2 to 11.0.4 Bumps [@testing-library/react](https://github.com/testing-library/react-testing-library) from 11.0.2 to 11.0.4. - [Release notes](https://github.com/testing-library/react-testing-library/releases) - [Changelog](https://github.com/testing-library/react-testing-library/blob/master/CHANGELOG.md) - [Commits](testing-library/react-testing-library@v11.0.2...v11.0.4) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * chore(deps-dev): bump cypress from 5.1.0 to 5.2.0 Bumps [cypress](https://github.com/cypress-io/cypress) from 5.1.0 to 5.2.0. - [Release notes](https://github.com/cypress-io/cypress/releases) - [Commits](cypress-io/cypress@v5.1.0...v5.2.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * chore(deps-dev): bump typescript from 4.0.2 to 4.0.3 Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.0.2 to 4.0.3. - [Release notes](https://github.com/Microsoft/TypeScript/releases) - [Commits](microsoft/TypeScript@v4.0.2...v4.0.3) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * feat: add api base prop in provider * chore(deps-dev): bump tsdx from 0.13.3 to 0.14.0 Bumps [tsdx](https://github.com/formium/tsdx) from 0.13.3 to 0.14.0. - [Release notes](https://github.com/formium/tsdx/releases) - [Commits](jaredpalmer/tsdx@v0.13.3...v0.14.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * test: update jest config * chore: make api base an optional prop * chore: update docs of api base prop in provider * test: add cypress test for api base * test: add integration test for api base Co-authored-by: devrnt <devrient.jonas@gmail.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
1 parent 8a1e699 commit c8492c8

File tree

8 files changed

+86
-10
lines changed

8 files changed

+86
-10
lines changed

cypress/integration/provider.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,16 @@ describe('provider with events', () => {
6060
cy.get('[data-cy=onHideText]').should('have.text', 'hide was called');
6161
});
6262
});
63+
64+
describe('provider with `apiBase`', () => {
65+
it('should set `api_base` if provided', () => {
66+
cy.visit('/providerApi');
67+
68+
cy.get('p').should('be.visible');
69+
70+
cy.window().should('have.deep.property', 'intercomSettings', {
71+
app_id: 'jcabc7e3',
72+
api_base: 'https://jcabc7e3.intercom-messenger.com',
73+
});
74+
});
75+
});

example/app.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ProviderPage,
77
UseIntercomPage,
88
ProviderEventsPage,
9+
ProviderApiPage,
910
UseIntercomTourPage,
1011
} from './modules';
1112

@@ -43,6 +44,7 @@ const App = () => {
4344
<Router>
4445
<Route path="/provider" component={ProviderPage} />
4546
<Route path="/providerEvents" component={ProviderEventsPage} />
47+
<Route path="/providerApi" component={ProviderApiPage} />
4648
<Route path="/useIntercom" component={UseIntercomPage} />
4749
<Route path="/useIntercomTour" component={UseIntercomTourPage} />
4850
<Route path="/" exact>

example/modules/provider/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default as ProviderPage } from './provider';
22
export { default as ProviderEventsPage } from './providerEvents';
3+
export { default as ProviderApiPage } from './providerApi';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as React from 'react';
2+
3+
import { IntercomProvider } from '../../../.';
4+
5+
const Provider = () => {
6+
return (
7+
<IntercomProvider
8+
appId="jcabc7e3"
9+
apiBase="https://jcabc7e3.intercom-messenger.com"
10+
autoBoot
11+
>
12+
<p>Intercom children</p>
13+
</IntercomProvider>
14+
);
15+
};
16+
17+
export default Provider;

src/contextTypes.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type IntercomContextValues = {
77
* @remarks This is useful in situations like a one-page Javascript based application
88
* where the user may not be logged in when the page loads.
99
* @param props the standard optional intercom props
10-
*
10+
*
1111
* @see {@link https://developers.intercom.com/installing-intercom/docs/intercom-javascript#section-intercomboot-intercomsettings}
1212
1313
*/
@@ -21,7 +21,7 @@ export type IntercomContextValues = {
2121
* given device or computer will keep these conversations in the Messenger for one week.
2222
*
2323
* @remarks This method will effectively clear out any user data that you have been passing through the JS API.
24-
24+
2525
* @see {@link https://developers.intercom.com/installing-intercom/docs/intercom-javascript#section-intercomshutdown}
2626
*/
2727
shutdown: () => void;
@@ -153,4 +153,12 @@ export type IntercomProviderProps = {
153153
* @remarks can be used for multistaged environments
154154
*/
155155
shouldInitialize?: boolean;
156+
157+
/**
158+
* If you need to route your Messenger requests through a different endpoint than the default
159+
*
160+
* @remarks Generally speaking, this is not needed.
161+
* Format https://${INTERCOM_APP_ID}.intercom-messenger.com
162+
*/
163+
apiBase?: string;
156164
};

src/provider.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import * as React from 'react';
2-
3-
import * as logger from './logger';
42
import IntercomAPI from './api';
5-
import initialize from './initialize';
63
import IntercomContext from './context';
74
import { IntercomContextValues, IntercomProviderProps } from './contextTypes';
8-
import { IntercomProps, RawIntercomBootProps } from './types';
5+
import initialize from './initialize';
6+
import * as logger from './logger';
97
import { mapIntercomPropsToRawIntercomProps } from './mappers';
8+
import { IntercomProps, RawIntercomBootProps } from './types';
109
import { isEmptyObject, isSSR } from './utils';
1110

1211
export const IntercomProvider = ({
@@ -17,6 +16,7 @@ export const IntercomProvider = ({
1716
onShow,
1817
onUnreadCountChange,
1918
shouldInitialize = !isSSR,
19+
apiBase,
2020
...rest
2121
}: IntercomProviderProps) => {
2222
if (!isEmptyObject(rest))
@@ -29,6 +29,8 @@ export const IntercomProvider = ({
2929
);
3030

3131
const memoizedAppId = React.useRef(appId);
32+
const memoizedApiBase = React.useRef(apiBase);
33+
3234
const isBooted = React.useRef(autoBoot);
3335
const memoizedShouldInitialize = React.useRef(shouldInitialize);
3436

@@ -41,8 +43,14 @@ export const IntercomProvider = ({
4143
IntercomAPI('onUnreadCountChange', onUnreadCountChange);
4244

4345
if (autoBoot) {
44-
IntercomAPI('boot', { app_id: memoizedAppId.current });
45-
window.intercomSettings = { app_id: memoizedAppId.current };
46+
IntercomAPI('boot', {
47+
app_id: memoizedAppId.current,
48+
...(memoizedApiBase.current && { api_base: memoizedApiBase.current }),
49+
});
50+
window.intercomSettings = {
51+
app_id: memoizedAppId.current,
52+
...(memoizedApiBase.current && { api_base: memoizedApiBase.current }),
53+
};
4654
}
4755
}
4856

@@ -83,6 +91,7 @@ export const IntercomProvider = ({
8391

8492
const metaData: RawIntercomBootProps = {
8593
app_id: memoizedAppId.current,
94+
...(memoizedApiBase.current && { api_base: memoizedApiBase.current }),
8695
...(props && mapIntercomPropsToRawIntercomProps(props)),
8796
};
8897

src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ export type IntercomMethod =
244244

245245
export type RawIntercomProps = RawMessengerAttributes & RawDataAttributes;
246246

247-
export type RawIntercomBootProps = { app_id: string } & RawIntercomProps;
247+
export type RawIntercomBootProps = {
248+
app_id: string;
249+
api_base?: string;
250+
} & RawIntercomProps;
248251

249252
export type IntercomProps = MessengerAttributes & DataAttributes;
250253

test/intercomProvider.test.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { render } from '@testing-library/react';
2+
import { act, render } from '@testing-library/react';
33
import { renderHook } from '@testing-library/react-hooks';
44

55
import config from '../config';
@@ -49,4 +49,27 @@ describe('IntercomProvider', () => {
4949

5050
expect(mockOnShow).not.toBeCalled();
5151
});
52+
53+
test('should set `window.intercomSettings.apiBase` on autoBoot', () => {
54+
const apiBase = `https://${INTERCOM_APP_ID}.intercom-messenger.com`;
55+
56+
const { result } = renderHook(() => useIntercom(), {
57+
wrapper: ({ children }) => (
58+
<IntercomProvider appId={INTERCOM_APP_ID} apiBase={apiBase}>
59+
{children}
60+
</IntercomProvider>
61+
),
62+
});
63+
64+
const { boot } = result.current;
65+
66+
act(() => {
67+
boot();
68+
});
69+
70+
expect(window.intercomSettings).toEqual({
71+
app_id: INTERCOM_APP_ID,
72+
api_base: apiBase,
73+
});
74+
});
5275
});

0 commit comments

Comments
 (0)