don't include empty csrf in url
Massimo Melina committed
Jan 22, 2023 at 08:42 UTC
302c9949f323598ed22ffb66c8e54bf7a4c7ce8b
1 file changed
+19
-4
frontend/src/api.ts
+19
-4
@@ -49,13 +49,15 @@ export function useApi(cmd: string | Falsy, params?: object) : any {
49
type EventHandler = (type:string, data?:any) => void
50
51
export function apiEvents(cmd: string, params: Dict, cb:EventHandler) {
52
- const csrf = getCsrf()
53
- const processed: Record<string,string> = { csrf: csrf && JSON.stringify(csrf) }
52
+ const processed: Record<string,string> = {}
53
for (const k in params) {
54
const v = params[k]
56
- if (v === undefined) continue
57
- processed[k] = JSON.stringify(v)
55
+ if (v !== undefined)
56
+ processed[k] = JSON.stringify(v)
57
}
58
+ const csrf = getCsrf()
59
+ if (csrf)
60
+ processed.csrf = JSON.stringify(csrf)
61
const source = new EventSource(PREFIX + cmd + '?' + new URLSearchParams(processed))
62
source.onopen = () => cb('connected')
63
source.onerror = err => cb('error', err)
@@ -76,3 +78,16 @@ export function apiEvents(cmd: string, params: Dict, cb:EventHandler) {
78
function getCsrf() {
79
return getCookie('csrf')
80
}
81
+
82
+export async function getNotification(channel: string, cb: (name: string, data:any) => void) {
83
+ return new Promise(resolve => {
84
+ const ret = apiEvents('get_notifications', { channel }, (type, entries) => {
85
+ if (type === 'connected')
86
+ return resolve(ret)
87
+ if (type !== 'msg') return
88
+ for (const { name, data } of entries)
89
+ if (name)
90
+ cb(name, data)
91
+ })
92
+ })
93
+}
\ No newline at end of file