Skip to content

Commit

Permalink
Upgrade Syncr (#1)
Browse files Browse the repository at this point in the history
* update package.json

* run prettier

* run npm prepare

* update README.md

* add MIT license file
  • Loading branch information
mudssrali authored Sep 29, 2022
1 parent 59097a6 commit 453c059
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 211 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 CERP

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
122 changes: 122 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Syncr

a bulletproof WebSocket API wrapper for creating and managing WebSocket connection to a server

[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
<a aria-label="Package size" href="https://bundlephobia.com/result?p=@cerp/syncr">
<img alt="" src="https://badgen.net/bundlephobia/minzip/@cerp/syncr">
</a>


## Install

Install with npm or yarn via

```
yarn add @cerp/syncr@1.1.2
```

or

```
npm i @cerp/syncr@1.1.2
```

## API

```ts
type Event = "connect" | "disconnect" | "message" | "verify"
interface Syncr {
url: string
ready: boolean
ws?: WebSocket
pingInterval?: number
pending: Map<string, {
resolve: (a: any) => any
reject: (a: any) => any
}>
message_timeout: number
connection_verified: boolean
verify(): void
connect(): Promise<void>
on(event: Event, f: Function): void
onNext(event: Event, f: Function): void
cleanup(): void
ping(): void
send(message: any, timeout?: number): Promise<any>
}
```

## Usage

- Use `syncr` with events

```ts
import Syncr from '@cerp/syncr'

const syncr = new Syncr("wss://example.com/ws")

// on connected to server
syncr.on('connect', () => console.log("connected to a server"))

// on disconnected from a server
syncr.on('disconnect', () => console.log("disconnected from a server"))

// on receiving a new message from server
syncr.on('message', (message: any) => console.log("new message", message))

// on verify a connection to server
syncr.on('verify', () => console.log("connection is verified"))
```

- Make a request to server using `Syncr.Send()` with `Promise`

```ts
import Syncr from '@cerp/syncr'
export const startService = () => {
const syncr = new Syncr("wss://example.com/ws")

syncr.send({
type: "START_SERVICE",
client_type: "Labs"
payload: {
initiator_id: "xxxx-xxxx"
req_token: "xxxx-xxxx"
req_timeout: 5000a
}
})
.then(response => {
// do something here
})
.catch(error => {
// do something here
})
}
```

`OR with async arrow style`

```ts
import Syncr from '@cerp/syncr'

export const startService = aysnc () => {
const syncr = new Syncr("wss://example.com/ws")

try {
const response = await syncr.send({
type: "START_SERVICE",
client: "Labs",
payload: {
initiator_id: "xxxx-xxxx"
req_token: "xxxx-xxxx"
req_timeout: 5000
}
})

// do something with reponse here

} catch(error) {
// do something with error here
}
}
```
4 changes: 2 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Syncr {
}
this.connection_verified = false;
this.cleanup();
yield sleep_1.default(9000 * Math.random() + 1000);
yield (0, sleep_1.default)(9000 * Math.random() + 1000);
this.connect();
});
this.ws.onerror = (err) => { }; //console.error("websocket err", err)
Expand Down Expand Up @@ -119,7 +119,7 @@ class Syncr {
// create promise, put in map
// when its returned, trigger it.
console.log("server --->", message);
const key = uuid_1.v4();
const key = (0, uuid_1.v4)();
return new Promise((resolve, reject) => {
this.pending.set(key, { resolve, reject });
if (!this.ws) {
Expand Down
31 changes: 14 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 31 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
{
"name": "@cerp/syncr",
"version": "1.1.2",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsc -p ."
},
"repository": {
"type": "git",
"url": "github.com/cerp/syncr"
},
"author": "Taimur Shah",
"license": "ISC",
"dependencies": {
"uuid": "^3.3.3"
},
"devDependencies": {
"@types/uuid": "^3.4.6"
},
"description": ""
}
"name": "@cerp/syncr",
"version": "1.1.3",
"description": "a bulletproof WebSocket API wrapper for creating and managing WebSocket connection to a server",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsc",
"prepare": "npm run build",
"prepublishOnly": "npm test"
},
"repository": {
"type": "git",
"url": "github.com/cerp/syncr"
},
"keywords": [
"websocket",
"websocket-api",
"ws-wrapper",
"ws-wrapper-api",
"ws-browser-api"
],
"author": "Taimur Shah",
"license": "MIT",
"dependencies": {
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/uuid": "^8.3.4",
"typescript": "^4.8.0"
}
}
Empty file removed readme.md
Empty file.
Loading

0 comments on commit 453c059

Please sign in to comment.