| 21 | import { ENDPOINTS } from './constants'; |
| 22 | |
| 23 | export class OpenSeaStreamClient { |
| 24 | private socket: Socket; |
| 25 | private channels: Map<string, Channel>; |
| 26 | private logLevel: LogLevel; |
| 27 | private onEvent: OnClientEvent; |
| 28 | |
| 29 | constructor({ |
| 30 | network = Network.MAINNET, |
| 31 | token, |
| 32 | apiUrl, |
| 33 | connectOptions, |
| 34 | logLevel = LogLevel.INFO, |
| 35 | onError = (error) => this.error(error), |
| 36 | onEvent = () => true |
| 37 | }: ClientConfig) { |
| 38 | const endpoint = apiUrl || ENDPOINTS[network]; |
| 39 | const webTransportDefault = |
| 40 | typeof window !== 'undefined' ? window.WebSocket : undefined; |
| 41 | this.socket = new Socket(endpoint, { |
| 42 | params: { token }, |
| 43 | transport: webTransportDefault, |
| 44 | ...connectOptions |
| 45 | }); |
| 46 | |
| 47 | this.socket.onError(onError); |
| 48 | this.channels = new Map<string, Channel>(); |
| 49 | this.logLevel = logLevel; |
| 50 | this.onEvent = onEvent; |
| 51 | } |
| 52 | |
| 53 | private debug(message: unknown) { |
| 54 | if (this.logLevel <= LogLevel.DEBUG) { |
| 55 | console.debug(`[DEBUG]: ${message}`); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private info(message: unknown) { |
| 60 | if (this.logLevel <= LogLevel.INFO) { |
| 61 | console.info(`[INFO]: ${message}`); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private warn(message: unknown) { |
| 66 | if (this.logLevel <= LogLevel.WARN) { |
| 67 | console.warn(`[WARN]: ${message}`); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private error(message: unknown) { |
| 72 | if (this.logLevel <= LogLevel.ERROR) { |
| 73 | console.error(`[ERROR]: ${message}`); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public connect = () => { |
| 78 | this.debug('Connecting to socket'); |
| 79 | this.socket.connect(); |
| 80 | }; |
nothing calls this directly
no test coverage detected