Edits history of script submission #9065 for ' Create a New Form (jotform)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Jotform = {
    	apiKey: string
    	baseUrl: string
    }
    
    export async function main(
    	resource: Jotform,
    	formData: {
    		questions: Array<{
    			type: string
    			text: string
    			order: number
    			name: string
    		}>
    		properties: {
    			title: string
    			height?: number
    		}
    	}
    ) {
    	const queryParams = new URLSearchParams({ apiKey: resource.apiKey })
    
    	const endpoint = `${resource.baseUrl}/form?${queryParams.toString()}`
    
    	// Construct the URLSearchParams body
    	const bodyParams = new URLSearchParams()
    
    	// Add questions to bodyParams
    	formData.questions.forEach((question, index) => {
    		bodyParams.append(`questions[${index}][type]`, question.type)
    		bodyParams.append(`questions[${index}][text]`, question.text)
    		bodyParams.append(`questions[${index}][order]`, question.order.toString())
    		bodyParams.append(`questions[${index}][name]`, question.name)
    	})
    
    	// Add properties to bodyParams
    	bodyParams.append('properties[title]', formData.properties.title)
    
    	if (formData.properties.height) {
    		bodyParams.append('properties[height]', formData.properties.height.toString())
    	}
    
    	// Send the request with the URL-encoded body
    	const response = await fetch(endpoint, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/x-www-form-urlencoded'
    		},
    		body: bodyParams.toString()
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    
    	return data
    }
    

    Submitted by hugo697 621 days ago