An open API service indexing awesome lists of open source software.

https://github.com/tacxou/nestjs_module_twurple

Twitch API/Chat/PubSub with Twurple NestJS module
https://github.com/tacxou/nestjs_module_twurple

abstract abstraction api async chat nestjs node nodejs npm promise pubsub spaces twitch twurple

Last synced: 3 months ago
JSON representation

Twitch API/Chat/PubSub with Twurple NestJS module

Awesome Lists containing this project

README

          



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:

```bash
# 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

```ts
// 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('TWURPLE_AUTH_PROVIDER'),
chatChannels: config.get('TWURPLE_CHAT_CHANNELS', []),
},
}),
}),
],
})
export class AppModule {}
```

### 2) Inject and use Twurple clients

```ts
// 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:

- https://twurple.js.org/
- https://twurple.js.org/docs/
- https://twurple.js.org/reference/
- https://twurple.js.org/reference/chat/classes/ChatClient.html
- https://twurple.js.org/reference/api/classes/ApiClient.html
- https://twurple.js.org/reference/pubsub/classes/PubSubClient.html

## Tests
```bash
bun test tests
bun test tests --watch
bun test tests --coverage --coverage-reporter lcov
```