All files / src PrivacyProtocolsClient.ts

100% Statements 14/14
83.33% Branches 5/6
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 312x   2x   2x   2x           5x 5x 5x       5x 4x   4x   2x       2x      
import { HttpClient } from '@wirewheel/cmp/src/HttpClient'
import { PrivacyProtocolsConfig } from '@wirewheel/cmp/src/entities/PrivacyProtocolsConfig'
import { isBrowser } from '@wirewheel/cmp/src/utils/isBrowser'
import { PersistentStore } from '@wirewheel/cmp/src/modules/PersistentStore'
import { PersistentStoreFactory } from '@wirewheel/cmp/src/modules/PersistentStoreFactory'
 
export class PrivacyProtocolsClient {
  private readonly httpClient: HttpClient
  private readonly attributes: Record<string, any>
  private readonly store: PersistentStore
 
  constructor(config: PrivacyProtocolsConfig) {
    this.httpClient = HttpClient.create(config)
    this.attributes = config.attributes || {}
    this.store = PersistentStoreFactory.getStore(config.cookieConfig)
  }
 
  async registerPrivacyProtocols(): Promise<void> {
    if (!isBrowser()) throw new Error('Operation only available for the browser')
    const subject = this.store.getOrCreateSubject()
 
    if (!subject.id || this.store.getGPCConfirmationBySubjectId(subject.id)) return
 
    await this.httpClient.post('/consents/gpc', {
      subject,
      attributes: this.attributes,
    })
    this.store.setGPCConfirmationBySubjectId(subject.id, [{ gpc: 1 }])
  }
}