This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShareTarget.js
67 lines (62 loc) · 1.83 KB
/
ShareTarget.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
export default class ShareTarget {
/**
* For use with the ShareTarget API.
* Also useful as example of use of this API.
*
* @see https://web.dev/web-share-target/
* @param Object init - such as "share_target" from webapp manifest
* @param FormData formData
*/
constructor({
method = 'GET',
enctype = 'application/x-www-form-urlencoded',
// action = null,
params = {
title: 'title',
text: 'text',
url: 'url',
files: {},
},
} = {}, formData = null) {
this.title = null;
this.text = null;
this.url = null;
this.files = {};
// @TODO check `location.pathname` against `action`
switch(method) {
case 'GET':
((search) => {
this.title = search.get(params.title || 'title');
this.text = search.get(params.text || 'text');
this.url = search.get(params.url || 'url');
})(new URL(location.href).searchParams);
break;
case 'POST':
if (! (formData instanceof FormData)) {
throw new Error('For POST shares, pass in a `FormData` object after config');
} else {
this.title = formData.get(params.title || 'title');
this.text = formData.get(params.text || 'text');
this.url = formData.get(params.url || 'url');
// Files only supported with enctype of 'multipart/form-data'
if (enctype === 'multipart/form-data') {
this.files = Object.fromEntries(params.files.map(({name/*, accept = []*/}) => {
if (formData.has(name)) {
const file = formData.get(name);
// @TODO check `accept` against `file.type`
if (! (file instanceof File)) {
throw new Error(`POST ${name} expects a file`);
} else {
return [name, file];
}
} else {
return [name, null];
}
}));
}
}
break;
default: throw new Error(`Unsupported method: ${method}`);
}
}
}