import { useApi } from '~/composables/useApi' import type { WeightData } from '~/services/dto/weight-data' export interface WorkflowService { getList: (isValid?: boolean | null) => Promise get: (id: number) => Promise create: (payload: TPayload) => Promise update: (id: number, payload: TPayload) => Promise getWeight: () => Promise } export function createWorkflowService( apiResource: string, toastPrefix: string ): WorkflowService { const getList = async (isValid: boolean | null = null): Promise => { const api = useApi() const query = isValid !== null ? { isValid } : {} return api.get(apiResource, query, { toastErrorKey: `errors.${toastPrefix}.list` }) } const get = async (id: number): Promise => { const api = useApi() return api.get(`${apiResource}/${id}`, {}, { toastErrorKey: `errors.${toastPrefix}.fetch` }) } const create = async (payload: TPayload): Promise => { const api = useApi() return api.post(apiResource, payload, { toastSuccessKey: `success.${toastPrefix}.create`, toastErrorKey: `errors.${toastPrefix}.create` }) } const update = async (id: number, payload: TPayload): Promise => { const api = useApi() return api.patch(`${apiResource}/${id}`, payload, { toastErrorKey: `errors.${toastPrefix}.update`, toastSuccessKey: `success.${toastPrefix}.update` }) } const getWeight = async (): Promise => { const api = useApi() return api.get(`${apiResource}/weigh`, {}, { toastErrorKey: `errors.${toastPrefix}.weigh` }) } return { getList, get, create, update, getWeight } }