-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
93 lines (80 loc) · 2.65 KB
/
index.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
const needle = require('needle');
function FrappeRequest(url, usr, pwd) {
// '''Create a FrappeRequest object'''
this.url = url;
// Login
let data = {
'cmd': 'login',
'usr': usr,
'pwd': pwd
};
return needle('post', url, data, { json: true })
.then(res => res.cookies)
.then(res => {
this.cookies = res;
return this;
})
.catch(err => console.error(err));
}
FrappeRequest.prototype.get_list = function(doctype) {
return needle('get', `${this.url}/api/resource/${doctype}`, { cookies: this.cookies })
.then(res => res.body)
.catch(err => console.error(err));
}
FrappeRequest.prototype.get_doc = function(doctype, name) {
return needle('get', `${this.url}/api/resource/${doctype}/${name}`, { cookies: this.cookies })
.then(res => res.body)
.catch(err => console.error(err));
}
FrappeRequest.prototype.get_value = function(doctype, fieldname, filters) {
let data = {
'cmd': 'frappe.client.get_value',
'doctype': doctype,
'fieldname': fieldname,
'filters': JSON.stringify(filters)
};
return needle('post', `${this.url}`, data, { cookies: this.cookies })
.then(res => res.body)
.catch(err => console.error(err));
}
FrappeRequest.prototype.get_api = function(method) {
return needle('get', `${this.url}/api/method/${method}/`, { cookies: this.cookies })
.then(res => res.body)
.catch(err => console.error(err));
}
FrappeRequest.prototype.post_api = function(method, data) {
return needle('post', `${this.url}/api/method/${method}/`, data, { cookies: this.cookies, json: true })
.then(res => res.body)
.catch(err => console.error(err));
}
FrappeRequest.prototype.set_value = function(doctype, docname, fieldname, value) {
let data = {
'cmd': 'frappe.client.set_value',
'doctype': doctype,
'name': docname,
'fieldname': fieldname,
'value': value
};
return needle('post', `${this.url}`, data, { cookies: this.cookies })
.then(res => res.body)
.catch(err => console.error(err));
}
FrappeRequest.prototype.insert = function(doc) {
return needle('post', `${this.url}/api/resource/${doc.doctype}`, { data: JSON.stringify(doc) }, { cookies: this.cookies })
.then(res => res.body)
.catch(err => console.error(err));
}
FrappeRequest.prototype.delete = function(doctype, name) {
let data = {
'cmd': 'frappe.client.delete',
'doctype': doctype,
'name': name
};
return needle('post', `${this.url}`, data, { cookies: this.cookies })
.then(res => res.body)
.catch(err => console.error(err));
}
// ''' Module Exports '''
module.exports.FrappeRequest = async function(url, usr, pwd) {
return await new FrappeRequest(url, usr, pwd);
}