UNPKG

@omneedia/client-js

Version:

Isomorphic Javascript client for Omneedia

52 lines 1.74 kB
import { io } from 'socket.io-client'; import OmneediaQueryDB from './lib/db'; import OmneediaRealtime from './lib/realtime'; import OmneediaAuth from './lib/auth'; import OmneediaRequest from './lib/request'; import prefix from './env/prefix'; export default class OmneediaClient { headers() { return { 'Content-Type': 'application/json', Authorization: 'Basic ' + this.KEY, apikey: this.KEY, }; } constructor(omneediaUrl, omneediaKey, options) { this.omneediaUrl = omneediaUrl; this.omneediaKey = omneediaKey; this.PREFIX = prefix; this.KEY = ''; this.URI = ''; this.socket = null; if (!omneediaUrl) throw new Error('omneediaUrl is required.'); if (!omneediaKey) throw new Error('omneediaKey is required.'); this.URI = omneediaUrl; this.KEY = omneediaKey; this.request = new OmneediaRequest(this); this.auth = new OmneediaAuth(this, options); this.options = options; } from(table) { return new OmneediaQueryDB(table, this); } channel(channel) { this.socket = io(String(this.URI), { path: this.PREFIX.REALTIME, withCredentials: true, extraHeaders: { apikey: `${this.KEY}`, Authorization: `Bearer ${this.KEY}`, }, }); this.socket.on('connect_error', function (e) { if (e.description == 401) throw `Invalid authentication credentials (401)`; throw `unhandled exception`; }); return new OmneediaRealtime(channel, this); } } //# sourceMappingURL=OmneediaClient.js.map