From 1dcb881d771cfe4416d56771d2d5f806022c71cd Mon Sep 17 00:00:00 2001 From: David Fall Date: Mon, 13 Jun 2022 10:19:03 -0700 Subject: [PATCH 1/4] chore: sarn-520 Update README.md with "proxy" client option documentation Declare "proxy" in client options table --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ce3a84dc1..8a74237f5 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 config object consisting of `scheme`(e.g. "https"), `host`(e.g. "mysite.com"), `port`(e.g. 80), and `path`(e.g. "/events") to post events to instead of `segment.io`. | + \* The default value of `debug` will be false in production. From 4c0445c93cd6932da70746a9a926065f37360a0f Mon Sep 17 00:00:00 2001 From: David Fall Date: Mon, 13 Jun 2022 10:28:39 -0700 Subject: [PATCH 2/4] feat: sarn-520 implement "proxy" client option Declare "proxy" in Config interface and reference in sendEvents.ts --- packages/core/src/api.ts | 3 ++- packages/core/src/types.ts | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) 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 = { From 8c7304aea58c1b1ed85fda36217615a1900b669e Mon Sep 17 00:00:00 2001 From: David Fall Date: Mon, 13 Jun 2022 10:35:26 -0700 Subject: [PATCH 3/4] feat: sarn-520 re-clarify "proxy" client option in README.md Transition from object to string data type since it should just be an override for 'https://api.segment.io/v1/b' batch url --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8a74237f5..3b8fed134 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ 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 config object consisting of `scheme`(e.g. "https"), `host`(e.g. "mysite.com"), `port`(e.g. 80), and `path`(e.g. "/events") to post events to instead of `segment.io`. | +| `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. From 45f4ce6afb200fc941d644b80864f96f730a5c97 Mon Sep 17 00:00:00 2001 From: David Fall Date: Mon, 13 Jun 2022 12:43:25 -0700 Subject: [PATCH 4/4] feat: sarn-520 add unit test for "proxy" to api.test.ts Assert that "proxy" overrides default 'https://api.segment.io/v1/b' batch url when provided in client options --- packages/core/src/__tests__/api.test.ts | 27 ++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) 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); }); });