npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ketch-sdk/ketch-data-layer

v1.4.1

Published

Ketch Data Layer interface

Downloads

337

Readme

ketch-data-layer

The ketch-data-layer library implements a Watcher and a set of Fetcher and Structure implementations for fetching and structuring identities.

The following Fetchers are implemented:

  • cookie
  • dataLayer
  • localStorage
  • managed
  • queryString
  • sessionStorage
  • window

The following Structures are implemented:

  • json
  • jwt
  • queryString
  • semicolon
  • string

Identity

An Identity can be described using the following interface:

export interface Identity {
  /**
   * type is the location on the page from which to retrieve identity information
   */
  type: IdentityType;

  /**
   * variable is the name to look up the identity value in the specified location
   */
  variable: string;

  /**
   * format is the encoding of the value
   */
  format: IdentityFormat;

  /**
   * key is the identifier to find the identity within the value if the format is IDENTITY_FORMAT_STRING
   * then key will be undefined
   */
  key?: string;

  /**
   * priority of the identity for consent conflict resolution
   */
  priority?: number;
}

Priority is ignored by this library.

Watcher

The primary class is the Watcher which contains a series of registered identities that it watches for changes to.

Creating a Watcher

const w = new Watcher(window, {interval, timeout})

Creates a new Watcher attached to the given window. The watcher will poll every interval until timeout seconds.

Add an identity

const name: string = ''
const identity: Identity | (() => Promise<string[]> = {}
w.add(name, identity)

Registers an identity with the given name using the provided identity configuration. There are two types of configuration allowed. The first type is an instance of an Identity object. If given this, the watcher selects from it's know library based on the specification in the Identity. The second option, which is a function returning a Promise of string values, can be used if the required identity cannot be described using Identity.

Start watching

await w.start()

Starts the Watcher watching based on the interval and timeout provided in the constructor.

Stop watching

w.stop()

Stops the Watcher.

Immediately notifying

await w.notify()

Immediately fetches and notifies about identities by emitting an identities event.

Add a listener

const listener: (...args: any[]) => void = () => {}
w.addListener('identities', listener)
w.on('identities', listener)

Add a listener function that gets called each time identities change. The argument to the listener will be an Identities map.

Add a one-time listener

const listener: (...args: any[]) => void = () => {}
w.once('identities', listener)

Adds a one-time listener function. The argument to the listener will be an Identities map.

Remove a listener

const listener: (...args: any[]) => void = () => {}
w.removeListener('identities', listener)
w.off('identities', listener)

Removes the given listener.

Remove all listeners

w.removeAllListeners('identities')

Removes all listeners.

Fetcher

A Fetcher is a function with the following signature:

type Fetcher = async (w: Window, name: string) => Promise<any[]>

The w parameter is the Window to attach to. The name is the name of the identity. The return must be an array of discovered, structured identities. The structured identities will be destructured using a Structure function to provide the identity value.

Structure

A Structure is a function with the following signature:

type Mapper = {
  [key: string]: string
}

type Structure = (value: any) => Mapper

Given some structured value, the job of the Structure function is to return a key-value map representing the values in that structure. If there is only a single identifiable value, then the key should be value.

Cookie

This library also exposes some utility functions that may be useful outside of the library's core purpose.

getCookie

import { getCookie } from '@ketch-sdk/ketch-data-layer/cookie'

const name: string = ''
const value = getCookie(window, name)

Retrieves the full raw value of the cookie with the given name.

setCookie

import { setCookie } from '@ketch-sdk/ketch-data-layer/cookie'

const name: string = ''
const value: any = ''
const ttl: number = 2 // 2 days
setCookie(window, name, value, ttl)

Sets the value of the cookie with the given name. The TTL of the cookie is set to ttl days. Note that long-lasting cookies are subject to Intelligent Tracking Prevention limitations.