Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
interface BasketBody {
basket_items: string[]
}
interface BasketResponse {
basket_items: string[]
}
export class HTTPBasketRepository implements BasketRepository {
private endpoint = 'https://www.fakeapi.com/baskets'
constructor(private httpClient: HTTPClient) {
}
getBasket(): Promise<Basket> {
return this.httpClient.get<BasketResponse>(this.endpoint)
.then(this.mapToBasket)
}
save(basket: Basket): Promise<void> {
return this.httpClient.post<BasketBody>(this.endpoint, this.mapToBookBody(basket))
}
private mapToBasket(basketResponse: BasketResponse): Basket {
const basket = new Basket()
basketResponse.basket_items.forEach(item => basket.add(new Book(item)))
return basket
}
private mapToBookBody(basket: Basket): BasketBody {
return { basket_items: basket.items.map(item => item.title) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.