Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandrews committed Jan 20, 2021
1 parent bd4c486 commit 0cb9085
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 75 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,23 @@ refreshed token.
#### Handling token refresh example

```js
// Optionally, use fetchUtil to help.
import fetchUtil from 'twitch-js/lib/utils/fetch'
import { URLSearchParams } from 'url'
import fetch from 'node-fetch'

const refreshToken = 'eyJfaWQmNzMtNGCJ9%6VFV5LNrZFUj8oU231/3Aj'
const clientId = 'fooid'
const secret = 'barbazsecret'

const searchParams = new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: refreshToken,
client_id: clientId,
client_secret: clientSecret,
})

const onAuthenticationFailure = () =>
fetchUtil('https://id.twitch.tv/oauth2/token', {
fetch(`https://id.twitch.tv/oauth2/token${searchParams.toString()}`, {
method: 'post',
search: {
grant_type: 'refresh_token',
refresh_token: refreshToken,
client_id: clientId,
client_secret: clientSecret,
},
}).then((response) => response.accessToken)

const token = 'cfabdegwdoklmawdzdo98xt2fo512y'
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
"start:nightly": "ts-node --project tsconfig.json --transpile-only --require dotenv/config __e2e__/nightly/nightly.e2e.ts",
"start:example": "ts-node-dev examples/node.js",
"predocs:build": "rimraf docs",
"docs:build": "typedoc",
"docs:build:watch": "ts-node-dev --exec yarn docs:build"
"docs:build": "typedoc --target ES6",
"docs:build:watch": "chokidar \"src/**/*.ts\" -c \"yarn docs:build\""
},
"dependencies": {
"camelcase-keys": "^6.2.2",
Expand Down Expand Up @@ -100,6 +100,7 @@
"@typescript-eslint/parser": "^4.2.0",
"@wessberg/rollup-plugin-ts": "^1.3.4",
"benchmark": "^2.1.4",
"chokidar-cli": "^2.1.0",
"codecov": "^3.0.2",
"dotenv": "^8.2.0",
"eslint": "^7.9.0",
Expand Down
75 changes: 49 additions & 26 deletions src/Api/Api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="ky" />

import ky from 'ky-universal'

import camelCaseKeys from 'camelcase-keys'
Expand All @@ -8,10 +10,17 @@ import * as validators from './utils/validators'
import { ApiOptions } from './types'
import { Settings } from './constants'

/**
* @see https://github.com/sindresorhus/ky/tree/v0.26.0#readme
*/
type Ky = typeof ky

/**
* Make requests to Twitch API.
* Make requests to Twitch Helix API.
*
* The API client is a simple wrapper around [[`Ky`]] that handles request
* headers, retries, and token refreshes. For additional options, see
* [Ky documentation](https://github.com/sindresorhus/ky/tree/v0.26.0#readme).
*
* ## Initializing
*
Expand All @@ -29,53 +38,68 @@ type Ky = typeof ky
*
* ## Making requests
*
* By default, the API client makes requests to the
* [Helix API](https://dev.twitch.tv/docs/api), and exposes [[Api.get]],
* [[Api.post]] and [[Api.put]] methods. Query string parameters and body
* parameters are provided via `options.search` and `options.body` properties,
* respectively.
*
* To make requests to the [Kraken/v5 API](https://dev.twitch.tv/docs/v5), use
* `options.version = 'kraken'`
* The API client makes requests to the
* [Helix API](https://dev.twitch.tv/docs/api). To make requests to the
* [Kraken/v5 API](https://dev.twitch.tv/docs/v5), use [[`Api.kraken`]].
*
* ### Examples
*
* #### Get bits leaderboard
* @example Get streams
* ```js
* api
* .get('bits/leaderboard', { search: { user_id: '44322889' } })
* api('streams', { searchParams: { first: 20 } })
* .then(response => {
* // Do stuff with response ...
* })
* ```
*
* #### Get the latest Overwatch live streams
* ```
* api
* .get('streams', { version: 'kraken', search: { game: 'Overwatch' } })
* @example Start commercial
* ```js
* api('channels/commercial', {
* method: 'post',
* json: { broadcaster_id: '41245072', length: 60 },
* })
* .then(response => {
* // Do stuff with response ...
* })
* ```
*
* #### Start a channel commercial
* ```
* const channelId = '44322889'
* @example Create clip
* ```js
* api
* .post(`channels/${channelId}/commercial`, {
* version: 'kraken',
* body: { length: 30 },
* })
* .post('clips', { searchParams: { broadcaster_id: '44322889' } })
* .then(response => {
* // Do stuff with response ...
* })
* ```
*/
interface Api extends Ky {}
class Api {
private options: ApiOptions
private log: Logger
private api: Ky

/**
* Make API request to Kraken API.
*
* @example Get the latest Overwatch live streams
* ```
* api
* .kraken
* .get('streams', { searchParams: { game: 'Overwatch' } })
* .then(response => {
* // Do stuff with response ...
* })
* ```
*
* @example Start a channel commercial
* ```
* const channelId = '44322889'
* api
* .kraken
* .post(`channels/${channelId}/commercial`, { json: { length: 30 } })
* .then(response => {
* // Do stuff with response ...
* })
* ```
*/
public kraken: Ky

constructor(options: Partial<ApiOptions>) {
Expand Down Expand Up @@ -174,6 +198,5 @@ class Api {
this.options = validators.apiOptions(options)
}
}
interface Api extends Ky {}

export default Api
49 changes: 13 additions & 36 deletions src/Chat/utils/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,8 @@ export const base = (rawMessages: string, username = ''): BaseMessage[] => {
* @see https://dev.twitch.tv/docs/irc/membership/#join-twitch-membership
*/
export const joinMessage = (baseMessage: BaseMessage): JoinMessage => {
const [
,
username,
,
,
channel,
] = /:(.+)!(.+)@(.+).tmi.twitch.tv JOIN (#.+)/g.exec(baseMessage._raw)
const [, username, , , channel] =
/:(.+)!(.+)@(.+).tmi.twitch.tv JOIN (#.+)/g.exec(baseMessage._raw) || []

return {
...baseMessage,
Expand All @@ -127,13 +122,8 @@ export const joinMessage = (baseMessage: BaseMessage): JoinMessage => {
* @see https://dev.twitch.tv/docs/irc/membership/#part-twitch-membership
*/
export const partMessage = (baseMessage: BaseMessage): PartMessage => {
const [
,
username,
,
,
channel,
] = /:(.+)!(.+)@(.+).tmi.twitch.tv PART (#.+)/g.exec(baseMessage._raw)
const [, username, , , channel] =
/:(.+)!(.+)@(.+).tmi.twitch.tv PART (#.+)/g.exec(baseMessage._raw) || []

return {
...baseMessage,
Expand All @@ -149,12 +139,8 @@ export const partMessage = (baseMessage: BaseMessage): PartMessage => {
* @see https://dev.twitch.tv/docs/irc/membership/#mode-twitch-membership
*/
export const modeMessage = (baseMessage: BaseMessage): ModeMessages => {
const [
,
channel,
mode,
username,
] = /:[^\s]+ MODE (#[^\s]+) (-|\+)o ([^\s]+)/g.exec(baseMessage._raw)
const [, channel, mode, username] =
/:[^\s]+ MODE (#[^\s]+) (-|\+)o ([^\s]+)/g.exec(baseMessage._raw) || []

const isModerator = mode === '+'

Expand Down Expand Up @@ -185,13 +171,8 @@ export const modeMessage = (baseMessage: BaseMessage): ModeMessages => {
* @see https://dev.twitch.tv/docs/irc/membership/#names-twitch-membership
*/
export const namesMessage = (baseMessage: BaseMessage): NamesMessage => {
const [
,
,
,
channel,
names,
] = /:(.+).tmi.twitch.tv 353 (.+) = (#.+) :(.+)/g.exec(baseMessage._raw)
const [, , , channel, names] =
/:(.+).tmi.twitch.tv 353 (.+) = (#.+) :(.+)/g.exec(baseMessage._raw) || []

const namesV = names.split(' ')

Expand All @@ -215,7 +196,7 @@ export const namesEndMessage = (baseMessage: BaseMessage): NamesEndMessage => {
,
channel,
// message,
] = /:(.+).tmi.twitch.tv 366 (.+) (#.+) :(.+)/g.exec(baseMessage._raw)
] = /:(.+).tmi.twitch.tv 366 (.+) (#.+) :(.+)/g.exec(baseMessage._raw) || []

return {
...baseMessage,
Expand Down Expand Up @@ -303,14 +284,10 @@ export const clearMessageMessage = (
export const hostTargetMessage = (
baseMessage: BaseMessage,
): HostTargetMessage => {
const [
,
channel,
username,
numberOfViewers,
] = /:tmi.twitch.tv HOSTTARGET (#[^\s]+) :([^\s]+)?\s?(\d+)?/g.exec(
baseMessage._raw,
)
const [, channel, username, numberOfViewers] =
/:tmi.twitch.tv HOSTTARGET (#[^\s]+) :([^\s]+)?\s?(\d+)?/g.exec(
baseMessage._raw,
) || []
const isStopped = username === '-'

return {
Expand Down
Loading

0 comments on commit 0cb9085

Please sign in to comment.