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.76 kB
text/typescript
import type { UserEntity, UserCreationData, UserUpdateData } from '../entities/User';
import type { UserPort } from '../ports/UserPort';
import { UserError } from '../services/UserService';
import { httpClient } from '../../../lib/http-client';
/**
* HTTP Adapter implementation for User domain
*/
export class UserAdapter implements UserPort {
private readonly endpoint = '/user';
async getAll(): Promise<UserEntity[]> {
try {
const response = await httpClient.get<UserEntity[]>(this.endpoint);
return response.data;
} catch (error) {
throw new UserError('REQUEST_ERROR', `Failed to fetch user list`);
}
}
async getById(id: string): Promise<UserEntity> {
try {
const response = await httpClient.get<UserEntity>(`${this.endpoint}/${id}`);
return response.data;
} catch (error) {
throw new UserError('REQUEST_ERROR', `Failed to fetch user with id ${id}`);
}
}
async create(data: UserCreationData): Promise<UserEntity> {
try {
const response = await httpClient.post<UserEntity>(this.endpoint, data);
return response.data;
} catch (error) {
throw new UserError('REQUEST_ERROR', `Failed to create user`);
}
}
async update(id: string, data: UserUpdateData): Promise<UserEntity> {
try {
const response = await httpClient.put<UserEntity>(`${this.endpoint}/${id}`, data);
return response.data;
} catch (error) {
throw new UserError('REQUEST_ERROR', `Failed to update user with id ${id}`);
}
}
async delete(id: string): Promise<void> {
try {
await httpClient.delete(`${this.endpoint}/${id}`);
} catch (error) {
throw new UserError('REQUEST_ERROR', `Failed to delete user with id ${id}`);
}
}
}