diff --git a/README.md b/README.md index ce3a84dc1..3b8fed134 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,8 @@ You must pass at least the `writeKey`. Additional configuration options are list | `defaultSettings` | undefined | Settings that will be used if the request to get the settings from Segment fails | | `autoAddSegmentDestination`| true | Set to false to skip adding the SegmentDestination plugin | | `storePersistor` | undefined | A custom persistor for the store that `analytics-react-native` leverages. Must match `Persistor` interface exported from [sovran-react-native](https://github.com/segmentio/sovran-react-native).| +| `proxy` | undefined | `proxy` is a batch url to post to instead of 'https://api.segment.io/v1/b'. | + \* The default value of `debug` will be false in production. diff --git a/packages/core/src/__tests__/api.test.ts b/packages/core/src/__tests__/api.test.ts index 81a434a43..9c2f10bfe 100644 --- a/packages/core/src/__tests__/api.test.ts +++ b/packages/core/src/__tests__/api.test.ts @@ -1,4 +1,5 @@ import { + Config, Context, EventType, SegmentAPIIntegrations, @@ -7,6 +8,7 @@ import { } from '../types'; import { sendEvents } from '../api'; import * as context from '../context'; +import { batchApi } from '../constants'; describe('#sendEvents', () => { beforeEach(() => { @@ -26,7 +28,7 @@ describe('#sendEvents', () => { .mockReturnValue('2001-01-01T00:00:00.000Z'); }); - it('sends an event', async () => { + async function sendAnEventPer(config: Config, toUrl: string) { const mockResponse = Promise.resolve('MANOS'); // @ts-ignore global.fetch = jest.fn(() => Promise.resolve(mockResponse)); @@ -57,13 +59,11 @@ describe('#sendEvents', () => { }; await sendEvents({ - config: { - writeKey: 'SEGMENT_KEY', - }, + config, events: [event], }); - expect(fetch).toHaveBeenCalledWith('https://api.segment.io/v1/b', { + expect(fetch).toHaveBeenCalledWith(toUrl, { method: 'POST', body: JSON.stringify({ batch: [event], @@ -75,5 +75,22 @@ describe('#sendEvents', () => { 'Content-Type': 'text/plain', }, }); + } + + it('sends an event', async () => { + const toSegmentBatchApi = batchApi; + const config = { + writeKey: 'SEGMENT_KEY', + }; + await sendAnEventPer(config, toSegmentBatchApi); + }); + + it('sends an event to proxy', async () => { + const toProxyUrl = 'https://myprox.io/b'; + const config = { + writeKey: 'SEGMENT_KEY', + proxy: toProxyUrl, + }; + await sendAnEventPer(config, toProxyUrl); }); }); diff --git a/packages/core/src/api.ts b/packages/core/src/api.ts index 6ec59399e..0dddc1665 100644 --- a/packages/core/src/api.ts +++ b/packages/core/src/api.ts @@ -9,7 +9,8 @@ export const sendEvents = async ({ config: Config; events: SegmentEvent[]; }) => { - await fetch(batchApi, { + const requestUrl = config.proxy || batchApi; + await fetch(requestUrl, { method: 'POST', body: JSON.stringify({ batch: events, diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 97b7ad1a7..ff9c48251 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -126,6 +126,7 @@ export type Config = { autoAddSegmentDestination?: boolean; collectDeviceId?: boolean; storePersistor?: Persistor; + proxy?: string; }; export type ClientMethods = {