brrr.now 1.1.0 → 1.1.7

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/dist/index.d.ts CHANGED
@@ -17,6 +17,7 @@ type BrrrNowError = Error & {
17
17
  status: number;
18
18
  statusText: string;
19
19
  body: string;
20
+ apiError?: string;
20
21
  };
21
22
  declare const sendNotification: (params: SendNotificationParams) => Promise<Response>;
22
23
  declare const isBrrrNowError: (error: unknown) => error is BrrrNowError;
package/dist/index.js CHANGED
@@ -8,13 +8,18 @@ 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, await response.text());
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 {
@@ -29,13 +34,17 @@ var createPayload = (params) => {
29
34
  "interruption-level": params.interruptionLevel
30
35
  };
31
36
  };
32
- var createBrrrNowError = (response, body) => {
33
- const error = new Error(`brrr.now request failed with ${response.status} ${response.statusText}${body ? `: ${body}` : ""}`);
37
+ var createBrrrNowError = (response, body, apiError) => {
38
+ const details = apiError ?? body;
39
+ const statusDetails = response.statusText ? ` ${response.statusText}` : "";
40
+ const message = response.ok ? `brrr.now API reported failure${details ? `: ${details}` : ""}` : `brrr.now request failed with ${response.status}${statusDetails}${details ? `: ${details}` : ""}`;
41
+ const error = new Error(message);
34
42
  return Object.assign(error, {
35
43
  name: "BrrrNowError",
36
44
  status: response.status,
37
45
  statusText: response.statusText,
38
- body
46
+ body,
47
+ apiError
39
48
  });
40
49
  };
41
50
  var resolveWebhookUrl = (webhook) => {
@@ -54,6 +63,37 @@ var serializeExpirationDate = (expirationDate) => {
54
63
  }
55
64
  return expirationDate;
56
65
  };
66
+ var readResponseBody = async (response) => {
67
+ return await response.clone().text();
68
+ };
69
+ var parseResponseBody = (body) => {
70
+ if (!body) {
71
+ return;
72
+ }
73
+ try {
74
+ const parsed = JSON.parse(body);
75
+ if (isResponseBody(parsed)) {
76
+ return parsed;
77
+ }
78
+ } catch {
79
+ return;
80
+ }
81
+ return;
82
+ };
83
+ var isResponseBody = (value) => {
84
+ if (!value || typeof value !== "object") {
85
+ return false;
86
+ }
87
+ const success = value.success;
88
+ if (success === true) {
89
+ return true;
90
+ }
91
+ if (success === false) {
92
+ const error = value.error;
93
+ return typeof error === "string" || typeof error === "undefined";
94
+ }
95
+ return false;
96
+ };
57
97
  export {
58
98
  sendNotification,
59
99
  isBrrrNowError
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brrr.now",
3
- "version": "1.1.0",
3
+ "version": "1.1.7",
4
4
  "description": "Tiny TypeScript client for sending push notifications with brrr.now webhooks",
5
5
  "keywords": [
6
6
  "brrr",