-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathkintonePUTRequest.js
47 lines (44 loc) · 1.64 KB
/
kintonePUTRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// kintonePUTRequest.js
// A function to upload the image file to Kintone. This returns a file key.
// The file key then needs to be uploaded to the record, to associate the file with the record.
// In short, it takes two steps to upload attachments.
const uploadFile = async (blob, name) => {
// The Kintone attachment API specifies we need "multipart data."
// One way of designating multipart data is by sending as formdata.
const formData = new FormData();
formData.append('__REQUEST_TOKEN__', kintone.getRequestToken());
formData.append('file', blob, name);
// We await the attachment API result from Kintone.
const fileKey = await fetch(kintone.api.url('/k/v1/file.json', true), {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
body: formData
}).then(res => res.json()).then(({ fileKey }) => fileKey);
// And return it from this function.
return fileKey;
};
// A function to update our Kintone record all in one go. We'll be calling the uploadFile function above in here.
export default async function updateKintone(recordID, image, dateTime) {
// Get our fileKey using the uploadFile function above...
const fileKey = await uploadFile(image, 'image.png');
// Put the fileKey in the putBody.
const putBody = {
app: import.meta.env.VITE_KINTONE_APPID,
id: recordID,
record: {
result: {
value: [{
fileKey
}]
},
dateTime: {
value: dateTime
}
}
};
const response = await kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', putBody);
// Await the response from the update record API, and then return it.
return response;
}