firecrawl 4.21.0 → 4.22.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/dist/{chunk-R625SBJY.js → chunk-XWPH5FOQ.js} +3 -3
- package/dist/index.cjs +184 -5
- package/dist/index.d.cts +155 -1
- package/dist/index.d.ts +155 -1
- package/dist/index.js +183 -4
- package/dist/{package-MRLSD24T.js → package-VALCXP74.js} +1 -1
- package/package.json +2 -2
- package/src/__tests__/unit/v2/pagination.test.ts +21 -0
- package/src/v2/client.ts +85 -0
- package/src/v2/methods/monitor.ts +181 -0
- package/src/v2/types.ts +149 -0
- package/src/v2/utils/httpClient.ts +14 -0
- package/src/v2/utils/pagination.ts +12 -9
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
require_package
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-XWPH5FOQ.js";
|
|
4
4
|
|
|
5
5
|
// src/v2/utils/httpClient.ts
|
|
6
6
|
import axios from "axios";
|
|
@@ -113,6 +113,15 @@ var HttpClient = class {
|
|
|
113
113
|
delete(endpoint, headers) {
|
|
114
114
|
return this.request({ method: "delete", url: endpoint, headers });
|
|
115
115
|
}
|
|
116
|
+
patch(endpoint, body, options) {
|
|
117
|
+
return this.request({
|
|
118
|
+
method: "patch",
|
|
119
|
+
url: endpoint,
|
|
120
|
+
data: body,
|
|
121
|
+
headers: options?.headers,
|
|
122
|
+
timeout: options?.timeoutMs
|
|
123
|
+
});
|
|
124
|
+
}
|
|
116
125
|
prepareHeaders(idempotencyKey) {
|
|
117
126
|
const headers = {};
|
|
118
127
|
if (idempotencyKey) headers["x-idempotency-key"] = idempotencyKey;
|
|
@@ -635,12 +644,13 @@ async function fetchAllPages(http, nextUrl, initial, pagination) {
|
|
|
635
644
|
break;
|
|
636
645
|
}
|
|
637
646
|
if (!payload?.success) break;
|
|
638
|
-
|
|
647
|
+
const pageData = Array.isArray(payload.data) ? payload.data : payload.data?.pages || [];
|
|
648
|
+
for (const d of pageData) {
|
|
639
649
|
if (maxResults != null && docs.length >= maxResults) break;
|
|
640
650
|
docs.push(d);
|
|
641
651
|
}
|
|
642
652
|
if (maxResults != null && docs.length >= maxResults) break;
|
|
643
|
-
current = payload.next ?? null;
|
|
653
|
+
current = payload.next ?? (Array.isArray(payload.data) ? null : payload.data?.next) ?? null;
|
|
644
654
|
pageCount += 1;
|
|
645
655
|
}
|
|
646
656
|
return docs;
|
|
@@ -1223,6 +1233,126 @@ async function getTokenUsageHistorical(http, byApiKey) {
|
|
|
1223
1233
|
}
|
|
1224
1234
|
}
|
|
1225
1235
|
|
|
1236
|
+
// src/v2/methods/monitor.ts
|
|
1237
|
+
function queryString(params) {
|
|
1238
|
+
if (!params) return "";
|
|
1239
|
+
const query = new URLSearchParams();
|
|
1240
|
+
for (const [key, value] of Object.entries(params)) {
|
|
1241
|
+
if (value !== void 0 && value !== null) query.set(key, String(value));
|
|
1242
|
+
}
|
|
1243
|
+
const str = query.toString();
|
|
1244
|
+
return str ? `?${str}` : "";
|
|
1245
|
+
}
|
|
1246
|
+
function dataOrThrow(res, action) {
|
|
1247
|
+
if (res.status !== 200 || !res.data?.success || res.data.data == null) {
|
|
1248
|
+
throwForBadResponse(res, action);
|
|
1249
|
+
}
|
|
1250
|
+
return res.data.data;
|
|
1251
|
+
}
|
|
1252
|
+
async function createMonitor(http, request) {
|
|
1253
|
+
try {
|
|
1254
|
+
const res = await http.post("/v2/monitor", request);
|
|
1255
|
+
return dataOrThrow(res, "create monitor");
|
|
1256
|
+
} catch (err) {
|
|
1257
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "create monitor");
|
|
1258
|
+
throw err;
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
async function listMonitors(http, options) {
|
|
1262
|
+
try {
|
|
1263
|
+
const res = await http.get(
|
|
1264
|
+
`/v2/monitor${queryString(options)}`
|
|
1265
|
+
);
|
|
1266
|
+
return dataOrThrow(res, "list monitors");
|
|
1267
|
+
} catch (err) {
|
|
1268
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "list monitors");
|
|
1269
|
+
throw err;
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
async function getMonitor(http, monitorId) {
|
|
1273
|
+
try {
|
|
1274
|
+
const res = await http.get(`/v2/monitor/${monitorId}`);
|
|
1275
|
+
return dataOrThrow(res, "get monitor");
|
|
1276
|
+
} catch (err) {
|
|
1277
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "get monitor");
|
|
1278
|
+
throw err;
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
async function updateMonitor(http, monitorId, request) {
|
|
1282
|
+
try {
|
|
1283
|
+
const res = await http.patch(
|
|
1284
|
+
`/v2/monitor/${monitorId}`,
|
|
1285
|
+
request
|
|
1286
|
+
);
|
|
1287
|
+
return dataOrThrow(res, "update monitor");
|
|
1288
|
+
} catch (err) {
|
|
1289
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "update monitor");
|
|
1290
|
+
throw err;
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
async function deleteMonitor(http, monitorId) {
|
|
1294
|
+
try {
|
|
1295
|
+
const res = await http.delete(`/v2/monitor/${monitorId}`);
|
|
1296
|
+
if (res.status !== 200 || !res.data?.success) {
|
|
1297
|
+
throwForBadResponse(res, "delete monitor");
|
|
1298
|
+
}
|
|
1299
|
+
return true;
|
|
1300
|
+
} catch (err) {
|
|
1301
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "delete monitor");
|
|
1302
|
+
throw err;
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
async function runMonitor(http, monitorId) {
|
|
1306
|
+
try {
|
|
1307
|
+
const res = await http.post(
|
|
1308
|
+
`/v2/monitor/${monitorId}/run`,
|
|
1309
|
+
{}
|
|
1310
|
+
);
|
|
1311
|
+
return dataOrThrow(res, "run monitor");
|
|
1312
|
+
} catch (err) {
|
|
1313
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "run monitor");
|
|
1314
|
+
throw err;
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
async function listMonitorChecks(http, monitorId, options) {
|
|
1318
|
+
try {
|
|
1319
|
+
const res = await http.get(
|
|
1320
|
+
`/v2/monitor/${monitorId}/checks${queryString(options)}`
|
|
1321
|
+
);
|
|
1322
|
+
return dataOrThrow(res, "list monitor checks");
|
|
1323
|
+
} catch (err) {
|
|
1324
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "list monitor checks");
|
|
1325
|
+
throw err;
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
async function getMonitorCheck(http, monitorId, checkId, options) {
|
|
1329
|
+
try {
|
|
1330
|
+
const { autoPaginate: _autoPaginate, maxPages: _maxPages, maxResults: _maxResults, maxWaitTime: _maxWaitTime, ...query } = options ?? {};
|
|
1331
|
+
const res = await http.get(
|
|
1332
|
+
`/v2/monitor/${monitorId}/checks/${checkId}${queryString(query)}`
|
|
1333
|
+
);
|
|
1334
|
+
const detail = dataOrThrow(res, "get monitor check");
|
|
1335
|
+
const next = res.data?.next ?? detail.next ?? null;
|
|
1336
|
+
const auto = options?.autoPaginate ?? true;
|
|
1337
|
+
if (!auto || !next) {
|
|
1338
|
+
return { ...detail, next };
|
|
1339
|
+
}
|
|
1340
|
+
return {
|
|
1341
|
+
...detail,
|
|
1342
|
+
pages: await fetchAllPages(
|
|
1343
|
+
http,
|
|
1344
|
+
next,
|
|
1345
|
+
detail.pages || [],
|
|
1346
|
+
options
|
|
1347
|
+
),
|
|
1348
|
+
next: null
|
|
1349
|
+
};
|
|
1350
|
+
} catch (err) {
|
|
1351
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "get monitor check");
|
|
1352
|
+
throw err;
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1226
1356
|
// src/v2/watcher.ts
|
|
1227
1357
|
import { EventEmitter } from "events";
|
|
1228
1358
|
var hasGlobalWebSocket = () => {
|
|
@@ -1604,6 +1734,55 @@ var FirecrawlClient = class {
|
|
|
1604
1734
|
async crawlParamsPreview(url, prompt) {
|
|
1605
1735
|
return crawlParamsPreview(this.http, url, prompt);
|
|
1606
1736
|
}
|
|
1737
|
+
// Monitor
|
|
1738
|
+
/**
|
|
1739
|
+
* Create a scheduled monitor.
|
|
1740
|
+
*/
|
|
1741
|
+
async createMonitor(request) {
|
|
1742
|
+
return createMonitor(this.http, request);
|
|
1743
|
+
}
|
|
1744
|
+
/**
|
|
1745
|
+
* List monitors for the authenticated team.
|
|
1746
|
+
*/
|
|
1747
|
+
async listMonitors(options) {
|
|
1748
|
+
return listMonitors(this.http, options);
|
|
1749
|
+
}
|
|
1750
|
+
/**
|
|
1751
|
+
* Get a monitor by id.
|
|
1752
|
+
*/
|
|
1753
|
+
async getMonitor(monitorId) {
|
|
1754
|
+
return getMonitor(this.http, monitorId);
|
|
1755
|
+
}
|
|
1756
|
+
/**
|
|
1757
|
+
* Update a monitor.
|
|
1758
|
+
*/
|
|
1759
|
+
async updateMonitor(monitorId, request) {
|
|
1760
|
+
return updateMonitor(this.http, monitorId, request);
|
|
1761
|
+
}
|
|
1762
|
+
/**
|
|
1763
|
+
* Delete a monitor.
|
|
1764
|
+
*/
|
|
1765
|
+
async deleteMonitor(monitorId) {
|
|
1766
|
+
return deleteMonitor(this.http, monitorId);
|
|
1767
|
+
}
|
|
1768
|
+
/**
|
|
1769
|
+
* Trigger a manual monitor check.
|
|
1770
|
+
*/
|
|
1771
|
+
async runMonitor(monitorId) {
|
|
1772
|
+
return runMonitor(this.http, monitorId);
|
|
1773
|
+
}
|
|
1774
|
+
/**
|
|
1775
|
+
* List checks for a monitor.
|
|
1776
|
+
*/
|
|
1777
|
+
async listMonitorChecks(monitorId, options) {
|
|
1778
|
+
return listMonitorChecks(this.http, monitorId, options);
|
|
1779
|
+
}
|
|
1780
|
+
/**
|
|
1781
|
+
* Get a monitor check with paginated page results and inline diffs.
|
|
1782
|
+
*/
|
|
1783
|
+
async getMonitorCheck(monitorId, checkId, options) {
|
|
1784
|
+
return getMonitorCheck(this.http, monitorId, checkId, options);
|
|
1785
|
+
}
|
|
1607
1786
|
// Batch
|
|
1608
1787
|
/**
|
|
1609
1788
|
* Start a batch scrape job for multiple URLs (async).
|
|
@@ -1809,7 +1988,7 @@ var FirecrawlApp = class {
|
|
|
1809
1988
|
if (typeof process !== "undefined" && process.env && process.env.npm_package_version) {
|
|
1810
1989
|
return process.env.npm_package_version;
|
|
1811
1990
|
}
|
|
1812
|
-
const packageJson = await import("./package-
|
|
1991
|
+
const packageJson = await import("./package-VALCXP74.js");
|
|
1813
1992
|
return packageJson.default.version;
|
|
1814
1993
|
} catch (error) {
|
|
1815
1994
|
const isTest = typeof process !== "undefined" && (process.env.JEST_WORKER_ID != null || false);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "firecrawl",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.22.0",
|
|
4
4
|
"description": "JavaScript SDK for Firecrawl API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"author": "Mendable.ai",
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"axios": "1.15.
|
|
22
|
+
"axios": "1.15.2",
|
|
23
23
|
"typescript-event-target": "^1.1.1",
|
|
24
24
|
"zod": "^3.23.8",
|
|
25
25
|
"zod-to-json-schema": "^3.23.0"
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, test, expect, jest } from "@jest/globals";
|
|
2
2
|
import { getCrawlStatus } from "../../../v2/methods/crawl";
|
|
3
3
|
import { getBatchScrapeStatus } from "../../../v2/methods/batch";
|
|
4
|
+
import { getMonitorCheck } from "../../../v2/methods/monitor";
|
|
4
5
|
|
|
5
6
|
describe("JS SDK v2 pagination", () => {
|
|
6
7
|
function makeHttp(getImpl: (url: string) => any) {
|
|
@@ -64,6 +65,26 @@ describe("JS SDK v2 pagination", () => {
|
|
|
64
65
|
expect(res.next).toBe("https://api/nextBatch");
|
|
65
66
|
});
|
|
66
67
|
|
|
68
|
+
test("monitor check: default autoPaginate aggregates pages and nulls next", async () => {
|
|
69
|
+
const first = { status: 200, data: { success: true, next: "https://api/m1", data: { id: "check1", monitorId: "mon1", status: "completed", trigger: "manual", billingStatus: "confirmed", summary: {}, createdAt: "now", updatedAt: "now", pages: [{ url: "a", status: "changed" }], next: "https://api/m1" } } };
|
|
70
|
+
const second = { status: 200, data: { success: true, next: null, data: { pages: [{ url: "b", status: "same" }], next: null } } };
|
|
71
|
+
const http = makeHttp((url) => {
|
|
72
|
+
if (url.includes("/v2/monitor/")) return first;
|
|
73
|
+
return second;
|
|
74
|
+
});
|
|
75
|
+
const res = await getMonitorCheck(http, "mon1", "check1");
|
|
76
|
+
expect(res.pages.length).toBe(2);
|
|
77
|
+
expect(res.next).toBeNull();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("monitor check: autoPaginate=false returns next", async () => {
|
|
81
|
+
const first = { status: 200, data: { success: true, next: "https://api/m1", data: { id: "check1", monitorId: "mon1", status: "completed", trigger: "manual", billingStatus: "confirmed", summary: {}, createdAt: "now", updatedAt: "now", pages: [{ url: "a", status: "changed" }], next: "https://api/m1" } } };
|
|
82
|
+
const http = makeHttp(() => first);
|
|
83
|
+
const res = await getMonitorCheck(http, "mon1", "check1", { autoPaginate: false });
|
|
84
|
+
expect(res.pages.length).toBe(1);
|
|
85
|
+
expect(res.next).toBe("https://api/m1");
|
|
86
|
+
});
|
|
87
|
+
|
|
67
88
|
test("crawl: maxWaitTime stops pagination after first page", async () => {
|
|
68
89
|
const first = { status: 200, data: { success: true, status: "completed", completed: 1, total: 5, next: "https://api/n1", data: [{ markdown: "a" }] } };
|
|
69
90
|
const p1 = { status: 200, data: { success: true, next: "https://api/n2", data: [{ markdown: "b" }] } };
|
package/src/v2/client.ts
CHANGED
|
@@ -32,6 +32,16 @@ import {
|
|
|
32
32
|
listBrowsers,
|
|
33
33
|
} from "./methods/browser";
|
|
34
34
|
import { getConcurrency, getCreditUsage, getQueueStatus, getTokenUsage, getCreditUsageHistorical, getTokenUsageHistorical } from "./methods/usage";
|
|
35
|
+
import {
|
|
36
|
+
createMonitor as createMonitorMethod,
|
|
37
|
+
deleteMonitor as deleteMonitorMethod,
|
|
38
|
+
getMonitor as getMonitorMethod,
|
|
39
|
+
getMonitorCheck as getMonitorCheckMethod,
|
|
40
|
+
listMonitorChecks as listMonitorChecksMethod,
|
|
41
|
+
listMonitors as listMonitorsMethod,
|
|
42
|
+
runMonitor as runMonitorMethod,
|
|
43
|
+
updateMonitor as updateMonitorMethod,
|
|
44
|
+
} from "./methods/monitor";
|
|
35
45
|
import type {
|
|
36
46
|
Document,
|
|
37
47
|
ParseFile,
|
|
@@ -60,6 +70,14 @@ import type {
|
|
|
60
70
|
ScrapeExecuteRequest,
|
|
61
71
|
ScrapeExecuteResponse,
|
|
62
72
|
ScrapeBrowserDeleteResponse,
|
|
73
|
+
CreateMonitorRequest,
|
|
74
|
+
ListMonitorChecksOptions,
|
|
75
|
+
ListMonitorsOptions,
|
|
76
|
+
Monitor,
|
|
77
|
+
MonitorCheck,
|
|
78
|
+
MonitorCheckDetail,
|
|
79
|
+
GetMonitorCheckOptions,
|
|
80
|
+
UpdateMonitorRequest,
|
|
63
81
|
} from "./types";
|
|
64
82
|
import { Watcher } from "./watcher";
|
|
65
83
|
import type { WatcherOptions } from "./watcher";
|
|
@@ -276,6 +294,73 @@ export class FirecrawlClient {
|
|
|
276
294
|
return crawlParamsPreview(this.http, url, prompt);
|
|
277
295
|
}
|
|
278
296
|
|
|
297
|
+
// Monitor
|
|
298
|
+
/**
|
|
299
|
+
* Create a scheduled monitor.
|
|
300
|
+
*/
|
|
301
|
+
async createMonitor(request: CreateMonitorRequest): Promise<Monitor> {
|
|
302
|
+
return createMonitorMethod(this.http, request);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* List monitors for the authenticated team.
|
|
307
|
+
*/
|
|
308
|
+
async listMonitors(options?: ListMonitorsOptions): Promise<Monitor[]> {
|
|
309
|
+
return listMonitorsMethod(this.http, options);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Get a monitor by id.
|
|
314
|
+
*/
|
|
315
|
+
async getMonitor(monitorId: string): Promise<Monitor> {
|
|
316
|
+
return getMonitorMethod(this.http, monitorId);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Update a monitor.
|
|
321
|
+
*/
|
|
322
|
+
async updateMonitor(
|
|
323
|
+
monitorId: string,
|
|
324
|
+
request: UpdateMonitorRequest,
|
|
325
|
+
): Promise<Monitor> {
|
|
326
|
+
return updateMonitorMethod(this.http, monitorId, request);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Delete a monitor.
|
|
331
|
+
*/
|
|
332
|
+
async deleteMonitor(monitorId: string): Promise<boolean> {
|
|
333
|
+
return deleteMonitorMethod(this.http, monitorId);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Trigger a manual monitor check.
|
|
338
|
+
*/
|
|
339
|
+
async runMonitor(monitorId: string): Promise<MonitorCheck> {
|
|
340
|
+
return runMonitorMethod(this.http, monitorId);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* List checks for a monitor.
|
|
345
|
+
*/
|
|
346
|
+
async listMonitorChecks(
|
|
347
|
+
monitorId: string,
|
|
348
|
+
options?: ListMonitorChecksOptions,
|
|
349
|
+
): Promise<MonitorCheck[]> {
|
|
350
|
+
return listMonitorChecksMethod(this.http, monitorId, options);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Get a monitor check with paginated page results and inline diffs.
|
|
355
|
+
*/
|
|
356
|
+
async getMonitorCheck(
|
|
357
|
+
monitorId: string,
|
|
358
|
+
checkId: string,
|
|
359
|
+
options?: GetMonitorCheckOptions,
|
|
360
|
+
): Promise<MonitorCheckDetail> {
|
|
361
|
+
return getMonitorCheckMethod(this.http, monitorId, checkId, options);
|
|
362
|
+
}
|
|
363
|
+
|
|
279
364
|
// Batch
|
|
280
365
|
/**
|
|
281
366
|
* Start a batch scrape job for multiple URLs (async).
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CreateMonitorRequest,
|
|
3
|
+
type ListMonitorsOptions,
|
|
4
|
+
type ListMonitorChecksOptions,
|
|
5
|
+
type Monitor,
|
|
6
|
+
type MonitorCheck,
|
|
7
|
+
type MonitorCheckDetail,
|
|
8
|
+
type MonitorCheckPage,
|
|
9
|
+
type GetMonitorCheckOptions,
|
|
10
|
+
type UpdateMonitorRequest,
|
|
11
|
+
} from "../types";
|
|
12
|
+
import { HttpClient } from "../utils/httpClient";
|
|
13
|
+
import {
|
|
14
|
+
throwForBadResponse,
|
|
15
|
+
normalizeAxiosError,
|
|
16
|
+
} from "../utils/errorHandler";
|
|
17
|
+
import { fetchAllPages } from "../utils/pagination";
|
|
18
|
+
|
|
19
|
+
type ApiResponse<T> = {
|
|
20
|
+
success: boolean;
|
|
21
|
+
data?: T;
|
|
22
|
+
id?: string;
|
|
23
|
+
error?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function queryString(params?: Record<string, unknown>): string {
|
|
27
|
+
if (!params) return "";
|
|
28
|
+
const query = new URLSearchParams();
|
|
29
|
+
for (const [key, value] of Object.entries(params)) {
|
|
30
|
+
if (value !== undefined && value !== null) query.set(key, String(value));
|
|
31
|
+
}
|
|
32
|
+
const str = query.toString();
|
|
33
|
+
return str ? `?${str}` : "";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function dataOrThrow<T>(res: { status: number; data?: ApiResponse<T> }, action: string): T {
|
|
37
|
+
if (res.status !== 200 || !res.data?.success || res.data.data == null) {
|
|
38
|
+
throwForBadResponse(res as any, action);
|
|
39
|
+
}
|
|
40
|
+
return res.data.data;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function createMonitor(
|
|
44
|
+
http: HttpClient,
|
|
45
|
+
request: CreateMonitorRequest,
|
|
46
|
+
): Promise<Monitor> {
|
|
47
|
+
try {
|
|
48
|
+
const res = await http.post<ApiResponse<Monitor>>("/v2/monitor", request as any);
|
|
49
|
+
return dataOrThrow(res, "create monitor");
|
|
50
|
+
} catch (err: any) {
|
|
51
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "create monitor");
|
|
52
|
+
throw err;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function listMonitors(
|
|
57
|
+
http: HttpClient,
|
|
58
|
+
options?: ListMonitorsOptions,
|
|
59
|
+
): Promise<Monitor[]> {
|
|
60
|
+
try {
|
|
61
|
+
const res = await http.get<ApiResponse<Monitor[]>>(
|
|
62
|
+
`/v2/monitor${queryString(options as Record<string, unknown>)}`,
|
|
63
|
+
);
|
|
64
|
+
return dataOrThrow(res, "list monitors");
|
|
65
|
+
} catch (err: any) {
|
|
66
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "list monitors");
|
|
67
|
+
throw err;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function getMonitor(
|
|
72
|
+
http: HttpClient,
|
|
73
|
+
monitorId: string,
|
|
74
|
+
): Promise<Monitor> {
|
|
75
|
+
try {
|
|
76
|
+
const res = await http.get<ApiResponse<Monitor>>(`/v2/monitor/${monitorId}`);
|
|
77
|
+
return dataOrThrow(res, "get monitor");
|
|
78
|
+
} catch (err: any) {
|
|
79
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "get monitor");
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function updateMonitor(
|
|
85
|
+
http: HttpClient,
|
|
86
|
+
monitorId: string,
|
|
87
|
+
request: UpdateMonitorRequest,
|
|
88
|
+
): Promise<Monitor> {
|
|
89
|
+
try {
|
|
90
|
+
const res = await http.patch<ApiResponse<Monitor>>(
|
|
91
|
+
`/v2/monitor/${monitorId}`,
|
|
92
|
+
request as any,
|
|
93
|
+
);
|
|
94
|
+
return dataOrThrow(res, "update monitor");
|
|
95
|
+
} catch (err: any) {
|
|
96
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "update monitor");
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function deleteMonitor(
|
|
102
|
+
http: HttpClient,
|
|
103
|
+
monitorId: string,
|
|
104
|
+
): Promise<boolean> {
|
|
105
|
+
try {
|
|
106
|
+
const res = await http.delete<ApiResponse<unknown>>(`/v2/monitor/${monitorId}`);
|
|
107
|
+
if (res.status !== 200 || !res.data?.success) {
|
|
108
|
+
throwForBadResponse(res, "delete monitor");
|
|
109
|
+
}
|
|
110
|
+
return true;
|
|
111
|
+
} catch (err: any) {
|
|
112
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "delete monitor");
|
|
113
|
+
throw err;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export async function runMonitor(
|
|
118
|
+
http: HttpClient,
|
|
119
|
+
monitorId: string,
|
|
120
|
+
): Promise<MonitorCheck> {
|
|
121
|
+
try {
|
|
122
|
+
const res = await http.post<ApiResponse<MonitorCheck>>(
|
|
123
|
+
`/v2/monitor/${monitorId}/run`,
|
|
124
|
+
{},
|
|
125
|
+
);
|
|
126
|
+
return dataOrThrow(res, "run monitor");
|
|
127
|
+
} catch (err: any) {
|
|
128
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "run monitor");
|
|
129
|
+
throw err;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function listMonitorChecks(
|
|
134
|
+
http: HttpClient,
|
|
135
|
+
monitorId: string,
|
|
136
|
+
options?: ListMonitorChecksOptions,
|
|
137
|
+
): Promise<MonitorCheck[]> {
|
|
138
|
+
try {
|
|
139
|
+
const res = await http.get<ApiResponse<MonitorCheck[]>>(
|
|
140
|
+
`/v2/monitor/${monitorId}/checks${queryString(options as Record<string, unknown>)}`,
|
|
141
|
+
);
|
|
142
|
+
return dataOrThrow(res, "list monitor checks");
|
|
143
|
+
} catch (err: any) {
|
|
144
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "list monitor checks");
|
|
145
|
+
throw err;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function getMonitorCheck(
|
|
150
|
+
http: HttpClient,
|
|
151
|
+
monitorId: string,
|
|
152
|
+
checkId: string,
|
|
153
|
+
options?: GetMonitorCheckOptions,
|
|
154
|
+
): Promise<MonitorCheckDetail> {
|
|
155
|
+
try {
|
|
156
|
+
const { autoPaginate: _autoPaginate, maxPages: _maxPages, maxResults: _maxResults, maxWaitTime: _maxWaitTime, ...query } = options ?? {};
|
|
157
|
+
const res = await http.get<ApiResponse<MonitorCheckDetail>>(
|
|
158
|
+
`/v2/monitor/${monitorId}/checks/${checkId}${queryString(query as Record<string, unknown>)}`,
|
|
159
|
+
);
|
|
160
|
+
const detail = dataOrThrow(res, "get monitor check");
|
|
161
|
+
const next = res.data?.next ?? detail.next ?? null;
|
|
162
|
+
const auto = options?.autoPaginate ?? true;
|
|
163
|
+
if (!auto || !next) {
|
|
164
|
+
return { ...detail, next };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
...detail,
|
|
169
|
+
pages: await fetchAllPages<MonitorCheckPage>(
|
|
170
|
+
http,
|
|
171
|
+
next,
|
|
172
|
+
detail.pages || [],
|
|
173
|
+
options,
|
|
174
|
+
),
|
|
175
|
+
next: null,
|
|
176
|
+
};
|
|
177
|
+
} catch (err: any) {
|
|
178
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "get monitor check");
|
|
179
|
+
throw err;
|
|
180
|
+
}
|
|
181
|
+
}
|