You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
3.1 KiB
139 lines
3.1 KiB
import ky from "ky";
|
|
import Activity, {ActivityUpdate} from "../models/activity";
|
|
import Period, {PeriodUpdate} from "../models/period";
|
|
|
|
function json(data) {
|
|
return {
|
|
body: JSON.stringify(data),
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
credentials: "include",
|
|
}
|
|
}
|
|
|
|
const k = ky.create({
|
|
credentials: "include",
|
|
})
|
|
|
|
export class StuffLogAPI {
|
|
async checkSession() {
|
|
return await k.get("/api/user/").json();
|
|
}
|
|
async login(username, password) {
|
|
return await k.post("/api/user/login", json({username,password})).json();
|
|
}
|
|
async register(username, password) {
|
|
return await k.post("/api/user/register", json({username,password})).json();
|
|
}
|
|
async logout() {
|
|
return await k.post("/api/user/logout").json();
|
|
}
|
|
|
|
/**
|
|
* List activities
|
|
*
|
|
* @returns {Activity[]}
|
|
*/
|
|
async listActivities() {
|
|
const data = await k.get("/api/activity/").json();
|
|
return data.activities.map(d => new Activity(d));
|
|
}
|
|
/**
|
|
* List periods
|
|
*
|
|
* @returns {{periods: Period[], activities: Activity[]}}
|
|
*/
|
|
async listPeriods() {
|
|
const data = await k.get("/api/period/").json();
|
|
|
|
return data.periods.map(d => new Period(d));
|
|
}
|
|
/**
|
|
* Find activity
|
|
*
|
|
* @param {string} id
|
|
* @returns {Activity}
|
|
*/
|
|
async findActivity(id) {
|
|
const data = await k.get(`/api/activity/${id}`).json();
|
|
return new Activity(data.activity);
|
|
}
|
|
/**
|
|
* Find period
|
|
*
|
|
* @param {string} id
|
|
* @returns {Period}
|
|
*/
|
|
async findPeriod(id) {
|
|
const data = await k.get(`/api/period/${id}`).json();
|
|
return new Period(data.period);
|
|
}
|
|
/**
|
|
* Create a new activity
|
|
*
|
|
* @param {Activity} activity
|
|
* @returns {Activity}
|
|
*/
|
|
async postActivity(activity) {
|
|
const data = await k.post(`/api/activity/`, json(activity)).json();
|
|
return new Activity(data.activity);
|
|
}
|
|
/**
|
|
* Create a new period
|
|
*
|
|
* @param {Period} period
|
|
* @returns {Period}
|
|
*/
|
|
async postPeriod(period) {
|
|
const data = await k.post(`/api/period/`, json(period)).json();
|
|
return new Period(data.period);
|
|
}
|
|
/**
|
|
* Update an activity
|
|
*
|
|
* @param {string} id
|
|
* @param {ActivityUpdate[]} updates
|
|
* @returns {Activity}
|
|
*/
|
|
async patchActivity(id, ...updates) {
|
|
const data = await k.patch(`/api/activity/${id}`, json(updates)).json();
|
|
return new Activity(data.activity);
|
|
}
|
|
/**
|
|
* Update an period
|
|
*
|
|
* @param {string} id
|
|
* @param {PeriodUpdate[]} updates
|
|
* @returns {Period}
|
|
*/
|
|
async patchPeriod(id, ...updates) {
|
|
const data = await k.patch(`/api/period/${id}`, json(updates)).json();
|
|
return new Period(data.period);
|
|
}
|
|
/**
|
|
* Update an activity
|
|
*
|
|
* @param {string} id
|
|
* @returns {Activity}
|
|
*/
|
|
async deleteActivity(id) {
|
|
const data = await k.delete(`/api/activity/${id}`).json();
|
|
return new Activity(data.activity);
|
|
}
|
|
|
|
/**
|
|
* Update an period
|
|
*
|
|
* @param {string} id
|
|
* @returns {Period}
|
|
*/
|
|
async deletePeriod(id) {
|
|
const data = await k.delete(`/api/period/${id}`).json();
|
|
return new Period(data.period);
|
|
}
|
|
}
|
|
|
|
const slApi = new StuffLogAPI();
|
|
|
|
export default slApi;
|