commit 7e956ccf2b0474d364e6ef021726b423d6836912 Author: wisplite Date: Wed Apr 8 14:47:36 2026 -0500 initial commit, super basic implementation diff --git a/README b/README new file mode 100644 index 0000000..103f126 --- /dev/null +++ b/README @@ -0,0 +1 @@ +TypeScript client for Tether \ No newline at end of file diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..52311fc --- /dev/null +++ b/index.ts @@ -0,0 +1,48 @@ +const subscribedQueries = new Map void>(); +let ws: WebSocket | null = null; + +export const connect = async (url: string): Promise => { + ws = new WebSocket(url); + ws.onopen = () => { + console.log('Connected to Tether'); + }; + ws.onmessage = (event) => { + const data = JSON.parse(event.data); + if (data.type === 'query') { + subscribedQueries.forEach((callback, query) => { + if (data.query === query) { + callback(data.data); + } + }); + } else if (data.type === 'error') { + console.error(data.error); + } + }; + ws.onclose = () => { + console.log('Disconnected from Tether'); + }; + return ws; +}; + +export const disconnect = (ws: WebSocket) => { + ws.close(); +}; + +export const subscribe = (query: string, callback: (data: any) => void) => { + subscribedQueries.set(query, callback); +}; + +export const unsubscribe = (query: string) => { + subscribedQueries.delete(query); +}; + +export const sendMutation = (mutationName: string, params: any) => { + if (!ws) { + throw new Error('Not connected to Tether'); + } + ws.send(JSON.stringify({ + type: 'mutation', + name: mutationName, + payload: params, + })); +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..2277bb9 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "tether-ts", + "version": "1.0.0", + "description": "TypeScript client for Tether", + "license": "ISC", + "author": "wisplite", + "type": "module", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + } +}