0

List saved searches

by
Published today

List one page of saved searches (reports and alerts) visible to the token. Owner and app default to "-" (all); each entry's acl.owner and acl.app are what Dispatch saved search needs. Page with count (default 30) and offset; next_offset is null on the last page.

Script splunk Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 hours ago
1
//native
2

3
/**
4
 * List saved searches
5
 * List one page of saved searches (reports and alerts) visible to the token. Owner and app default to "-" (all); each entry's acl.owner and acl.app are what Dispatch saved search needs. Page with count (default 30) and offset; next_offset is null on the last page.
6
 */
7
export async function main(
8
  auth: RT.Splunk,
9
  owner: string | undefined,
10
  app: string | undefined,
11
  search: string | undefined,
12
  count: number | undefined,
13
  offset: number | undefined
14
) {
15
  const base = auth.base_url.replace(/\/+$/, "")
16
  const pageSize = count || 30
17
  const start = offset ?? 0
18
  const url = new URL(
19
    `${base}/servicesNS/${encodeURIComponent(owner || "-")}/${encodeURIComponent(app || "-")}/saved/searches`
20
  )
21
  url.searchParams.append("output_mode", "json")
22
  url.searchParams.append("count", String(pageSize))
23
  url.searchParams.append("offset", String(start))
24
  if (search) url.searchParams.append("search", search)
25

26
  const response = await fetch(url, {
27
    headers: {
28
      Authorization: `Bearer ${auth.token}`,
29
      Accept: "application/json",
30
    },
31
  })
32

33
  if (!response.ok) {
34
    throw new Error(`${response.status} ${await response.text()}`)
35
  }
36

37
  const data = (await response.json()) as {
38
    entry?: unknown[]
39
    paging?: { total?: number }
40
  }
41
  const returned = data.entry?.length ?? 0
42
  const total = data.paging?.total
43
  // Stop on an empty page (Splunk SDK paginator rule), or earlier when the
44
  // feed's paging.total says this page reached the end.
45
  const done =
46
    returned === 0 || (typeof total === "number" && start + returned >= total)
47
  return { ...data, next_offset: done ? null : start + returned }
48
}
49