-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
92 lines (83 loc) · 3.44 KB
/
test.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as securelay from './script.js'; // 'https://securelay.github.io/api/script.js';
import securelayUrl from './script.js'; // 'https://cdn.jsdelivr.net/gh/securelay/api@main/script.min.js';
console.log('Version =', securelay.version);
const [endpoint, ID] = securelayUrl();
console.log('endpoint = ' + endpoint);
console.log('endpoint_ID = ' + ID);
console.log('OneSignal App ID for formonit =', await securelay.appId(ID, 'formonit'));
const key = await securelay.key(ID);
console.log('private key = ' + key);
console.log('public URL for the above key = ' + await securelay.publicUrl(key, ID));
console.log('private URL for the above key = ' + await securelay.privateUrl(key, ID));
console.log('Lets take a sample key and a sample ID from the Securelay API documentation');
const sampleKey = 'A3zTryeMxkq';
const sampleID = 'alz2h';
console.log('Sample private key = ' + sampleKey);
console.log('Sample endpoint ID = ' + sampleID);
const stdArgs = [sampleKey, sampleID];
const samplePublicUrl = await securelay.publicUrl(...stdArgs);
console.log('public URL for the sample = ' + samplePublicUrl);
console.log('Trying publishing...');
console.log('Testing pubJson() with field = "json":');
try {
await securelay.pubJson(...stdArgs, { hello: 'world' }, { field: 'json' });
console.log('JSON publication successful. Retrieval link: ' + samplePublicUrl + '/json');
} catch (err) {
console.error('Error during publishing JSON\n' + err.message);
}
console.log('Testing pubForm() with field = "form":');
const formData = new FormData();
formData.append('hello', 'world');
try {
await securelay.pubForm(...stdArgs, formData, { field: 'form' });
console.log('Form publication successful. Retrieval link: ' + samplePublicUrl + '/form');
} catch (err) {
console.error('Error during publishing form\n' + err.message);
}
console.log('Testing pubText():');
try {
await securelay.pubText(...stdArgs, 'hello world');
console.log('Text publication successful. Retrieval link: ' + samplePublicUrl);
} catch (err) {
console.error('Error during publishing text\n' + err.message);
}
console.log('Renewing published content returns:');
try {
console.log(JSON.stringify(await securelay.renew(sampleKey, sampleID)));
} catch (err) {
console.error('Error during publishing text\n' + err.message);
}
console.log('Deleting published content...');
try {
await securelay.unpublish(...stdArgs);
} catch (err) {
console.error('Error during publishing text\n' + err.message);
}
console.log('Publishing, retrieving and deleting a secret ...');
try {
const data = 'classified';
await securelay.pubText(...stdArgs, data, { password: 'someSecret' });
console.log('Published:', data);
console.log('Retrieved:', await fetch(samplePublicUrl + '?password=someSecret')
.then((response) => {
if (!response.ok) throw new Error(response.status);
return response.json();
})
.then((json) => json.data)
);
console.log('Deleting the data ...');
await securelay.unpublish(...stdArgs, { secret: true });
} catch (err) {
console.error('Error with password protected publication text\n' + err.message);
}
console.log('Trying syncing after a public post. Should get [{"hello":"world"}]');
try {
await fetch(samplePublicUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ hello: 'world' })
});
console.log(JSON.stringify(await securelay.sync(...stdArgs)));
} catch (err) {
console.error('Error during syncing\n' + err.message);
}