Skip to content

Instantly share code, notes, and snippets.

@pragmat1c
Forked from ofer-papaya/fullstory.d.ts
Last active August 24, 2022 17:36
Show Gist options
  • Save pragmat1c/8e0c60dec3a797af2723d685ade879a3 to your computer and use it in GitHub Desktop.
Save pragmat1c/8e0c60dec3a797af2723d685ade879a3 to your computer and use it in GitHub Desktop.
Fullstory API typescript definitions
interface Fullstory {
identify(uid: string, userVars?: object): void;
setUserVars(userVars: object): void;
getCurrentSessionURL(now?: boolean): string;
log(msg: string): void;
log(level: string, msg: string): void;
shutdown(): void;
restart(): void;
consent(consent: boolean): void;
event(eventName: string, eventProperties: object): void;
anonymize();
}
declare var FS: Fullstory;
@pragmat1c
Copy link
Author

Based on the API as documented here:
https://developer.fullstory.com/introduction

@jereddanielson
Copy link

Per the property name requirements we can refine the type for FS.event a bit more:

event(
  eventName: string,
  eventProperties:
    | { [key: `${string}_str`]: string }
    | { [key: `${string}${'_int' | '_real'}`]: number }
    | { [key: `${string}_date`]: Date }
    | { [key: `${string}_bool`]: boolean }
    | { [key: `${string}_strs`]: string[] }
    | { [key: `${string}${'_ints' | '_reals'}`]: number[] }
    | { [key: `${string}_dates`]: Date[] }
    | { [key: `${string}_bools`]: boolean[] }
): void;

These restrictions seem to apply similarly for FS.setUserVars and FS.identify with a few exceptions for "reserved properties", you could add those too and it would look something like this:

setUserVars(
  properties:
    | { [key: `${string}_str`]: string }
    | { [key: `${string}${'_int' | '_real'}`]: number }
    | { [key: `${string}_date`]: Date }
    | { [key: `${string}_bool`]: boolean }
    | { [key: `${string}_strs`]: string[] }
    | { [key: `${string}${'_ints' | '_reals'}`]: number[] }
    | { [key: `${string}_dates`]: Date[] }
    | { [key: `${string}_bools`]: boolean[] }
    | 'uid'
    | 'displayName'
    | 'email'
    | 'acctId'
    | 'website'
): void;

Of course, we can take advantage of some aliases to clean this all up a bit. The final result would be more like so:

type TypedFSPropertyNames =
  | { [key: `${string}_str`]: string }
  | { [key: `${string}${'_int' | '_real'}`]: number }
  | { [key: `${string}_date`]: Date }
  | { [key: `${string}_bool`]: boolean }
  | { [key: `${string}_strs`]: string[] }
  | { [key: `${string}${'_ints' | '_reals'}`]: number[] }
  | { [key: `${string}_dates`]: Date[] }
  | { [key: `${string}_bools`]: boolean[] };

type ReservedFSPropertyNames =
  | 'uid'
  | 'displayName'
  | 'email'
  | 'acctId'
  | 'website';

type Fullstory = {
  identify(
    uid: string,
    userVars?: TypedFSPropertyNames & ReservedFSPropertyNames
  );
  setUserVars(properties: TypedFSPropertyNames & ReservedFSPropertyNames): void;
  getCurrentSessionURL(now?: boolean): string;
  log(msg: string): void;
  log(level: string, msg: string): void;
  shutdown(): void;
  restart(): void;
  consent(consent: boolean): void;
  event(eventName: string, eventProperties: TypedFSPropertyNames): void;
};

declare var FS: Fullstory;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment