snowplow / snowplow-javascript-tracker

Snowplow event tracker for client-side and server-side JavaScript. Add analytics to your websites, web apps and servers.
http://snowplowanalytics.com
BSD 3-Clause "New" or "Revised" License
541 stars 218 forks source link

Update ContextGenerator function return type to also accept SelfDescribingJson list and undefined #1070

Closed af-tomwilkins closed 2 years ago

af-tomwilkins commented 2 years ago

Describe the bug ContextGenerator functions can be passed to addGlobalContext. The type definition of ContextGenerator is as follows:

type ContextGenerator = (args?: ContextEvent) => SelfDescribingJson;

This constraint doesn't appear to reflect what's possible with Context Generators:

My suggestion is to update the type definition to be this:

type ContextGenerator = (args?: ContextEvent) => SelfDescribingJson | SelfDescribingJson[] | undefined;

To Reproduce

Using the example from then documentation should redline in an IDE using TypeScript.

import { addGlobalContexts } from '@snowplow/browser-tracker';
const contextGenerator = (args) => {
  if (args.eventType == 'pv') {
    return {
      schema: 'iglu:com.acme.marketing/some_event/jsonschema/1-0-0',
      data: { test: 1 },
    };
  }
};
addGlobalContexts([contextGenerator]);

Expected behavior addGlobalContexts should accept ContextGenerator functions that return SelfDescribingJson, a SelfDescribingJson list, or undefined.

paulboocock commented 2 years ago

@matus-tomlein could you look at this for ~v3.3~ v3.4?