1 | |
2 | type Miro = { |
3 | token: string; |
4 | }; |
5 | type Base64 = string; |
6 | |
7 | * Update image item using file from device |
8 | * Updates an image item on a board.Required scope boards:write Rate limiting Level 2 |
9 | */ |
10 | export async function main( |
11 | auth: Miro, |
12 | board_id_PlatformFileUpload: string, |
13 | item_id: string, |
14 | body: { |
15 | data?: { |
16 | title?: string; |
17 | altText?: string; |
18 | position?: { x?: number; y?: number }; |
19 | geometry?: { height?: number; width?: number; rotation?: number }; |
20 | parent?: { id?: string }; |
21 | }; |
22 | resource: { |
23 | base64: Base64; |
24 | type: |
25 | | "image/png" |
26 | | "image/jpeg" |
27 | | "image/gif" |
28 | | "application/pdf" |
29 | | "appication/json" |
30 | | "text/csv" |
31 | | "text/plain" |
32 | | "audio/mpeg" |
33 | | "audio/wav" |
34 | | "video/mp4"; |
35 | name: string; |
36 | }; |
37 | }, |
38 | ) { |
39 | const url = new URL( |
40 | `https://api.miro.com//v2/boards/${board_id_PlatformFileUpload}/images/${item_id}`, |
41 | ); |
42 |
|
43 | const formData = new FormData(); |
44 | for (const [k, v] of Object.entries(body)) { |
45 | if (v !== undefined) { |
46 | if (["resource"].includes(k)) { |
47 | const { base64, type, name } = v as { |
48 | base64: Base64; |
49 | type: string; |
50 | name: string; |
51 | }; |
52 | formData.append( |
53 | k, |
54 | new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], { |
55 | type, |
56 | }), |
57 | name, |
58 | ); |
59 | } else { |
60 | formData.append(k, String(v)); |
61 | } |
62 | } |
63 | } |
64 | const response = await fetch(url, { |
65 | method: "PATCH", |
66 | headers: { |
67 | Authorization: "Bearer " + auth.token, |
68 | }, |
69 | body: formData, |
70 | }); |
71 | if (!response.ok) { |
72 | const text = await response.text(); |
73 | throw new Error(`${response.status} ${text}`); |
74 | } |
75 | return await response.json(); |
76 | } |
77 |
|