Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Slack API TypeScript types generated by @seratch

License

Notifications You must be signed in to change notification settings

seratch/seratch-slack-types

Repository files navigation

Important Notice

This project is no longer maintained. As its successor, you can use https://github.com/seratch/slack-web-api-client , which provides up-to-date types of API responses.

Slack API TypeScript types generated by @seratch

https://www.npmjs.com/package/seratch-slack-types

This npm package is a collection of TypeScript type definition of Slack APIs.

Currently, the package support the following APIs.

  • API Methods (web-api)
  • Events API (events-api)
  • Real Time Messaging API (rtm-api)
  • Incoming payloads from Slack Platform (app-backend)
    • Dialogs (dialog_submission, cancellation, suggestion)
    • Interactive Messages (interactive_message, block_action)
    • Message Actions (message_action)
    • Slash Commands

How to use

import * as express from 'express';
import { Express, Request, Response } from 'express';

import * as Slack from '@slack/web-api';
import * as WebApi from 'seratch-slack-types/web-api';
import * as EventsApi from 'seratch-slack-types/events-api';

export const slackApi = new Slack.WebClient(process.env.SLACK_API_TOKEN);

export const app: Express = express();

app.post('/events', function (req: Request, res: Response) {
  const body = JSON.parse(req.body); // still "any" here

  if (body.type === 'url_verification') {
    // url verification
    res.status(200).send(body.event.challenge);

  } else if (body.type === 'event_callback' && body.event.type === 'message') { // still "any" here
    const payload = body as EventsApi.MessagePayload;
    // `payload.event.text` here is typesafe
    slackApi.api.test({ text: payload.event.text })
      .then((response: WebApi.ApiTestResponse) => { // `response` is typesafe
        if (response.ok) {
         // do something here
        } else {
         // do something here
        }
      }).catch(reason => {
        // do something here
      });
  } else {
    // do something here
  }
  res.status(200).json({ message: 'thanks!' });
});

How are the types generated

These types are generated from jSlack library's type definitions in Java + actual JSON responses fetched by running Slack Java SDK's unit tests. If you're interested in the details, take a look at Slack's Java SDK.

The coverage may not be 100% yet. A portion of the properties may be incorrect. If you find missing properties or something wrong, let us know here: https://github.com/seratch/seratch-slack-types/issues

License

The MIT License