HubSpot / hubspot-api-nodejs

HubSpot API NodeJS Client Libraries for V3 version of the API
Apache License 2.0
311 stars 101 forks source link

Type: DependentFieldDependentField #488

Open luca-smartpricing opened 3 months ago

luca-smartpricing commented 3 months ago

In FieldGroup.ts the fields key is fields': Array<DependentFieldDependentField>. Ok, but if i open DependentFieldDependentField.ts i can found only one fieldType fieldType: DependentFieldDependentFieldFieldTypeEnum As you see fieldType has only one type: payment_link_radio.

declare enum DependentFieldDependentFieldFieldTypeEnum {
    PaymentLinkRadio = "payment_link_radio"
}

Where are the other FieldTypeEnum such as

I think it's a typing error

Screenshot 2024-06-03 alle 17 55 37
BojanHribernik commented 2 weeks ago

I have the same problem. This is how I solved my use-case:

import {
  FieldGroup as BaseFieldGroup,
  FormDefinitionBaseFormTypeEnum,
  FormDisplayOptions,
  HubSpotFormConfiguration,
} from '@hubspot/api-client/lib/codegen/marketing/forms'

export enum FieldType {
  SingleLineText = 'single_line_text',
  Email = 'email',
  Phone = 'phone',
  Number = 'number',
  MultiLineText = 'multi_line_text',
  Dropdown = 'dropdown',
  SingleCheckbox = 'single_checkbox',
  Radio = 'radio',
  MultipleCheckboxes = 'multiple_checkboxes',
}

export type Field = Omit<BaseFieldGroup['fields'][0], 'fieldType'> & {
  fieldType: FieldType
}

export type FieldGroup = Omit<BaseFieldGroup, 'fields'> & { fields: Field[] }

export type FormDefinition = {
  formType: FormDefinitionBaseFormTypeEnum
  id: string
  name: string
  createdAt: Date
  updatedAt: Date
  archived: boolean
  archivedAt?: Date
  fieldGroups: Array<FieldGroup>
  configuration: HubSpotFormConfiguration
  displayOptions: FormDisplayOptions
  legalConsentOptions: any
  [key: string]: any // <<<<< fix for Remix unstable_defineLoader types
}

export async function getForm(formId: string) {
  try {
    const res = await fetch(`https://api.hubapi.com/marketing/v3/forms/${formId}`, {
      cache: 'no-cache',
      headers: {
        Authorization: `Bearer ${HUBSPOT_ACCESS_TOKEN}`,
      },
    })
    if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`)
    const data = (await res.json()) as FormDefinition
    console.log('getForm', JSON.stringify(data, null, 2))
    return data
  } catch (error) {
    if (error instanceof Error) {
      console.error(error)
      throw new Error(error.message)
    } else {
      console.error(error)
      throw new Error('An unknown error occurred.')
    }
  }
}