import type { EntityType, AttributeDefinition, AttributeDefinitionFilters, CreateAttributeDefinitionRequest, UpdateAttributeDefinitionRequest, AttributeValues, SetAttributeValuesRequest, } from './types' const API_BASE = '/api' /** * Fetch attribute definitions for an entity type */ export async function fetchAttributeDefinitions( entityType: EntityType, filters?: AttributeDefinitionFilters ): Promise { const params = new URLSearchParams({ entityType }) if (filters?.category) params.set('category', filters.category) if (filters?.isActive !== undefined) params.set('isActive', String(filters.isActive)) const response = await fetch(`${API_BASE}/attribute-definitions?${params}`) if (!response.ok) { throw new Error(`Failed to fetch attribute definitions: ${response.statusText}`) } return response.json() } /** * Fetch a single attribute definition by code */ export async function fetchAttributeDefinitionByCode( code: string ): Promise { const response = await fetch(`${API_BASE}/attribute-definitions/code/${code}`) if (!response.ok) { throw new Error(`Failed to fetch attribute definition: ${response.statusText}`) } return response.json() } /** * Fetch a single attribute definition by ID */ export async function fetchAttributeDefinitionById( id: string ): Promise { const response = await fetch(`${API_BASE}/attribute-definitions/${id}`) if (!response.ok) { throw new Error(`Failed to fetch attribute definition: ${response.statusText}`) } return response.json() } /** * Create a new attribute definition */ export async function createAttributeDefinition( data: CreateAttributeDefinitionRequest ): Promise { const response = await fetch(`${API_BASE}/attribute-definitions`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }) if (!response.ok) { const error = await response.json().catch(() => ({ message: response.statusText })) throw new Error(error.message || 'Failed to create attribute definition') } return response.json() } /** * Update an attribute definition */ export async function updateAttributeDefinition( id: string, data: UpdateAttributeDefinitionRequest ): Promise { const response = await fetch(`${API_BASE}/attribute-definitions/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }) if (!response.ok) { const error = await response.json().catch(() => ({ message: response.statusText })) throw new Error(error.message || 'Failed to update attribute definition') } return response.json() } /** * Delete an attribute definition */ export async function deleteAttributeDefinition(id: string): Promise { const response = await fetch(`${API_BASE}/attribute-definitions/${id}`, { method: 'DELETE', }) if (!response.ok) { throw new Error(`Failed to delete attribute definition: ${response.statusText}`) } } /** * Fetch all entity types */ export async function fetchEntityTypes(): Promise { const response = await fetch(`${API_BASE}/attribute-definitions/meta/entity-types`) if (!response.ok) { throw new Error(`Failed to fetch entity types: ${response.statusText}`) } return response.json() } /** * Fetch categories for an entity type */ export async function fetchAttributeCategories( entityType: EntityType ): Promise { const response = await fetch( `${API_BASE}/attribute-definitions/meta/categories?entityType=${entityType}` ) if (!response.ok) { throw new Error(`Failed to fetch categories: ${response.statusText}`) } return response.json() } /** * Fetch all attribute values for an entity */ export async function fetchAttributeValues( entityType: EntityType, entityId: string ): Promise { const params = new URLSearchParams({ entityType, entityId }) const response = await fetch(`${API_BASE}/attribute-values?${params}`) if (!response.ok) { throw new Error(`Failed to fetch attribute values: ${response.statusText}`) } return response.json() } /** * Fetch a single attribute value */ export async function fetchAttributeValue( entityType: EntityType, entityId: string, code: string ): Promise { const params = new URLSearchParams({ entityType, entityId }) const response = await fetch(`${API_BASE}/attribute-values/${code}?${params}`) if (!response.ok) { throw new Error(`Failed to fetch attribute value: ${response.statusText}`) } return response.json() } /** * Set multiple attribute values at once */ export async function setAttributeValues( entityType: EntityType, entityId: string, attributes: AttributeValues ): Promise { const params = new URLSearchParams({ entityType, entityId }) const body: SetAttributeValuesRequest = { attributes } const response = await fetch(`${API_BASE}/attribute-values?${params}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) if (!response.ok) { const error = await response.json().catch(() => ({ message: response.statusText })) throw new Error(error.message || 'Failed to set attribute values') } } /** * Set a single attribute value */ export async function setAttributeValue( entityType: EntityType, entityId: string, code: string, value: unknown ): Promise { const params = new URLSearchParams({ entityType, entityId }) const response = await fetch(`${API_BASE}/attribute-values/${code}?${params}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ value }), }) if (!response.ok) { const error = await response.json().catch(() => ({ message: response.statusText })) throw new Error(error.message || 'Failed to set attribute value') } } /** * Delete an attribute value */ export async function deleteAttributeValue( entityType: EntityType, entityId: string, code: string ): Promise { const params = new URLSearchParams({ entityType, entityId }) const response = await fetch(`${API_BASE}/attribute-values/${code}?${params}`, { method: 'DELETE', }) if (!response.ok) { throw new Error(`Failed to delete attribute value: ${response.statusText}`) } }