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