MCPcopy Create free account
hub / github.com/SwishHQ/spread / apiRequest

Function apiRequest

web/src/api/client.ts:58–131  ·  view source on GitHub ↗
(
  config: AxiosRequestConfig
)

Source from the content-addressed store, hash-verified

56
57// Generic API request function
58export const apiRequest = async <T>(
59 config: AxiosRequestConfig
60): Promise<ApiResponse<T>> => {
61 try {
62 const response: AxiosResponse = await client(config);
63
64 console.log(`API Response from ${config.url}:`, response.data);
65
66 // Check if response has the expected structure with nested data
67 if (response.data && response.data.success !== undefined) {
68 // Show error toast if the response indicates failure
69 if (!response.data.success) {
70 const errorMessage = formatErrorMessage(
71 response.data.message || "An error occurred"
72 );
73 toast.error(errorMessage, {
74 position: "top-right",
75 autoClose: 5000,
76 hideProgressBar: false,
77 closeOnClick: true,
78 pauseOnHover: true,
79 draggable: true,
80 });
81 }
82 return response.data as ApiResponse<T>;
83 }
84
85 // Fallback for APIs that don't follow the standard response structure
86 return {
87 success: true,
88 data: response.data,
89 };
90 } catch (error) {
91 if (axios.isAxiosError(error)) {
92 // Handle axios errors
93 const errorMessage = formatErrorMessage(
94 error.response?.data?.message ||
95 error.response?.data?.error ||
96 error.message ||
97 "An error occurred"
98 );
99
100 toast.error(errorMessage, {
101 position: "top-right",
102 autoClose: 5000,
103 hideProgressBar: false,
104 closeOnClick: true,
105 pauseOnHover: true,
106 draggable: true,
107 });
108
109 return {
110 success: false,
111 error: errorMessage,
112 };
113 }
114
115 // Handle non-axios errors

Callers

nothing calls this directly

Calls 1

formatErrorMessageFunction · 0.85

Tested by

no test coverage detected