Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Aug 20, 2023
1 parent 43534d2 commit 24f35f4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
23 changes: 6 additions & 17 deletions packages/consent/consent-tools/src/domain/create-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
CreateWrapperSettings,
CDNSettings,
} from '../types'
import { validateCategories, validateSettings } from './validation'
import {
validateAnalyticsInstance,
validateCategories,
validateSettings,
} from './validation'
import { createConsentStampingMiddleware } from './consent-stamping'
import { pipe, pick, uniq } from '../utils'
import { AbortLoadError, LoadContext } from './load-cancellation'
Expand All @@ -28,7 +32,7 @@ export const createWrapper: CreateWrapper = (createWrapperOptions) => {
} = createWrapperOptions

return (analytics) => {
assertAnalyticsInstance(analytics)
validateAnalyticsInstance(analytics)
const ogLoad = analytics.load

const loadWithConsent: AnyAnalytics['load'] = async (
Expand Down Expand Up @@ -235,18 +239,3 @@ const disableIntegrations = (
)
return results
}

function assertAnalyticsInstance(
analytics: unknown
): asserts analytics is AnyAnalytics {
if (
analytics &&
typeof analytics === 'object' &&
'load' in analytics &&
'on' in analytics &&
'addSourceMiddleware' in analytics
) {
return
}
throw new Error('analytics is not an Analytics instance')
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function assertIsFunction(

export function assertIsObject(
val: unknown,
variableName: string
variableName = 'value'
): asserts val is object {
if (val === null || typeof val !== 'object') {
throw new ValidationError(`${variableName} is not an object`, val)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Categories, CreateWrapperSettings } from '../../types'
import { AnyAnalytics, Categories, CreateWrapperSettings } from '../../types'
import { assertIsFunction, assertIsObject } from './common-validators'
import { ValidationError } from './validation-error'

Expand Down Expand Up @@ -58,3 +58,17 @@ export function validateSettings(options: {
'registerOnConsentChanged'
)
}

export function validateAnalyticsInstance(
analytics: unknown
): asserts analytics is AnyAnalytics {
assertIsObject(analytics)
if (
'load' in analytics &&
'on' in analytics &&
'addSourceMiddleware' in analytics
) {
return
}
throw new ValidationError('analytics is not an Analytics instance', analytics)
}

0 comments on commit 24f35f4

Please sign in to comment.