Plan stuff. Log stuff.
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.

138 lines
3.1 KiB

4 years ago
  1. import ky from "ky";
  2. import Activity, {ActivityUpdate} from "../models/activity";
  3. import Period, {PeriodUpdate} from "../models/period";
  4. function json(data) {
  5. return {
  6. body: JSON.stringify(data),
  7. headers: {
  8. "Content-Type": "application/json",
  9. },
  10. credentials: "include",
  11. }
  12. }
  13. const k = ky.create({
  14. credentials: "include",
  15. })
  16. export class StuffLogAPI {
  17. async checkSession() {
  18. return await k.get("/api/user/").json();
  19. }
  20. async login(username, password) {
  21. return await k.post("/api/user/login", json({username,password})).json();
  22. }
  23. async register(username, password) {
  24. return await k.post("/api/user/register", json({username,password})).json();
  25. }
  26. async logout() {
  27. return await k.post("/api/user/logout").json();
  28. }
  29. /**
  30. * List activities
  31. *
  32. * @returns {Activity[]}
  33. */
  34. async listActivities() {
  35. const data = await k.get("/api/activity/").json();
  36. return data.activities.map(d => new Activity(d));
  37. }
  38. /**
  39. * List periods
  40. *
  41. * @returns {{periods: Period[], activities: Activity[]}}
  42. */
  43. async listPeriods() {
  44. const data = await k.get("/api/period/").json();
  45. return data.periods.map(d => new Period(d));
  46. }
  47. /**
  48. * Find activity
  49. *
  50. * @param {string} id
  51. * @returns {Activity}
  52. */
  53. async findActivity(id) {
  54. const data = await k.get(`/api/activity/${id}`).json();
  55. return new Activity(data.activity);
  56. }
  57. /**
  58. * Find period
  59. *
  60. * @param {string} id
  61. * @returns {Period}
  62. */
  63. async findPeriod(id) {
  64. const data = await k.get(`/api/period/${id}`).json();
  65. return new Period(data.period);
  66. }
  67. /**
  68. * Create a new activity
  69. *
  70. * @param {Activity} activity
  71. * @returns {Activity}
  72. */
  73. async postActivity(activity) {
  74. const data = await k.post(`/api/activity/`, json(activity)).json();
  75. return new Activity(data.activity);
  76. }
  77. /**
  78. * Create a new period
  79. *
  80. * @param {Period} period
  81. * @returns {Period}
  82. */
  83. async postPeriod(period) {
  84. const data = await k.post(`/api/period/`, json(period)).json();
  85. return new Period(data.period);
  86. }
  87. /**
  88. * Update an activity
  89. *
  90. * @param {string} id
  91. * @param {ActivityUpdate[]} updates
  92. * @returns {Activity}
  93. */
  94. async patchActivity(id, ...updates) {
  95. const data = await k.patch(`/api/activity/${id}`, json(updates)).json();
  96. return new Activity(data.activity);
  97. }
  98. /**
  99. * Update an period
  100. *
  101. * @param {string} id
  102. * @param {PeriodUpdate[]} updates
  103. * @returns {Period}
  104. */
  105. async patchPeriod(id, ...updates) {
  106. const data = await k.patch(`/api/period/${id}`, json(updates)).json();
  107. return new Period(data.period);
  108. }
  109. /**
  110. * Update an activity
  111. *
  112. * @param {string} id
  113. * @returns {Activity}
  114. */
  115. async deleteActivity(id) {
  116. const data = await k.delete(`/api/activity/${id}`).json();
  117. return new Activity(data.activity);
  118. }
  119. /**
  120. * Update an period
  121. *
  122. * @param {string} id
  123. * @returns {Period}
  124. */
  125. async deletePeriod(id) {
  126. const data = await k.delete(`/api/period/${id}`).json();
  127. return new Period(data.period);
  128. }
  129. }
  130. const slApi = new StuffLogAPI();
  131. export default slApi;