2
0
mirror of https://github.com/frappe/books.git synced 2025-02-03 04:28:32 +00:00
books/frappe/backends/rest_client.js

89 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-01-03 15:42:40 +05:30
const frappe = require('frappe-core');
const path = require('path');
class RESTClient {
2018-01-08 18:00:14 +05:30
constructor({server, protocol='http', fetch}) {
2018-01-03 15:42:40 +05:30
this.server = server;
this.protocol = protocol;
2018-01-08 18:00:14 +05:30
frappe.fetch = fetch;
2018-01-03 15:42:40 +05:30
this.json_headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}
connect() {
}
async insert(doctype, doc) {
doc.doctype = doctype;
let url = this.protocol + '://' + path.join(this.server, `/api/resource/${frappe.slug(doctype)}`);
2018-01-08 18:00:14 +05:30
let response = await frappe.fetch(url, {
2018-01-03 15:42:40 +05:30
method: 'POST',
headers: this.json_headers,
body: JSON.stringify(doc)
});
return await response.json();
}
async get(doctype, name) {
let url = this.protocol + '://' + path.join(this.server, `/api/resource/${frappe.slug(doctype)}/${name}`);
2018-01-08 18:00:14 +05:30
let response = await frappe.fetch(url, {
2018-01-03 15:42:40 +05:30
method: 'GET',
headers: this.json_headers
});
return await response.json();
}
async get_all({doctype, fields, filters, start, limit, sort_by, order}) {
let url = this.protocol + '://' + path.join(this.server, `/api/resource/${frappe.slug(doctype)}`);
2018-01-08 18:00:14 +05:30
let response = await frappe.fetch(url, {
2018-01-03 15:42:40 +05:30
method: 'GET',
params: {
fields: JSON.stringify(fields),
filters: JSON.stringify(filters),
start: start,
limit: limit,
sort_by: sort_by,
order: order
},
headers: this.json_headers
});
return await response.json();
}
async update(doctype, doc) {
doc.doctype = doctype;
let url = this.protocol + '://' + path.join(this.server, `/api/resource/${frappe.slug(doctype)}/${doc.name}`);
2018-01-08 18:00:14 +05:30
let response = await frappe.fetch(url, {
2018-01-03 15:42:40 +05:30
method: 'PUT',
headers: this.json_headers,
body: JSON.stringify(doc)
});
return await response.json();
}
async delete(doctype, name) {
let url = this.protocol + '://' + path.join(this.server, `/api/resource/${frappe.slug(doctype)}/${name}`);
2018-01-08 18:00:14 +05:30
let response = await frappe.fetch(url, {
2018-01-03 15:42:40 +05:30
method: 'DELETE',
headers: this.json_headers
});
return await response.json();
}
close() {
}
}
module.exports = {
Database: RESTClient
}