mirror of
https://github.com/franjsco/trackmyd-bot
synced 2025-06-05 22:19:29 +02:00
add api
This commit is contained in:
53
api.js
Normal file
53
api.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
const fetch = require('node-fetch');
|
||||||
|
const { api } = require('./config');
|
||||||
|
|
||||||
|
const devicesPath = `${api.baseURL}${api.paths.devices}`;
|
||||||
|
|
||||||
|
function getDevices() {
|
||||||
|
const init = {
|
||||||
|
method: 'GET',
|
||||||
|
headers: api.headers,
|
||||||
|
};
|
||||||
|
|
||||||
|
return fetch(devicesPath, init);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getInfoDevice(name) {
|
||||||
|
const init = {
|
||||||
|
method: 'GET',
|
||||||
|
headers: api.headers,
|
||||||
|
};
|
||||||
|
|
||||||
|
const path = `${devicesPath}?name=${name}`;
|
||||||
|
return fetch(path, init);
|
||||||
|
}
|
||||||
|
|
||||||
|
function addDevice(name) {
|
||||||
|
const body = {
|
||||||
|
name,
|
||||||
|
};
|
||||||
|
|
||||||
|
const init = {
|
||||||
|
method: 'POST',
|
||||||
|
headers: api.headers,
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
};
|
||||||
|
|
||||||
|
return fetch(devicesPath, init);
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeDevice(id) {
|
||||||
|
const init = {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: api.headers,
|
||||||
|
};
|
||||||
|
|
||||||
|
const path = `${devicesPath}${id}`;
|
||||||
|
return fetch(path, init);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports.getDevices = getDevices;
|
||||||
|
module.exports.getInfoDevice = getInfoDevice;
|
||||||
|
module.exports.addDevice = addDevice;
|
||||||
|
module.exports.removeDevice = removeDevice;
|
@@ -9,8 +9,11 @@
|
|||||||
"api": {
|
"api": {
|
||||||
"baseURL": "http://localhost:3500/api",
|
"baseURL": "http://localhost:3500/api",
|
||||||
"headers": {
|
"headers": {
|
||||||
"secret": "mysecret",
|
"Authorization": "Basic Y2xpZW50MjpjbGllbnQy",
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
"paths": {
|
||||||
|
"devices": "/devices/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"logger": {
|
"logger": {
|
||||||
|
Reference in New Issue
Block a user