1
1
import { Api } from "coder/site/src/api/api"
2
+ import { ProxyAgent } from "proxy-agent"
2
3
import * as vscode from "vscode"
3
4
import { WebSocket } from "ws"
4
5
import { errToStr } from "./api-helper"
5
- import { Storage } from "./storage"
6
- import { ProxyAgent } from "proxy-agent"
6
+ import { type Storage } from "./storage"
7
7
8
8
type InboxMessage = {
9
9
unread_count : number
@@ -14,26 +14,26 @@ type InboxMessage = {
14
14
targets : string [ ]
15
15
title : string
16
16
content : string
17
- actions : {
18
- [ key : string ] : string
19
- }
17
+ actions : Record < string , string >
20
18
read_at : string
21
19
created_at : string
22
20
}
23
21
}
24
22
25
- const TemplateWorkspaceOutOfMemory = "a9d027b4-ac49-4fb1-9f6d-45af15f64e7a"
26
- const TemplateWorkspaceOutOfDisk = "f047f6a3-5713-40f7-85aa-0394cce9fa3a"
23
+ // These are the template IDs of our notifications.
24
+ // Maybe in the future we should avoid hardcoding
25
+ // these in both coderd and here.
26
+ const TEMPLATE_WORKSPACE_OUT_OF_MEMORY = "a9d027b4-ac49-4fb1-9f6d-45af15f64e7a"
27
+ const TEMPLATE_WORKSPACE_OUT_OF_DISK = "f047f6a3-5713-40f7-85aa-0394cce9fa3a"
27
28
28
29
export class Inbox implements vscode . Disposable {
30
+ private readonly storage : Storage
29
31
private disposed = false
30
32
private socket : WebSocket
31
33
32
- constructor (
33
- httpAgent : ProxyAgent ,
34
- restClient : Api ,
35
- private readonly storage : Storage ,
36
- ) {
34
+ constructor ( httpAgent : ProxyAgent , restClient : Api , storage : Storage ) {
35
+ this . storage = storage
36
+
37
37
const baseUrlRaw = restClient . getAxiosInstance ( ) . defaults . baseURL
38
38
if ( ! baseUrlRaw ) {
39
39
throw new Error ( "No base URL set on REST client" )
@@ -43,11 +43,12 @@ export class Inbox implements vscode.Disposable {
43
43
const socketProto = baseUrl . protocol === "https:" ? "wss:" : "ws:"
44
44
const socketUrlRaw = `${ socketProto } //${ baseUrl . host } /api/v2/notifications/watch`
45
45
46
+ const coderSessionTokenHeader = "Coder-Session-Token"
46
47
this . socket = new WebSocket ( new URL ( socketUrlRaw ) , {
47
48
followRedirects : true ,
48
49
agent : httpAgent ,
49
50
headers : {
50
- "Coder-Session-Token" : restClient . getAxiosInstance ( ) . defaults . headers . common [ "Coder-Session-Token" ] as
51
+ [ coderSessionTokenHeader ] : restClient . getAxiosInstance ( ) . defaults . headers . common [ coderSessionTokenHeader ] as
51
52
| string
52
53
| undefined ,
53
54
} ,
@@ -66,8 +67,8 @@ export class Inbox implements vscode.Disposable {
66
67
const inboxMessage = JSON . parse ( data . toString ( ) ) as InboxMessage
67
68
68
69
if (
69
- inboxMessage . notification . template_id === TemplateWorkspaceOutOfDisk ||
70
- inboxMessage . notification . template_id === TemplateWorkspaceOutOfMemory
70
+ inboxMessage . notification . template_id === TEMPLATE_WORKSPACE_OUT_OF_DISK ||
71
+ inboxMessage . notification . template_id === TEMPLATE_WORKSPACE_OUT_OF_MEMORY
71
72
) {
72
73
vscode . window . showWarningMessage ( inboxMessage . notification . title )
73
74
}
0 commit comments