The frontend component of PuffPastry, meant to interface with the backend, which in turn interfaces with the smart contract.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

useFetch.ts 1.8 KiB

vor 11 Monaten
vor 11 Monaten
vor 11 Monaten
vor 11 Monaten
vor 11 Monaten
vor 11 Monaten
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * @deprecated This method is deprecated.
  3. */
  4. export const useFetch = () => {
  5. interface FetchConfig {
  6. baseUrl: string;
  7. headers: HeadersInit;
  8. }
  9. interface ApiResponse<T> {
  10. data: T;
  11. status: number;
  12. }
  13. const DEFAULT_CONFIG: FetchConfig = {
  14. baseUrl: '/api/v1',
  15. headers: {
  16. 'Content-Type': 'application/json'
  17. }
  18. };
  19. const buildQueryString = (params?: Record<string, string | number>): string => {
  20. if (!params) return '';
  21. return '?' + Object.entries(params)
  22. .map(([key, value]) => `${key}=${value}`)
  23. .join('&');
  24. };
  25. /**
  26. * @deprecated This function is deprecated.
  27. */
  28. const createRequest = async <T>(
  29. endpoint: string,
  30. method: 'GET' | 'POST',
  31. options: RequestInit = {}
  32. ): Promise<ApiResponse<T>> => {
  33. const response = await fetch(`${DEFAULT_CONFIG.baseUrl}${endpoint}`, {
  34. method,
  35. mode: 'cors',
  36. headers: new Headers(DEFAULT_CONFIG.headers),
  37. ...options
  38. });
  39. const data = await response.json();
  40. return {data, status: response.status};
  41. };
  42. /**
  43. * @deprecated This function is deprecated.
  44. */
  45. const get = async <T>(
  46. endpoint: string,
  47. queryParams?: Record<string, string | number>
  48. ): Promise<T> => {
  49. const query = buildQueryString(queryParams);
  50. const response = await createRequest<T>(`${endpoint}${query}`, 'GET');
  51. return response.data;
  52. };
  53. /**
  54. * @deprecated This function is deprecated.
  55. */
  56. const post = async <T, U>(endpoint: string, payload: T): Promise<U> => {
  57. const response = await createRequest<U>(endpoint, 'POST', {
  58. body: JSON.stringify(payload)
  59. });
  60. return response.data;
  61. };
  62. return { get, post }
  63. }