userSettingService.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // web/services/userSettingService.ts
  2. import { API_BASE_URL } from '../utils/constants';
  3. import { AppSettings } from '../types'; // Frontend AppSettings interface
  4. // Assuming backend returns all AppSettings fields, plus id, userId, createdAt, updatedAt
  5. interface UserSettingResponse extends AppSettings {
  6. id: string;
  7. userId: string;
  8. createdAt: Date;
  9. updatedAt: Date;
  10. }
  11. export const userSettingService = {
  12. async get(token: string): Promise<UserSettingResponse> {
  13. const response = await fetch(`${API_BASE_URL}/settings`, {
  14. method: 'GET',
  15. headers: {
  16. 'Content-Type': 'application/json',
  17. Authorization: `Bearer ${token}`,
  18. },
  19. });
  20. if (!response.ok) {
  21. const errorData = await response.json();
  22. throw new Error(errorData.message || 'Failed to fetch user settings');
  23. }
  24. return response.json();
  25. },
  26. async update(token: string, settings: Partial<AppSettings>): Promise<UserSettingResponse> {
  27. const response = await fetch(`${API_BASE_URL}/settings`, {
  28. method: 'PUT',
  29. headers: {
  30. 'Content-Type': 'application/json',
  31. Authorization: `Bearer ${token}`,
  32. },
  33. body: JSON.stringify(settings),
  34. });
  35. if (!response.ok) {
  36. const errorData = await response.json();
  37. throw new Error(errorData.message || 'Failed to update user settings');
  38. }
  39. return response.json();
  40. },
  41. };