Reads the cookie consent configuration and records consent choices, for a consent UI you build yourself. Available as lynkow.cookies on the server and in the browser. For the built-in banner, use lynkow.consent.

Access via: lynkow.cookies

Methods

3 methods

clearCache

TypeScript
clearCache(): void

Clears this service's cached consent configuration for every locale, so the next getConfig() fetches it again. Only useful with cache: true.

Returns: void

TypeScript
lynkow.cookies.clearCache()

getConfig

TypeScript
getConfig(options?: BaseRequestOptions): Promise<CookieConfig>

Fetches the site's cookie consent configuration: mode, banner texts and styling, categories and the third-party scripts gated on each category. Texts and category labels come in the request locale, falling back to the site's default locale. A site with no consent settings returns a default configuration with enabled: false. With cache: true, cached for 10 minutes per locale.

Parameter

Type

Description

options

BaseRequestOptions

locale overrides the client locale; fetchOptions passes a framework cache directive, useful since the banner usually renders on every page.

Returns: Promise<CookieConfig>

TypeScript
const config = await lynkow.cookies.getConfig({
  fetchOptions: { next: { revalidate: 3600, tags: ['lynkow:consent'] } },
})
if (config.enabled) {
  console.log(config.categories) // [{id: 'necessary', ...}, {id: 'analytics', ...}]
  console.log(config.texts.acceptAll) // "Accept all"
}

logConsent

TypeScript
logConsent(preferences: CookiePreferences, options?: ConsentLogOptions): Promise<ConsentLogResponse>

Records a visitor's consent choice in the site's consent log. In a browser, visitorId, action and pageUrl are filled in for you. Outside a browser, pass visitorId. The API records the banner locale it serves for locale and the matching text version of CookiesService.getConfig. Resolves to a confirmation message. Throws LynkowError BAD_REQUEST (status 0, nothing sent) outside a browser without visitorId, and VALIDATION_ERROR for an invalid field such as a relative pageUrl or more than 100 categories.

Parameter

Type

Description

preferences

CookiePreferences

Category id to accepted flag, sent as consentGiven. At most 100 keys of 1 to 50 characters.

options

ConsentLogOptions

action, visitorId, pageUrl (see ConsentLogOptions); locale is the banner language, defaulting to the client locale.

Returns: Promise<ConsentLogResponse>

TypeScript
// Revoking a granted consent: 'withdraw' is never inferred.
await lynkow.cookies.logConsent(
  { necessary: true, analytics: false, marketing: false },
  { action: 'withdraw' }
)