recipes/web/repo/ingredient/rest.ts

36 lines
951 B
TypeScript
Raw Normal View History

2022-05-28 23:28:07 +03:00
import { Lang } from "../../context.ts";
2022-05-27 01:08:25 +03:00
import { Ingredient } from "../../domain/ingredient/types.ts";
import { RestLang } from "../misc_types.ts";
import { IngredientRepo } from "./types.ts";
export class RestIngredientRepo implements IngredientRepo {
2022-05-28 23:28:07 +03:00
async fetchIngredients(lang: Lang): Promise<Ingredient[]> {
const url = new URL("http://localhost:33333/api/ingredients");
url.searchParams.set("lang", lang);
2022-05-27 01:08:25 +03:00
const res = await fetch(
2022-05-28 23:28:07 +03:00
url.toString(),
2022-05-27 01:08:25 +03:00
{ headers: { "content-type": "application/json" } },
);
const restIngrs: RestIngredient[] = await res.json();
2022-05-27 23:07:07 +03:00
return restIngrs.map(intoAppIngredient).sort((a, b) =>
a.name.localeCompare(b.name)
);
2022-05-27 01:08:25 +03:00
}
}
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,
};
}