Skip to content

Commit a2b1a35

Browse files
Merge pull request #480 from mftee/main
fixed build warnings
2 parents 0141cd2 + 92b277e commit a2b1a35

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

frontend/lib/api/assets.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { apiClient } from '@/lib/api/client';
2+
import {
3+
Asset,
4+
AssetDocument,
5+
AssetHistoryEvent,
6+
AssetHistoryFilters,
7+
AssetNote,
8+
AssetUser,
9+
CreateMaintenanceInput,
10+
CreateNoteInput,
11+
Department,
12+
MaintenanceRecord,
13+
TransferAssetInput,
14+
UpdateAssetStatusInput,
15+
} from '@/lib/query/types/asset';
16+
17+
export const assetApiClient = {
18+
getAsset(id: string): Promise<Asset> {
19+
return apiClient.request<Asset>(`/assets/${id}`);
20+
},
21+
22+
getAssetHistory(id: string, filters?: AssetHistoryFilters): Promise<AssetHistoryEvent[]> {
23+
const params = new URLSearchParams();
24+
if (filters) {
25+
Object.entries(filters).forEach(([key, value]) => {
26+
if (value !== undefined && value !== null) {
27+
params.append(key, String(value));
28+
}
29+
});
30+
}
31+
const qs = params.toString();
32+
return apiClient.request<AssetHistoryEvent[]>(
33+
`/assets/${id}/history${qs ? `?${qs}` : ''}`
34+
);
35+
},
36+
37+
getAssetDocuments(id: string): Promise<AssetDocument[]> {
38+
return apiClient.request<AssetDocument[]>(`/assets/${id}/documents`);
39+
},
40+
41+
getMaintenanceRecords(id: string): Promise<MaintenanceRecord[]> {
42+
return apiClient.request<MaintenanceRecord[]>(`/assets/${id}/maintenance`);
43+
},
44+
45+
getAssetNotes(id: string): Promise<AssetNote[]> {
46+
return apiClient.request<AssetNote[]>(`/assets/${id}/notes`);
47+
},
48+
49+
getDepartments(): Promise<Department[]> {
50+
return apiClient.request<Department[]>('/departments');
51+
},
52+
53+
getUsers(): Promise<AssetUser[]> {
54+
return apiClient.request<AssetUser[]>('/users');
55+
},
56+
57+
updateAssetStatus(id: string, data: UpdateAssetStatusInput): Promise<Asset> {
58+
return apiClient.request<Asset>(`/assets/${id}/status`, {
59+
method: 'PATCH',
60+
body: JSON.stringify(data),
61+
});
62+
},
63+
64+
transferAsset(id: string, data: TransferAssetInput): Promise<Asset> {
65+
return apiClient.request<Asset>(`/assets/${id}/transfer`, {
66+
method: 'POST',
67+
body: JSON.stringify(data),
68+
});
69+
},
70+
71+
deleteAsset(id: string): Promise<void> {
72+
return apiClient.request<void>(`/assets/${id}`, { method: 'DELETE' });
73+
},
74+
75+
uploadDocument(assetId: string, file: File, name?: string): Promise<AssetDocument> {
76+
const form = new FormData();
77+
form.append('file', file);
78+
if (name) form.append('name', name);
79+
return apiClient.request<AssetDocument>(`/assets/${assetId}/documents`, {
80+
method: 'POST',
81+
body: form,
82+
headers: {},
83+
});
84+
},
85+
86+
deleteDocument(assetId: string, documentId: string): Promise<void> {
87+
return apiClient.request<void>(`/assets/${assetId}/documents/${documentId}`, {
88+
method: 'DELETE',
89+
});
90+
},
91+
92+
createMaintenanceRecord(assetId: string, data: CreateMaintenanceInput): Promise<MaintenanceRecord> {
93+
return apiClient.request<MaintenanceRecord>(`/assets/${assetId}/maintenance`, {
94+
method: 'POST',
95+
body: JSON.stringify(data),
96+
});
97+
},
98+
99+
createNote(assetId: string, data: CreateNoteInput): Promise<AssetNote> {
100+
return apiClient.request<AssetNote>(`/assets/${assetId}/notes`, {
101+
method: 'POST',
102+
body: JSON.stringify(data),
103+
});
104+
},
105+
};

frontend/lib/api/client.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { RegisterInput, LoginInput, AuthResponse } from '@/lib/query/types';
2+
3+
const BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
4+
5+
async function request<T>(path: string, options: RequestInit = {}): Promise<T> {
6+
const url = `${BASE_URL}${path}`;
7+
8+
const token =
9+
typeof window !== 'undefined' ? localStorage.getItem('token') : null;
10+
11+
const headers: Record<string, string> = {
12+
'Content-Type': 'application/json',
13+
...(options.headers as Record<string, string>),
14+
};
15+
16+
if (token) {
17+
headers['Authorization'] = `Bearer ${token}`;
18+
}
19+
20+
const res = await fetch(url, { ...options, headers });
21+
22+
if (!res.ok) {
23+
const error = await res.json().catch(() => ({ message: res.statusText }));
24+
throw { message: error.message ?? res.statusText, statusCode: res.status };
25+
}
26+
27+
if (res.status === 204) {
28+
return undefined as T;
29+
}
30+
31+
return res.json() as Promise<T>;
32+
}
33+
34+
export const apiClient = {
35+
request,
36+
37+
register(data: RegisterInput): Promise<AuthResponse> {
38+
return request<AuthResponse>('/auth/register', {
39+
method: 'POST',
40+
body: JSON.stringify(data),
41+
});
42+
},
43+
44+
login(data: LoginInput): Promise<AuthResponse> {
45+
return request<AuthResponse>('/auth/login', {
46+
method: 'POST',
47+
body: JSON.stringify(data),
48+
});
49+
},
50+
};

0 commit comments

Comments
 (0)