Edits history of script submission #9045 for ' Update Field (datocms)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Datocms = {
    	apiKey: string
    }
    
    export async function main(
    	resource: Datocms,
    	fieldId: string,
    	fieldData: {
    		label?: 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/fields/${fieldId}`
    
    	// Construct the request body
    	const body = {
    		data: {
    			type: 'field',
    			id: fieldId,
    			attributes: fieldData
    		}
    	}
    
    	const response = await fetch(endpoint, {
    		method: 'PUT',
    		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