Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | 1x 1x 1x 1x 44x 44x 44x 1x 1x 1x 16x 16x 16x 16x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 9x 9x 2x 7x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 1x 1x 12x 12x 12x 12x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x | import type { RecipeOut, Recipe, MealOut, Meal, MealRecipe, Ingredient as DomainIngredient, ShoppingList, ShoppingListItem, ShoppingListItemWithRefs, MealInput } from './types'
import type { components } from '@/api/types'
export function toDate(value: string | Date | null | undefined): Date | null {
if (value === null || value === undefined) return null
return typeof value === 'string' ? new Date(value) : value
}
// Small helper to decode optional lookup maps without repeating loops everywhere
export function decodeLookup<TIn, TOut>(
raw: Record<string, TIn> | null | undefined,
decode: (v: TIn) => TOut
): Record<string, TOut> | undefined {
if (!raw) return undefined
const out: Record<string, TOut> = {}
for (const key of Object.keys(raw)) {
if (!Object.prototype.hasOwnProperty.call(raw, key)) continue
const maybe = raw[key]
if (maybe === undefined) continue
const decoded = decode(maybe)
out[String(key)] = decoded
}
return out
}
export function decodeRecipe(r: RecipeOut | null | undefined): Recipe {
if (!r) throw new Error('Invalid recipe payload')
return {
...r,
dateCreated: toDate(r.dateCreated),
dateHidden: toDate(r.dateHidden),
}
}
export function decodeMeal(m: MealOut | null | undefined): Meal {
if (!m) throw new Error('Invalid meal payload')
const recipes = Array.isArray(m.recipes)
? m.recipes.map((mr) => decodeMealRecipe(mr))
: []
return {
...m,
suggestedDate: toDate(m.suggestedDate),
purchaseDate: toDate(m.purchaseDate),
consumedDate: toDate(m.consumedDate),
recipes,
chefs: m.chefs ?? [],
consumers: m.consumers ?? [],
cleanup: m.cleanup ?? [],
extraIngredients: m.extraIngredients ?? [],
}
}
function decodeMealRecipe(mr: components['schemas']['MealRecipe-Output'] | null | undefined): MealRecipe {
if (!mr) throw new Error('Invalid meal recipe payload')
return {
...mr,
recipe: mr.recipe ? decodeRecipe(mr.recipe) : null,
}
}
export function decodeIngredient(i: components['schemas']['Ingredient'] | null | undefined): DomainIngredient {
if (!i) throw new Error('Invalid ingredient payload')
// API guarantees quantity is a number; pass through
return { ...i }
}
export function decodeIngredients(list: components['schemas']['Ingredient'][] | null | undefined): DomainIngredient[] {
if (!Array.isArray(list)) return []
return list.map((i) => decodeIngredient(i))
}
function decodeShoppingListItem(i: components['schemas']['ShoppingListItem'] | null | undefined): ShoppingListItem {
if (!i) throw new Error('Invalid shopping list item payload')
return {
...i,
createdDate: toDate(i.createdDate),
}
}
export function decodeShoppingListItems(list: components['schemas']['ShoppingListItem'][] | null | undefined): ShoppingListItemWithRefs[] {
if (!Array.isArray(list)) return []
// Build a new array with item clones to allow optional refs to be attached later
return list.map((raw) => ({ ...decodeShoppingListItem(raw) }))
}
export function decodeShoppingList(v: components['schemas']['ShoppingList'] | null | undefined): ShoppingList {
if (!v) throw new Error('Invalid shopping list payload')
const { items: rawItems, ...rest } = v
const items = Array.isArray(rawItems) ? decodeShoppingListItems(rawItems) : undefined
return {
...rest,
createdDate: toDate(v.createdDate),
...(items ? { items } : {}),
}
}
// Helper to convert domain Meal to MealInput, keeping Date→string conversion in boundary
export function toMealInput(meal: Meal): MealInput {
return {
id: meal.id,
suggestedDate: meal.suggestedDate ? meal.suggestedDate.toISOString() : new Date().toISOString(),
consumedDate: meal.consumedDate ? meal.consumedDate.toISOString() : null,
purchaseDate: meal.purchaseDate ? meal.purchaseDate.toISOString() : null,
chefs: meal.chefs,
cleanup: meal.cleanup,
consumers: meal.consumers,
recipes: meal.recipes.map((r) => ({
mealId: r.mealId,
recipeId: r.recipeId,
servings: r.servings,
recipe: null,
})),
extraIngredients: meal.extraIngredients,
}
}
|