Comparing version 3.0.2 to 4.0.0
@@ -1,4 +0,2 @@ | ||
/// <reference types="ws" /> | ||
import { Filter, News } from "./types"; | ||
import { ErrorEvent, CloseEvent } from "isomorphic-ws"; | ||
import { SubscribeOptions } from "./types"; | ||
export declare class Api { | ||
@@ -8,5 +6,6 @@ private apikey; | ||
private socket?; | ||
private subscribed; | ||
constructor(apikey: string, host?: string); | ||
subscribe(filter: Filter, callback: (news: News[]) => void, errorCallback?: (errorEvent: ErrorEvent) => void, openCallback?: () => void, closeCallback?: (closeEvent: CloseEvent) => void): void; | ||
subscribe(options: SubscribeOptions): void; | ||
unsubscribe(): void; | ||
} |
@@ -13,7 +13,10 @@ "use strict"; | ||
this.host = host; | ||
this.subscribed = false; | ||
} | ||
subscribe(filter, callback, errorCallback, openCallback, closeCallback) { | ||
subscribe(options) { | ||
if (options.automaticReconnect == undefined) | ||
options.automaticReconnect = true; | ||
const urlParams = new URLSearchParams({ | ||
apikey: this.apikey, | ||
filter: JSON.stringify(filter) | ||
filter: JSON.stringify(options.filter) | ||
}); | ||
@@ -33,3 +36,3 @@ this.socket = new isomorphic_ws_1.default(`${this.host}/v1/ws/news?${urlParams.toString()}`); | ||
else | ||
callback(response.data); | ||
options.callback(response.data); | ||
}; | ||
@@ -41,15 +44,19 @@ this.socket.onerror = (event) => { | ||
} | ||
if (errorCallback) | ||
errorCallback(event); | ||
if (options.errorCallback) | ||
options.errorCallback(event); | ||
}; | ||
this.socket.onopen = () => { | ||
if (openCallback) | ||
openCallback(); | ||
this.subscribed = true; | ||
if (options.openCallback) | ||
options.openCallback(); | ||
}; | ||
this.socket.onclose = (event) => { | ||
if (closeCallback) | ||
closeCallback(event); | ||
if (options.closeCallback) | ||
options.closeCallback(event); | ||
if (this.subscribed && options.automaticReconnect) | ||
setTimeout(() => this.subscribe(options), 1000); | ||
}; | ||
} | ||
unsubscribe() { | ||
this.subscribed = false; | ||
this.socket.close(); | ||
@@ -56,0 +63,0 @@ } |
@@ -0,1 +1,3 @@ | ||
/// <reference types="ws" /> | ||
import { CloseEvent, ErrorEvent } from "isomorphic-ws"; | ||
export interface Filter { | ||
@@ -55,1 +57,9 @@ query?: Query; | ||
} | ||
export interface SubscribeOptions { | ||
filter: Filter; | ||
callback: (news: News[]) => void; | ||
errorCallback?: (errorEvent: ErrorEvent) => void; | ||
openCallback?: () => void; | ||
closeCallback?: (closeEvent: CloseEvent) => void; | ||
automaticReconnect?: boolean; | ||
} |
{ | ||
"name": "newsware", | ||
"version": "3.0.2", | ||
"version": "4.0.0", | ||
"description": "Typescript client for interacting with the Newsware API", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,2 +0,2 @@ | ||
import {ApiHost, Filter, News, Response} from "./types"; | ||
import {ApiHost, Response, SubscribeOptions} from "./types"; | ||
import WebSocket, {MessageEvent, ErrorEvent, CloseEvent} from "isomorphic-ws" | ||
@@ -6,2 +6,3 @@ | ||
private socket?: WebSocket | ||
private subscribed = false | ||
@@ -14,12 +15,9 @@ constructor( | ||
subscribe ( | ||
filter: Filter, | ||
callback: (news: News[]) => void, | ||
errorCallback?: (errorEvent: ErrorEvent) => void, | ||
openCallback?: () => void, | ||
closeCallback?: (closeEvent: CloseEvent) => void | ||
) { | ||
subscribe(options: SubscribeOptions) { | ||
if (options.automaticReconnect == undefined) | ||
options.automaticReconnect = true | ||
const urlParams = new URLSearchParams({ | ||
apikey: this.apikey, | ||
filter: JSON.stringify(filter) | ||
filter: JSON.stringify(options.filter) | ||
}) | ||
@@ -38,3 +36,3 @@ this.socket = new WebSocket(`${this.host}/v1/ws/news?${urlParams.toString()}`) | ||
} else | ||
callback(response.data) | ||
options.callback(response.data) | ||
} | ||
@@ -44,14 +42,20 @@ | ||
if (event.message?.includes('403')) { | ||
event = {... event, message: "Not authorized, make sure your api key is correct and active"} | ||
event = {...event, message: "Not authorized, make sure your api key is correct and active"} | ||
} | ||
if (errorCallback) errorCallback(event) | ||
if (options.errorCallback) | ||
options.errorCallback(event) | ||
} | ||
this.socket.onopen = () => { | ||
if (openCallback) openCallback() | ||
this.subscribed = true | ||
if (options.openCallback) | ||
options.openCallback() | ||
} | ||
this.socket.onclose = (event: CloseEvent) => { | ||
if (closeCallback) closeCallback(event) | ||
if (options.closeCallback) | ||
options.closeCallback(event) | ||
if (this.subscribed && options.automaticReconnect) | ||
setTimeout(() => this.subscribe(options), 1000) | ||
} | ||
@@ -61,4 +65,5 @@ } | ||
unsubscribe() { | ||
this.subscribed = false | ||
this.socket!!.close() | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
import {CloseEvent, ErrorEvent} from "isomorphic-ws"; | ||
export interface Filter { | ||
@@ -63,2 +65,11 @@ query?: Query | ||
SEC = "SEC" | ||
} | ||
export interface SubscribeOptions { | ||
filter: Filter, | ||
callback: (news: News[]) => void, | ||
errorCallback?: (errorEvent: ErrorEvent) => void, | ||
openCallback?: () => void, | ||
closeCallback?: (closeEvent: CloseEvent) => void, | ||
automaticReconnect?: boolean | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17524
380