refactor(consent-sdk): split ConsentManager + framework adapters under 500 LOC
Phase 4: extract config defaults, Google Consent Mode helper, and framework adapter internals into sibling files so every source file is under the hard cap. Public API surface preserved; all 135 tests green, tsup build + tsc typecheck clean. - core/ConsentManager 525 -> 467 LOC (extract config + google helpers) - react/index 511 LOC -> 199 LOC barrel + components/hooks/context - vue/index 511 LOC -> 32 LOC barrel + components/composables/context/plugin - angular/index 509 LOC -> 45 LOC barrel + interface/service/module/templates Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -20,45 +20,11 @@ import { ConsentAPI } from './ConsentAPI';
|
||||
import { EventEmitter } from '../utils/EventEmitter';
|
||||
import { generateFingerprint } from '../utils/fingerprint';
|
||||
import { SDK_VERSION } from '../version';
|
||||
|
||||
/**
|
||||
* Default-Konfiguration
|
||||
*/
|
||||
const DEFAULT_CONFIG: Partial<ConsentConfig> = {
|
||||
language: 'de',
|
||||
fallbackLanguage: 'en',
|
||||
ui: {
|
||||
position: 'bottom',
|
||||
layout: 'modal',
|
||||
theme: 'auto',
|
||||
zIndex: 999999,
|
||||
blockScrollOnModal: true,
|
||||
},
|
||||
consent: {
|
||||
required: true,
|
||||
rejectAllVisible: true,
|
||||
acceptAllVisible: true,
|
||||
granularControl: true,
|
||||
vendorControl: false,
|
||||
rememberChoice: true,
|
||||
rememberDays: 365,
|
||||
geoTargeting: false,
|
||||
recheckAfterDays: 180,
|
||||
},
|
||||
categories: ['essential', 'functional', 'analytics', 'marketing', 'social'],
|
||||
debug: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* Default Consent-State (nur Essential aktiv)
|
||||
*/
|
||||
const DEFAULT_CONSENT: ConsentCategories = {
|
||||
essential: true,
|
||||
functional: false,
|
||||
analytics: false,
|
||||
marketing: false,
|
||||
social: false,
|
||||
};
|
||||
import {
|
||||
DEFAULT_CONSENT,
|
||||
mergeConsentConfig,
|
||||
} from './consent-manager-config';
|
||||
import { updateGoogleConsentMode as applyGoogleConsent } from './consent-manager-google';
|
||||
|
||||
/**
|
||||
* ConsentManager - Zentrale Klasse fuer Consent-Verwaltung
|
||||
@@ -389,15 +355,10 @@ export class ConsentManager {
|
||||
// ===========================================================================
|
||||
|
||||
/**
|
||||
* Konfiguration zusammenfuehren
|
||||
* Konfiguration zusammenfuehren — delegates to the extracted helper.
|
||||
*/
|
||||
private mergeConfig(config: ConsentConfig): ConsentConfig {
|
||||
return {
|
||||
...DEFAULT_CONFIG,
|
||||
...config,
|
||||
ui: { ...DEFAULT_CONFIG.ui, ...config.ui },
|
||||
consent: { ...DEFAULT_CONFIG.consent, ...config.consent },
|
||||
} as ConsentConfig;
|
||||
return mergeConsentConfig(config);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -434,31 +395,12 @@ export class ConsentManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Google Consent Mode v2 aktualisieren
|
||||
* Google Consent Mode v2 aktualisieren — delegates to the extracted helper.
|
||||
*/
|
||||
private updateGoogleConsentMode(): void {
|
||||
if (typeof window === 'undefined' || !this.currentConsent) {
|
||||
return;
|
||||
if (applyGoogleConsent(this.currentConsent)) {
|
||||
this.log('Google Consent Mode updated');
|
||||
}
|
||||
|
||||
const gtag = (window as unknown as { gtag?: (...args: unknown[]) => void }).gtag;
|
||||
if (typeof gtag !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
const { categories } = this.currentConsent;
|
||||
|
||||
gtag('consent', 'update', {
|
||||
ad_storage: categories.marketing ? 'granted' : 'denied',
|
||||
ad_user_data: categories.marketing ? 'granted' : 'denied',
|
||||
ad_personalization: categories.marketing ? 'granted' : 'denied',
|
||||
analytics_storage: categories.analytics ? 'granted' : 'denied',
|
||||
functionality_storage: categories.functional ? 'granted' : 'denied',
|
||||
personalization_storage: categories.functional ? 'granted' : 'denied',
|
||||
security_storage: 'granted',
|
||||
});
|
||||
|
||||
this.log('Google Consent Mode updated');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
58
consent-sdk/src/core/consent-manager-config.ts
Normal file
58
consent-sdk/src/core/consent-manager-config.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* ConsentManager default configuration + merge helpers.
|
||||
*
|
||||
* Phase 4: extracted from ConsentManager.ts to keep the main class under 500 LOC.
|
||||
*/
|
||||
|
||||
import type { ConsentCategories, ConsentConfig } from '../types';
|
||||
|
||||
/**
|
||||
* Default configuration applied when a consumer omits optional fields.
|
||||
*/
|
||||
export const DEFAULT_CONFIG: Partial<ConsentConfig> = {
|
||||
language: 'de',
|
||||
fallbackLanguage: 'en',
|
||||
ui: {
|
||||
position: 'bottom',
|
||||
layout: 'modal',
|
||||
theme: 'auto',
|
||||
zIndex: 999999,
|
||||
blockScrollOnModal: true,
|
||||
},
|
||||
consent: {
|
||||
required: true,
|
||||
rejectAllVisible: true,
|
||||
acceptAllVisible: true,
|
||||
granularControl: true,
|
||||
vendorControl: false,
|
||||
rememberChoice: true,
|
||||
rememberDays: 365,
|
||||
geoTargeting: false,
|
||||
recheckAfterDays: 180,
|
||||
},
|
||||
categories: ['essential', 'functional', 'analytics', 'marketing', 'social'],
|
||||
debug: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* Default consent state — only essential category is active.
|
||||
*/
|
||||
export const DEFAULT_CONSENT: ConsentCategories = {
|
||||
essential: true,
|
||||
functional: false,
|
||||
analytics: false,
|
||||
marketing: false,
|
||||
social: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* Merge a user-supplied config onto DEFAULT_CONFIG, preserving nested objects.
|
||||
*/
|
||||
export function mergeConsentConfig(config: ConsentConfig): ConsentConfig {
|
||||
return {
|
||||
...DEFAULT_CONFIG,
|
||||
...config,
|
||||
ui: { ...DEFAULT_CONFIG.ui, ...config.ui },
|
||||
consent: { ...DEFAULT_CONFIG.consent, ...config.consent },
|
||||
} as ConsentConfig;
|
||||
}
|
||||
38
consent-sdk/src/core/consent-manager-google.ts
Normal file
38
consent-sdk/src/core/consent-manager-google.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Google Consent Mode v2 integration helper.
|
||||
*
|
||||
* Phase 4: extracted from ConsentManager.ts. Updates gtag() with the
|
||||
* current consent category state whenever consent changes.
|
||||
*/
|
||||
|
||||
import type { ConsentState } from '../types';
|
||||
|
||||
/**
|
||||
* Update Google Consent Mode v2 based on the current consent categories.
|
||||
* No-op when running outside the browser or when gtag is not loaded.
|
||||
* Returns true if the gtag update was actually applied.
|
||||
*/
|
||||
export function updateGoogleConsentMode(consent: ConsentState | null): boolean {
|
||||
if (typeof window === 'undefined' || !consent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const gtag = (window as unknown as { gtag?: (...args: unknown[]) => void }).gtag;
|
||||
if (typeof gtag !== 'function') {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { categories } = consent;
|
||||
|
||||
gtag('consent', 'update', {
|
||||
ad_storage: categories.marketing ? 'granted' : 'denied',
|
||||
ad_user_data: categories.marketing ? 'granted' : 'denied',
|
||||
ad_personalization: categories.marketing ? 'granted' : 'denied',
|
||||
analytics_storage: categories.analytics ? 'granted' : 'denied',
|
||||
functionality_storage: categories.functional ? 'granted' : 'denied',
|
||||
personalization_storage: categories.functional ? 'granted' : 'denied',
|
||||
security_storage: 'granted',
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user