Skip to content

Latest commit

 

History

History
59 lines (42 loc) · 2.71 KB

README.md

File metadata and controls

59 lines (42 loc) · 2.71 KB

Segment TSD Generator

Overview

Segment's Protocols enable users to creating tracking plans and then govern all events that are sent to Segment to ensure they conform to this tracking plan. This project creates TypeScript definition files from these to give developers strongly typed access to Segment's various JavaScript/TypeScript APIs and SDKs.

Comparison to TypeWriter

Segment already has an open-source library called TypeWriter that does something similar (although it also supports the iOS and Android SDKs; however on the JavaScript/TypeScript front, it is specifically used for Analytics JS). TypeWriter takes a different approach to this project. TypeWriter creates helper functions, where-as this project merely creates TypeScript definitions that fit over the "vanilla" calls of e.g. the Analytics JS SDK.

Reasons to use TypeWriter

  • You want typing definitions for the iOS and Android SDKS
  • You want to use convenience methods for Analytics JS rather than make calls against the raw API

Reasons to use Segment TSD Generator

Install

Install via NPM:

npm i --save segment-tsd-generator

or Yarn:

yarn install segment-tsd-generator

Example Usage

import {getTrackingPlan} from 'segment-typescript-api/cjs/config-api'
import generator from 'segment-tsd-generator'
import * as fs from 'fs'

let WORKSPACE_TOKEN = '123'
let WORKSPACE_SLUG = 'my-workspace'

getTrackingPlan(WORKSPACE_TOKEN, WORKSPACE_SLUG, 'My Tracking Plan').then(generator).then(tsd=>{
  fs.writeFile('my_tracking_plan.d.ts', tsd, 'utf8', (err) => {
    if (err) {
      console.err(err)
    } else {
      // Do awesome stuff with you definition file
    }
  })
})