feat: Add model feature for piece and component
This commit is contained in:
113
app/services/modelTypes.ts
Normal file
113
app/services/modelTypes.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { useRequestFetch } from '#imports';
|
||||
import type { FetchOptions } from 'ofetch';
|
||||
|
||||
export type ModelCategory = 'COMPONENT' | 'PIECE';
|
||||
|
||||
export interface ModelTypePayload {
|
||||
name: string;
|
||||
code: string;
|
||||
category: ModelCategory;
|
||||
notes?: string | null;
|
||||
description?: string | null;
|
||||
}
|
||||
|
||||
export interface ModelType extends ModelTypePayload {
|
||||
id: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface ModelTypeListParams {
|
||||
q?: string;
|
||||
category?: ModelCategory;
|
||||
sort?: 'name' | 'code' | 'createdAt';
|
||||
dir?: 'asc' | 'desc';
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}
|
||||
|
||||
export interface ModelTypeListResponse {
|
||||
items: ModelType[];
|
||||
total: number;
|
||||
offset: number;
|
||||
limit: number;
|
||||
}
|
||||
|
||||
const ENDPOINT = '/api/model-types';
|
||||
|
||||
function resolveBaseUrl() {
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
return runtimeConfig.public.apiBaseUrl || '';
|
||||
}
|
||||
|
||||
function createOptions<T>(options: FetchOptions<T> = {}) {
|
||||
return {
|
||||
baseURL: resolveBaseUrl(),
|
||||
credentials: 'include' as const,
|
||||
...options,
|
||||
};
|
||||
}
|
||||
|
||||
export function listModelTypes(params: ModelTypeListParams = {}, opts: { signal?: AbortSignal } = {}) {
|
||||
const requestFetch = useRequestFetch();
|
||||
const query: Record<string, string | number> = {};
|
||||
|
||||
if (params.q) {
|
||||
query.q = params.q;
|
||||
}
|
||||
if (params.category) {
|
||||
query.category = params.category;
|
||||
}
|
||||
if (params.sort) {
|
||||
query.sort = params.sort;
|
||||
}
|
||||
if (params.dir) {
|
||||
query.dir = params.dir;
|
||||
}
|
||||
if (typeof params.limit === 'number') {
|
||||
query.limit = params.limit;
|
||||
}
|
||||
if (typeof params.offset === 'number') {
|
||||
query.offset = params.offset;
|
||||
}
|
||||
|
||||
return requestFetch<ModelTypeListResponse>(ENDPOINT, createOptions({
|
||||
method: 'GET',
|
||||
query,
|
||||
signal: opts.signal,
|
||||
}));
|
||||
}
|
||||
|
||||
export function createModelType(payload: ModelTypePayload, opts: { signal?: AbortSignal } = {}) {
|
||||
const requestFetch = useRequestFetch();
|
||||
return requestFetch<ModelType>(ENDPOINT, createOptions({
|
||||
method: 'POST',
|
||||
body: payload,
|
||||
signal: opts.signal,
|
||||
}));
|
||||
}
|
||||
|
||||
export function updateModelType(id: string, payload: Partial<ModelTypePayload>, opts: { signal?: AbortSignal } = {}) {
|
||||
const requestFetch = useRequestFetch();
|
||||
return requestFetch<ModelType>(`${ENDPOINT}/${id}`, createOptions({
|
||||
method: 'PATCH',
|
||||
body: payload,
|
||||
signal: opts.signal,
|
||||
}));
|
||||
}
|
||||
|
||||
export function deleteModelType(id: string, opts: { signal?: AbortSignal } = {}) {
|
||||
const requestFetch = useRequestFetch();
|
||||
return requestFetch<void>(`${ENDPOINT}/${id}`, createOptions({
|
||||
method: 'DELETE',
|
||||
signal: opts.signal,
|
||||
}));
|
||||
}
|
||||
|
||||
export function getModelType(id: string, opts: { signal?: AbortSignal } = {}) {
|
||||
const requestFetch = useRequestFetch();
|
||||
return requestFetch<ModelType>(`${ENDPOINT}/${id}`, createOptions({
|
||||
method: 'GET',
|
||||
signal: opts.signal,
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user