type Datocms = {
apiKey: string
}
export async function main(
resource: Datocms,
modelId: string,
fieldData: {
label: string
field_type: string
api_key: string
localized?: boolean
hint?: string
validators?: Record<string, any>
appearance?: Record<string, any>
default_value?: any
required?: boolean
}
) {
const endpoint = `https://site-api.datocms.com/item-types/${modelId}/fields`
// Construct the request body
const body = {
data: {
type: 'field',
attributes: fieldData
}
}
const response = await fetch(endpoint, {
method: 'POST',
headers: {
Authorization: `Bearer ${resource.apiKey}`,
Accept: 'application/json',
'X-Api-Version': '3',
'Content-Type': 'application/vnd.api+json'
},
body: JSON.stringify(body)
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()
return data
}
Submitted by hugo697 621 days ago