mirror of
https://github.com/frappe/books.git
synced 2025-02-03 04:28:32 +00:00
122 lines
2.6 KiB
JavaScript
122 lines
2.6 KiB
JavaScript
const frappe = require('frappe-core');
|
|
const path = require('path');
|
|
|
|
class RESTClient {
|
|
constructor({server, protocol='http', fetch}) {
|
|
this.server = server;
|
|
this.protocol = protocol;
|
|
|
|
this.init_type_map();
|
|
|
|
frappe.fetch = fetch;
|
|
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)}`);
|
|
let response = await frappe.fetch(url, {
|
|
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}`);
|
|
let response = await frappe.fetch(url, {
|
|
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)}`);
|
|
let response = await frappe.fetch(url, {
|
|
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}`);
|
|
let response = await frappe.fetch(url, {
|
|
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}`);
|
|
|
|
let response = await frappe.fetch(url, {
|
|
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
|
|
}
|
|
}
|
|
|
|
close() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = {
|
|
Database: RESTClient
|
|
} |