Edits history of script submission #23814 for ' List Dynamic Content Items (zendesk)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Dynamic Content Items
     * List the account's dynamic content items (reusable, localized text referenced as `{{dc.name}}` in macros, triggers and articles). Cursor-paged: pass `meta.after_cursor` as `cursor` while `meta.has_more` is true.
     */
    export async function main(
      auth: RT.Zendesk,
      sort:
        | "created_at"
        | "-created_at"
        | "updated_at"
        | "-updated_at"
        | "locale"
        | "-locale"
        | "active"
        | "-active"
        | "outdated"
        | "-outdated"
        | undefined,
      page_size: number | undefined,
      cursor: string | undefined
    ) {
      const url = new URL(
        `https://${auth.subdomain}.zendesk.com/api/v2/dynamic_content/items`
      )
      url.searchParams.append("page[size]", String(page_size || 100))
      if (sort) {
        url.searchParams.append("sort", sort)
      }
      if (cursor) {
        url.searchParams.append("page[after]", cursor)
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: auth.token
            ? `Bearer ${auth.token}`
            : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago