UNPKG

@opensea/stream-js

Version:

A TypeScript SDK to receive pushed updates from OpenSea over websocket

3 lines (2 loc) 5.6 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("phoenix")):"function"==typeof define&&define.amd?define(["exports","phoenix"],n):n((e||self).streamJs={},e.phoenix)}(this,function(e,n){function t(){return t=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},t.apply(this,arguments)}function o(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,o=new Array(n);t<n;t++)o[t]=e[t];return o}var r,i,c;e.Network=void 0,(e.Network||(e.Network={})).MAINNET="mainnet",e.EventType=void 0,(r=e.EventType||(e.EventType={})).ITEM_METADATA_UPDATED="item_metadata_updated",r.ITEM_LISTED="item_listed",r.ITEM_SOLD="item_sold",r.ITEM_TRANSFERRED="item_transferred",r.ITEM_RECEIVED_OFFER="item_received_offer",r.ITEM_RECEIVED_BID="item_received_bid",r.ITEM_CANCELLED="item_cancelled",r.COLLECTION_OFFER="collection_offer",r.TRAIT_OFFER="trait_offer",r.ORDER_INVALIDATE="order_invalidate",r.ORDER_REVALIDATE="order_revalidate",e.LogLevel=void 0,(i=e.LogLevel||(e.LogLevel={}))[i.DEBUG=20]="DEBUG",i[i.INFO=30]="INFO",i[i.WARN=40]="WARN",i[i.ERROR=50]="ERROR";var s=((c={})[e.Network.MAINNET]="wss://stream.openseabeta.com/socket",c);e.OpenSeaStreamClient=/*#__PURE__*/function(){function r(r){var i=this,c=r.network,a=void 0===c?e.Network.MAINNET:c,u=r.token,f=r.apiUrl,l=r.connectOptions,d=r.logLevel,E=void 0===d?e.LogLevel.INFO:d,v=r.onError,g=void 0===v?function(e){return i.error(e)}:v,h=r.onEvent,T=void 0===h?function(){return!0}:h;this.socket=void 0,this.channels=void 0,this.logLevel=void 0,this.onEvent=void 0,this.connect=function(){i.debug("Connecting to socket"),i.socket.connect()},this.disconnect=function(e){return void 0===e&&(e=function(){return i.info("Successfully disconnected from socket")}),i.channels.clear(),i.socket.disconnect(e)},this.createChannel=function(e){var n=i.socket.channel(e);return n.join().receive("ok",function(){return i.info('Successfully joined channel "'+e+'"')}).receive("error",function(){return i.error('Failed to join channel "'+e+'"')}),i.channels.set(e,n),n},this.getChannel=function(e){var n=i.channels.get(e);return n||(i.debug('Creating channel for topic: "'+e+'"'),n=i.createChannel(e)),n},this.on=function(e,n,t){i.socket.connect();var o="collection:"+n;i.debug("Fetching channel "+o);var r=i.getChannel(o);i.debug("Subscribing to "+e+" events on "+o);var c=i.onEvent;return r.on(e,function(o){c(n,e,o)&&t(o)}),function(){i.debug("Unsubscribing from "+e+" events on "+o),r.leave().receive("ok",function(){i.channels.delete(o),i.info('Successfully left channel "'+o+'" listening for '+e)})}},this.onItemMetadataUpdated=function(n,t){return i.debug('Listening for item metadata updates on "'+n+'"'),i.on(e.EventType.ITEM_METADATA_UPDATED,n,t)},this.onItemCancelled=function(n,t){return i.debug('Listening for item cancellations on "'+n+'"'),i.on(e.EventType.ITEM_CANCELLED,n,t)},this.onItemListed=function(n,t){return i.debug('Listening for item listings on "'+n+'"'),i.on(e.EventType.ITEM_LISTED,n,t)},this.onItemSold=function(n,t){return i.debug('Listening for item sales on "'+n+'"'),i.on(e.EventType.ITEM_SOLD,n,t)},this.onItemTransferred=function(n,t){return i.debug('Listening for item transfers on "'+n+'"'),i.on(e.EventType.ITEM_TRANSFERRED,n,t)},this.onItemReceivedOffer=function(n,t){return i.debug('Listening for item offers on "'+n+'"'),i.on(e.EventType.ITEM_RECEIVED_OFFER,n,t)},this.onItemReceivedBid=function(n,t){return i.debug('Listening for item bids on "'+n+'"'),i.on(e.EventType.ITEM_RECEIVED_BID,n,t)},this.onCollectionOffer=function(n,t){return i.debug('Listening for collection offers on "'+n+'"'),i.on(e.EventType.COLLECTION_OFFER,n,t)},this.onTraitOffer=function(n,t){return i.debug('Listening for trait offers on "'+n+'"'),i.on(e.EventType.TRAIT_OFFER,n,t)},this.onOrderInvalidate=function(n,t){return i.debug('Listening for order invalidation events on "'+n+'"'),i.on(e.EventType.ORDER_INVALIDATE,n,t)},this.onOrderRevalidate=function(n,t){return i.debug('Listening for order revalidation events on "'+n+'"'),i.on(e.EventType.ORDER_REVALIDATE,n,t)},this.onEvents=function(e,n,t){var r=n.map(function(n){return i.on(n,e,t)});return function(){for(var e,n=function(e,n){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,n){if(e){if("string"==typeof e)return o(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?o(e,n):void 0}}(e))){t&&(e=t);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(r);!(e=n()).done;)(0,e.value)()}};var p="undefined"!=typeof window?window.WebSocket:void 0;this.socket=new n.Socket(f||s[a],t({params:{token:u},transport:p},l)),this.socket.onError(g),this.channels=new Map,this.logLevel=E,this.onEvent=T}var i=r.prototype;return i.debug=function(n){this.logLevel<=e.LogLevel.DEBUG&&console.debug("[DEBUG]:",n)},i.info=function(n){this.logLevel<=e.LogLevel.INFO&&console.info("[INFO]:",n)},i.warn=function(n){this.logLevel<=e.LogLevel.WARN&&console.warn("[WARN]:",n)},i.error=function(n){this.logLevel<=e.LogLevel.ERROR&&console.error("[ERROR]:",n)},r}()}); //# sourceMappingURL=index.umd.js.map