0.1.1 • Published 7 months ago

@nestjs-twurple/api v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

NestJS Twurple API

A NestJS wrapper for @twurple/api package.

This module can be used alone or in combination with other @nestjs-twurple modules.

!IMPORTANT These packages require twurple version 7.0 or higher.

Table of Contents

Installation

This module can be used in combination with @nestjs-twurple/auth module. Install it if necessary.

yarn:

yarn add @nestjs-twurple/api @twurple/auth @twurple/api

npm:

npm i @nestjs-twurple/api @twurple/auth @twurple/api

Usage

For basic information, check out the general documentation at the root of the repository @nestjs-twurple.

Also take a look at the official @twurple/api reference and guides: Calling the Twitch API.

Import and Registration

The module must be register either with register or registerAsync static methods.

To create an API client, you must provide TwurpleApiOptions. The options below extended from the ApiConfig interface provided by @twurple/api package, so the example below may become outdated at some point.

interface TwurpleApiIptions {
	authProvider: AuthProvider;
	fetchOptions?: TwitchApiCallFetchOptions;
	logger?: Partial<LoggerOptions>;
}

Example of using register method:

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { RefreshingAuthProvider } from '@twurple/auth';
import { TwurpleApiModule } from '@nestjs-twurple/api';

@Module({
	imports: [
		ConfigModule.forRoot({ isGlobal: true }),
		TwurpleApiModule.register({
			isGlobal: true,
			authProvider: new RefreshingAuthProvider({
				// ...
			})
		})
	]
})
export class AppModule {}

You can also use TwurpleAuthModule from @nestjs-twurple/auth package to inject the auth provider:

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TWURPLE_AUTH_PROVIDER, TwurpleAuthModule } from '@nestjs-twurple/auth';
import { TwurpleApiModule } from '@nestjs-twurple/api';
import { AuthProvider } from '@twurple/auth';

@Module({
	imports: [
		ConfigModule.forRoot({ isGlobal: true }),
		TwurpleAuthModule.registerAsync({
			isGlobal: true,
			inject: [ConfigService],
			useFactory: (configService: ConfigService) => {
				return {
					type: 'refreshing',
					clientId: configService.get('TWITCH_CLIENT_ID'),
					clientSecret: configService.get('TWITCH_CLIENT_SECRET')
				};
			}
		}),
		TwurpleApiModule.registerAsync({
			isGlobal: true,
			inject: [TWURPLE_AUTH_PROVIDER],
			useFactory: (authProvider: AuthProvider) => {
				// Here we are able to access the auth provider instance
				// provided by TwurpleAuthModule
				return { authProvider };
			}
		})
	]
})
export class AppModule {}

Using the ApiClient

The module internally creates an ApiClient instance. You can inject it anywhere you need it using @InjectApiClient() decorator:

import { Injectable } from '@nestjs/common';
import { InjectApiClient } from '@nestjs-twurple/api';
import { ApiClient } from '@twurple/api';

@Injectable()
export class CustomProvider {
	constructor(@InjectApiClient() private readonly _apiClient: ApiClient) {}
}

Alternatively, you can use TWURPLE_API_CLIENT token to inject the ApiClient instance:

import { Inject, Injectable } from '@nestjs/common';
import { TWURPLE_API_CLIENT } from '@nestjs-twurple/api';
import { ApiClient } from '@twurple/api';

@Injectable()
export class CustomProvider {
	constructor(@Inject(TWURPLE_API_CLIENT) private readonly _apiClient: ApiClient) {}
}
0.1.0

7 months ago

0.1.1

7 months ago

0.0.1

1 year ago