2018-01-16 06:09:17 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-01-12 12:25:07 +00:00
|
|
|
const path = require('path');
|
|
|
|
|
2018-01-15 11:55:31 +00:00
|
|
|
module.exports = class RESTClient {
|
2018-02-08 09:38:47 +00:00
|
|
|
constructor({ server, protocol = 'http' }) {
|
2018-01-12 12:25:07 +00:00
|
|
|
this.server = server;
|
|
|
|
this.protocol = protocol;
|
|
|
|
|
2018-02-08 06:46:38 +00:00
|
|
|
this.initTypeMap();
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async insert(doctype, doc) {
|
|
|
|
doc.doctype = doctype;
|
2018-02-08 09:38:47 +00:00
|
|
|
let url = this.getURL('/api/resource', frappe.slug(doctype));
|
|
|
|
return await this.fetch(url, {
|
2018-01-12 12:25:07 +00:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(doc)
|
2018-02-08 09:38:47 +00:00
|
|
|
})
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async get(doctype, name) {
|
2018-02-08 09:38:47 +00:00
|
|
|
let url = this.getURL('/api/resource', frappe.slug(doctype), name);
|
|
|
|
return await this.fetch(url, {
|
2018-01-12 12:25:07 +00:00
|
|
|
method: 'GET',
|
2018-02-08 09:38:47 +00:00
|
|
|
headers: this.getHeaders()
|
|
|
|
})
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
async getAll({ doctype, fields, filters, start, limit, sort_by, order }) {
|
|
|
|
let url = this.getURL('/api/resource', frappe.slug(doctype));
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
url = url + "?" + this.getQueryString({
|
2018-01-12 12:25:07 +00:00
|
|
|
fields: JSON.stringify(fields),
|
|
|
|
filters: JSON.stringify(filters),
|
|
|
|
start: start,
|
|
|
|
limit: limit,
|
|
|
|
sort_by: sort_by,
|
|
|
|
order: order
|
|
|
|
});
|
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
return await this.fetch(url, {
|
2018-01-12 12:25:07 +00:00
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async update(doctype, doc) {
|
|
|
|
doc.doctype = doctype;
|
2018-02-08 09:38:47 +00:00
|
|
|
let url = this.getURL('/api/resource', frappe.slug(doctype), doc.name);
|
|
|
|
|
|
|
|
return await this.fetch(url, {
|
2018-01-12 12:25:07 +00:00
|
|
|
method: 'PUT',
|
|
|
|
body: JSON.stringify(doc)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(doctype, name) {
|
2018-02-08 09:38:47 +00:00
|
|
|
let url = this.getURL('/api/resource', frappe.slug(doctype), name);
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
return await this.fetch(url, {
|
2018-01-12 12:25:07 +00:00
|
|
|
method: 'DELETE',
|
|
|
|
});
|
2018-02-08 09:38:47 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
async deleteMany(doctype, names) {
|
|
|
|
let url = this.getURL('/api/resource', frappe.slug(doctype));
|
|
|
|
|
|
|
|
return await this.fetch(url, {
|
|
|
|
method: 'DELETE',
|
|
|
|
body: JSON.stringify(names)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async exists(doctype, name) {
|
|
|
|
return (await this.getValue(doctype, name, 'name')) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getValue(doctype, name, fieldname) {
|
|
|
|
let url = this.getURL('/api/resource', frappe.slug(doctype), name, fieldname);
|
|
|
|
|
|
|
|
return (await this.fetch(url, {
|
|
|
|
method: 'GET',
|
|
|
|
})).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetch(url, args) {
|
|
|
|
args.headers = this.getHeaders();
|
|
|
|
let response = await frappe.fetch(url, args);
|
2018-02-09 12:55:55 +00:00
|
|
|
let data = await response.json();
|
|
|
|
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw Error(data.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
getURL(...parts) {
|
|
|
|
return this.protocol + '://' + path.join(this.server, ...parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
getQueryString(params) {
|
2018-01-12 12:25:07 +00:00
|
|
|
return Object.keys(params)
|
|
|
|
.map(k => params[k] != null ? encodeURIComponent(k) + '=' + encodeURIComponent(params[k]) : null)
|
|
|
|
.filter(v => v)
|
|
|
|
.join('&');
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
getHeaders() {
|
|
|
|
return {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
2018-01-23 13:18:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 06:46:38 +00:00
|
|
|
initTypeMap() {
|
2018-01-12 12:25:07 +00:00
|
|
|
this.type_map = {
|
2018-02-08 09:38:47 +00:00
|
|
|
'Currency': true
|
|
|
|
, 'Int': true
|
|
|
|
, 'Float': true
|
|
|
|
, 'Percent': true
|
|
|
|
, 'Check': true
|
|
|
|
, 'Small Text': true
|
|
|
|
, 'Long Text': true
|
|
|
|
, 'Code': true
|
|
|
|
, 'Text Editor': true
|
|
|
|
, 'Date': true
|
|
|
|
, 'Datetime': true
|
|
|
|
, 'Time': true
|
|
|
|
, 'Text': true
|
|
|
|
, 'Data': true
|
|
|
|
, 'Link': true
|
|
|
|
, 'Dynamic Link': true
|
|
|
|
, 'Password': true
|
|
|
|
, 'Select': true
|
|
|
|
, 'Read Only': true
|
|
|
|
, 'Attach': true
|
|
|
|
, 'Attach Image': true
|
|
|
|
, 'Signature': true
|
|
|
|
, 'Color': true
|
|
|
|
, 'Barcode': true
|
|
|
|
, 'Geolocation': true
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|