1 | type Jotform = { |
2 | apiKey: string |
3 | baseUrl: string |
4 | } |
5 |
|
6 | export async function main( |
7 | resource: Jotform, |
8 | formData: { |
9 | questions: Array<{ |
10 | type: string |
11 | text: string |
12 | order: number |
13 | name: string |
14 | }> |
15 | properties: { |
16 | title: string |
17 | height?: number |
18 | } |
19 | } |
20 | ) { |
21 | const queryParams = new URLSearchParams({ apiKey: resource.apiKey }) |
22 |
|
23 | const endpoint = `${resource.baseUrl}/form?${queryParams.toString()}` |
24 |
|
25 | |
26 | const bodyParams = new URLSearchParams() |
27 |
|
28 | |
29 | formData.questions.forEach((question, index) => { |
30 | bodyParams.append(`questions[${index}][type]`, question.type) |
31 | bodyParams.append(`questions[${index}][text]`, question.text) |
32 | bodyParams.append(`questions[${index}][order]`, question.order.toString()) |
33 | bodyParams.append(`questions[${index}][name]`, question.name) |
34 | }) |
35 |
|
36 | |
37 | bodyParams.append('properties[title]', formData.properties.title) |
38 |
|
39 | if (formData.properties.height) { |
40 | bodyParams.append('properties[height]', formData.properties.height.toString()) |
41 | } |
42 |
|
43 | |
44 | const response = await fetch(endpoint, { |
45 | method: 'POST', |
46 | headers: { |
47 | 'Content-Type': 'application/x-www-form-urlencoded' |
48 | }, |
49 | body: bodyParams.toString() |
50 | }) |
51 |
|
52 | if (!response.ok) { |
53 | throw new Error(`HTTP error! status: ${response.status}`) |
54 | } |
55 |
|
56 | const data = await response.json() |
57 |
|
58 | return data |
59 | } |
60 |
|