Comparing version 0.1.1 to 0.2.0
@@ -5,2 +5,3 @@ declare type PlainObject = { | ||
declare type OnMessageCallback = (payload: any, message: string) => void; | ||
declare type MessageInterceptor = (messageObject: PlainObject) => PlainObject; | ||
export default class MyWebSocket { | ||
@@ -17,2 +18,6 @@ /** WebSocket instance */ | ||
responseCallbacksByRequestId: Map<string, OnMessageCallback>; | ||
/** Incoming message interceptors */ | ||
incomingInterceptors: Set<MessageInterceptor>; | ||
/** Outgoing message interceptors */ | ||
outgoingInterceptors: Set<MessageInterceptor>; | ||
/** | ||
@@ -147,3 +152,19 @@ * Constructor | ||
off(type: string): void; | ||
/** Message interceptors */ | ||
interceptors: { | ||
/** Outgoing message interceptors */ | ||
outgoing: { | ||
/** Add an outgoing message interceptor (which returns modified message object) */ | ||
use: (interceptor: MessageInterceptor) => MessageInterceptor; | ||
/** Remove a registered outgoing interceptor */ | ||
eject: (interceptor: MessageInterceptor) => void; | ||
}; | ||
incoming: { | ||
/** Add an incoming message interceptor (which returns modified message object) */ | ||
use: (interceptor: MessageInterceptor) => MessageInterceptor; | ||
/** Remove a registered incoming interceptor */ | ||
eject: (interceptor: MessageInterceptor) => void; | ||
}; | ||
}; | ||
} | ||
export {}; |
@@ -73,4 +73,31 @@ "use strict"; | ||
function MyWebSocket(ws) { | ||
var _this = this; | ||
/** Timeout for response message waiting */ | ||
this.timeout = DEFAULT_TIMEOUT; | ||
/** Message interceptors */ | ||
this.interceptors = { | ||
/** Outgoing message interceptors */ | ||
outgoing: { | ||
/** Add an outgoing message interceptor (which returns modified message object) */ | ||
use: function (interceptor) { | ||
_this.outgoingInterceptors.add(interceptor); | ||
return interceptor; | ||
}, | ||
/** Remove a registered outgoing interceptor */ | ||
eject: function (interceptor) { | ||
_this.outgoingInterceptors.delete(interceptor); | ||
}, | ||
}, | ||
incoming: { | ||
/** Add an incoming message interceptor (which returns modified message object) */ | ||
use: function (interceptor) { | ||
_this.incomingInterceptors.add(interceptor); | ||
return interceptor; | ||
}, | ||
/** Remove a registered incoming interceptor */ | ||
eject: function (interceptor) { | ||
_this.incomingInterceptors.delete(interceptor); | ||
}, | ||
}, | ||
}; | ||
if (typeof ws === 'string') { | ||
@@ -86,2 +113,4 @@ this.ws = new WebSocket(ws); | ||
this.responseCallbacksByRequestId = new Map(); | ||
this.incomingInterceptors = new Set(); | ||
this.outgoingInterceptors = new Set(); | ||
} | ||
@@ -154,2 +183,5 @@ /** | ||
messageObject = this.addType(messageObject, type); | ||
this.outgoingInterceptors.forEach(function (interceptor) { | ||
messageObject = interceptor(messageObject); | ||
}); | ||
return JSON.stringify(messageObject); | ||
@@ -251,2 +283,5 @@ }; | ||
var messageObject = JSON.parse(message); | ||
this.incomingInterceptors.forEach(function (interceptor) { | ||
messageObject = interceptor(messageObject); | ||
}); | ||
var type = this.getType(messageObject); | ||
@@ -253,0 +288,0 @@ var payload = this.getPayload(messageObject); |
{ | ||
"name": "waitsocket", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Library for better handling WebSocket interactions and waiting for response messages", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -24,2 +24,14 @@ # WaitSocket.js | ||
* Ability to add interceptors to modify incoming and outgoing messages: | ||
```javascript | ||
waitSocket.interceptors.incoming.use((messageObject) => { | ||
console.log('Let\'s see what we have received', messageObject); | ||
// And modify the object | ||
return { | ||
...messageObject, | ||
something: 'new', | ||
}; | ||
}); | ||
``` | ||
* Flexible message format customization | ||
@@ -26,0 +38,0 @@ |
@@ -11,2 +11,3 @@ import { nanoid } from 'nanoid'; | ||
type OnMessageCallback = (payload: any, message: string) => void; | ||
type MessageInterceptor = (messageObject: PlainObject) => PlainObject; | ||
@@ -37,2 +38,8 @@ /** WebSocket ready state */ | ||
/** Incoming message interceptors */ | ||
incomingInterceptors: Set<MessageInterceptor>; | ||
/** Outgoing message interceptors */ | ||
outgoingInterceptors: Set<MessageInterceptor>; | ||
/** | ||
@@ -58,2 +65,4 @@ * Constructor | ||
this.responseCallbacksByRequestId = new Map(); | ||
this.incomingInterceptors = new Set(); | ||
this.outgoingInterceptors = new Set(); | ||
} | ||
@@ -141,2 +150,5 @@ | ||
messageObject = this.addType(messageObject, type); | ||
this.outgoingInterceptors.forEach((interceptor) => { | ||
messageObject = interceptor(messageObject); | ||
}); | ||
return JSON.stringify(messageObject); | ||
@@ -241,3 +253,7 @@ } | ||
const message = event.data; | ||
const messageObject = JSON.parse(message); | ||
let messageObject = JSON.parse(message); | ||
this.incomingInterceptors.forEach((interceptor) => { | ||
messageObject = interceptor(messageObject); | ||
}); | ||
const type = this.getType(messageObject); | ||
@@ -287,2 +303,29 @@ const payload = this.getPayload(messageObject); | ||
} | ||
/** Message interceptors */ | ||
public interceptors = { | ||
/** Outgoing message interceptors */ | ||
outgoing: { | ||
/** Add an outgoing message interceptor (which returns modified message object) */ | ||
use: (interceptor: MessageInterceptor) => { | ||
this.outgoingInterceptors.add(interceptor); | ||
return interceptor; | ||
}, | ||
/** Remove a registered outgoing interceptor */ | ||
eject: (interceptor: MessageInterceptor) => { | ||
this.outgoingInterceptors.delete(interceptor); | ||
}, | ||
}, | ||
incoming: { | ||
/** Add an incoming message interceptor (which returns modified message object) */ | ||
use: (interceptor: MessageInterceptor) => { | ||
this.incomingInterceptors.add(interceptor); | ||
return interceptor; | ||
}, | ||
/** Remove a registered incoming interceptor */ | ||
eject: (interceptor: MessageInterceptor) => { | ||
this.incomingInterceptors.delete(interceptor); | ||
}, | ||
}, | ||
}; | ||
} |
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
42794
796
106