recipes/web/repo/ingredient/rest.ts

30 lines
762 B
TypeScript

import { Ingredient } from "../../domain/ingredient/types.ts";
import { RestLang } from "../misc_types.ts";
import { IngredientRepo } from "./types.ts";
export class RestIngredientRepo implements IngredientRepo {
async fetchIngredients(): Promise<Ingredient[]> {
const res = await fetch(
"http://localhost:33333/api/ingredients",
{ headers: { "content-type": "application/json" } },
);
const restIngrs: RestIngredient[] = await res.json();
return restIngrs.map(intoAppIngredient);
}
}
export interface RestIngredient {
readonly key: string;
readonly lang: RestLang;
readonly name: string;
}
export function intoAppIngredient(rest: RestIngredient): Ingredient {
return {
key: rest.key,
name: rest.name,
};
}