-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use new SalesWings API urls * Do not check response status in testAuthentication * Fix testAuthentication * Remove unused file
- Loading branch information
1 parent
81c45f9
commit eae86be
Showing
28 changed files
with
279 additions
and
681 deletions.
There are no files selected for viewing
17 changes: 11 additions & 6 deletions
17
packages/destination-actions/src/destinations/saleswings/__tests__/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
import nock from 'nock' | ||
import { createTestIntegration } from '@segment/actions-core' | ||
import Definition from '../index' | ||
import { apiBaseUrl } from '../api' | ||
import { getAccountUrl } from '../api' | ||
|
||
const testDestination = createTestIntegration(Definition) | ||
|
||
describe('Saleswings', () => { | ||
const env = 'helium' | ||
describe('testAuthentication', () => { | ||
it('should validate authentication inputs', async () => { | ||
nock(apiBaseUrl).get('/project/account').matchHeader('authorization', 'Bearer myApiKey').reply(200, {}) | ||
await expect(testDestination.testAuthentication({ apiKey: 'myApiKey' })).resolves.not.toThrowError() | ||
nock(getAccountUrl(env)).get('').matchHeader('authorization', 'Bearer myApiKey').reply(200, {}) | ||
await expect( | ||
testDestination.testAuthentication({ apiKey: 'myApiKey', environment: env }) | ||
).resolves.not.toThrowError() | ||
}) | ||
|
||
it('should reject invalid API key', async () => { | ||
nock(apiBaseUrl).get('/project/account').matchHeader('authorization', 'Bearer myApiKey').reply(200, {}) | ||
nock(apiBaseUrl).get('/project/account').reply(401, {}) | ||
await expect(testDestination.testAuthentication({ apiKey: 'invalidApiKey' })).rejects.toThrowError() | ||
nock(getAccountUrl(env)).get('').matchHeader('authorization', 'Bearer myApiKey').reply(200, {}) | ||
nock(getAccountUrl(env)).get('').reply(401, {}) | ||
await expect( | ||
testDestination.testAuthentication({ apiKey: 'invalidApiKey', environment: env }) | ||
).rejects.toThrowError() | ||
}) | ||
}) | ||
}) |
51 changes: 6 additions & 45 deletions
51
packages/destination-actions/src/destinations/saleswings/api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,7 @@ | ||
export const apiBaseUrl = 'https://helium.saleswings.pro/api/core' | ||
export type EventType = 'track' | 'identify' | 'page' | 'screen' | ||
|
||
export type Event = TrackingEvent | PageVisitEvent | ||
|
||
export type EventBatch = { | ||
events: Event[] | ||
} | ||
|
||
export class TrackingEvent { | ||
leadRefs: LeadRef[] | ||
kind: string | ||
data: string | ||
url?: string | ||
referrerUrl?: string | ||
userAgent?: string | ||
timestamp: number | ||
values: ValueMap | ||
readonly type: string = 'tracking' | ||
|
||
public constructor(fields?: Partial<TrackingEvent>) { | ||
Object.assign(this, fields) | ||
} | ||
} | ||
|
||
export class PageVisitEvent { | ||
leadRefs: LeadRef[] | ||
url: string | ||
referrerUrl?: string | ||
userAgent?: string | ||
timestamp: number | ||
readonly type: string = 'page-visit' | ||
|
||
public constructor(fields?: Partial<PageVisitEvent>) { | ||
Object.assign(this, fields) | ||
} | ||
} | ||
|
||
export type Value = string | number | boolean | ||
export type ValueMap = { [k: string]: Value } | ||
|
||
export type LeadRefType = 'email' | 'client-id' | ||
|
||
export type LeadRef = { | ||
type: LeadRefType | ||
value: string | ||
} | ||
export const submitEventUrl = (env: string, eventType: EventType): string => | ||
`https://${env}.saleswings.pro/api/segment/events/${eventType}` | ||
export const submitEventBatchUrl = (env: string, eventType: EventType): string => | ||
`https://${env}.saleswings.pro/api/segment/events/${eventType}/batches` | ||
export const getAccountUrl = (env: string): string => `https://${env}.saleswings.pro/api/core/project/account` |
29 changes: 7 additions & 22 deletions
29
packages/destination-actions/src/destinations/saleswings/common.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,23 @@ | ||
import { RequestFn } from '@segment/actions-core' | ||
import { apiBaseUrl, Event, EventBatch } from './api' | ||
import { submitEventUrl, submitEventBatchUrl, EventType } from './api' | ||
import { Settings } from './generated-types' | ||
|
||
export function perform<Payload>(convertEvent: (payload: Payload) => Event | undefined): RequestFn<Settings, Payload> { | ||
export function perform<Payload>(eventType: EventType): RequestFn<Settings, Payload> { | ||
return (request, data) => { | ||
const event = convertEvent(data.payload) | ||
if (!event) return | ||
return request(`${apiBaseUrl}/events`, { | ||
return request(submitEventUrl(data.settings.environment, eventType), { | ||
method: 'post', | ||
json: event, | ||
json: data.payload, | ||
headers: { Authorization: `Bearer ${data.settings.apiKey}` } | ||
}) | ||
} | ||
} | ||
|
||
export function performBatch<Payload>( | ||
convertEvent: (payload: Payload) => Event | undefined | ||
): RequestFn<Settings, Payload[]> { | ||
export function performBatch<Payload>(eventType: EventType): RequestFn<Settings, Payload[]> { | ||
return (request, data) => { | ||
const batch = convertEventBatch(data.payload, convertEvent) | ||
if (!batch) return | ||
return request(`${apiBaseUrl}/events/batches`, { | ||
return request(submitEventBatchUrl(data.settings.environment, eventType), { | ||
method: 'post', | ||
json: batch, | ||
json: data.payload, | ||
headers: { Authorization: `Bearer ${data.settings.apiKey}` } | ||
}) | ||
} | ||
} | ||
|
||
function convertEventBatch<Payload>( | ||
payloads: Payload[], | ||
convertEvent: (payload: Payload) => Event | undefined | ||
): EventBatch | undefined { | ||
const events = payloads.map(convertEvent).filter((evt) => evt) as Event[] | ||
if (events.length == 0) return undefined | ||
return { events } | ||
} |
30 changes: 0 additions & 30 deletions
30
packages/destination-actions/src/destinations/saleswings/converter.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/destination-actions/src/destinations/saleswings/generated-types.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 11 additions & 35 deletions
46
...destinations/saleswings/submitIdentifyEvent/__tests__/__snapshots__/snapshot.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.