mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
chore(FE-41): use BaseApiService class
This commit is contained in:
@@ -1,6 +1,4 @@
|
|||||||
import axios from 'axios';
|
import { BaseApiService } from '@/services/api/base';
|
||||||
import { httpClient, httpClientFetcher } from '@/services/http/client';
|
|
||||||
import { BaseApiResponse } from '@/types/api/api-general';
|
|
||||||
import {
|
import {
|
||||||
CreateUomPayload,
|
CreateUomPayload,
|
||||||
Uom,
|
Uom,
|
||||||
@@ -17,98 +15,19 @@ import {
|
|||||||
UpdateLocationPayload,
|
UpdateLocationPayload,
|
||||||
} from '@/types/api/master-data/location';
|
} from '@/types/api/master-data/location';
|
||||||
|
|
||||||
export class MasterDataApi<T, CreatePayloadGeneric, UpdatePayloadGeneric> {
|
export const UomApi = new BaseApiService<
|
||||||
basePath: string;
|
|
||||||
|
|
||||||
constructor(basePath: string) {
|
|
||||||
this.basePath = basePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getAllFetcher(endpoint: string): Promise<BaseApiResponse<T[]>> {
|
|
||||||
return await httpClientFetcher<BaseApiResponse<T[]>>(endpoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
async getSingle(id: number) {
|
|
||||||
try {
|
|
||||||
const getSinglePath = `${this.basePath}/${id}`;
|
|
||||||
const getSingleRes = await httpClient<BaseApiResponse<T>>(getSinglePath);
|
|
||||||
|
|
||||||
return getSingleRes;
|
|
||||||
} catch (error) {
|
|
||||||
if (axios.isAxiosError<BaseApiResponse<T>>(error)) {
|
|
||||||
return error.response?.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async create(payload: CreatePayloadGeneric) {
|
|
||||||
try {
|
|
||||||
const createRes = await httpClient<BaseApiResponse<T>>(this.basePath, {
|
|
||||||
method: 'POST',
|
|
||||||
body: payload,
|
|
||||||
});
|
|
||||||
|
|
||||||
return createRes;
|
|
||||||
} catch (error: unknown) {
|
|
||||||
if (axios.isAxiosError<BaseApiResponse<T>>(error)) {
|
|
||||||
return error.response?.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async update(id: number, payload: UpdatePayloadGeneric) {
|
|
||||||
try {
|
|
||||||
const updatePath = `${this.basePath}/${id}`;
|
|
||||||
const updateRes = await httpClient<BaseApiResponse<T>>(updatePath, {
|
|
||||||
method: 'PATCH',
|
|
||||||
body: payload,
|
|
||||||
});
|
|
||||||
|
|
||||||
return updateRes;
|
|
||||||
} catch (error: unknown) {
|
|
||||||
if (axios.isAxiosError<BaseApiResponse<T>>(error)) {
|
|
||||||
return error.response?.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async delete(id: number) {
|
|
||||||
try {
|
|
||||||
const deletePath = `${this.basePath}/${id}`;
|
|
||||||
const deleteRes = await httpClient<BaseApiResponse>(deletePath, {
|
|
||||||
method: 'DELETE',
|
|
||||||
});
|
|
||||||
|
|
||||||
return deleteRes;
|
|
||||||
} catch (error) {
|
|
||||||
if (axios.isAxiosError<BaseApiResponse>(error)) {
|
|
||||||
return error.response?.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const UomApi = new MasterDataApi<
|
|
||||||
Uom,
|
Uom,
|
||||||
CreateUomPayload,
|
CreateUomPayload,
|
||||||
UpdateUomPayload
|
UpdateUomPayload
|
||||||
>('/master-data/uoms');
|
>('/master-data/uoms');
|
||||||
|
|
||||||
export const AreaApi = new MasterDataApi<
|
export const AreaApi = new BaseApiService<
|
||||||
Area,
|
Area,
|
||||||
CreateAreaPayload,
|
CreateAreaPayload,
|
||||||
UpdateAreaPayload
|
UpdateAreaPayload
|
||||||
>('/master-data/areas');
|
>('/master-data/areas');
|
||||||
|
|
||||||
export const LocationApi = new MasterDataApi<
|
export const LocationApi = new BaseApiService<
|
||||||
Location,
|
Location,
|
||||||
CreateLocationPayload,
|
CreateLocationPayload,
|
||||||
UpdateLocationPayload
|
UpdateLocationPayload
|
||||||
|
|||||||
Reference in New Issue
Block a user