Edits history of script submission #23323 for ' Delete Label (gmail)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_label_id = string
    
    // Dropdown of the mailbox's user labels (system labels can't be deleted).
    export async function label_id(auth: RT.Gmail) {
      const response = await fetch(
        "https://gmail.googleapis.com/gmail/v1/users/me/labels",
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { labels } = (await response.json()) as {
        labels: { id: string; name: string; type: string }[]
      }
      return labels
        .filter((l) => l.type === "user")
        .map((l) => ({ value: l.id, label: `${l.name} (${l.id})` }))
    }
    
    /**
     * Delete Label
     * Permanently delete a user label and remove it from every message and thread it is applied to. The messages themselves are kept. Requires gmail.labels or gmail.modify on the connection.
     */
    export async function main(auth: RT.Gmail, label_id: DynSelect_label_id) {
      const response = await fetch(
        `https://gmail.googleapis.com/gmail/v1/users/me/labels/${label_id}`,
        {
          method: "DELETE",
          headers: { Authorization: `Bearer ${auth.token}` },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return { success: true }
    }
    

    Submitted by hugo989 6 hours ago