mitter.io
  • Welcome
  • Migrating from cloud to on-prem
  • Get mitter.io
    • Custom configuration
  • Getting started
    • Build Your First Android App
      • Setup
      • Authenticate a User
      • Start a Basic Chat
      • Selective Deliveries
      • Custom Payloads
    • Build Your First iOS App
      • Overview
      • Installation
      • Basic Setup
      • Receive Push Messages
      • Storyboard
      • Channel List
      • Channel Window
    • Build Your First Web App
      • Setting Up Your App
      • Start a Basic Chat
      • Selective Deliveries
    • Build Your First React Native app
  • Platform Reference
    • Introduction
    • Concepts
    • Authorization and Access
    • Calling the APIs
    • Users
    • Channels
      • Channel Streams and Typing Indicators
    • Messages
    • Delivery Endpoints (Push Notifications)
    • Federated Authentication
    • Basic Permissions and Privileges
    • ACLs and Advanced Permission Model
    • Metadata
  • SDKs
    • Android
      • Getting Started
      • Set up FCM
      • Presence and Timeline Events
      • Profiles, Pagination and Locators
      • Using the UI Framework
    • iOS
      • Installation
      • Basic Setup
      • Get the current user details
      • Create a Channel
      • Messaging
      • Push Messages
    • Javascript
      • Using the UI framework (web only)
      • For react-native
      • For node.js
      • TSDocs / JSDocs
      • For Typescript Users
    • Java (Backend)
Powered by GitBook
On this page
  • Type-matching functions
  • API calls using clients
  1. SDKs
  2. Javascript

For Typescript Users

mitter.io SDKs are compiled with typescript 3.0.3, but target typescript 2.7.+ Versions of typescript below 2.7 might work, but are not supported.

All mitter.io web/javacsript SDKs are written in typescript and as such are bundled with typings automatically. This section details on how to optimally use the mitter.io libraries if you are using typescript.

Type-matching functions

If you are using type-matching functions to get the type of a payload for example, the functions are implemented as predicates, so typescript will automatically cast it within the branch. For example:

mitter.subscribeToPaylod(payload => {
    if (payload['@type'] === 'NewMessagePayload') {
        // The next line will throw an error, since the type of
        // payload is MessagingPipelinePayload
        console.log('New message', payload.message.textPayload)
    } else {
        // The next will not throw an error, since the callback
        // argument is already typed to MessagingPipelinePayload
        console.log('New payload', payload.globalPipelinePayloadId)
    }
})

Instead, if you were to use the bundled type-matching functions:

mitter.subscribeToPayload(payload => {
    if (isNewMessagingPayload(payload)) {
        // The next line is OK, since the isNewMessagingPayload is a type
        // predicate, which tells the compiler that if true, then the
        // argument (payload) was of type NewMessagingPayload
        console.log('New message', payload.message.textPayload)
    }
})

API calls using clients

All bundled clients are typed and will throw an error when using incorrect request/response values

mitter.clients().channels().newChannel({
    // ERROR. Missing property 'defaultRuleSet'
    channelId: 'my-new-channel'
})

mitter.clients().channels().newChannel(new Channel(...))
    // ERROR. No property `wrongProperty` on type Channel
    .then(x => console.log('New channel', channel.wrongProperty)

PreviousTSDocs / JSDocsNextJava (Backend)

Last updated 6 years ago

NOTE The above does not apply if you are using fetch and/or axios to make API calls. The internal clients that are used with are exposed in case you wish to use them with your own axios clients. Do refer to the ts-docs bundled with @mitter-io/core on how to access these objects.

restyped