-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotinorest.js
93 lines (74 loc) · 2.48 KB
/
photinorest.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
93
class PhotinoRest {
static DEBUG = true;
static SENDMESSAGE;
static RECEIVEMESSAGE;
static QUEUE = {};
static VERSION = "0.0.1";
static METHODS = {
GET: 0,
POST: 1,
PUT: 2,
DELETE: 3
};
static log(msg) {
console.log(`[PhotinoRest] ${msg}`);
}
static dbg(msg) {
if(PhotinoRest.DEBUG) PhotinoRest.log(msg);
}
static setup() {
PhotinoRest.RECEIVEMESSAGE(PhotinoRest.receiveMessage);
}
static receiveMessage(message) {
console.log(`Message Recieve`);
const parse = JSON.parse(message);
const { time } = parse.headers;
if(PhotinoRest.QUEUE[time] === undefined) return;
PhotinoRest.QUEUE[time].resolve(parse);
delete PhotinoRest.QUEUE[time];
}
static prepare(path, params, method, resolve, reject) {
const time = Date.now().toString();
this.QUEUE[time] = { resolve, reject };
return JSON.stringify({
headers: {
time,
version: PhotinoRest.VERSION,
method
},
path,
body: params
});
}
static parse(res) {
return JSON.parse(res);
}
static async GET(path, params) {
PhotinoRest.dbg(`GET: ${path} | ${params}`);
return new Promise((resolve, reject) => {
PhotinoRest.SENDMESSAGE(PhotinoRest.prepare(path, params, PhotinoRest.METHODS.GET, resolve, reject));
});
}
static async POST(path, params) {
PhotinoRest.dbg(`POST: ${path} | ${params}`);
return new Promise((resolve, reject) => {
PhotinoRest.SENDMESSAGE(PhotinoRest.prepare(path, params, PhotinoRest.METHODS.POST, resolve, reject));
});
}
static async PUT(path, params) {
PhotinoRest.dbg(`PUT: ${path} | ${params}`);
return new Promise((resolve, reject) => {
PhotinoRest.SENDMESSAGE(PhotinoRest.prepare(path, params, PhotinoRest.METHODS.PUT, resolve, reject));
});
}
static async DELETE(path, params) {
PhotinoRest.dbg(`DELETE: ${path} | ${params}`);
return new Promise((resolve, reject) => {
PhotinoRest.SENDMESSAGE(PhotinoRest.prepare(path, params, PhotinoRest.METHODS.DELETE, resolve, reject));
});
}
}
PhotinoRest.SENDMESSAGE = window.external.sendMessage;
PhotinoRest.RECEIVEMESSAGE = window.external.receiveMessage;
PhotinoRest.setup();
window.PR = window.PhotinoRest = PhotinoRest;