Git Product home page Git Product logo

twitch's Introduction

Twitch.js

ATTENTION: This library is still under initial development.

Public APIs may break with any release that increases y in version 0.y.z until this library reaches 1.0 (as per SemVer).

Installation

To add Twitch.js to your project, just execute:

yarn add twitch

Basic usage

Constructing a client instance

The first thing you do is constructing a Twitch client instance. The easiest way to get one is to supply static credentials:

import TwitchClient from 'twitch';

const clientId = '123abc';
const accessToken = 'def456';
const twitchClient = TwitchClient.withCredentials(clientId, accessToken);

You can also have the client refresh the tokens automatically if necessary by supplying an additional parameter containing the necessary data:

import TwitchClient, {AccessToken} from 'twitch';

const clientId = '123abc';
const accessToken = 'def456';
const clientSecret = 'foobar';
const refreshToken = '999999';
const twitchClient = TwitchClient.withCredentials(clientId, accessToken, {clientSecret, refreshToken, onRefresh: (token: AccessToken) => {
	// do things with the new token data, e.g. save them in your database
}});

The following sections assume that you have created a twitchClient already.

Calling the Twitch API

The API methods are organized into the different API subresources, like /users and /streams. All the API methods are async and thus can be awaited. Have a quick example:

async function isStreamLive(userName: string) {
	try {
		const user = await twitchClient.users.getUserByName(userName);
		await twitchClient.streams.getStreamByChannel(user.id); // will reject the promise if the stream is not live
		return true;
	} catch (e) {
		return false;
	}
}

We make extensive use of convenience methods that fetch related resources, so this can also be written a bit easier:

async function isStreamLive(userName: string) {
	try {
		const user = await twitchClient.users.getUserByName(userName);
		await user.getStream(); // will reject the promise if the stream is not live
		return true;
	} catch (e) {
		return false;
	}
}

Connecting to Twitch Chat

TODO

Connecting to PubSub

TODO

Support

You can join the Discord Server for support.

twitch's People

Contributors

d-fischer avatar freaktechnik avatar nihaals avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.