All files / src/lib/shortbread shortbread.ts

0% Statements 0/14
0% Branches 0/2
0% Functions 0/3
0% Lines 0/14

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                                                                                                                                             
import { getLocalStoreWriter } from "./getLocalStoreWriter";
 
export interface ShortbreadOptions {
  domain?: string;
  parent?: HTMLElement;
  language?: Navigator["language"];
  __storeWriter?: (cookie: Document["cookie"]) => void;
}
 
export interface Shortbread {
  /**
   * Call this function to be notified when you cookie category is allowed
   */
  access: (
    name: "essential" | "performance" | "functional" | "advertising",
    onAllowed: () => void
  ) => void;
  /**
   * Call to ask the user the type of cookies they'd like to receive
   */
  checkForCookieConsent: () => void;
  /**
   * Open the Customize Cookie Dialog
   */
  customizeCookies: () => void;
  /**
   * Returns the consent cookie or undefined if not yet set
   */
  getConsentCookie: () => Document["cookie"] | undefined;
  /**
   * Call this to ask if the cookie has been set
   */
  hasConsent: (cookieName: string) => boolean;
}
 
// We only want to manage a single instance at a time
let instance: Shortbread | undefined;
 
/**
 * Call to initialize the shortbread instance. Must be called before all other methods
 */
export const initialize = async () => {
  const options: ShortbreadOptions = {
    domain: window.location.hostname,
    language: navigator.language,
    __storeWriter: getLocalStoreWriter(),
  };
 
  // Import the shortbread source
  const { AWSCShortbread } = await import("./source");
  instance = AWSCShortbread(options);
};
 
// Shortbread wrappers
 
const callIfDefined =
  <T extends keyof Shortbread>(method: T) =>
  (...args: Parameters<Shortbread[T]>): ReturnType<Shortbread[T]> => {
    if (!instance) {
      throw new Error("shortbread has not been initialized");
    }
 
    // Annotations are correct for consumers
    return (instance[method] as any)(...args);
  };
 
/**
 * Call this function to be notified when you cookie category is allowed
 */
export const access = callIfDefined("access");
/**
 * Call to ask the user the type of cookies they'd like to receive
 */
export const checkForCookieConsent = callIfDefined("checkForCookieConsent");
/**
 * Open the Customize Cookie Dialog
 */
export const customizeCookies = callIfDefined("customizeCookies");
/**
 * Returns the consent cookie or undefined if not yet set
 */
export const getConsentCookie = callIfDefined("getConsentCookie");
/**
 * Call this to ask if the cookie has been set
 */
export const hasConsent = callIfDefined("hasConsent");