Edits history of script submission #23479 for ' Update Form (hubspot)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_form_id = string
    
    // Dropdown of the account's forms.
    export async function form_id(auth: RT.Hubspot) {
      const response = await fetch(
        "https://api.hubapi.com/marketing/v3/forms?limit=100",
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { results } = (await response.json()) as {
        results: { id: string; name: string }[]
      }
      return results.map((f) => ({ value: f.id, label: f.name }))
    }
    
    /**
     * Update Form
     * Update parts of a marketing form (name, fieldGroups, configuration, displayOptions, legalConsentOptions). Fields you omit keep their value; `fieldGroups` is replaced as a whole. Requires the forms scope.
     */
    export async function main(
      auth: RT.Hubspot,
      form_id: DynSelect_form_id,
      body: { [key: string]: any }
    ) {
      const url = new URL(`https://api.hubapi.com/marketing/v3/forms/${form_id}`)
    
      const response = await fetch(url, {
        method: "PATCH",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(body),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago