Skip to content

Commit 360ec32

Browse files
fix: move event models and publishEvents() method to server-request lib
1 parent 6e4c497 commit 360ec32

File tree

24 files changed

+125
-182
lines changed

24 files changed

+125
-182
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
export * from './request'
22
export * from './userError'
3+
export * from './utils/paramUtils'
4+
export * from './models/devcycleEvent'
5+
export * from './models/requestEvent'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export interface DevCycleEvent {
2+
/**
3+
* type of the event
4+
*/
5+
type: string
6+
7+
/**
8+
* date event occurred according to client stored as time since epoch
9+
*/
10+
date?: number
11+
12+
/**
13+
* target / subject of event. Contextual to event type
14+
*/
15+
target?: string
16+
17+
/**
18+
* value for numerical events. Contextual to event type
19+
*/
20+
value?: number
21+
22+
/**
23+
* extra metadata for event. Contextual to event type
24+
*/
25+
metaData?: Record<string, unknown>
26+
}

sdk/nodejs/src/models/requestEvent.ts renamed to lib/shared/server-request/src/models/requestEvent.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { EventTypes } from '../eventQueue'
2-
import {
3-
DevCycleEvent,
4-
checkParamDefined,
5-
checkParamString,
6-
} from '@devcycle/js-cloud-server-sdk'
1+
import { checkParamDefined, checkParamString } from '../utils/paramUtils'
2+
import { DevCycleEvent } from './devcycleEvent'
3+
4+
export const AggregateEventTypes: Record<string, string> = {
5+
variableEvaluated: 'variableEvaluated',
6+
aggVariableEvaluated: 'aggVariableEvaluated',
7+
variableDefaulted: 'variableDefaulted',
8+
aggVariableDefaulted: 'aggVariableDefaulted',
9+
}
10+
11+
export const EventTypes: Record<string, string> = {
12+
...AggregateEventTypes,
13+
}
714

