-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonblob
More file actions
120 lines (89 loc) · 3.42 KB
/
jsonblob
File metadata and controls
120 lines (89 loc) · 3.42 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
function jsonblob(){
// https://jsonblob.com/api
var obj = {};
var url = 'https://jsonblob.com/api/jsonBlob/';
var id;
var time = 500;
var timer;
var headers = {'content-type':'application/json'};
var util = {};
util.query = {};
obj.util = {};
//:
obj.util.query=async function(){
var tst = window.location.search.slice(1);
if(!tst)return;
var json = await read(tst);
if(!json)return;
id = tst;
return json;
}//query
obj.util.query.set=function(){return util.query.set()}
util.query.set=function(){
var url = window.location.toString();
var search = window.location.search;
if(search){
url = url.slice(0,-search.length);
}
if(id){
url += '?'+id;
}
window.history.replaceState(null,'',url);
}//set
obj.util.save=function(json,set=true){return util.save(json,set=true)}
util.save=async function(json,set=true){
if(id){
await update(id,json);
}else{
await create(json);
}
if(set){
util.query.set();
}
}//save
obj.util.save.debounce=function(fn){
clearTimeout(timer);
var json = fn();
timer = setTimeout(util.save,time,json);
}//debounce
obj.util.delete=async function(set=true){
if(id){
var result = await del(id);
if(result){
id = null;
}
}
if(!set)return;
util.query.set();
}//delete
//:
obj.create=function(json){return create(json)}
async function create(json){
var body = JSON.stringify(json);
var res = await fetch(url,{method:'post',headers,body});
if(!res.ok)return;
var loc = res.headers.get('location');
var i = loc.lastIndexOf('/');
id = loc.slice(i+1);
return id;
}//create
obj.read=function(id){return read(id)}
async function read(id){
var res = await fetch(url+id);
if(!res.ok)return;
var json = await res.json();
return json;
}//read
obj.update=async function(id,json){return update(id,json)}
async function update(id,json){
var body = JSON.stringify(json);
var res = await fetch(url+id,{method:'put',headers,body});
return res.ok;
}//update
obj.delete=function(id){return del(id)}
async function del(id){
var res = await fetch(url+id,{method:'delete'});
return res.ok;
}//delete
return obj;
}//jsonblob