ruch
Version:
Revolutionary React TypeScript CLI with hexagonal architecture & AI-powered development assistance. Create maintainable, scalable applications with domain-driven design and integrated AI tooling.
55 lines (49 loc) • 1.85 kB
text/typescript
import type { ProductEntity, ProductCreationData, ProductUpdateData } from '../entities/Product';
import type { ProductPort } from '../ports/ProductPort';
import { ProductError } from '../services/ProductService';
import { httpClient } from '../../../lib/http-client';
/**
* HTTP Adapter implementation for Product domain
*/
export class ProductAdapter implements ProductPort {
private readonly endpoint = '/product';
async getAll(): Promise<ProductEntity[]> {
try {
const response = await httpClient.get<ProductEntity[]>(this.endpoint);
return response.data;
} catch (error) {
throw new ProductError('REQUEST_ERROR', `Failed to fetch product list`);
}
}
async getById(id: string): Promise<ProductEntity> {
try {
const response = await httpClient.get<ProductEntity>(`${this.endpoint}/${id}`);
return response.data;
} catch (error) {
throw new ProductError('REQUEST_ERROR', `Failed to fetch product with id ${id}`);
}
}
async create(data: ProductCreationData): Promise<ProductEntity> {
try {
const response = await httpClient.post<ProductEntity>(this.endpoint, data);
return response.data;
} catch (error) {
throw new ProductError('REQUEST_ERROR', `Failed to create product`);
}
}
async update(id: string, data: ProductUpdateData): Promise<ProductEntity> {
try {
const response = await httpClient.put<ProductEntity>(`${this.endpoint}/${id}`, data);
return response.data;
} catch (error) {
throw new ProductError('REQUEST_ERROR', `Failed to update product with id ${id}`);
}
}
async delete(id: string): Promise<void> {
try {
await httpClient.delete(`${this.endpoint}/${id}`);
} catch (error) {
throw new ProductError('REQUEST_ERROR', `Failed to delete product with id ${id}`);
}
}
}