Building with Nylas using Typescript

5 min read
Tags:

Introduction

We recently announced the launch of the Nylas API v3. In conjunction with updating the version of the API, we are releasing updated versions of the Nylas SDKs!

The latest version of the Nylas Node SDK not only improves functionality but also introduces major changes to align with modern JavaScript features such as ES6 imports. We also built out the Nylas Node SDK with Typescript support. This will support developers in creating and maintaining new integrations to the Nylas Calendar, Email and Contacts APIs.

In this blog post, we’ll delve into the benefits of building with the Nylas Node SDK using Typescript!

Check out our recent livestream covering this blog post:

What’s new in Nylas Node SDK?

Before diving into some code samples, let’s highlight what the Nylas Node SDK brings in the latest version:

  • Modern JavaScript Features: Support for ES6 imports for cleaner, more modular code
  • TypeScript Support: Type safety and improved developer tooling

Embracing TypeScript with Nylas

TypeScript is an abstraction or superset of JavaScript. The benefit of building with types is the ability to support static typing which enables the following benefits:

  • Using type safety to catch errors early in development, support maintainability or when upgrading to a new version of a library with breaking changes
  • Auto-completion allows you to better understand the capabilities of functions across your codebase and even when importing functionality from third-party libraries
  • Seamlessly integrate TypeScript into existing JavaScript codebases, so a codebase can be migrated to TypeScript in steps instead of all at once

Migrate to Nylas API 3 using TypeScript

Upgrading to a new version of an API can be daunting, especially with breaking changes. Let’s first ensure you have typescript installed and configured for your repository:

npm install typescript

If you do not have typescript configured for your project, consider running the following commands to set the recommended configuration:

npx tsc --init

Next, let’s grab the latest version of the Nylas Node SDK with Typescript support:

npm i nylas@latest

Ensure the version of the Nylas Node SDK is at least version 7.0.0. TypeScript can be your ally in this process:

1. Identifying Changes: Use TypeScript to detect areas in your code that need updates. Type errors will guide you through necessary modifications by running the watch command:

npx tsc --watch

2. Refactoring with Confidence: TypeScript’s type system helps ensure that your refactoring doesn’t introduce new bugs. Here is an example of using the Nylas Node SDK types to access available functions to list messages:

Leverage TypeScript when migrating library versions

TypeScript is a powerful tool when migrating between changes such as upgrading to a major version of the SDK.

A good example is migrating between the Nylas Node SDK (v6 to v7), where you can take advantage of the introduction of types to identify where breaking changes exist in your application:

In the above example, we are retrieving the first five calendars using the syntax for the Nylas Node SDK (v6) and have upgraded the SDK package to v7. We receive an error telling us that the argument is not correct and that we need to provide an identifier, which is the user’s grant identifier.

Here is the migration example from Nylas Node SDK v6 to v7:

// Nylas Node SDK v6:
const Nylas = require('nylas')

Nylas.config({
  // replace with Nylas credentials
  clientId: process.env.NYLAS_CLIENT_ID,
  clientSecret: process.env.NYLAS_CLIENT_SECRET,
})
// Pass in connected user's access token
const nylas = Nylas.with(process.env.ACCESS_TOKEN)

const calendars = await nylas
  .calendars
  .list({limit: 5})
  .then(calendars => console.log('Calendars:', calendars))
// Nylas Node SDK v7:
import 'dotenv/config'
import Nylas from 'nylas'

const NylasConfig = {
 // replace with Nylas credentials
 apiKey: process.env.NYLAS_API_KEY,
 apiUri: process.env.NYLAS_API_URI,
}

const nylas = new Nylas(NylasConfig)

async function fetchFiveAvailableCalendars() {
  const calendars = await nylas.calendars.list({
    // Pass in connected user's grant id
    identifier: process.env.NYLAS_GRANT_ID,
    limit: 5
  })

  console.log('Calendars:', calendars)
}

fetchFiveAvailableCalendars() 

Best practices and tips

To wrap up, we’ll share some best practices and tips for using TypeScript with the Nylas Node SDK:

  • Spend time configuring the tsconfig.json to match the needs of your development team, a good starting example would be Airbnb’s configs
  • If you are finding too many errors at once, consider using the typescript functionality to exclude specific parts of the code if this helps with debugging and makes addressing errors more approachable
  • Consider checking types before allowing a Git commit to be created as a general practice using a tool like husky (i.e. pre-commit check).

Build time!

The introduction of TypeScript support in Nylas API v3 is more than just a feature update; it’s a commitment to modern, robust, and efficient development. Start integrating TypeScript into your Nylas projects today and lead the way in innovative, type-safe application development.

You can sign up for Nylas for free and start building! Continue building with Nylas by exploring different quickstart guides or by visiting our developer documentation.

Related resources

How to build a CRM in 3 sprints with Nylas

What is a CRM? CRM stands for Customer Relationship Management, and it’s basically a way…

How to create an appointment scheduler in your React app

Learn how to create an appointment scheduler in your React app using Nylas Scheduler. Streamline bookings and UX with step-by-step guidance.

Beyond APIs: Designing elegant, smart Web Components

Dive into the world of smart Web Components for advanced API integration solutions. Design elegant, customizable elements to elevate user experiences.