brrr.now 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +46 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,6 +21,7 @@ const response = await sendNotification({
|
|
|
21
21
|
webhook: process.env.BRRR_WEBHOOK!,
|
|
22
22
|
title: "Coffee Machine Offline",
|
|
23
23
|
message: "The coffee machine is currently unreachable.",
|
|
24
|
+
threadId: "ops-coffee",
|
|
24
25
|
sound: "upbeat_bells",
|
|
25
26
|
openUrl: "https://status.example.com/coffee-machine",
|
|
26
27
|
});
|
|
@@ -52,6 +53,7 @@ On success, the API body is `{"success":true}`.
|
|
|
52
53
|
- `title`
|
|
53
54
|
- `subtitle`
|
|
54
55
|
- `message`
|
|
56
|
+
- `threadId`
|
|
55
57
|
- `sound`: `NotificationSound`
|
|
56
58
|
- `openUrl`
|
|
57
59
|
- `imageUrl`
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ interface SendNotificationParams {
|
|
|
5
5
|
title?: string;
|
|
6
6
|
subtitle?: string;
|
|
7
7
|
message?: string;
|
|
8
|
+
threadId?: string;
|
|
8
9
|
sound?: NotificationSound;
|
|
9
10
|
openUrl?: string;
|
|
10
11
|
imageUrl?: string;
|
|
@@ -17,6 +18,7 @@ type BrrrNowError = Error & {
|
|
|
17
18
|
status: number;
|
|
18
19
|
statusText: string;
|
|
19
20
|
body: string;
|
|
21
|
+
apiError?: string;
|
|
20
22
|
};
|
|
21
23
|
declare const sendNotification: (params: SendNotificationParams) => Promise<Response>;
|
|
22
24
|
declare const isBrrrNowError: (error: unknown) => error is BrrrNowError;
|
package/dist/index.js
CHANGED
|
@@ -8,19 +8,25 @@ var sendNotification = async (params) => {
|
|
|
8
8
|
},
|
|
9
9
|
body: JSON.stringify(createPayload(params))
|
|
10
10
|
});
|
|
11
|
+
const body = await readResponseBody(response);
|
|
12
|
+
const responseBody = parseResponseBody(body);
|
|
11
13
|
if (!response.ok) {
|
|
12
|
-
throw createBrrrNowError(response,
|
|
14
|
+
throw createBrrrNowError(response, body, responseBody?.success === false ? responseBody.error : undefined);
|
|
15
|
+
}
|
|
16
|
+
if (responseBody?.success === false) {
|
|
17
|
+
throw createBrrrNowError(response, body, responseBody.error);
|
|
13
18
|
}
|
|
14
19
|
return response;
|
|
15
20
|
};
|
|
16
21
|
var isBrrrNowError = (error) => {
|
|
17
|
-
return error instanceof Error && error.name === "BrrrNowError" && typeof error.status === "number" && typeof error.statusText === "string" && typeof error.body === "string";
|
|
22
|
+
return error instanceof Error && error.name === "BrrrNowError" && typeof error.status === "number" && typeof error.statusText === "string" && typeof error.body === "string" && (typeof error.apiError === "string" || typeof error.apiError === "undefined");
|
|
18
23
|
};
|
|
19
24
|
var createPayload = (params) => {
|
|
20
25
|
return {
|
|
21
26
|
title: params.title,
|
|
22
27
|
subtitle: params.subtitle,
|
|
23
28
|
message: params.message,
|
|
29
|
+
thread_id: params.threadId,
|
|
24
30
|
sound: params.sound,
|
|
25
31
|
open_url: params.openUrl,
|
|
26
32
|
image_url: params.imageUrl,
|
|
@@ -29,13 +35,17 @@ var createPayload = (params) => {
|
|
|
29
35
|
"interruption-level": params.interruptionLevel
|
|
30
36
|
};
|
|
31
37
|
};
|
|
32
|
-
var createBrrrNowError = (response, body) => {
|
|
33
|
-
const
|
|
38
|
+
var createBrrrNowError = (response, body, apiError) => {
|
|
39
|
+
const details = apiError ?? body;
|
|
40
|
+
const statusDetails = response.statusText ? ` ${response.statusText}` : "";
|
|
41
|
+
const message = response.ok ? `brrr.now API reported failure${details ? `: ${details}` : ""}` : `brrr.now request failed with ${response.status}${statusDetails}${details ? `: ${details}` : ""}`;
|
|
42
|
+
const error = new Error(message);
|
|
34
43
|
return Object.assign(error, {
|
|
35
44
|
name: "BrrrNowError",
|
|
36
45
|
status: response.status,
|
|
37
46
|
statusText: response.statusText,
|
|
38
|
-
body
|
|
47
|
+
body,
|
|
48
|
+
apiError
|
|
39
49
|
});
|
|
40
50
|
};
|
|
41
51
|
var resolveWebhookUrl = (webhook) => {
|
|
@@ -54,6 +64,37 @@ var serializeExpirationDate = (expirationDate) => {
|
|
|
54
64
|
}
|
|
55
65
|
return expirationDate;
|
|
56
66
|
};
|
|
67
|
+
var readResponseBody = async (response) => {
|
|
68
|
+
return await response.clone().text();
|
|
69
|
+
};
|
|
70
|
+
var parseResponseBody = (body) => {
|
|
71
|
+
if (!body) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const parsed = JSON.parse(body);
|
|
76
|
+
if (isResponseBody(parsed)) {
|
|
77
|
+
return parsed;
|
|
78
|
+
}
|
|
79
|
+
} catch {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
return;
|
|
83
|
+
};
|
|
84
|
+
var isResponseBody = (value) => {
|
|
85
|
+
if (!value || typeof value !== "object") {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
const success = value.success;
|
|
89
|
+
if (success === true) {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
if (success === false) {
|
|
93
|
+
const error = value.error;
|
|
94
|
+
return typeof error === "string" || typeof error === "undefined";
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
};
|
|
57
98
|
export {
|
|
58
99
|
sendNotification,
|
|
59
100
|
isBrrrNowError
|