@crvouga/mockingbird-service-edamam 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/README.md +125 -0
- package/dist/chunk-DWHV3RH2.js +3485 -0
- package/dist/chunk-DWHV3RH2.js.map +7 -0
- package/dist/chunk-XGIH4NN7.js +355 -0
- package/dist/chunk-XGIH4NN7.js.map +7 -0
- package/dist/cli.js +19 -0
- package/dist/cli.js.map +7 -0
- package/dist/index.d.ts +988 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +7 -0
- package/dist/server.d.ts +1294 -0
- package/dist/server.js +12 -0
- package/dist/server.js.map +7 -0
- package/package.json +89 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# @crvouga/mockingbird-service-edamam
|
|
2
|
+
|
|
3
|
+
Stateful mock of the **Edamam** APIs our apps call, answering from a built-in food and recipe
|
|
4
|
+
corpus: the Food Database v2 parser (text and UPC), nutrients and image recognition, Nutrition
|
|
5
|
+
Analysis (`nutrition-data`, `nutrition-details`), Recipe Search v2 (search with filters and
|
|
6
|
+
`_cont` paging, by URI, by id), the Meal Planner v1 `select`, and Shopping List v2. Nutrition
|
|
7
|
+
logging, barcode scans, photo logging, recipe search, meal plans and grocery lists then work in
|
|
8
|
+
tests without Edamam keys, quotas or network.
|
|
9
|
+
|
|
10
|
+
- Operation coverage: [SUPPORT.md](https://github.com/crvouga/mockingbird/blob/main/packages/service/edamam/SUPPORT.md)
|
|
11
|
+
- The contract (`openapi.yaml`) is hand-authored from Edamam's per-API docs and our consumers:
|
|
12
|
+
`metrics/adapters/outbound/edamam-nutrition.adapter.ts`,
|
|
13
|
+
`meal-planning/adapters/outbound/edamam-meal-planning.adapter.ts`, and the Makor chat
|
|
14
|
+
service's `tools/nutrition/client.py`.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install -D @crvouga/mockingbird-service-edamam
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
ESM only. Node >= 22 or Bun >= 1.2. No native dependencies. Serve it with
|
|
23
|
+
`npx mockingbird-edamam serve`, `createServer` from `./server` (Node), or `createRuntime` with
|
|
24
|
+
any Fetch server.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
Both backend adapters hardcode `https://api.edamam.com` (seam: a base-URL env for
|
|
29
|
+
`EDAMAM_BASE_URL` / `EDAMAM_MEAL_BASE_URL`, and the Makor client's `EDAMAM_BASE_URL`). Keys can be
|
|
30
|
+
any values (`EDAMAM_FOOD_APP_ID/KEY` or `EDAMAM_APP_ID/KEY`, `EDAMAM_MEAL_APP_ID/KEY`, the Makor
|
|
31
|
+
`edamam_*` settings); without them our adapters report `unavailable` and never call out.
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npx mockingbird-edamam serve --port 8824
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { createRuntime } from "@crvouga/mockingbird-service-edamam"
|
|
39
|
+
|
|
40
|
+
const edamam = createRuntime()
|
|
41
|
+
const parsed = await edamam.fetch(
|
|
42
|
+
new Request(
|
|
43
|
+
"http://edamam.test/api/food-database/v2/parser?app_id=a&app_key=k&ingr=2%20large%20eggs&nutrition-type=logging",
|
|
44
|
+
),
|
|
45
|
+
)
|
|
46
|
+
// → {text, parsed: [{food: {foodId: "food_egg", label: "Egg", nutrients: {ENERC_KCAL: 143, …}},
|
|
47
|
+
// quantity: 2, measure: {uri: "…#Measure_large", label: "Large", weight: 50}}], hints: […]}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Routes
|
|
51
|
+
|
|
52
|
+
Every call takes `app_id` and `app_key` as query parameters (the meal planner and shopping list
|
|
53
|
+
also accept `Authorization: Basic app_id:app_key`, which our adapter sends).
|
|
54
|
+
|
|
55
|
+
| Route | Behaviour |
|
|
56
|
+
| --- | --- |
|
|
57
|
+
| `GET /api/food-database/v2/parser` | `ingr` is parsed into a leading quantity ("2", "1/2", "a", "two"), a measure the food has ("cup", "large", "slice", "g", "oz") and the best-matching food → `parsed[]`; every food sharing a word → `hints[]` with `measures`. Filters: `categoryLabel` (`food` drops meals), `category`, `health` (repeatable), `calories` (per 100 g). `upc` looks up a packaged food (unknown UPC 404). |
|
|
58
|
+
| `POST /api/food-database/v2/nutrients` | `{ingredients: [{quantity, measureURI, foodId}]}` → `calories`, `totalWeight`, `totalNutrients`, `totalDaily`, `dietLabels`, `healthLabels`, `ingredients[].parsed[]`; an unknown food or measure is 422. |
|
|
59
|
+
| `POST /api/food-database/nutrients-from-image?beta=true` | `{image: data URL or http URL}` → `{parsed: {food, quantity, measure}, recipe: {label, calories, totalNutrients}}`, deterministic per image (or pinned with `PUT /__admin/vision`). |
|
|
60
|
+
| `GET /api/nutrition-data?ingr=` | One ingredient line; unparsable is 422. |
|
|
61
|
+
| `POST /api/nutrition-details` | `{ingr: [...], title?, yield?}`; no lines is 422, any unparsable line 555. |
|
|
62
|
+
| `GET /api/recipes/v2` | `type` required; `q`, `health`, `diet`, `mealType`, `dishType`, `cuisineType`, `excluded` (repeatable), `calories` and `nutrients[CODE]` ranges per serving, `time`, `random`, `imageSize`. 20 per page; `_links.next.href` carries `_cont`. |
|
|
63
|
+
| `GET /api/recipes/v2/by-uri` | Up to 20 `uri` parameters; unknown URIs are skipped. |
|
|
64
|
+
| `GET /api/recipes/v2/{id}` | One recipe (the `_links.self` target). |
|
|
65
|
+
| `POST /api/meal-planner/v1/{app_id}/select` | `{size, plan: {accept, fit, exclude, sections: {Breakfast: {…}, …}}}` → `{status: OK \| INCOMPLETE, selection: [{sections: {<name>: {assigned, _links}}}]}`. Each section gets a recipe satisfying its and the plan's `accept` predicates (`health`, `meal`, `dish`), its per-serving `fit`, and `exclude`, varying by day; an unfillable section has no `assigned` and the status is `INCOMPLETE`. A plan without sections is 400. |
|
|
66
|
+
| `POST /api/shopping-list/v2` | `{entries: [{quantity, measure?: Measure_serving, item: recipe uri}]}` → ingredients aggregated per food in grams (`Measure_serving` scales by servings over the recipe's yield); `?shopping-cart=true&beta=true` adds `_links.shopping-cart`. |
|
|
67
|
+
|
|
68
|
+
Errors: Food Database and Nutrition Analysis answer `{status: "error", error, message}`;
|
|
69
|
+
Recipe Search, Meal Planner and Shopping List answer `[{errorCode, message, params}]`.
|
|
70
|
+
|
|
71
|
+
**Corpus** (`DEFAULT_FOODS`, `DEFAULT_RECIPES`): 18 foods with per-100 g nutrients and measures
|
|
72
|
+
(including a meal, two packaged products with UPCs `850000000012` and `850000000036`, and one
|
|
73
|
+
UPC `850000000029` with no nutrition data) and 8 recipes built from them, so recipe totals,
|
|
74
|
+
per-serving values and shopping lists agree with the food database.
|
|
75
|
+
|
|
76
|
+
### Admin (beyond the standard contract)
|
|
77
|
+
|
|
78
|
+
| Route | Effect |
|
|
79
|
+
| --- | --- |
|
|
80
|
+
| `GET` / `POST /__admin/foods` | List foods, or add one (`{foodId, label, nutrients, measures: [{uri, label, weight}], upc?, brand?, category?, categoryLabel?, healthLabels?}`). |
|
|
81
|
+
| `GET` / `POST /__admin/recipes` | List recipes, or add one (`{id, label, yield, ingredients: [{foodId, quantity, measure, text}], mealType?, dishType?, cuisineType?, dietLabels?, healthLabels?, cautions?, totalTime?}`). |
|
|
82
|
+
| `PUT /__admin/vision` | `{foodId, quantity?, measure?}` pins what image recognition returns; `{notFound: true}` recognises nothing; `null` restores the default. |
|
|
83
|
+
| `GET/PUT /__admin/settings` | `{apps?: [{appId, appKey}], requireAccountUser?}`. |
|
|
84
|
+
|
|
85
|
+
Fault presets (`POST /__admin/faults {"preset": "<name>", "count"?: n}`): `rate_limited` (429),
|
|
86
|
+
`payment_required` (402), `unauthorized` (401), `server_error` (500), `parser_schema_drift`,
|
|
87
|
+
`recipe_quality` (555), `vision_not_found`, `meal_plan_incomplete`, `meal_plan_timeout`,
|
|
88
|
+
`slow` (12 s, past our 10 s timeouts), `connection_drop`.
|
|
89
|
+
|
|
90
|
+
### Namespaces
|
|
91
|
+
|
|
92
|
+
`x-mockingbird-namespace`, a `/ns/<name>` prefix on the base URL (works for the nutrition adapter
|
|
93
|
+
and the Makor client, which concatenate paths; the meal adapter resolves paths with
|
|
94
|
+
`new URL(endpoint, base)`, which drops a prefix), or by application id:
|
|
95
|
+
`PUT /__admin/credentials {"credentials": {"<app_id>": "<namespace>"}}`.
|
|
96
|
+
|
|
97
|
+
### Deliberately not modelled
|
|
98
|
+
|
|
99
|
+
- Edamam's NLP and databases: the parser understands leading quantities, known measures and
|
|
100
|
+
corpus food names only; real food and recipe content needs a recorded corpus.
|
|
101
|
+
- Food Database autocomplete, brand search and `nutrients` qualifiers; Recipe Search user
|
|
102
|
+
recipes (`type=user`), `field=` projection and images other than the URL.
|
|
103
|
+
- Meal Planner plan-level `fit` beyond a day-calorie check, `mark` weighting, nested
|
|
104
|
+
sub-sections, and shopping-cart checkout pages.
|
|
105
|
+
|
|
106
|
+
## API
|
|
107
|
+
|
|
108
|
+
| Export | Kind | Description |
|
|
109
|
+
| --- | --- | --- |
|
|
110
|
+
| `EdamamAPI` | class | The in-process mock: `fetch(request)`, `reset()`, `addFood(food)`, `addRecipe(seed)`, `recipes()`. Options: `sqlite`, `now`, `namespace`, `foods`, `recipes`, `settings`. |
|
|
111
|
+
| `createRuntime` | function | The mock with the full service contract (health, admin, namespaces, credentials, presets). Options: `foods`, `recipes`, `settings`, `clock`, `seed`, `adminKey`, `onLog`, `sqlite`. |
|
|
112
|
+
| `EDAMAM_PRESETS` | object | Every named fault preset. |
|
|
113
|
+
| `EDAMAM_NAMESPACE` | string | The service name, `"edamam"`. |
|
|
114
|
+
| `ACCOUNT_USER_HEADER` | string | `edamam-account-user`. |
|
|
115
|
+
| `DEFAULT_FOODS`, `DEFAULT_RECIPES` | arrays | The built-in corpus. |
|
|
116
|
+
| `MEASURE_URI`, `RECIPE_URI`, `NUTRIENTS` | values | Edamam's measure and recipe URI prefixes, and the nutrient codes served. |
|
|
117
|
+
| `buildRecipe` | function | A recipe in Recipe Search v2 shape from a seed and the food corpus. |
|
|
118
|
+
| `parseLine` | function | The ingredient-line parser. |
|
|
119
|
+
| `perServing` | function | A recipe's per-serving value of a nutrient code. |
|
|
120
|
+
| `appIdCredential` | function | The `app_id` a request carries (how credentials map to namespaces). |
|
|
121
|
+
| `foodError`, `recipeErrors` | functions | Build each API family's error response. |
|
|
122
|
+
| `document`, `operationIds`, `supportedOperationIds` | values | The OpenAPI contract and its operation ids. |
|
|
123
|
+
| `createServer`, `serveTarget`, `DEFAULT_PORT` (`./server`) | Node | Serve over `node:http`; the `serve` CLI target (`--app`, `--require-account-user`); port 8824. |
|
|
124
|
+
|
|
125
|
+
Part of [mockingbird](https://github.com/crvouga/mockingbird).
|