Edits history of script submission #23095 for ' Create DNS Record (cloudflare)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_zone_id = string
    
    // Dropdown of the account's zones so users pick a domain instead of pasting its 32-char id.
    // ponytail: first 50 zones only (API max per_page); paste the id directly beyond that.
    export async function zone_id(auth: RT.Cloudflare) {
      const response = await fetch(
        "https://api.cloudflare.com/client/v4/zones?per_page=50",
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { result } = (await response.json()) as {
        result: { id: string; name: string }[]
      }
      return result.map((z) => ({ value: z.id, label: z.name }))
    }
    
    /**
     * Create DNS Record
     * Create a DNS record in a zone. ttl 1 means automatic; proxied only applies to A, AAAA and CNAME records.
     */
    export async function main(
      auth: RT.Cloudflare,
      zone_id: DynSelect_zone_id,
      type:
        | "A"
        | "AAAA"
        | "CNAME"
        | "MX"
        | "TXT"
        | "NS"
        | "SRV"
        | "CAA"
        | "PTR"
        | "HTTPS"
        | "SVCB"
        | "LOC"
        | "NAPTR"
        | "CERT"
        | "DNSKEY"
        | "DS"
        | "SMIMEA"
        | "SSHFP"
        | "TLSA"
        | "URI"
        | "OPENPGPKEY",
      name: string,
      content: string,
      ttl: number | undefined,
      proxied: boolean | undefined,
      priority: number | undefined,
      comment: string | undefined
    ) {
      const response = await fetch(
        `https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records`,
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({
            type,
            name,
            content,
            ttl: ttl ?? 1,
            proxied: proxied ?? undefined,
            priority: priority ?? undefined,
            comment: comment ?? undefined,
          }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 4 hours ago