recipes/web/repo/ingredient/rest.ts

36 lines
951 B
TypeScript

import { Lang } from "../../context.ts";
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(lang: Lang): Promise<Ingredient[]> {
const url = new URL("http://localhost:33333/api/ingredients");
url.searchParams.set("lang", lang);
const res = await fetch(
url.toString(),
{ headers: { "content-type": "application/json" } },
);
const restIngrs: RestIngredient[] = await res.json();
return restIngrs.map(intoAppIngredient).sort((a, b) =>
a.name.localeCompare(b.name)
);
}
}
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,
};
}