segment-tsd-generator
v0.1.11
Published
Generate .d.ts files from Segment Protocol Tracking Plans
Downloads
33
Readme
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
- You have already used the raw Analytics JS methods and want to retrofit Types over the top of your existing code
- You want to use the typings for Node JS calling against the HTTP Tracking API, or in Custom Sources or Custom Destinations. For this use-case, you may also consider using Segment TypeScript Definitions, which provide TypeScript definition files for these APIs/environments.
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
}
})
})