Skip to content

tacxou/nestjs_module_twurple

Repository files navigation

Nest Logo

Twitch API/Chat/PubSub with Twurple NestJS module

NPM Version Package License Publish Package to npmjs GitHub contributors


Twurple Module

Twurple module for the NestJS framework.

Installation

Install the package with your preferred package manager:

# bun
bun add @tacxou/nestjs_module_twurple

# npm
npm install @tacxou/nestjs_module_twurple

# yarn
yarn add @tacxou/nestjs_module_twurple

# pnpm
pnpm add @tacxou/nestjs_module_twurple

Quick start examples

1) Configure the module

// app.module.ts
import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { TwurpleModule, TwurpleOptions } from '@tacxou/nestjs_module_twurple'

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    TwurpleModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        config: {
          authProvider: config.getOrThrow<TwurpleOptions['authProvider']>('TWURPLE_AUTH_PROVIDER'),
          chatChannels: config.get<string[]>('TWURPLE_CHAT_CHANNELS', []),
        },
      }),
    }),
  ],
})
export class AppModule {}

2) Inject and use Twurple clients

// twitch.service.ts
import { Injectable, OnModuleInit } from '@nestjs/common'
import {
  InjectTwurpleApi,
  InjectTwurpleChat,
  InjectTwurplePubsub,
} from '@tacxou/nestjs_module_twurple'
import { ApiClient } from '@twurple/api'
import { ChatClient } from '@twurple/chat'
import { PubSubClient } from '@twurple/pubsub'

@Injectable()
export class TwitchService implements OnModuleInit {
  constructor(
    @InjectTwurpleApi() private readonly api: ApiClient,
    @InjectTwurpleChat() private readonly chat: ChatClient,
    @InjectTwurplePubsub() private readonly pubSub: PubSubClient,
  ) {}

  async onModuleInit() {
    this.chat.onMessage((channel, user, message) => {
      if (message === '!ping') {
        void this.chat.say(channel, `Pong @${user}`)
      }
    })

    // Example API call
    const me = await this.api.users.getMe()
    console.log(`Twurple connected as: ${me?.displayName ?? 'unknown user'}`)

    // Example PubSub subscription
    // (adapt to your auth flow and topics)
    // await this.pubSub.registerUserListener(...)
  }
}

Official Twurple documentation

This module wraps and exposes Twurple clients for NestJS. For questions about Twurple classes, methods, events, and behavior, always refer to the official Twurple documentation:

Tests

bun test tests
bun test tests --watch
bun test tests --coverage --coverage-reporter lcov