api.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import axios from 'axios';
  2. import { baseApi } from './baseApi';
  3. /**
  4. * Generic api call format
  5. * @param {string} token - Bearer token.
  6. * @param {Object} params - Query params.
  7. * @param {Object} pathParams - Path params.
  8. * @param {(err: Object, res: Object) => void} callback - Callback function.
  9. */
  10. const checkAuth = baseApi('GET', '/api/auth/check');
  11. const registerUser = baseApi<{
  12. email: string,
  13. password: string
  14. }>('POST', '/api/users');
  15. const logInUser = baseApi<{
  16. email: string,
  17. password: string
  18. }>('POST', '/api/login');
  19. const logOutUser = baseApi('POST', '/api/logout');
  20. const getUser = baseApi<{}, { id: number }>('GET', pathParams => {
  21. return `/api/users/${pathParams.id}`;
  22. });
  23. const updateUser = baseApi<{
  24. rawKubeConfig?: string,
  25. allowedContexts?: string[]
  26. }, { id: number }>('PUT', pathParams => {
  27. return `/api/users/${pathParams.id}`;
  28. });
  29. const getContexts = baseApi<{}, { id: number }>('GET', pathParams => {
  30. return `/api/users/${pathParams.id}/contexts`;
  31. });
  32. // Bundle export to allow default api import (api.<method> is more readable)
  33. export default {
  34. checkAuth,
  35. registerUser,
  36. logInUser,
  37. logOutUser,
  38. getUser,
  39. updateUser,
  40. getContexts,
  41. }