-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
46 lines (33 loc) · 1.43 KB
/
Copy pathindex.ts
File metadata and controls
46 lines (33 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { actions } from "./actions"
import { closeClient, sendClient } from "./websocket"
function webSocket(action: string, data: any = {}) {
console.log("SEND WebSocket", action, data)
sendClient(URL, action, data)
}
function close() {
closeClient()
}
function sendREST(action: string, data: any = {}) {
console.log("SEND REST", action, data)
const body = JSON.stringify({ action, ...data })
// fetch(URL, { method: "POST", body, headers: { "Content-type": "application/json; charset=UTF-8", "Access-Control-Allow-Origin": "*" }, cache: "reload", mode: "no-cors" }).catch(error)
fetch(URL, { method: "POST", body, headers: { "Content-type": "application/json; charset=UTF-8" } }).catch(error)
}
function sendHTTP(action: string, data: any = {}) {
console.log("SEND HTTP", action, data)
const query = `?action=${action}&data=${JSON.stringify(data)}`
fetch(URL + query, { method: "POST", signal: AbortSignal.timeout(100) }).catch(error)
}
function error(err: Error) {
if (err.name === "TimeoutError" || err.name === "AbortError") return
console.error("Could not connect. Make sure WebSocket/REST is enabled in FreeShow settings>Connection!")
}
let URL = ""
// url ex: http://localhost:5505
const api = (url: string) => {
if (!url) throw new Error("Missing port!")
URL = url
console.log("INIT API AT", URL)
return { actions, webSocket, sendREST, sendHTTP, close }
}
export default api