815
export class DVCRequestEvent {
916
type: string

lib/shared/server-request/src/request.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
import fetchWithRetry, { RequestInitWithRetry } from 'fetch-retry'
2+
import { DVCLogger, SDKEventBatchRequestBody } from '@devcycle/types'
3+
4+
export const HOST = '.devcycle.com'
5+
export const EVENT_URL = 'https://events'
6+
export const EVENTS_PATH = '/v1/events/batch'
7+
8+
export async function publishEvents(
9+
logger: DVCLogger,
10+
sdkKey: string,
11+
eventsBatch: SDKEventBatchRequestBody,
12+
eventsBaseURLOverride?: string,
13+
): Promise<Response> {
14+
const url = eventsBaseURLOverride
15+
? `${eventsBaseURLOverride}${EVENTS_PATH}`
16+
: `${EVENT_URL}${HOST}${EVENTS_PATH}`
17+
return await post(
18+
url,
19+
{
20+
body: JSON.stringify({ batch: eventsBatch }),
21+
},
22+
sdkKey,
23+
)
24+
}
225

326
export class ResponseError extends Error {
427
constructor(message: string) {

sdk/edge-worker-server/__tests__/models/requestEvent.spec.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { DVCRequestEvent } from '../../src/models/requestEvent'
2-
// import { EventTypes } from '../../src/eventQueue'
1+
import { DVCRequestEvent, EventTypes } from '@devcycle/server-request'
32

43
describe('DVCRequestEvent Unit Tests', () => {
54
it('should construct custom DVCRequestEvent from DVCEvent', () => {
@@ -28,21 +27,21 @@ describe('DVCRequestEvent Unit Tests', () => {
2827
})
2928
})
3029

31-
// it('should construct an event for an internal DVC Event', () => {
32-
// const requestEvent = new DVCRequestEvent(
33-
// {
34-
// type: EventTypes.variableEvaluated,
35-
// },
36-
// 'user_id',
37-
// )
38-
// expect(requestEvent).toEqual(
39-
// expect.objectContaining({
40-
// type: EventTypes.variableEvaluated,
41-
// user_id: 'user_id',
42-
// clientDate: expect.any(Number),
43-
// }),
44-
// )
45-
// })
30+
it('should construct an event for an internal DVC Event', () => {
31+
const requestEvent = new DVCRequestEvent(
32+
{
33+
type: EventTypes.variableEvaluated,
34+
},
35+
'user_id',
36+
)
37+
expect(requestEvent).toEqual(
38+
expect.objectContaining({
39+
type: EventTypes.variableEvaluated,
40+
user_id: 'user_id',
41+
clientDate: expect.any(Number),
42+
}),
43+
)
44+
})
4645

4746
it('should check that type is defined as a string', () => {
4847
expect(() => new (DVCRequestEvent as any)({})).toThrow(

sdk/edge-worker-server/src/client.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ import {
99
DevCycleUser,
1010
DVCVariable,
1111
VariableParam,
12-
checkParamDefined,
1312
dvcDefaultLogger,
1413
DVCVariableValue,
1514
DVCVariableSet,
1615
DVCFeatureSet,
17-
DevCycleEvent,
1816
DevCycleCloudOptions,
1917
} from '@devcycle/js-cloud-server-sdk'
2018
import { DVCPopulatedUserFromDevCycleUser } from './models/populatedUserHelpers'
21-
import { UserError } from '@devcycle/server-request'
19+
import {
20+
UserError,
21+
DevCycleEvent,
22+
checkParamDefined,
23+
} from '@devcycle/server-request'
2224
import { generateBucketedConfig } from '@devcycle/bucketing'
2325

2426
const castIncomingUser = (user: DevCycleUser) => {

sdk/edge-worker-server/src/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import {
33
DevCycleUser,
44
DevCycleCloudClient,
55
dvcDefaultLogger,
6-
isValidServerSDKKey,
76
DevCycleOptions,
8-
DevCycleEvent,
97
DVCVariableValue,
108
JSON,
119
DVCJSON,
@@ -17,6 +15,7 @@ import {
1715
DVCFeatureSet,
1816
DevCycleCloudOptions,
1917
} from '@devcycle/js-cloud-server-sdk'
18+
import { isValidServerSDKKey, DevCycleEvent } from '@devcycle/server-request'
2019

2120
export {
2221
DevCycleEdgeClient,
@@ -36,10 +35,14 @@ export {
3635
}
3736
export { dvcDefaultLogger }
3837

39-
type DevCycleOptionsCloudEnabled = DevCycleCloudOptions & {
38+
type DevCycleEdgeOptions = DevCycleCloudOptions & {
39+
enableCloudBucketing?: boolean
40+
}
41+
42+
type DevCycleOptionsCloudEnabled = DevCycleEdgeOptions & {
4043
enableCloudBucketing: true
4144
}
42-
type DevCycleOptionsLocalEnabled = DevCycleCloudOptions & {
45+
type DevCycleOptionsLocalEnabled = DevCycleEdgeOptions & {
4346
enableCloudBucketing?: false
4447
}
4548

@@ -53,11 +56,11 @@ export function initializeDevCycle(
5356
): DevCycleCloudClient
5457
export function initializeDevCycle(
5558
sdkKey: string,
56-
options?: DevCycleCloudOptions,
59+
options?: DevCycleEdgeOptions,
5760
): DevCycleEdgeClient | DevCycleCloudClient
5861
export function initializeDevCycle(
5962
sdkKey: string,
60-
options: DevCycleCloudOptions = {},
63+
options: DevCycleEdgeOptions = {},
6164
): DevCycleEdgeClient | DevCycleCloudClient {
6265
if (!sdkKey) {
6366
throw new Error('Missing SDK key! Call initialize with a valid SDK key')

sdk/edge-worker-server/src/models/requestEvent.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

sdk/edge-worker-server/src/request.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)