32 lines
818 B
TypeScript
32 lines
818 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).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,
|
|
};
|
|
}
|