Skip to content

Commit

Permalink
Use track root for push url.
Browse files Browse the repository at this point in the history
  • Loading branch information
lisaah committed Feb 3, 2022
1 parent 3bf20dc commit a17ad53
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 32 deletions.
8 changes: 3 additions & 5 deletions lib/regions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
export class Region {
readonly trackUrl: string;
readonly apiUrl: string;
readonly trackPushUrl?: string;

constructor(trackUrl: string, apiUrl: string, trackPushUrl?: string) {
constructor(trackUrl: string, apiUrl: string) {
this.trackUrl = trackUrl;
this.apiUrl = apiUrl;
this.trackPushUrl = trackPushUrl;
}
}

export const RegionUS = new Region('https://track.customer.io/api/v1', 'https://api.customer.io/v1', 'https://track.customer.io/push');
export const RegionEU = new Region('https://track-eu.customer.io/api/v1', 'https://api-eu.customer.io/v1', 'https://track-eu.customer.io/push');
export const RegionUS = new Region('https://track.customer.io/api/v1', 'https://api.customer.io/v1');
export const RegionEU = new Region('https://track-eu.customer.io/api/v1', 'https://api-eu.customer.io/v1');
17 changes: 2 additions & 15 deletions lib/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import { Region, RegionUS } from './regions';
import { isEmpty } from './utils';
import { IdentifierType } from './types';

type TrackDefaults = RequestOptions & { region: Region; url?: string; apiUrl?: string; trackPushUrl?: string };

class MissingConfigError extends Error {
constructor(param: string) {
super(param);
this.message = `${param} is required`;
}
}
type TrackDefaults = RequestOptions & { region: Region; url?: string; apiUrl?: string; };

class MissingParamError extends Error {
constructor(param: string) {
Expand All @@ -27,7 +20,6 @@ export class TrackClient {
request: Request;
trackRoot: string;
apiRoot: string;
trackPushRoot?: string;

constructor(siteid: BasicAuth['siteid'], apikey: BasicAuth['apikey'], defaults: Partial<TrackDefaults> = {}) {
if (defaults.region && !(defaults.region instanceof Region)) {
Expand All @@ -41,7 +33,6 @@ export class TrackClient {

this.trackRoot = this.defaults.url ? this.defaults.url : this.defaults.region.trackUrl;
this.apiRoot = this.defaults.apiUrl ? this.defaults.apiUrl : this.defaults.region.apiUrl;
this.trackPushRoot = this.defaults.trackPushUrl ? this.defaults.trackPushUrl : this.defaults.region.trackPushUrl;
}

identify(customerId: string | number, data: RequestData = {}) {
Expand Down Expand Up @@ -108,11 +99,7 @@ export class TrackClient {
}

trackPush(data: PushRequestData = {}) {
if (isEmpty(this.trackPushRoot)) {
throw new MissingConfigError('trackPushRoot');

}
return this.request.post(`${this.trackPushRoot}/events`, data);
return this.request.post(`${this.trackRoot}/push/events`, data);
}

addDevice(customerId: string | number, device_id: string, platform: string, data = {}) {
Expand Down
15 changes: 3 additions & 12 deletions test/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ test('constructor sets necessary variables', (t) => {
t.is(t.context.client.apikey, 'abc');
t.is(t.context.client.trackRoot, RegionUS.trackUrl);
t.is(t.context.client.apiRoot, RegionUS.apiUrl);
t.is(t.context.client.trackPushRoot, RegionUS.trackPushUrl);

t.truthy(t.context.client.request);
t.is(t.context.client.request.siteid, '123');
Expand All @@ -39,7 +38,6 @@ test('constructor sets correct URL for different regions', (t) => {
t.is(client.apikey, 'abc');
t.is(client.trackRoot, region.trackUrl);
t.is(client.apiRoot, region.apiUrl);
t.is(client.trackPushRoot, region.trackPushUrl);

t.truthy(client.request);
t.is(client.request.siteid, '123');
Expand All @@ -48,13 +46,12 @@ test('constructor sets correct URL for different regions', (t) => {
});

test('constructor sets correct URL for custom URLs', (t) => {
let client = new TrackClient('123', 'abc', { url: 'https://example.com/url', apiUrl: 'https://example.com/apiUrl', trackPushUrl: 'https://example.com/push' });
let client = new TrackClient('123', 'abc', { url: 'https://example.com/url', apiUrl: 'https://example.com/apiUrl' });

t.is(client.siteid, '123');
t.is(client.apikey, 'abc');
t.is(client.trackRoot, 'https://example.com/url');
t.is(client.apiRoot, 'https://example.com/apiUrl');
t.is(client.trackPushRoot, 'https://example.com/push');

t.truthy(client.request);
t.is(client.request.siteid, '123');
Expand Down Expand Up @@ -139,7 +136,7 @@ test('#trackPush works', (t) => {
sinon.stub(t.context.client.request, 'post');
t.context.client.trackPush();
t.truthy(
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.trackPushUrl}/events`, {}),
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.trackUrl}/push/events`, {}),
);

t.context.client.trackPush({
Expand All @@ -149,7 +146,7 @@ test('#trackPush works', (t) => {
timestamp: 1613063089
});
t.truthy(
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.trackPushUrl}/events`, {
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.trackUrl}/push/events`, {
delivery_id: "RPILAgUBcRhIBqSfeiIwdIYJKxTY",
event: "opened",
device_id: "CIO-Delivery-Token from the notification",
Expand All @@ -158,12 +155,6 @@ test('#trackPush works', (t) => {
);
});

test('#trackPush throws error if trackPushRoot is missing', (t) => {
t.context.client = new TrackClient('123', 'abc', { region: new Region('https://example.com/url', 'https://example.com/apiUrl')});
sinon.stub(t.context.client.request, 'post');
t.throws(() => t.context.client.trackPush(), { message: 'trackPushRoot is required' });
});

ID_INPUTS.forEach(([input, expected]) => {
test(`#trackPageView works for ${input}`, (t) => {
sinon.stub(t.context.client.request, 'post');
Expand Down

0 comments on commit a17ad53

Please sign in to comment.