UNPKG

c15t

Version:

<div align="center"> <img src="https://c15t.com/logo-icon.png" alt="c15t Logo" width="64" height="64" /> <h1>c15t</h1> <p>Transform privacy consent from a compliance checkbox into a fully observable system</p>

155 lines 4.2 kB
/** * Client factory for creating consent management clients. * This module provides the main factory function for creating * client instances based on configuration options. */ import type { StoreOptions } from '../store'; import { type EndpointHandlers } from './client-custom'; import type { ConsentManagerCallbacks, ConsentManagerInterface } from './client-interface'; export type { ConsentManagerCallbacks, ConsentManagerInterface, } from './client-interface'; import type { RetryConfig } from './types'; export type { FetchOptions, ResponseContext, RetryConfig } from './types'; /** * Configuration for Custom mode * Allows for complete control over endpoint handling */ export type CustomClientOptions = { /** * Operating mode - custom endpoint implementation */ mode: 'custom'; /** * Custom handlers for each consent endpoint * Implement your own logic for each API operation */ endpointHandlers: EndpointHandlers; /** * Global callbacks for request events */ callbacks?: ConsentManagerCallbacks; /** * Store configuration options */ store?: StoreOptions; /** * Backend URL is not used in custom mode */ backendURL?: never; }; export type C15TClientOptions = { /** * c15t mode (default) - requires a backend URL */ mode?: 'c15t'; /** * Backend URL for API endpoints */ backendURL: string; /** * Additional HTTP headers */ headers?: Record<string, string>; /** * Custom fetch implementation */ customFetch?: typeof fetch; /** * Retry configuration */ retryConfig?: RetryConfig; }; export type OfflineClientOptions = { /** * Offline mode - disables all API requests */ mode: 'offline'; /** * Not used in offline mode */ backendURL?: never; /** * Additional HTTP headers (not used in offline mode) */ headers?: never; /** * Custom fetch implementation (not used in offline mode) */ customFetch?: never; }; /** * Union type of all possible client options */ export type ConsentManagerOptions = { /** * Client callbacks */ callbacks?: ConsentManagerCallbacks; store?: StoreOptions; } & (CustomClientOptions | C15TClientOptions | OfflineClientOptions); /** * Creates a new consent management client. * * This factory function creates the appropriate client implementation based on * the provided options. It supports three main operating modes: * * 1. c15t mode - Makes actual HTTP requests to a consent management backend * 2. Custom mode - Uses provided handler functions instead of HTTP requests * 3. Offline mode - Disables all API requests and returns empty successful responses * * @param options - Configuration options for the client * @returns A client instance that implements the ConsentManagerInterface * * @example * Basic c15t client with backend URL: * ```typescript * const client = configureConsentManager({ * backendURL: '/api/c15t' * }); * ``` * * @example * c15t client with custom backend URL: * ```typescript * const client = configureConsentManager({ * backendURL: 'https://api.example.com/consent' * }); * ``` * * @example * Offline client (for testing): * ```typescript * const client = configureConsentManager({ * mode: 'offline' * }); * ``` * * @example * Custom client with handler functions: * ```typescript * const client = configureConsentManager({ * mode: 'custom', * endpointHandlers: { * showConsentBanner: async () => ({ * data: { showConsentBanner: true }, * ok: true, * error: null, * response: null * }), * setConsent: async (options) => ({ * data: { success: true }, * ok: true, * error: null, * response: null * }), * verifyConsent: async (options) => ({ * data: { valid: true }, * ok: true, * error: null, * response: null * }) * } * }); * ``` */ export declare function configureConsentManager(options: ConsentManagerOptions): ConsentManagerInterface; //# sourceMappingURL=client-factory.d.ts.map