Skip to content

Types

All types are exported from @uabcodus/consent/core.

The main configuration object passed to createConsent().

interface ConsentConfig<TCategories extends Record<string, CategoryConfig>> {
preset?: Partial<ConsentConfig<Record<string, CategoryConfig>>>;
mode?: ConsentMode;
revision?: number;
hideFromBots?: boolean;
manageScripts?: boolean;
scriptType?: string;
autoClearCookies?: boolean;
categories: TCategories;
storage?: StorageAdapter;
initialCookie?: CookieValue | string | null;
callbacks?: ConsentCallbacks;
}
type ConsentMode = "opt-in" | "opt-out";
interface CategoryConfig {
enabled?: boolean;
readOnly?: boolean;
autoClear?: {
reloadPage?: boolean;
cookies?: CookieItem[];
};
services?: Record<string, ServiceConfig>;
}
interface ServiceConfig {
onAccept?: () => void;
onReject?: () => void;
cookies?: CookieItem[];
}
interface CookieConfig {
name: string;
expiresAfterDays: number | ((acceptType: AcceptType) => number);
domain: string;
path: string;
secure: boolean;
sameSite: "Lax" | "Strict" | "None";
}
interface StorageAdapter {
get(): CookieValue;
set(value: CookieValue): void;
remove(): void;
}

Factory functions:

function cookieStorage(config: CookieConfig): StorageAdapter;
function localStorageStorage(name: string, expiresAfterMs?: number): StorageAdapter;
interface CookieItem {
name: string | RegExp;
path?: string;
domain?: string;
}

interface ConsentState {
valid: boolean;
skipped: boolean;
categories: Record<string, { accepted: boolean; readOnly: boolean }>;
services: Record<string, Record<string, boolean>>;
cookie: CookieValue | null;
mode: ConsentMode;
acceptType: AcceptType;
}
type AcceptType = "all" | "custom" | "necessary";
interface CookieValue {
categories: string[];
services: Record<string, string[]>;
revision: number;
data: unknown;
consentId: string;
consentTimestamp: string;
lastConsentTimestamp?: string;
expirationTime?: number;
languageCode?: string;
}

interface ConsentCallbacks {
onFirstConsent?: (payload: { cookie: CookieValue }) => void;
onConsent?: (payload: { cookie: CookieValue }) => void;
onChange?: (payload: CookieChange) => void;
}
interface CookieChange {
cookie: CookieValue;
changedCategories: string[];
changedServices: Record<string, string[]>;
}

The main API object returned by createConsent().

interface ConsentInstance<TCategories extends Record<string, CategoryConfig>> {
readonly state: Readonly<ConsentState>;
accept: (categories: CategoryAcceptArg<TCategories>, excludedCategories?: string[]) => void;
reject: (categories: CategoryAcceptArg<TCategories>) => void;
acceptedCategory: (category: CategoryNames<TCategories>) => boolean;
acceptService: (
service: ServiceAcceptArg<TCategories, CategoryNames<TCategories>>,
category: CategoryNames<TCategories>
) => void;
rejectService: (
service: ServiceAcceptArg<TCategories, CategoryNames<TCategories>>,
category: CategoryNames<TCategories>
) => void;
acceptedService: (
service: ServiceNames<TCategories, CategoryNames<TCategories>>,
category: CategoryNames<TCategories>
) => boolean;
eraseCookies: (
cookies: string | RegExp | Array<string | RegExp>,
path?: string,
domain?: string
) => void;
getCookie: (field?: string) => unknown;
setCookieData: (props: { value: unknown; mode: "set" | "update" }) => boolean;
getConfig: <K extends string>(field?: K) => unknown;
validConsent: () => boolean;
loadScript: (src: string, attrs?: Record<string, string>) => Promise<boolean>;
reset: (deleteCookie?: boolean) => void;
subscribe: (listener: (state: Readonly<ConsentState>) => void) => () => void;
on: (event: string, listener: (...args: unknown[]) => void) => () => void;
off: (event: string, listener: (...args: unknown[]) => void) => void;
destroy: () => void;
}

These are utility types that help with type inference. You typically don’t need to import them directly — they power the generic type parameter on ConsentInstance.

type CategoryNames<TCategories> = keyof TCategories & string;

Extracts category name strings from your config.

type CategoryAcceptArg<TCategories> =
| "all"
| "necessary"
| CategoryNames<TCategories>
| Array<CategoryNames<TCategories>>;

The union of all valid arguments to accept() and reject().

type ServiceNames<
TCategories extends Record<string, CategoryConfig>,
TCategory extends CategoryNames<TCategories>
> =
NonNullable<TCategories[TCategory]["services"]> extends Record<infer K, ServiceConfig>
? K & string
: never;

Extracts service name strings from a category’s services config.

type ServiceAcceptArg<TCategories, TCategory> =
| "all"
| ServiceNames<TCategories, TCategory>
| Array<ServiceNames<TCategories, TCategory>>;

The union of all valid arguments to acceptService() and rejectService().