Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit eaeb940

Browse files
authored
feat: Create Client Proxy Option (segmentio#570)
1 parent 2660186 commit eaeb940

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ You must pass at least the `writeKey`. Additional configuration options are list
110110
| `defaultSettings` | undefined | Settings that will be used if the request to get the settings from Segment fails |
111111
| `autoAddSegmentDestination`| true | Set to false to skip adding the SegmentDestination plugin |
112112
| `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).|
113+
| `proxy` | undefined | `proxy` is a batch url to post to instead of 'https://api.segment.io/v1/b'. |
114+
113115

114116
\* The default value of `debug` will be false in production.
115117

packages/core/src/__tests__/api.test.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
Config,
23
Context,
34
EventType,
45
SegmentAPIIntegrations,
@@ -7,6 +8,7 @@ import {
78
} from '../types';
89
import { sendEvents } from '../api';
910
import * as context from '../context';
11+
import { batchApi } from '../constants';
1012

1113
describe('#sendEvents', () => {
1214
beforeEach(() => {
@@ -26,7 +28,7 @@ describe('#sendEvents', () => {
2628
.mockReturnValue('2001-01-01T00:00:00.000Z');
2729
});
2830

29-
it('sends an event', async () => {
31+
async function sendAnEventPer(config: Config, toUrl: string) {
3032
const mockResponse = Promise.resolve('MANOS');
3133
// @ts-ignore
3234
global.fetch = jest.fn(() => Promise.resolve(mockResponse));
@@ -57,13 +59,11 @@ describe('#sendEvents', () => {
5759
};
5860

5961
await sendEvents({
60-
config: {
61-
writeKey: 'SEGMENT_KEY',
62-
},
62+
config,
6363
events: [event],
6464
});
6565

66-
expect(fetch).toHaveBeenCalledWith('https://api.segment.io/v1/b', {
66+
expect(fetch).toHaveBeenCalledWith(toUrl, {
6767
method: 'POST',
6868
body: JSON.stringify({
6969
batch: [event],
@@ -75,5 +75,22 @@ describe('#sendEvents', () => {
7575
'Content-Type': 'text/plain',
7676
},
7777
});
78+
}
79+
80+
it('sends an event', async () => {
81+
const toSegmentBatchApi = batchApi;
82+
const config = {
83+
writeKey: 'SEGMENT_KEY',
84+
};
85+
await sendAnEventPer(config, toSegmentBatchApi);
86+
});
87+
88+
it('sends an event to proxy', async () => {
89+
const toProxyUrl = 'https://myprox.io/b';
90+
const config = {
91+
writeKey: 'SEGMENT_KEY',
92+
proxy: toProxyUrl,
93+
};
94+
await sendAnEventPer(config, toProxyUrl);
7895
});
7996
});

packages/core/src/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export const sendEvents = async ({
99
config: Config;
1010
events: SegmentEvent[];
1111
}) => {
12-
await fetch(batchApi, {
12+
const requestUrl = config.proxy || batchApi;
13+
await fetch(requestUrl, {
1314
method: 'POST',
1415
body: JSON.stringify({
1516
batch: events,

packages/core/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export type Config = {
126126
autoAddSegmentDestination?: boolean;
127127
collectDeviceId?: boolean;
128128
storePersistor?: Persistor;
129+
proxy?: string;
129130
};
130131

131132
export type ClientMethods = {

0 commit comments

Comments
 (0)