Edits history of script submission #23348 for ' Find Emails (ms_outlook)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_folder = string
    
    export async function folder(auth: RT.MsOutlook, mailbox: string | undefined) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const r = await fetch(`${base}/mailFolders?$top=100&$select=id,displayName`, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!r.ok) throw new Error(`${r.status} ${await r.text()}`)
      const { value } = (await r.json()) as {
        value: { id: string; displayName: string }[]
      }
      return value.map((f) => ({ value: f.id, label: f.displayName }))
    }
    
    /**
     * Find Emails
     * List or search messages, newest first by default, in the whole mailbox or one folder. `search` takes Outlook search syntax (e.g. `from:alice@example.com hasAttachments:true`) and cannot be combined with `filter` or `order_by`; `filter` takes OData (e.g. `isRead eq false`, `importance eq 'high'`, `receivedDateTime ge 2026-01-01T00:00:00Z`). Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Mail.Read (covered by the default Mail.ReadWrite) (a shared mailbox needs Mail.Read.Shared, not a default).
     */
    export async function main(
      auth: RT.MsOutlook,
      folder: DynSelect_folder | undefined,
      search: string | undefined,
      filter: string | undefined,
      order_by: string | undefined,
      select: string | undefined,
      top: number = 25,
      next_link: string | undefined,
      body_format: "html" | "text" = "html",
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      if (
        next_link != null &&
        next_link !== "" &&
        !next_link.startsWith("https://graph.microsoft.com/")
      ) {
        throw new Error(
          "next_link must be an @odata.nextLink returned by Microsoft Graph"
        )
      }
      let url: URL
      if (next_link != null && next_link !== "") {
        url = new URL(next_link)
      } else {
        url = new URL(
          folder != null && folder !== ""
            ? `${base}/mailFolders/${encodeURIComponent(folder)}/messages`
            : `${base}/messages`
        )
        url.searchParams.append("$top", String(top))
        if (search != null && search !== "") {
          url.searchParams.append("$search", `"${search.replace(/"/g, '\\"')}"`)
        } else if (
          (filter == null || filter === "") &&
          (order_by == null || order_by === "")
        ) {
          url.searchParams.append("$orderby", "receivedDateTime desc")
        }
        if (filter != null && filter !== "") {
          url.searchParams.append("$filter", filter)
        }
        if (order_by != null && order_by !== "") {
          url.searchParams.append("$orderby", order_by)
        }
        if (select != null && select !== "") {
          url.searchParams.append("$select", select)
        }
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
          Prefer: `outlook.body-content-type="${body_format}"`,
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago

  • bunnative
    //native
    
    export type DynSelect_folder = string
    
    export async function folder(auth: RT.Outlook, mailbox: string | undefined) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const r = await fetch(`${base}/mailFolders?$top=100&$select=id,displayName`, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!r.ok) throw new Error(`${r.status} ${await r.text()}`)
      const { value } = (await r.json()) as {
        value: { id: string; displayName: string }[]
      }
      return value.map((f) => ({ value: f.id, label: f.displayName }))
    }
    
    /**
     * Find Emails
     * List or search messages, newest first by default, in the whole mailbox or one folder. `search` takes Outlook search syntax (e.g. `from:alice@example.com hasAttachments:true`) and cannot be combined with `filter` or `order_by`; `filter` takes OData (e.g. `isRead eq false`, `importance eq 'high'`, `receivedDateTime ge 2026-01-01T00:00:00Z`). Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Mail.Read (covered by the default Mail.ReadWrite) (a shared mailbox needs Mail.Read.Shared, not a default).
     */
    export async function main(
      auth: RT.Outlook,
      folder: DynSelect_folder | undefined,
      search: string | undefined,
      filter: string | undefined,
      order_by: string | undefined,
      select: string | undefined,
      top: number = 25,
      next_link: string | undefined,
      body_format: "html" | "text" = "html",
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      if (
        next_link != null &&
        next_link !== "" &&
        !next_link.startsWith("https://graph.microsoft.com/")
      ) {
        throw new Error(
          "next_link must be an @odata.nextLink returned by Microsoft Graph"
        )
      }
      let url: URL
      if (next_link != null && next_link !== "") {
        url = new URL(next_link)
      } else {
        url = new URL(
          folder != null && folder !== ""
            ? `${base}/mailFolders/${encodeURIComponent(folder)}/messages`
            : `${base}/messages`
        )
        url.searchParams.append("$top", String(top))
        if (search != null && search !== "") {
          url.searchParams.append("$search", `"${search.replace(/"/g, '\\"')}"`)
        } else if (
          (filter == null || filter === "") &&
          (order_by == null || order_by === "")
        ) {
          url.searchParams.append("$orderby", "receivedDateTime desc")
        }
        if (filter != null && filter !== "") {
          url.searchParams.append("$filter", filter)
        }
        if (order_by != null && order_by !== "") {
          url.searchParams.append("$orderby", order_by)
        }
        if (select != null && select !== "") {
          url.searchParams.append("$select", select)
        }
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
          Prefer: `outlook.body-content-type="${body_format}"`,
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 11 hours ago

  • bunnative
    //native
    
    export type DynSelect_folder = string
    
    export async function folder(auth: RT.Outlook, mailbox: string | undefined) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const r = await fetch(`${base}/mailFolders?$top=100&$select=id,displayName`, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!r.ok) throw new Error(`${r.status} ${await r.text()}`)
      const { value } = (await r.json()) as {
        value: { id: string; displayName: string }[]
      }
      return value.map((f) => ({ value: f.id, label: f.displayName }))
    }
    
    /**
     * Find Emails
     * List or search messages, newest first by default, in the whole mailbox or one folder. `search` takes Outlook search syntax (e.g. `from:alice@example.com hasAttachments:true`) and cannot be combined with `filter` or `order_by`; `filter` takes OData (e.g. `isRead eq false`, `importance eq 'high'`, `receivedDateTime ge 2026-01-01T00:00:00Z`). Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Mail.Read on the connection (Mail.Read.Shared for a shared mailbox).
     */
    export async function main(
      auth: RT.Outlook,
      folder: DynSelect_folder | undefined,
      search: string | undefined,
      filter: string | undefined,
      order_by: string | undefined,
      select: string | undefined,
      top: number = 25,
      next_link: string | undefined,
      body_format: "html" | "text" = "html",
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      if (
        next_link != null &&
        next_link !== "" &&
        !next_link.startsWith("https://graph.microsoft.com/")
      ) {
        throw new Error(
          "next_link must be an @odata.nextLink returned by Microsoft Graph"
        )
      }
      let url: URL
      if (next_link != null && next_link !== "") {
        url = new URL(next_link)
      } else {
        url = new URL(
          folder != null && folder !== ""
            ? `${base}/mailFolders/${encodeURIComponent(folder)}/messages`
            : `${base}/messages`
        )
        url.searchParams.append("$top", String(top))
        if (search != null && search !== "") {
          url.searchParams.append("$search", `"${search.replace(/"/g, '\\"')}"`)
        } else if (
          (filter == null || filter === "") &&
          (order_by == null || order_by === "")
        ) {
          url.searchParams.append("$orderby", "receivedDateTime desc")
        }
        if (filter != null && filter !== "") {
          url.searchParams.append("$filter", filter)
        }
        if (order_by != null && order_by !== "") {
          url.searchParams.append("$orderby", order_by)
        }
        if (select != null && select !== "") {
          url.searchParams.append("$select", select)
        }
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
          Prefer: `outlook.body-content-type="${body_format}"`,
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 11 hours ago