Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

[CM-574] Depend on live-connect-common and live-connect-handler #110

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ and the `fallback` is a `function()`
- `function pixelGet (uri, onload)`
where the `onload` is a `function()`

If one of the functions is not available in the external handler, LiveConnect will fall back to stubs to ensure that the overall functionality isn't being affected. It is recommended to provide full implementations of the interfaces. An example for StorageHandler ([here](./test/shared/utils/storage.ts)) and for CallHandler ([here](./test/shared/utils/calls.ts)) are provided.
If one of the functions is not available in the external handler, LiveConnect will fall back to stubs to ensure that the overall functionality isn't being affected. It is recommended to provide full implementations of the interfaces. Default
implementations of the handlers can be found in the `live-connect-handlers` project.

With custom implementations the initialisation looks like this:
With custom implementations the initialization can look like this:
```javascript
import { LiveConnect } from 'live-connect-js/src/live-connect'
import { LiveConnect } from 'live-connect-js'

const storageHandler = {
getCookie: (key) => {
let m = window.document.cookie.match('(^|;)\\s*' + key + '\\s*=\\s*([^;]*)\\s*(;|$)')
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"release:ci:major": "release-it major --ci"
},
"dependencies": {
"live-connect-common": "0.1.0-alpha.3",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will cut proper releases once this is approved

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider to do the same for the handler as well

"tiny-hashes": "1.0.1"
},
"devDependencies": {
Expand Down Expand Up @@ -102,6 +103,7 @@
"express": "^4.17.1",
"fs-extra": "^9.0.1",
"js-cookie": "^3.0.1",
"live-connect-handlers": "0.1.0-alpha.1",
"mocha": "^6.2.3",
"mocha-jsdom": "^2.0.0",
"mocha-junit-reporter": "^2.0.0",
Expand Down
8 changes: 4 additions & 4 deletions src/enrichers/identifiers-nohash.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { containsEmailField, isEmail } from '../utils/email'
import { safeToString, isArray, trim } from '../utils/types'
import { safeToString, isArray, trim } from 'live-connect-common'
import { EventBus, RetrievedIdentifier, State } from '../types'
import { MinimalStorageHandler } from '../handlers/storage-handler'
import { WrappedReadOnlyStorageHandler } from '../handlers/storage-handler'

export function enrich(state: State, storageHandler: MinimalStorageHandler, eventBus: EventBus): State {
export function enrich(state: State, storageHandler: WrappedReadOnlyStorageHandler, eventBus: EventBus): State {
try {
return _parseIdentifiersToResolve(state, storageHandler)
} catch (e) {
Expand All @@ -12,7 +12,7 @@ export function enrich(state: State, storageHandler: MinimalStorageHandler, even
}
}

function _parseIdentifiersToResolve(state: State, storageHandler: MinimalStorageHandler): State {
function _parseIdentifiersToResolve(state: State, storageHandler: WrappedReadOnlyStorageHandler): State {
state.identifiersToResolve = state.identifiersToResolve || []
const cookieNames = isArray(state.identifiersToResolve) ? state.identifiersToResolve : safeToString(state.identifiersToResolve).split(',')
const identifiers: RetrievedIdentifier[] = []
Expand Down
8 changes: 4 additions & 4 deletions src/enrichers/identifiers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { replaceEmailsWithHashes } from '../utils/email'
import { safeToString, isString, isArray } from '../utils/types'
import { safeToString, isString, isArray } from 'live-connect-common'
import { EventBus, HashedEmail, State, RetrievedIdentifier } from '../types'
import { MinimalStorageHandler } from '../handlers/storage-handler'
import { WrappedReadOnlyStorageHandler } from '../handlers/storage-handler'

export function enrich(state: State, storageHandler: MinimalStorageHandler, eventBus: EventBus): State {
export function enrich(state: State, storageHandler: WrappedReadOnlyStorageHandler, eventBus: EventBus): State {
try {
return _getIdentifiers(_parseIdentifiersToResolve(state), storageHandler)
} catch (e) {
Expand All @@ -29,7 +29,7 @@ function _parseIdentifiersToResolve(state: State): string[] {
return cookieNames
}

function _getIdentifiers(cookieNames: string[], storageHandler: MinimalStorageHandler): State {
function _getIdentifiers(cookieNames: string[], storageHandler: WrappedReadOnlyStorageHandler): State {
const identifiers: RetrievedIdentifier[] = []
let hashes: HashedEmail[] = []
for (let i = 0; i < cookieNames.length; i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/enrichers/people-verified.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PEOPLE_VERIFIED_LS_ENTRY } from '../utils/consts'
import { EventBus, State } from '../types'
import { MinimalStorageHandler } from '../handlers/storage-handler'
import { WrappedReadOnlyStorageHandler } from '../handlers/storage-handler'

export function enrich(state: State, storageHandler: MinimalStorageHandler, eventBus: EventBus): State {
export function enrich(state: State, storageHandler: WrappedReadOnlyStorageHandler, eventBus: EventBus): State {
try {
return { peopleVerifiedId: state.peopleVerifiedId || storageHandler.getDataFromLocalStorage(PEOPLE_VERIFIED_LS_ENTRY) || undefined }
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/enrichers/privacy-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { State } from '../types'
import { isNonEmpty } from '../utils/types'
import { isNonEmpty } from 'live-connect-common'

export function enrich(state: State): State {
if (isNonEmpty(state) && isNonEmpty(state.gdprApplies)) {
Expand Down
9 changes: 4 additions & 5 deletions src/events/error-pixel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { PixelSender } from '../pixel/sender'
import { StateWrapper } from '../pixel/state'
import * as page from '../enrichers/page'
import * as C from '../utils/consts'
import { ERRORS_CHANNEL, isRecord, isString } from 'live-connect-common'
import { EventBus, State } from '../types'
import { CallHandler } from '../handlers/call-handler'
import { isRecord, isString } from '../utils/types'
import { WrappedCallHandler } from '../handlers/call-handler'

const MAX_ERROR_FIELD_LENGTH = 120

Expand Down Expand Up @@ -52,11 +51,11 @@ export function asErrorDetails(e: unknown): State {
}
}

export function register(state: State, callHandler: CallHandler, eventBus: EventBus): void {
export function register(state: State, callHandler: WrappedCallHandler, eventBus: EventBus): void {
try {
const pixelSender = new PixelSender(state, callHandler, eventBus)

eventBus.on(C.ERRORS_PREFIX, (error) => {
eventBus.on(ERRORS_CHANNEL, (error) => {
pixelSender.sendPixel(new StateWrapper(asErrorDetails(error), eventBus).combineWith(state || {}).combineWith(page.enrich({})))
})
} catch (e) {
Expand Down
7 changes: 3 additions & 4 deletions src/events/event-bus.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// @ts-nocheck
import { ReplayEmitter, wrapError } from './replayemitter'
import * as C from '../utils/consts'
import { isFunction } from '../utils/types'
import { ReplayEmitter, wrapError, isFunction, ERRORS_CHANNEL } from 'live-connect-common'
import { EventBus } from '../types'
import * as C from '../utils/consts'

function initBus(size?: number): EventBus {
if (typeof size === 'number' && size >= 0) {
Expand All @@ -19,7 +18,7 @@ function extendBusIfNeeded(bus: EventBus) {

bus.emitErrorWithMessage = function (name, message, e = {}) {
const wrappedError = wrapError(name, message, e)
return bus.emit(C.ERRORS_PREFIX, wrappedError)
return bus.emit(ERRORS_CHANNEL, wrappedError)
}

bus.emitError = function (name, exception) {
Expand Down
134 changes: 0 additions & 134 deletions src/events/replayemitter.ts

This file was deleted.

21 changes: 4 additions & 17 deletions src/handlers/call-handler.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
import { EventBus, ExternalCallHandler } from '../types'
import { EventBus, CallHandler } from 'live-connect-common'
import { WrappingContext } from '../utils/wrapping'

interface WrappedExternalCallHandler {
ajaxGet: (
url: string,
onSuccess: (responseText: string, response: unknown) => void,
onError?: (error: unknown) => void,
timeout?: number
) => void
pixelGet: (
url: string,
onLoad?: () => void
) => void
}

export class CallHandler {
private functions: WrappedExternalCallHandler
export class WrappedCallHandler implements CallHandler {
private functions

constructor (externalCallHandler: ExternalCallHandler, eventBus: EventBus) {
constructor (externalCallHandler: CallHandler, eventBus: EventBus) {
const wrapper = new WrappingContext(externalCallHandler, 'CallHandler', eventBus)

this.functions = {
Expand Down
Loading