mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-26 18:42:49 +00:00
refactor(api client): reuse the existing services instead
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Freight, PaginatedResponse } from "@edr/types";
|
||||
import { client, endpoint, unwrap } from "@/utils/api";
|
||||
import { endpoint } from "@/utils/api";
|
||||
import type {
|
||||
CreateFileUploadFieldDto,
|
||||
CreateFileUploadSettingDto,
|
||||
@@ -8,6 +8,10 @@ import type {
|
||||
UpdateFileUploadFieldDto,
|
||||
UpdateFileUploadSettingDto,
|
||||
} from "@/types/fileUploadSettings";
|
||||
import { bookingsService } from "./bookings.service";
|
||||
import { consignmentsService } from "./consignments.service";
|
||||
import { trackingService } from "./tracking.service";
|
||||
import { fileUploadSettingsService } from "./fileUploadSettings.service";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// API definition
|
||||
@@ -18,19 +22,13 @@ export const api = {
|
||||
list: endpoint<void, PaginatedResponse<Freight.IBooking>>(
|
||||
"bookings",
|
||||
"list",
|
||||
async () => {
|
||||
const { data } = await client.get("/bookings");
|
||||
return unwrap(data);
|
||||
},
|
||||
bookingsService.list,
|
||||
),
|
||||
|
||||
get: endpoint<{ id: string }, Freight.IBooking>(
|
||||
"bookings",
|
||||
"get",
|
||||
async ({ id }) => {
|
||||
const { data } = await client.get(`/bookings/${id}`);
|
||||
return unwrap(data);
|
||||
},
|
||||
({ id }) => bookingsService.get(id),
|
||||
),
|
||||
|
||||
create: endpoint<
|
||||
@@ -42,17 +40,12 @@ export const api = {
|
||||
trainId?: string;
|
||||
},
|
||||
Freight.IBooking
|
||||
>("bookings", "create", async (input) => {
|
||||
const { data } = await client.post("/bookings", input);
|
||||
return unwrap(data);
|
||||
}),
|
||||
>("bookings", "create", (input) => bookingsService.create(input)),
|
||||
|
||||
remove: endpoint<{ id: string }, void>(
|
||||
"bookings",
|
||||
"remove",
|
||||
async ({ id }) => {
|
||||
await client.delete(`/bookings/${id}`);
|
||||
},
|
||||
({ id }) => bookingsService.remove(id),
|
||||
),
|
||||
},
|
||||
|
||||
@@ -60,19 +53,13 @@ export const api = {
|
||||
list: endpoint<void, PaginatedResponse<Freight.IConsignment>>(
|
||||
"consignments",
|
||||
"list",
|
||||
async () => {
|
||||
const { data } = await client.get("/consignments");
|
||||
return unwrap(data);
|
||||
},
|
||||
consignmentsService.list,
|
||||
),
|
||||
|
||||
get: endpoint<{ id: string }, Freight.IConsignment>(
|
||||
"consignments",
|
||||
"get",
|
||||
async ({ id }) => {
|
||||
const { data } = await client.get(`/consignments/${id}`);
|
||||
return unwrap(data);
|
||||
},
|
||||
({ id }) => consignmentsService.get(id),
|
||||
),
|
||||
},
|
||||
|
||||
@@ -80,112 +67,74 @@ export const api = {
|
||||
forConsignment: endpoint<
|
||||
{ consignmentId: string },
|
||||
Freight.ITrackingEvent[]
|
||||
>("tracking", "forConsignment", async ({ consignmentId }) => {
|
||||
const { data } = await client.get(`/tracking/${consignmentId}`);
|
||||
return unwrap(data);
|
||||
}),
|
||||
>("tracking", "forConsignment", ({ consignmentId }) =>
|
||||
trackingService.forConsignment(consignmentId),
|
||||
),
|
||||
},
|
||||
|
||||
fileUploadSettings: {
|
||||
list: endpoint<void, FileUploadSetting[]>(
|
||||
"file-upload-settings",
|
||||
"list",
|
||||
async () => {
|
||||
const { data } = await client.get("/api/file-upload-settings");
|
||||
return unwrap(data);
|
||||
},
|
||||
fileUploadSettingsService.list,
|
||||
),
|
||||
|
||||
getById: endpoint<{ id: string }, FileUploadSetting>(
|
||||
"file-upload-settings",
|
||||
"getById",
|
||||
async ({ id }) => {
|
||||
const { data } = await client.get(`/api/file-upload-settings/${id}`);
|
||||
return unwrap(data);
|
||||
},
|
||||
({ id }) => fileUploadSettingsService.getById(id),
|
||||
),
|
||||
|
||||
getByCode: endpoint<{ code: string }, FileUploadSetting>(
|
||||
"file-upload-settings",
|
||||
"getByCode",
|
||||
async ({ code }) => {
|
||||
const { data } = await client.get(
|
||||
`/api/file-upload-settings/by-code/${encodeURIComponent(code)}`,
|
||||
);
|
||||
return unwrap(data);
|
||||
},
|
||||
({ code }) => fileUploadSettingsService.getByCode(code),
|
||||
),
|
||||
|
||||
create: endpoint<CreateFileUploadSettingDto, FileUploadSetting>(
|
||||
"file-upload-settings",
|
||||
"create",
|
||||
async (payload) => {
|
||||
const { data } = await client.post(
|
||||
"/api/file-upload-settings",
|
||||
payload,
|
||||
);
|
||||
return unwrap(data);
|
||||
},
|
||||
(payload) => fileUploadSettingsService.create(payload),
|
||||
),
|
||||
|
||||
update: endpoint<
|
||||
{ id: string; dto: UpdateFileUploadSettingDto },
|
||||
FileUploadSetting
|
||||
>("file-upload-settings", "update", async ({ id, dto }) => {
|
||||
const { data } = await client.patch(
|
||||
`/api/file-upload-settings/${id}`,
|
||||
dto,
|
||||
);
|
||||
return unwrap(data);
|
||||
}),
|
||||
>("file-upload-settings", "update", ({ id, dto }) =>
|
||||
fileUploadSettingsService.update(id, dto),
|
||||
),
|
||||
|
||||
remove: endpoint<{ id: string }, void>(
|
||||
"file-upload-settings",
|
||||
"remove",
|
||||
async ({ id }) => {
|
||||
await client.delete(`/api/file-upload-settings/${id}`);
|
||||
},
|
||||
({ id }) => fileUploadSettingsService.remove(id),
|
||||
),
|
||||
|
||||
replaceFields: endpoint<
|
||||
{ id: string; fields: CreateFileUploadFieldDto[] },
|
||||
FileUploadField[]
|
||||
>("file-upload-settings", "replaceFields", async ({ id, fields }) => {
|
||||
const { data } = await client.put(
|
||||
`/api/file-upload-settings/${id}/fields`,
|
||||
fields,
|
||||
);
|
||||
return unwrap(data);
|
||||
}),
|
||||
>("file-upload-settings", "replaceFields", ({ id, fields }) =>
|
||||
fileUploadSettingsService.replaceFields(id, fields),
|
||||
),
|
||||
|
||||
addField: endpoint<
|
||||
{ settingId: string; dto: CreateFileUploadFieldDto },
|
||||
FileUploadField
|
||||
>("file-upload-settings", "addField", async ({ settingId, dto }) => {
|
||||
const { data } = await client.post(
|
||||
`/api/file-upload-settings/${settingId}/fields`,
|
||||
dto,
|
||||
);
|
||||
return unwrap(data);
|
||||
}),
|
||||
>("file-upload-settings", "addField", ({ settingId, dto }) =>
|
||||
fileUploadSettingsService.addField(settingId, dto),
|
||||
),
|
||||
|
||||
updateField: endpoint<
|
||||
{ fieldId: string; dto: UpdateFileUploadFieldDto },
|
||||
FileUploadField
|
||||
>("file-upload-settings", "updateField", async ({ fieldId, dto }) => {
|
||||
const { data } = await client.patch(
|
||||
`/api/file-upload-settings/fields/${fieldId}`,
|
||||
dto,
|
||||
);
|
||||
return unwrap(data);
|
||||
}),
|
||||
>("file-upload-settings", "updateField", ({ fieldId, dto }) =>
|
||||
fileUploadSettingsService.updateField(fieldId, dto),
|
||||
),
|
||||
|
||||
removeField: endpoint<{ fieldId: string }, void>(
|
||||
"file-upload-settings",
|
||||
"removeField",
|
||||
async ({ fieldId }) => {
|
||||
await client.delete(`/api/file-upload-settings/fields/${fieldId}`);
|
||||
},
|
||||
({ fieldId }) => fileUploadSettingsService.removeField(fieldId),
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Freight, PaginatedResponse } from "@edr/types";
|
||||
|
||||
import { api } from "../utils/api";
|
||||
import { api } from "./crud";
|
||||
|
||||
export interface CreateBookingPayload {
|
||||
reference: string;
|
||||
|
||||
Reference in New Issue
Block a user