summaryrefslogtreecommitdiff
path: root/static/src/assets/api.js
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2021-09-02 17:02:20 -0600
committerBrian Picciano <mediocregopher@gmail.com>2021-09-02 17:02:20 -0600
commit34f44cb5d5d6316009f242d27d2f3d69f4d5b90e (patch)
tree116a6e2db06dac246e794d1bc7f9e8b0c04f22e9 /static/src/assets/api.js
parent6bebc3fae73b5f5f68e5de9e083635cfab9037b1 (diff)
implementation of basic chat page which can show history and not much else
Diffstat (limited to 'static/src/assets/api.js')
-rw-r--r--static/src/assets/api.js52
1 files changed, 49 insertions, 3 deletions
diff --git a/static/src/assets/api.js b/static/src/assets/api.js
index 7ce2e89..4447bb1 100644
--- a/static/src/assets/api.js
+++ b/static/src/assets/api.js
@@ -1,5 +1,7 @@
import * as utils from "/assets/utils.js";
+const csrfTokenCookie = "csrf_token";
+
const doFetch = async (req) => {
let res, jsonRes;
try {
@@ -53,13 +55,13 @@ const call = async (route, opts = {}) => {
requiresPow = false,
} = opts;
- if (!utils.cookies["csrf_token"])
- throw "csrf_token cookie not set, can't make api call";
+ if (!utils.cookies[csrfTokenCookie])
+ throw `${csrfTokenCookie} cookie not set, can't make api call`;
const reqOpts = {
method,
headers: {
- "X-CSRF-Token": utils.cookies["csrf_token"],
+ "X-CSRF-Token": utils.cookies[csrfTokenCookie],
},
};
@@ -80,6 +82,50 @@ const call = async (route, opts = {}) => {
return doFetch(req);
}
+const ws = async (route, opts = {}) => {
+ const {
+ requiresPow = false,
+ } = opts;
+
+ const docURL = new URL(document.URL);
+ const protocol = docURL.protocol == "http:" ? "ws:" : "wss:";
+
+ const params = new URLSearchParams();
+ const csrfToken = utils.cookies[csrfTokenCookie];
+
+ if (!csrfToken)
+ throw `${csrfTokenCookie} cookie not set, can't make api call`;
+
+ params.set("csrfToken", csrfToken);
+
+ if (requiresPow) {
+ const {seed, solution} = await solvePow();
+ params.set("powSeed", seed);
+ params.set("powSolution", solution);
+ }
+
+ const rawConn = new WebSocket(`${protocol}//${docURL.host}${route}?${params.toString()}`);
+
+ const conn = {
+ next: () => new Promise((resolve, reject) => {
+ rawConn.onmessage = (m) => {
+ const mj = JSON.parse(m.data);
+ resolve(mj);
+ };
+ rawConn.onerror = reject;
+ rawConn.onclose = reject;
+ }),
+
+ close: rawConn.close,
+ };
+
+ return new Promise((resolve, reject) => {
+ rawConn.onopen = () => resolve(conn);
+ rawConn.onerror = reject;
+ });
+}
+
export {
call,
+ ws
}