FitTrackee/fittrackee_client/src/fitTrackeeApi/index.js

70 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-01-18 19:34:38 +01:00
import { createApiRequest } from '../utils'
2018-04-29 18:23:46 +02:00
export default class FitTrackeeApi {
2019-01-18 19:34:38 +01:00
static getData(target, data = {}) {
let url = target
if (data.id || (target === 'users' && data.username)) {
url = `${url}/${data.username ? data.username : data.id}`
} else if (Object.keys(data).length > 0) {
2018-06-07 19:28:06 +02:00
url += '?'
2019-08-28 15:35:22 +02:00
Object.keys(data).map(key => (url += `&${key}=${data[key]}`))
}
2018-05-28 12:35:17 +02:00
const params = {
url: url,
2018-04-29 18:23:46 +02:00
method: 'GET',
2018-05-28 12:35:17 +02:00
type: 'application/json',
}
2019-01-18 19:34:38 +01:00
return createApiRequest(params)
2018-04-29 18:23:46 +02:00
}
2018-04-30 20:08:18 +02:00
2018-04-30 21:38:09 +02:00
static addData(target, data) {
2018-05-28 12:35:17 +02:00
const params = {
2019-01-18 19:34:38 +01:00
url: target,
2018-04-30 21:38:09 +02:00
method: 'POST',
2018-05-28 12:35:17 +02:00
body: data,
type: 'application/json',
}
2019-01-18 19:34:38 +01:00
return createApiRequest(params)
2018-04-30 21:38:09 +02:00
}
2018-06-12 12:51:23 +02:00
static addDataWithFile(target, data) {
const params = {
2019-01-18 19:34:38 +01:00
url: target,
2018-06-12 12:51:23 +02:00
method: 'POST',
body: data,
}
2019-01-18 19:34:38 +01:00
return createApiRequest(params)
2018-06-12 12:51:23 +02:00
}
static postData(target, data) {
const params = {
2019-08-28 15:35:22 +02:00
url: `${target}${data.id ? `/${data.id}` : ''}`,
2018-06-12 12:51:23 +02:00
method: 'POST',
body: data,
type: 'application/json',
}
2019-01-18 19:34:38 +01:00
return createApiRequest(params)
2018-06-12 12:51:23 +02:00
}
2018-04-30 20:08:18 +02:00
static updateData(target, data) {
2018-05-28 12:35:17 +02:00
const params = {
url: `${target}${
data.id ? `/${data.id}` : data.username ? `/${data.username}` : ''
}`,
2018-04-30 20:08:18 +02:00
method: 'PATCH',
2018-05-28 12:35:17 +02:00
body: data,
type: 'application/json',
}
2019-01-18 19:34:38 +01:00
return createApiRequest(params)
2018-04-30 20:08:18 +02:00
}
2018-04-30 21:38:09 +02:00
static deleteData(target, id) {
2018-05-28 12:35:17 +02:00
const params = {
2019-01-18 19:34:38 +01:00
url: `${target}/${id}`,
2018-04-30 21:38:09 +02:00
method: 'DELETE',
2018-05-28 12:35:17 +02:00
type: 'application/json',
}
2019-01-18 19:34:38 +01:00
return createApiRequest(params)
2018-04-30 21:38:09 +02:00
}
2018-04-29 18:23:46 +02:00
}