@stateworks/integration 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ import { Callback, Events, Event, EventPromise } from './types';
2
+ import WebSocket from 'isomorphic-ws';
3
+ /**
4
+ * Client
5
+ */
6
+ export declare class Client {
7
+ /**
8
+ * WebSocket instance
9
+ */
10
+ ws: WebSocket;
11
+ /**
12
+ * Client ID
13
+ */
14
+ id?: string;
15
+ /**
16
+ * Promises
17
+ */
18
+ promises: Map<string, EventPromise>;
19
+ /**
20
+ * Channels
21
+ */
22
+ channels: Map<string, Callback>;
23
+ /**
24
+ * Handlers
25
+ */
26
+ handlers: Map<Events, Callback>;
27
+ /**
28
+ * Constructor
29
+ *
30
+ * @param ws The Websocket instance
31
+ */
32
+ constructor(ws: WebSocket);
33
+ /**
34
+ * Subscribe
35
+ *
36
+ * @param channel The channel that will be subscribed
37
+ * @param callback The handler that will be called on channel message
38
+ */
39
+ subscribe(channel: string, callback: Callback): Promise<Event>;
40
+ /**
41
+ * Unsubscribe
42
+ *
43
+ * @param channel The channel that will be unsubscribed
44
+ */
45
+ unsubscribe(channel: string): Promise<Event>;
46
+ /**
47
+ * Publish
48
+ *
49
+ * @param channel The channel that will be used
50
+ * @param payload The payload that will be sent
51
+ */
52
+ publish(channel: string, payload: Object): Promise<Event>;
53
+ /**
54
+ * Send
55
+ *
56
+ * @param client_id The client id that will be used
57
+ * @param payload The payload that will be sent
58
+ */
59
+ send(client_id: string, payload: Object): Promise<Event>;
60
+ /**
61
+ * Broadcast
62
+ *
63
+ * @param payload The payload that will be broadcast
64
+ */
65
+ broadcast(payload: Object): Promise<Event>;
66
+ /**
67
+ * On
68
+ *
69
+ * @param event The event that will be used
70
+ * @param callback The callback that will be called on event
71
+ */
72
+ on(event: Events, callback: Callback): void;
73
+ }
package/dist/client.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Client=void 0;const uuid_1=require("uuid");class Client{constructor(s){this.promises=new Map,this.channels=new Map,this.handlers=new Map,this.ws=s}async subscribe(s,n){return new Promise((e,i)=>{const t=(0,uuid_1.v4)();this.channels.set(s,n),this.promises.set(t,{on_success:e,on_error:i}),this.ws.send(JSON.stringify({transaction_id:t,action:"subscribe",params:{channel:s}}))})}async unsubscribe(s){return new Promise((n,e)=>{const i=(0,uuid_1.v4)();this.channels.delete(s),this.promises.set(i,{on_success:n,on_error:e}),this.ws.send(JSON.stringify({transaction_id:i,action:"unsubscribe",params:{channel:s}}))})}async publish(s,n){return new Promise((e,i)=>{const t=(0,uuid_1.v4)();this.promises.set(t,{on_success:e,on_error:i}),this.ws.send(JSON.stringify({transaction_id:t,action:"publish",params:{channel:s,payload:n}}))})}async send(s,n){return new Promise((e,i)=>{const t=(0,uuid_1.v4)();this.promises.set(t,{on_success:e,on_error:i}),this.ws.send(JSON.stringify({transaction_id:t,action:"send",params:{to_client_id:s,payload:n}}))})}async broadcast(s){return new Promise((n,e)=>{const i=(0,uuid_1.v4)();this.promises.set(i,{on_success:n,on_error:e}),this.ws.send(JSON.stringify({transaction_id:i,action:"broadcast",params:{payload:s}}))})}on(s,n){this.handlers.set(s,n)}}exports.Client=Client;
@@ -0,0 +1,3 @@
1
+ export * from "./types";
2
+ export * from "./sdk";
3
+ export * from "./client";
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,i){void 0===i&&(i=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,i,o)}:function(e,t,r,i){void 0===i&&(i=r),e[i]=t[r]}),__exportStar=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||__createBinding(t,e,r)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./types"),exports),__exportStar(require("./sdk"),exports),__exportStar(require("./client"),exports);
package/dist/sdk.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { Client } from './client';
2
+ /**
3
+ * Sdk
4
+ */
5
+ export declare class Sdk {
6
+ /**
7
+ * Connect
8
+ *
9
+ * @param url The URL of the server
10
+ */
11
+ connect(url: string): Promise<Client>;
12
+ }
package/dist/sdk.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var __importDefault=this&&this.__importDefault||function(s){return s&&s.__esModule?s:{default:s}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Sdk=void 0;const isomorphic_ws_1=__importDefault(require("isomorphic-ws")),client_1=require("./client");class Sdk{async connect(s){return new Promise((e,a)=>{const n=new isomorphic_ws_1.default(s),t=new client_1.Client(n);t.ws.onerror=a,t.ws.onopen=()=>{},t.ws.onmessage=s=>{const a=JSON.parse(s.data);t.id=a.data.client_id,t.ws.onmessage=s=>{const e=JSON.parse(s.data);switch(e.action){case"ack":if(t.promises.has(e.transaction_id)){const s=t.promises.get(e.transaction_id);"success"==e.status?s.on_success(e):s.on_error(e)}if(t.handlers.has("ack")){t.handlers.get("ack")(e)}break;case"broadcast":if(t.handlers.has("broadcast")){t.handlers.get("broadcast")(e)}break;case"send":if(t.handlers.has("send")){t.handlers.get("send")(e)}break;case"publish":if(t.handlers.has("publish")){t.handlers.get("publish")(e)}const s=e;if(t.channels.has(s.params.channel)){t.channels.get(s.params.channel)(e)}}},e(t)}})}}exports.Sdk=Sdk;
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Callback
3
+ */
4
+ export type Callback = (event: any) => void;
5
+ /**
6
+ * Events
7
+ */
8
+ export type Events = 'broadcast' | 'publish' | 'send' | 'ack';
9
+ /**
10
+ * Statuses
11
+ */
12
+ export type Statuses = 'success' | 'failed';
13
+ /**
14
+ * Statuses
15
+ */
16
+ export type Messages = 'ok' | 'no effect' | 'unprocessable entity' | 'pong';
17
+ /**
18
+ * Event
19
+ */
20
+ export interface Event {
21
+ /**
22
+ * Transaction ID
23
+ */
24
+ transaction_id: string;
25
+ /**
26
+ * Action
27
+ */
28
+ action: Events;
29
+ /**
30
+ * Status
31
+ */
32
+ status: Statuses;
33
+ /**
34
+ * Message
35
+ */
36
+ message: Messages;
37
+ /**
38
+ * Timestamp
39
+ */
40
+ timestamp: number;
41
+ /**
42
+ * Runtime
43
+ */
44
+ runtime: number;
45
+ /**
46
+ * Data
47
+ */
48
+ data: Object;
49
+ }
50
+ /**
51
+ * Event Promise
52
+ */
53
+ export interface EventPromise {
54
+ /**
55
+ * On Success
56
+ */
57
+ on_success: Function;
58
+ /**
59
+ * On Error
60
+ */
61
+ on_error: Function;
62
+ }
63
+ /**
64
+ * Welcome Event
65
+ */
66
+ export interface WelcomeEvent extends Event {
67
+ /**
68
+ * Data
69
+ */
70
+ data: {
71
+ /**
72
+ * Client ID
73
+ */
74
+ client_id: string;
75
+ };
76
+ }
77
+ /**
78
+ * Broadcast Event
79
+ */
80
+ export interface BroadcastEvent extends Event {
81
+ /**
82
+ * Params
83
+ */
84
+ params: {
85
+ /**
86
+ * Payload
87
+ */
88
+ payload: Object;
89
+ };
90
+ }
91
+ /**
92
+ * Publish Event
93
+ */
94
+ export interface PublishEvent extends Event {
95
+ /**
96
+ * Params
97
+ */
98
+ params: {
99
+ /**
100
+ * Channel
101
+ */
102
+ channel: string;
103
+ /**
104
+ * Payload
105
+ */
106
+ payload: Object;
107
+ };
108
+ }
109
+ /**
110
+ * Send Event
111
+ */
112
+ export interface SendEvent extends Event {
113
+ /**
114
+ * Params
115
+ */
116
+ params: {
117
+ /**
118
+ * From Client ID
119
+ */
120
+ from_client_id: string;
121
+ /**
122
+ * To Client ID
123
+ */
124
+ to_client_id: string;
125
+ /**
126
+ * Payload
127
+ */
128
+ payload: Object;
129
+ };
130
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@stateworks/integration",
3
+ "version": "1.0.0",
4
+ "license": "UNLICENSED",
5
+ "author": "Ian Torres",
6
+ "type": "commonjs",
7
+ "main": "dist/src/index.js",
8
+ "types": "dist/src/index.d.ts",
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "scripts": {
16
+ "build": "tsc",
17
+ "clean": "rm -rf dist",
18
+ "test": "vitest --run",
19
+ "test:watch": "vitest",
20
+ "test:coverage": "vitest --run --coverage --reporter verbose",
21
+ "minify": "find dist -name \"*.js\" -type f -exec terser {} --compress --mangle --output {} \\;",
22
+ "format": "prettier --write ."
23
+ },
24
+ "dependencies": {
25
+ "isomorphic-ws": "^5.0.0",
26
+ "uuid": "^13.0.0",
27
+ "ws": "^8.19.0"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^22.15.2",
31
+ "@vitest/coverage-v8": "3.1.3",
32
+ "c8": "^10.1.3",
33
+ "prettier": "^3.5.3",
34
+ "terser": "^5.44.1",
35
+ "typescript": "^5.8.3",
36
+ "vite": "^6.3.4",
37
+ "vitest": "^3.1.2"
38
+ }
39
+ }