simple-helix-api
Advanced tools
Comparing version 3.3.2 to 3.4.0-beta.1
@@ -16,7 +16,9 @@ "use strict"; | ||
method: "websocket", | ||
session_id: 0 | ||
session_id: 0, | ||
connected_at: "" | ||
}; | ||
this.deafultConnectOptions = { | ||
this.defaultConnectionOptions = { | ||
debug: false | ||
}; | ||
this.options = this.defaultConnectionOptions; | ||
this.subscribedEvents = {}; | ||
@@ -33,47 +35,47 @@ this.events = new node_events_1.EventEmitter(); | ||
} | ||
connect(options = this.deafultConnectOptions) { | ||
connect(options = this.defaultConnectionOptions) { | ||
this.options = options; | ||
this.connection = new reconnecting_websocket_1.default(this.endpoint, [], { | ||
WebSocket: ws_1.default, | ||
startClosed: true, | ||
maxRetries: Infinity | ||
}); | ||
return new Promise((resolve, reject) => { | ||
this.client = new reconnecting_websocket_1.default(this.endpoint, [], { | ||
WebSocket: ws_1.default, | ||
startClosed: true, | ||
maxRetries: Infinity | ||
}); | ||
this.client.addEventListener("error", reason => { | ||
this.onDisconnect(reason); | ||
return this.client.reconnect(); | ||
}); | ||
this.client.addEventListener("close", reason => { | ||
this.onDisconnect(reason); | ||
return reject(reason); | ||
}); | ||
this.client.addEventListener("message", message => { | ||
const data = JSON.parse(message.data); | ||
if (options.debug) { | ||
console.log("[simple-helix-api] EventSub Data:", data); | ||
} | ||
if (data.metadata.message_type === "session_welcome") { | ||
this.onConnect(data); | ||
return resolve(this); | ||
} | ||
return this.onMessage(data); | ||
}); | ||
return this.client.reconnect(); | ||
this.connection.onclose = ({ reason }) => this.onDisconnect(reason, reject); | ||
this.connection.onmessage = ({ data }) => this.onMessage(data, resolve); | ||
return this.connection.reconnect(); | ||
}); | ||
} | ||
disconnect() { | ||
if (this.client?.OPEN) { | ||
this.client.close(); | ||
if (this.connection.readyState === this.connection.OPEN) { | ||
this.connection.close(4003, "Manual disconnecting"); | ||
} | ||
return this.onDisconnect("Manual disconnecting"); | ||
return this.onDisconnect("Manual Disconnecting", () => (false)); | ||
} | ||
onConnect(data) { | ||
this.transport.session_id = data.payload.session.id; | ||
this.transport.connected_at = data.payload.connected_at; | ||
this.events.emit(this.WebsocketEvents.CONNECTED); | ||
} | ||
onDisconnect(reason) { | ||
onDisconnect(reason, reject) { | ||
if (this.connection.readyState === this.connection.CLOSING) { | ||
return reject(false); | ||
} | ||
this.transport.session_id = 0; | ||
this.subscribedEvents = {}; | ||
this.events.emit(this.WebsocketEvents.DISCONNECTED, reason); | ||
return reject(reason); | ||
} | ||
onMessage(data) { | ||
onMessage(data, resolve) { | ||
const parsed = JSON.parse(data); | ||
if (this.options.debug) { | ||
console.log("[simple-helix-api] EventSub Data:", parsed); | ||
} | ||
if (parsed.metadata.message_type === "session_welcome") { | ||
resolve(this); | ||
return this.onConnect(parsed); | ||
} | ||
return this.onEventMessage(parsed); | ||
} | ||
onEventMessage(data) { | ||
const messageType = data.metadata.message_type; | ||
@@ -104,5 +106,5 @@ switch (messageType) { | ||
get connected() { | ||
return Boolean(this.client?.OPEN); | ||
return this.connection.readyState === this.connection.OPEN; | ||
} | ||
} | ||
exports.default = EventSub; |
@@ -16,2 +16,7 @@ "use strict"; | ||
this.secureEndpoint = "wss://irc-ws.chat.twitch.tv:443"; | ||
this.defaultOptions = { | ||
debug: false, | ||
secure: true | ||
}; | ||
this.options = this.defaultOptions; | ||
this.globalUserState = {}; | ||
@@ -29,3 +34,3 @@ this.userState = {}; | ||
} | ||
connect(username, password, channels = [username], options) { | ||
connect(username, password, channels = [username], options = this.defaultOptions) { | ||
if (!username || !password) { | ||
@@ -37,2 +42,4 @@ throw new Error("You must to specify username and password"); | ||
} | ||
this.options = options; | ||
this.channels = channels; | ||
const endpoint = options.secure | ||
@@ -47,67 +54,92 @@ ? this.secureEndpoint | ||
return new Promise((resolve, reject) => { | ||
this.connection.addEventListener("open", () => { | ||
this.connection.send("CAP REQ :twitch.tv/commands twitch.tv/membership twitch.tv/tags"); | ||
this.connection.send(`PASS ${password}`); | ||
this.connection.send(`NICK ${username}`); | ||
}); | ||
this.connection.addEventListener("close", reason => { | ||
this.events.emit(this.WebsocketEvents.DISCONNECTED, reason); | ||
}); | ||
this.connection.addEventListener("message", rawMesage => { | ||
const ircMessage = rawMesage.data.trim(); | ||
if (options.debug) { | ||
console.log("[simple-helix-api] Chat Raw Message:", ircMessage); | ||
this.connection.onopen = () => this.auth(username, password); | ||
this.connection.onclose = ({ reason }) => this.onDisconnect(reason, reject); | ||
this.connection.onmessage = ({ data }) => this.onMessage(data, resolve, reject); | ||
return this.connection.reconnect(); | ||
}); | ||
} | ||
prepareMessage(data) { | ||
const ircMessage = data.trim(); | ||
if (this.options.debug) { | ||
console.log("[simple-helix-api] Chat Raw Message:", ircMessage); | ||
} | ||
return ircMessage.split("\r\n").map(irc_message_ts_1.parse); | ||
} | ||
auth(username, password) { | ||
this.connection.send("CAP REQ :twitch.tv/commands twitch.tv/membership twitch.tv/tags"); | ||
this.connection.send(`PASS ${password}`); | ||
this.connection.send(`NICK ${username}`); | ||
} | ||
join(channels) { | ||
this.channels = this.parseChannels(channels); | ||
this.connection.send(`JOIN ${this.channels.join(",")}`); | ||
} | ||
initGlobalState(parsed) { | ||
this.globalUserState = { | ||
...parsed.tags, | ||
...this.message(parsed) | ||
}; | ||
} | ||
initUserState(parsed) { | ||
this.userState = { | ||
...parsed.tags, | ||
...this.message(parsed) | ||
}; | ||
} | ||
ping(parsed) { | ||
this.connection.send(`PONG :${parsed.param}`); | ||
} | ||
onConnected() { | ||
this.events.emit(this.WebsocketEvents.CONNECTED); | ||
return this; | ||
} | ||
onDisconnect(reason, reject) { | ||
if (this.connection.readyState === this.connection.CLOSING) { | ||
return reject(false); | ||
} | ||
this.events.emit(this.WebsocketEvents.DISCONNECTED, reason); | ||
return reject(reason); | ||
} | ||
onMessage(data, resolve, reject) { | ||
for (const parsed of this.prepareMessage(data)) { | ||
if (!(parsed?.command)) { | ||
return false; | ||
} | ||
switch (parsed.command) { | ||
case "001": { | ||
this.join(this.channels); | ||
break; | ||
} | ||
for (const message of ircMessage.split("\r\n")) { | ||
const parsed = (0, irc_message_ts_1.parse)(message); | ||
if (!parsed?.command) { | ||
return; | ||
case "GLOBALUSERSTATE": { | ||
this.initGlobalState(parsed); | ||
break; | ||
} | ||
case "USERSTATE": { | ||
this.initUserState(parsed); | ||
break; | ||
} | ||
case "366": { | ||
resolve(this.onConnected()); | ||
break; | ||
} | ||
case "NOTICE": { | ||
if (this.AuthErrors.includes(parsed.params[1])) { | ||
reject(parsed.params[1]); | ||
} | ||
switch (parsed.command) { | ||
case "001": { | ||
this.channels = this.parseChannels(channels); | ||
this.connection.send(`JOIN ${this.channels.join(",")}`); | ||
break; | ||
} | ||
case "GLOBALUSERSTATE": { | ||
this.globalUserState = { | ||
...parsed.tags, | ||
...this.message(parsed) | ||
}; | ||
break; | ||
} | ||
case "USERSTATE": { | ||
this.userState = { | ||
...parsed.tags, | ||
...this.message(parsed) | ||
}; | ||
break; | ||
} | ||
case "366": { | ||
this.events.emit(this.WebsocketEvents.CONNECTED); | ||
resolve(this); | ||
break; | ||
} | ||
case "NOTICE": { | ||
if (this.AuthErrors.includes(parsed.params[1])) { | ||
reject(parsed.params[1]); | ||
} | ||
break; | ||
} | ||
case "PING": { | ||
this.connection.send(`PONG :${parsed.param}`); | ||
break; | ||
} | ||
default: { | ||
this.onMessage(parsed); | ||
break; | ||
} | ||
} | ||
this.events.emit(parsed.command, parsed); | ||
break; | ||
} | ||
}); | ||
return this.connection.reconnect(); | ||
}); | ||
case "PING": { | ||
this.ping(parsed); | ||
break; | ||
} | ||
default: { | ||
this.onChatMessage(parsed); | ||
break; | ||
} | ||
} | ||
this.events.emit(parsed.command, parsed); | ||
} | ||
return true; | ||
} | ||
onMessage(parsed) { | ||
onChatMessage(parsed) { | ||
switch (parsed.command) { | ||
@@ -176,5 +208,9 @@ case "PRIVMSG": { | ||
command(command, args = [], channel = this.channels[0]) { | ||
command = "/" + command.replace("/", ""); | ||
if (!command.startsWith("/")) { | ||
command = "/" + command; | ||
} | ||
if (args.length > 0) { | ||
command += " " + (Array.isArray(args) ? args.join(" ") : args); | ||
command += " " + (Array.isArray(args) | ||
? args.join(" ") | ||
: args); | ||
} | ||
@@ -190,5 +226,5 @@ return this.say(command, channel); | ||
get connected() { | ||
return Boolean(this.connection?.OPEN); | ||
return this.connection.readyState === this.connection.OPEN; | ||
} | ||
} | ||
exports.default = TMIClient; |
{ | ||
"name": "simple-helix-api", | ||
"version": "3.3.2", | ||
"version": "3.4.0-beta.1", | ||
"description": "The Simple Helix API allows developers to easily develop applications for Twitch", | ||
@@ -10,3 +10,3 @@ "main": "./dist/index.js", | ||
"dependencies": { | ||
"axios": "^1.2.0", | ||
"axios": "^1.2.1", | ||
"irc-message": "^3.0.2", | ||
@@ -18,8 +18,8 @@ "irc-message-ts": "^3.0.6", | ||
"devDependencies": { | ||
"@types/jest": "^29.2.3", | ||
"@types/jest": "^29.2.4", | ||
"@types/ws": "^8.5.3", | ||
"@typescript-eslint/eslint-plugin": "^5.45.0", | ||
"@typescript-eslint/parser": "^5.45.0", | ||
"@typescript-eslint/eslint-plugin": "^5.45.1", | ||
"@typescript-eslint/parser": "^5.45.1", | ||
"dotenv": "^16.0.3", | ||
"eslint": "^8.28.0", | ||
"eslint": "^8.29.0", | ||
"jest": "^29.3.1", | ||
@@ -48,2 +48,2 @@ "ts-jest": "^29.0.3", | ||
} | ||
} | ||
} |
@@ -11,5 +11,5 @@ import { AxiosRequestHeaders } from "axios"; | ||
vips(broadcaster_id: number, params?: {}): Promise<any>; | ||
allVips(broadcaster_id: number, limit?: any): Promise<any>; | ||
allVips(broadcaster_id: number, limit?: number): Promise<any>; | ||
whisper(from_user_id: number, to_user_id: number, message: string): Promise<any>; | ||
} | ||
export default Channel; |
@@ -8,9 +8,9 @@ import { AxiosRequestHeaders } from "axios"; | ||
bannedUsers(broadcaster_id: number, params?: {}): Promise<any>; | ||
allBannedUsers(broadcaster_id: number, limit?: any): Promise<any>; | ||
allBannedUsers(broadcaster_id: number, limit?: number): Promise<any>; | ||
moderators(broadcaster_id: number, params?: {}): Promise<any>; | ||
allModerators(broadcaster_id: number, limit?: any): Promise<any>; | ||
allModerators(broadcaster_id: number, limit?: number): Promise<any>; | ||
addModerator(broadcaster_id: number, user_id: number): Promise<any>; | ||
removeModerator(broadcaster_id: number, user_id: number): Promise<any>; | ||
blockedTerms(broadcaster_id: number, moderator_id: number, params?: {}): Promise<any>; | ||
allBlockedTerms(broadcaster_id: number, moderator_id: number, limit?: any): Promise<any>; | ||
allBlockedTerms(broadcaster_id: number, moderator_id: number, limit?: number): Promise<any>; | ||
addBlockedTerm(broadcaster_id: number, moderator_id: number, text: string): Promise<any>; | ||
@@ -17,0 +17,0 @@ removeBlockedTerm(broadcaster_id: number, moderator_id: number, id: number): Promise<any>; |
@@ -14,4 +14,4 @@ import { AxiosRequestHeaders } from "axios"; | ||
get(broadcaster_id: number, params?: {}): Promise<any>; | ||
all(broadcaster_id: number, limit?: any): Promise<any>; | ||
all(broadcaster_id: number, limit?: number): Promise<any>; | ||
} | ||
export default Polls; |
@@ -15,4 +15,4 @@ import { AxiosRequestHeaders } from "axios"; | ||
get(broadcaster_id: number, params?: {}): Promise<any>; | ||
all(broadcaster_id: number, limit?: any): Promise<any>; | ||
all(broadcaster_id: number, limit?: number): Promise<any>; | ||
} | ||
export default Predictions; |
@@ -14,5 +14,5 @@ import { AxiosRequestHeaders } from "axios"; | ||
redemption(broadcaster_id: number, reward_id: number, params?: {}): Promise<any>; | ||
all(broadcaster_id: number, limit?: any): Promise<any>; | ||
all(broadcaster_id: number, limit?: number): Promise<any>; | ||
updateRedemption(broadcaster_id: number, id: number, reward_id: number, status: "FULFILLED" | "CANCELED"): Promise<any>; | ||
} | ||
export default Rewards; |
@@ -6,6 +6,6 @@ import { AxiosRequestHeaders } from "axios"; | ||
categories(query: string, params?: {}): Promise<any>; | ||
allCategories(query: string, limit?: any): Promise<any>; | ||
allCategories(query: string, limit?: number): Promise<any>; | ||
channels(query: string, params?: {}): Promise<any>; | ||
allChannels(query: string, limit?: any): Promise<any>; | ||
allChannels(query: string, limit?: number): Promise<any>; | ||
} | ||
export default Search; |
@@ -7,6 +7,6 @@ import { AxiosRequestHeaders } from "axios"; | ||
streams(params?: {}): Promise<any>; | ||
allStreams(limit?: any): Promise<any>; | ||
allStreams(limit?: number): Promise<any>; | ||
followedStreams(user_id: number, params?: {}): Promise<any>; | ||
allFollowedStreams(user_id: number, limit?: any): Promise<any>; | ||
allFollowedStreams(user_id: number, limit?: number): Promise<any>; | ||
} | ||
export default Stream; |
@@ -6,5 +6,5 @@ import { AxiosRequestHeaders } from "axios"; | ||
broadcaster(broadcaster_id: number, params?: {}): Promise<any>; | ||
allBroadcaster(broadcaster_id: number, limit?: any): Promise<any>; | ||
allBroadcaster(broadcaster_id: number, limit?: number): Promise<any>; | ||
checkUser(broadcaster_id: number, user_id: number): Promise<any>; | ||
} | ||
export default Subscriptions; |
@@ -8,4 +8,4 @@ import { AxiosRequestHeaders } from "axios"; | ||
getTags(params?: {}): Promise<any>; | ||
all(limit?: any): Promise<any>; | ||
all(limit?: number): Promise<any>; | ||
} | ||
export default Tags; |
@@ -10,3 +10,3 @@ import { AxiosRequestHeaders } from "axios"; | ||
follows(to_id: number, params?: {}): Promise<any>; | ||
allFollows(to_id: number, limit?: any): Promise<any>; | ||
allFollows(to_id: number, limit?: number): Promise<any>; | ||
blocklist(broadcaster_id: number, params?: {}): Promise<any>; | ||
@@ -13,0 +13,0 @@ manageBlock(method: string | undefined, target_user_id: number, params?: {}): Promise<any>; |
@@ -13,3 +13,3 @@ import { AxiosRequestConfig } from "axios"; | ||
requestEndpoint(endpoint: string, data?: any, requestOptions?: AxiosRequestConfig, raw?: boolean): Promise<any>; | ||
requestAll(broadcaster_id: any | any[], context: any, builder: string, limit?: any, first?: number): Promise<any>; | ||
requestAll(broadcaster_id: any | any[], context: any, builder: string, limit?: number, first?: number): Promise<any>; | ||
requestCustom(endpoint: string, broadcaster_id: number, params?: {}, requestOptions?: TRequestConfig): Promise<any>; | ||
@@ -16,0 +16,0 @@ handleError(error: string): void; |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
95580
59
2292
2
Updatedaxios@^1.2.1