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

122 lines
2.6 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;
this.init_type_map();
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();
}
init_type_map() {
this.type_map = {
'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-03 15:42:40 +05:30
close() {
}
}
module.exports = {
Database: RESTClient
}