-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
64 lines (57 loc) · 1.96 KB
/
api.ts
File metadata and controls
64 lines (57 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { HttpClient } from '@pingpong-js/fetch';
export const getApiUrl = () => {
// In browser
if (typeof window !== 'undefined') {
// Production: use the deployed backend
if (window.location.hostname !== 'localhost') {
return 'https://api.mockly.codes';
}
// Local dev: use separate API server
return 'http://localhost:8080';
}
// Server-side: use env var or defaults
if (process.env.NEXT_PUBLIC_API_URL) {
return process.env.NEXT_PUBLIC_API_URL;
}
return process.env.NODE_ENV === 'production'
? 'https://api.mockly.codes' // Production backend
: 'http://localhost:8080'; // Local dev API server
};
// Lazy singleton pattern - only create client when first accessed
let _apiClient: HttpClient | null = null;
export function getClient(): HttpClient {
if (!_apiClient) {
_apiClient = new HttpClient({
baseURL: getApiUrl(),
timeout: 30000,
headers: {
'Content-Type': 'application/json'
},
retries: 2,
retryDelay: 1000,
});
}
return _apiClient;
}
// Simple wrapper object with methods
export const apiClient = {
get: (url: string, options?: any) => getClient().get(url, options),
post: (url: string, data?: any, options?: any) => getClient().post(url, data, options),
put: (url: string, data?: any, options?: any) => getClient().put(url, data, options),
patch: (url: string, data?: any, options?: any) => getClient().patch(url, data, options),
delete: (url: string, options?: any) => getClient().delete(url, options),
head: (url: string, options?: any) => getClient().head(url, options),
options: (url: string, options?: any) => getClient().options(url, options),
};
// Helper function to create a client for server-side rendering
export const createApiClient = (baseURL?: string) => {
return new HttpClient({
baseURL: baseURL || getApiUrl(),
timeout: 30000,
headers: {
'Content-Type': 'application/json'
},
retries: 2,
retryDelay: 1000,
});
};