Skip to content

Commit 71a1108

Browse files
authored
BrowserTracker: fix method type definitions (close #1259) (#1284)
* BrowserTracker: fix method type definitions Per the [TypeScript docs](https://www.typescriptlang.org/docs/handbook/2/functions.html#return-type-void), functions that return values are type-compatible with functions declared as having void return type; however if you're consuming these type definitions this becomes annoying as you are required to cast `as unknown as T` at the call sites to get the correct types actually returned from these methods.
1 parent ffb1994 commit 71a1108

File tree

8 files changed

+76
-26
lines changed

8 files changed

+76
-26
lines changed

api-docs/docs/browser-tracker/browser-tracker.api.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ export interface BrowserTracker {
7272
enableActivityTrackingCallback: (configuration: ActivityTrackingConfiguration & ActivityTrackingConfigurationCallback) => void;
7373
enableAnonymousTracking: (configuration?: EnableAnonymousTrackingConfiguration) => void;
7474
flushBuffer: (configuration?: FlushBufferConfiguration) => void;
75-
getCookieName: (basename: string) => void;
76-
getDomainSessionIndex: () => void;
77-
getDomainUserId: () => void;
78-
getDomainUserInfo: () => void;
75+
getCookieName: (basename: string) => string;
76+
getDomainSessionIndex: () => number;
77+
getDomainUserId: () => string;
78+
getDomainUserInfo: () => ParsedIdCookie;
7979
getPageViewId: () => string;
8080
getTabId: () => string | null;
81-
getUserId: () => void;
81+
getUserId: () => string | null | undefined;
8282
id: string;
8383
namespace: string;
8484
newSession: () => void;
@@ -267,6 +267,21 @@ export interface PageViewEvent {
267267
title?: string | null;
268268
}
269269

270+
// @public
271+
export type ParsedIdCookie = [
272+
cookieDisabled: string,
273+
domainUserId: string,
274+
cookieCreateTs: number,
275+
visitCount: number,
276+
nowTs: number,
277+
lastVisitTs: number | undefined,
278+
sessionId: string,
279+
previousSessionId: string,
280+
firstEventId: string,
281+
firstEventTs: number | undefined,
282+
eventIndex: number
283+
];
284+
270285
// @public (undocumented)
271286
export type Platform = "web" | "mob" | "pc" | "srv" | "app" | "tv" | "cnsl" | "iot";
272287

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-tracker-core",
5+
"comment": "Update method signatures for BrowserTracker",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker-core"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-tracker-core",
5+
"comment": "",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker-core"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-tracker",
5+
"comment": "",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker"
10+
}

libraries/browser-tracker-core/src/tracker/id_cookie.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
import { PayloadBuilder } from '@snowplow/tracker-core';
3232
import { v4 as uuid } from 'uuid';
33-
import { ClientSession } from './types';
33+
import { ClientSession, ParsedIdCookie } from './types';
3434

3535
/**
3636
* Indices of cookie values
@@ -47,20 +47,6 @@ const cookieDisabledIndex = 0,
4747
firstEventTsInMsIndex = 9,
4848
eventIndexIndex = 10;
4949

50-
export type ParsedIdCookie = [
51-
string, // cookieDisabled
52-
string, // domainUserId
53-
number, // cookieCreateTs
54-
number, // visitCount
55-
number, // nowTs
56-
number | undefined, // lastVisitTs
57-
string, // sessionId
58-
string, // previousSessionId
59-
string, // firstEventId
60-
number | undefined, // firstEventTs
61-
number // eventIndex
62-
];
63-
6450
export function emptyIdCookie() {
6551
const idCookie: ParsedIdCookie = ['1', '', 0, 0, 0, undefined, '', '', '', undefined, 0];
6652
return idCookie;

libraries/browser-tracker-core/src/tracker/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
ClearUserDataConfiguration,
4848
ClientSession,
4949
ExtendedCrossDomainLinkerOptions,
50+
ParsedIdCookie,
5051
} from './types';
5152
import {
5253
parseIdCookie,
@@ -59,7 +60,6 @@ import {
5960
updateFirstEventInIdCookie,
6061
visitCountFromIdCookie,
6162
cookiesEnabledInIdCookie,
62-
ParsedIdCookie,
6363
clientSessionFromIdCookie,
6464
incrementEventIndexInIdCookie,
6565
emptyIdCookie,

libraries/browser-tracker-core/src/tracker/types.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,23 @@ export interface BrowserPluginConfiguration extends CorePluginConfiguration {
365365
plugin: BrowserPlugin;
366366
}
367367

368+
/**
369+
* The format of state elements stored in the `id` cookie.
370+
*/
371+
export type ParsedIdCookie = [
372+
cookieDisabled: string,
373+
domainUserId: string,
374+
cookieCreateTs: number,
375+
visitCount: number,
376+
nowTs: number,
377+
lastVisitTs: number | undefined,
378+
sessionId: string,
379+
previousSessionId: string,
380+
firstEventId: string,
381+
firstEventTs: number | undefined,
382+
eventIndex: number
383+
];
384+
368385
/**
369386
* The Browser Tracker
370387
*/
@@ -383,7 +400,7 @@ export interface BrowserTracker {
383400
*
384401
* @returns Domain session index
385402
*/
386-
getDomainSessionIndex: () => void;
403+
getDomainSessionIndex: () => number;
387404

388405
/**
389406
* Get the current page view ID
@@ -404,28 +421,28 @@ export interface BrowserTracker {
404421
*
405422
* @returns Cookie name
406423
*/
407-
getCookieName: (basename: string) => void;
424+
getCookieName: (basename: string) => string;
408425

409426
/**
410427
* Get the current user ID (as set previously with setUserId()).
411428
*
412429
* @returns Business-defined user ID
413430
*/
414-
getUserId: () => void;
431+
getUserId: () => string | null | undefined;
415432

416433
/**
417434
* Get visitor ID (from first party cookie)
418435
*
419436
* @returns Visitor ID (or null, if not yet known)
420437
*/
421-
getDomainUserId: () => void;
438+
getDomainUserId: () => string;
422439

423440
/**
424441
* Get the visitor information (from first party cookie)
425442
*
426443
* @returns The domain user information array
427444
*/
428-
getDomainUserInfo: () => void;
445+
getDomainUserInfo: () => ParsedIdCookie;
429446

430447
/**
431448
* Override referrer

trackers/browser-tracker/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
EventBatch,
4343
GetBatch,
4444
PostBatch,
45+
ParsedIdCookie,
4546
} from '@snowplow/browser-tracker-core';
4647
import { version } from '@snowplow/tracker-core';
4748

@@ -89,6 +90,7 @@ export {
8990
EventBatch,
9091
GetBatch,
9192
PostBatch,
93+
ParsedIdCookie,
9294
};
9395
export { version };
9496
export * from './api';

0 commit comments

Comments
 (0)