Skip to content

Commit

Permalink
Fix SelfDescribingJson type to allow optional keys in type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jethron committed Jul 26, 2024
1 parent 0bb7954 commit b58a218
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
8 changes: 6 additions & 2 deletions api-docs/docs/browser-tracker/browser-tracker.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ export interface ClientSession extends Record<string, unknown> {
}

// @public
export interface CommonEventProperties<T = Record<string, unknown>> {
export interface CommonEventProperties<T extends {
[_: string]: unknown;
} = Record<string, unknown>> {
context?: Array<SelfDescribingJson<T>> | null;
// Warning: (ae-forgotten-export) The symbol "Timestamp" needs to be exported by the entry point index.module.d.ts
timestamp?: Timestamp | null;
Expand Down Expand Up @@ -330,7 +332,9 @@ export interface SelfDescribingEvent {
}

// @public
export type SelfDescribingJson<T extends Record<keyof T, unknown> = Record<string, unknown>> = {
export type SelfDescribingJson<T extends {
[_: string]: unknown;
} = Record<string, unknown>> = {
schema: string;
data: T;
};
Expand Down
4 changes: 3 additions & 1 deletion api-docs/docs/node-tracker/node-tracker.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ export interface SelfDescribingEvent {
}

// @public
export type SelfDescribingJson<T extends Record<keyof T, unknown> = Record<string, unknown>> = {
export type SelfDescribingJson<T extends {
[_: string]: unknown;
} = Record<string, unknown>> = {
schema: string;
data: T;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/browser-plugin-snowplow-ecommerce",
"comment": "",
"type": "none"
}
],
"packageName": "@snowplow/browser-plugin-snowplow-ecommerce"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/tracker-core",
"comment": "Fix SelfDescribingJson type to allow optional keys in type parameter",
"type": "none"
}
],
"packageName": "@snowplow/tracker-core"
}
8 changes: 4 additions & 4 deletions libraries/tracker-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { LOG } from './logger';
* Export interface for any Self-Describing JSON such as context or Self Describing events
* @typeParam T - The type of the data object within a SelfDescribingJson
*/
export type SelfDescribingJson<T extends Record<keyof T, unknown> = Record<string, unknown>> = {
export type SelfDescribingJson<T extends { [_: string]: unknown } = Record<string, unknown>> = {
/**
* The schema string
* @example 'iglu:com.snowplowanalytics.snowplow/web_page/jsonschema/1-0-0'
Expand All @@ -61,7 +61,7 @@ export type SelfDescribingJson<T extends Record<keyof T, unknown> = Record<strin
* Export interface for any Self-Describing JSON which has the data attribute as an array
* @typeParam T - The type of the data object within the SelfDescribingJson data array
*/
export type SelfDescribingJsonArray<T extends Record<keyof T, unknown> = Record<string, unknown>> = {
export type SelfDescribingJsonArray<T extends { [_: string]: unknown } = Record<string, unknown>> = {
/**
* The schema string
* @example 'iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-1'
Expand All @@ -70,7 +70,7 @@ export type SelfDescribingJsonArray<T extends Record<keyof T, unknown> = Record<
/**
* The data array which should conform to the supplied schema
*/
data: Array<T>;
data: (T extends SelfDescribingJson ? T : SelfDescribingJson<T>)[];
};

/**
Expand Down Expand Up @@ -119,7 +119,7 @@ function getTimestamp(timestamp?: Timestamp | null): TimestampPayload {
}

/** Additional data points to set when tracking an event */
export interface CommonEventProperties<T = Record<string, unknown>> {
export interface CommonEventProperties<T extends { [_: string]: unknown } = Record<string, unknown>> {
/** Add context to an event by setting an Array of Self Describing JSON */
context?: Array<SelfDescribingJson<T>> | null;
/** Set the true timestamp or overwrite the device sent timestamp on an event */
Expand Down
3 changes: 2 additions & 1 deletion plugins/browser-plugin-snowplow-ecommerce/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ export interface User {
email?: string;
}

export interface CommonEcommerceEventProperties<T = Record<string, unknown>> extends CommonEventProperties<T> {
export interface CommonEcommerceEventProperties<T extends { [_: string]: unknown } = Record<string, unknown>>
extends CommonEventProperties<T> {
/** Add context to an event by setting an Array of Self Describing JSON */
context?: Array<SelfDescribingJson<T>>;
}
Expand Down

0 comments on commit b58a218

Please sign in to comment.