Comparing version 10.0.0-beta.2 to 10.0.0-beta.7
@@ -68,2 +68,4 @@ /// <reference types="ws" /> | ||
automaticReconnect?: boolean; | ||
endpoint?: EndpointDescription; | ||
reconnectDelay?: number; | ||
callback: (response: WebsocketResponse) => void; | ||
@@ -127,1 +129,5 @@ errorCallback?: (error: WebsocketErrorResponse) => void; | ||
export type FiltersMetadata = FilterType.TICKERS | FilterType.CATEGORY_CODES | FilterType.SOURCE | FilterType.CIKS; | ||
export type ApiConfig = { | ||
endpoint: EndpointDescription; | ||
reconnectDelay?: number; | ||
}; |
/// <reference types="ws" /> | ||
import { ConnectOptions, EndpointDescription, SubscribeOptions, WebsocketRequest } from "./types"; | ||
import { ConnectOptions, SubscribeOptions, WebsocketRequest } from "./types"; | ||
import WebSocket from "isomorphic-ws"; | ||
@@ -9,6 +9,6 @@ export declare class WsApi { | ||
reconnectMessages: WebsocketRequest[]; | ||
connectOptions?: ConnectOptions; | ||
constructor(apikey: string, options: ConnectOptions, endpoint?: EndpointDescription); | ||
private options; | ||
constructor(apikey: string, options: ConnectOptions); | ||
changeApikey(apikey: string): void; | ||
connect(options: ConnectOptions): void; | ||
connect(): void; | ||
subscribe(options: SubscribeOptions, resubscribeOnReconnect?: boolean): void; | ||
@@ -15,0 +15,0 @@ unsubscribe(subscriptionId: string): void; |
@@ -9,8 +9,18 @@ "use strict"; | ||
const enums_1 = require("./enums"); | ||
const defaultOptions = { | ||
automaticReconnect: true, | ||
endpoint: enums_1.Endpoint.PRODUCTION, | ||
reconnectDelay: 1000, | ||
errorCallback: () => { }, | ||
openCallback: () => { }, | ||
closeCallback: () => { }, | ||
callback: () => { } | ||
}; | ||
class WsApi { | ||
constructor(apikey, options, endpoint = enums_1.Endpoint.PRODUCTION) { | ||
constructor(apikey, options) { | ||
this.apikey = apikey; | ||
this.reconnectMessages = []; | ||
this.websocketEndpoint = endpoint.websocketProtocol + "://" + endpoint.host + "/ws/v3"; | ||
this.connect(options); | ||
this.options = Object.assign(Object.assign({}, defaultOptions), options); | ||
this.websocketEndpoint = this.options.endpoint.websocketProtocol + "://" + this.options.endpoint.host + "/ws/v3"; | ||
this.connect(); | ||
} | ||
@@ -20,8 +30,5 @@ changeApikey(apikey) { | ||
this.closeConnection(); | ||
if (this.connectOptions) | ||
this.connect(this.connectOptions); | ||
this.connect(); | ||
} | ||
connect(options) { | ||
options.automaticReconnect = options.automaticReconnect === undefined ? true : options.automaticReconnect; | ||
this.connectOptions = options; | ||
connect() { | ||
const urlParams = new URLSearchParams({ | ||
@@ -33,7 +40,7 @@ apikey: this.apikey, | ||
const response = JSON.parse(event.data.toString()); | ||
if (response.type === enums_1.WebsocketResponseType.ERROR && options.errorCallback) { | ||
options.errorCallback(response); | ||
if (response.type === enums_1.WebsocketResponseType.ERROR && this.options.errorCallback) { | ||
this.options.errorCallback(response); | ||
} | ||
else | ||
options.callback(response); | ||
this.options.callback(response); | ||
}; | ||
@@ -54,23 +61,20 @@ this.socket.onerror = (event) => { | ||
} | ||
if (options.errorCallback) | ||
options.errorCallback({ | ||
method: enums_1.WebsocketMethod.SOCKET_ERROR, | ||
type: enums_1.WebsocketResponseType.ERROR, | ||
value: { | ||
message | ||
} | ||
}); | ||
this.options.errorCallback({ | ||
method: enums_1.WebsocketMethod.SOCKET_ERROR, | ||
type: enums_1.WebsocketResponseType.ERROR, | ||
value: { | ||
message | ||
} | ||
}); | ||
}; | ||
this.socket.onopen = () => { | ||
if (options.openCallback) | ||
options.openCallback(); | ||
if (options.automaticReconnect) | ||
this.options.openCallback(); | ||
if (this.options.automaticReconnect) | ||
this.reconnectMessages.map(message => this.sendSocketMessage(message)); | ||
}; | ||
this.socket.onclose = (event) => { | ||
if (options.closeCallback) | ||
options.closeCallback(event); | ||
if (options.automaticReconnect) | ||
this.options.closeCallback(event); | ||
if (this.options.automaticReconnect) | ||
setTimeout(() => { | ||
this.connect(options); | ||
this.connect(); | ||
}, 200); | ||
@@ -77,0 +81,0 @@ }; |
{ | ||
"name": "newsware", | ||
"version": "10.0.0-beta.2", | ||
"version": "10.0.0-beta.7", | ||
"description": "Typescript client for interacting with the Newsware API", | ||
@@ -54,2 +54,2 @@ "main": "lib/src/index.js", | ||
} | ||
} | ||
} |
@@ -78,3 +78,5 @@ import { CloseEvent } from "isomorphic-ws" | ||
export interface ConnectOptions { | ||
automaticReconnect?: boolean | ||
automaticReconnect?: boolean // Defaults to true | ||
endpoint?: EndpointDescription // Defaults to production | ||
reconnectDelay?: number, // In milliseconds, default 1000 | ||
callback: (response: WebsocketResponse) => void, | ||
@@ -143,1 +145,6 @@ errorCallback?: (error: WebsocketErrorResponse) => void, | ||
export type FiltersMetadata = FilterType.TICKERS | FilterType.CATEGORY_CODES | FilterType.SOURCE | FilterType.CIKS | ||
export type ApiConfig = { | ||
endpoint: EndpointDescription | ||
reconnectDelay?: number // In milliseconds | ||
} |
@@ -1,5 +0,15 @@ | ||
import { ConnectOptions, EndpointDescription, SubscribeOptions, WebsocketRequest, WebsocketResponse, } from "./types"; | ||
import { ConnectOptions, SubscribeOptions, WebsocketRequest, WebsocketResponse, } from "./types"; | ||
import WebSocket, { CloseEvent, ErrorEvent, MessageEvent } from "isomorphic-ws" | ||
import { Endpoint, WebsocketMethod, WebsocketResponseType } from "./enums"; | ||
const defaultOptions: Required<ConnectOptions> = { | ||
automaticReconnect: true, | ||
endpoint: Endpoint.PRODUCTION, | ||
reconnectDelay: 1000, | ||
errorCallback: () => { }, | ||
openCallback: () => { }, | ||
closeCallback: () => { }, | ||
callback: () => { } | ||
} | ||
export class WsApi { | ||
@@ -9,11 +19,11 @@ private readonly websocketEndpoint: string | ||
reconnectMessages: WebsocketRequest[] = [] | ||
connectOptions?: ConnectOptions | ||
private options: Required<ConnectOptions> | ||
constructor( | ||
private apikey: string, | ||
options: ConnectOptions, | ||
endpoint: EndpointDescription = Endpoint.PRODUCTION | ||
options: ConnectOptions | ||
) { | ||
this.websocketEndpoint = endpoint.websocketProtocol + "://" + endpoint.host + "/ws/v3" | ||
this.connect(options) | ||
this.options = { ...defaultOptions, ...options } | ||
this.websocketEndpoint = this.options.endpoint.websocketProtocol + "://" + this.options.endpoint.host + "/ws/v3" | ||
this.connect() | ||
} | ||
@@ -24,10 +34,6 @@ | ||
this.closeConnection() | ||
if (this.connectOptions) | ||
this.connect(this.connectOptions) | ||
this.connect() | ||
} | ||
connect(options: ConnectOptions) { | ||
options.automaticReconnect = options.automaticReconnect === undefined ? true : options.automaticReconnect | ||
this.connectOptions = options | ||
connect() { | ||
const urlParams = new URLSearchParams({ | ||
@@ -40,6 +46,6 @@ apikey: this.apikey, | ||
const response = JSON.parse(event.data.toString()) as WebsocketResponse | ||
if (response.type === WebsocketResponseType.ERROR && options.errorCallback) { | ||
options.errorCallback(response) | ||
if (response.type === WebsocketResponseType.ERROR && this.options.errorCallback) { | ||
this.options.errorCallback(response) | ||
} else | ||
options.callback(response) | ||
this.options.callback(response) | ||
} | ||
@@ -61,16 +67,14 @@ | ||
if (options.errorCallback) | ||
options.errorCallback({ | ||
method: WebsocketMethod.SOCKET_ERROR, | ||
type: WebsocketResponseType.ERROR, | ||
value: { | ||
message | ||
} | ||
}) | ||
this.options.errorCallback({ | ||
method: WebsocketMethod.SOCKET_ERROR, | ||
type: WebsocketResponseType.ERROR, | ||
value: { | ||
message | ||
} | ||
}) | ||
} | ||
this.socket.onopen = () => { | ||
if (options.openCallback) | ||
options.openCallback() | ||
if (options.automaticReconnect) | ||
this.options.openCallback() | ||
if (this.options.automaticReconnect) | ||
this.reconnectMessages.map(message => this.sendSocketMessage(message)) | ||
@@ -80,7 +84,6 @@ } | ||
this.socket.onclose = (event: CloseEvent) => { | ||
if (options.closeCallback) | ||
options.closeCallback(event) | ||
if (options.automaticReconnect) | ||
this.options.closeCallback(event) | ||
if (this.options.automaticReconnect) | ||
setTimeout(() => { | ||
this.connect(options) | ||
this.connect() | ||
}, 200) | ||
@@ -87,0 +90,0 @@ } |
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
92832
33
1833