Skip to content

feat: Create Client Proxy Option #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
27 changes: 22 additions & 5 deletions packages/core/src/__tests__/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Config,
Context,
EventType,
SegmentAPIIntegrations,
Expand All @@ -7,6 +8,7 @@ import {
} from '../types';
import { sendEvents } from '../api';
import * as context from '../context';
import { batchApi } from '../constants';

describe('#sendEvents', () => {
beforeEach(() => {
Expand All @@ -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));
Expand Down Expand Up @@ -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],
Expand All @@ -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);
});
});
3 changes: 2 additions & 1 deletion packages/core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export type Config = {
autoAddSegmentDestination?: boolean;
collectDeviceId?: boolean;
storePersistor?: Persistor;
proxy?: string;
};

export type ClientMethods = {
Expand Down