@tbs-fitness/contract 0.4.0 → 0.6.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.
Files changed (60) hide show
  1. package/README.md +25 -0
  2. package/dist/cjs/auth/auth.dto.d.ts +4 -4
  3. package/dist/cjs/auth/index.d.ts +1 -0
  4. package/dist/cjs/auth/index.js +1 -0
  5. package/dist/cjs/auth/otp.dto.d.ts +45 -0
  6. package/dist/cjs/auth/otp.dto.js +39 -0
  7. package/dist/cjs/client/client-auth.dto.d.ts +81 -0
  8. package/dist/cjs/client/client-auth.dto.js +49 -0
  9. package/dist/cjs/client/index.d.ts +2 -0
  10. package/dist/cjs/client/index.js +18 -0
  11. package/dist/cjs/coach/coach-auth.dto.d.ts +0 -40
  12. package/dist/cjs/coach/coach-auth.dto.js +1 -30
  13. package/dist/cjs/coach/index.d.ts +1 -0
  14. package/dist/cjs/coach/index.js +1 -0
  15. package/dist/cjs/enums/index.d.ts +40 -2
  16. package/dist/cjs/enums/index.js +58 -3
  17. package/dist/cjs/feature-flags/feature-flag.dto.d.ts +4 -4
  18. package/dist/cjs/feature-flags/feature-flag.dto.js +1 -1
  19. package/dist/cjs/food/client-food.dto.d.ts +49 -0
  20. package/dist/cjs/food/client-food.dto.js +26 -0
  21. package/dist/cjs/food/food-import.dto.d.ts +160 -0
  22. package/dist/cjs/food/food-import.dto.js +110 -0
  23. package/dist/cjs/food/food-suggestion.dto.d.ts +374 -0
  24. package/dist/cjs/food/food-suggestion.dto.js +170 -0
  25. package/dist/cjs/food/food.dto.d.ts +339 -0
  26. package/dist/cjs/food/food.dto.js +256 -0
  27. package/dist/cjs/food/index.d.ts +5 -0
  28. package/dist/cjs/food/index.js +21 -0
  29. package/dist/cjs/food/nutrition.d.ts +75 -0
  30. package/dist/cjs/food/nutrition.js +158 -0
  31. package/dist/esm/auth/auth.dto.d.ts +4 -4
  32. package/dist/esm/auth/index.d.ts +1 -0
  33. package/dist/esm/auth/index.js +1 -0
  34. package/dist/esm/auth/otp.dto.d.ts +45 -0
  35. package/dist/esm/auth/otp.dto.js +36 -0
  36. package/dist/esm/client/client-auth.dto.d.ts +81 -0
  37. package/dist/esm/client/client-auth.dto.js +46 -0
  38. package/dist/esm/client/index.d.ts +2 -0
  39. package/dist/esm/client/index.js +2 -0
  40. package/dist/esm/coach/coach-auth.dto.d.ts +0 -40
  41. package/dist/esm/coach/coach-auth.dto.js +1 -30
  42. package/dist/esm/coach/index.d.ts +1 -0
  43. package/dist/esm/coach/index.js +1 -0
  44. package/dist/esm/enums/index.d.ts +40 -2
  45. package/dist/esm/enums/index.js +57 -2
  46. package/dist/esm/feature-flags/feature-flag.dto.d.ts +4 -4
  47. package/dist/esm/feature-flags/feature-flag.dto.js +1 -1
  48. package/dist/esm/food/client-food.dto.d.ts +49 -0
  49. package/dist/esm/food/client-food.dto.js +23 -0
  50. package/dist/esm/food/food-import.dto.d.ts +160 -0
  51. package/dist/esm/food/food-import.dto.js +107 -0
  52. package/dist/esm/food/food-suggestion.dto.d.ts +374 -0
  53. package/dist/esm/food/food-suggestion.dto.js +167 -0
  54. package/dist/esm/food/food.dto.d.ts +339 -0
  55. package/dist/esm/food/food.dto.js +253 -0
  56. package/dist/esm/food/index.d.ts +5 -0
  57. package/dist/esm/food/index.js +5 -0
  58. package/dist/esm/food/nutrition.d.ts +75 -0
  59. package/dist/esm/food/nutrition.js +144 -0
  60. package/package.json +21 -1
@@ -0,0 +1,339 @@
1
+ import { z } from 'zod';
2
+ export declare const MAX_FOOD_NAME_LENGTH = 120;
3
+ export declare const MAX_FOOD_NOTES_LENGTH = 1000;
4
+ export declare const MAX_FOOD_METADATA_VALUES = 50;
5
+ export declare const MAX_FOOD_SEARCH_LENGTH = 100;
6
+ export declare const foodIdParamSchema: z.ZodUUID;
7
+ export declare const metadataTypeIdParamSchema: z.ZodUUID;
8
+ export declare const metadataValueIdParamSchema: z.ZodUUID;
9
+ export declare const servingSchema: z.ZodObject<{
10
+ label: z.ZodString;
11
+ quantity: z.ZodNumber;
12
+ unit: z.ZodEnum<{
13
+ g: "g";
14
+ ml: "ml";
15
+ }>;
16
+ }, z.core.$strict>;
17
+ export type ServingInput = z.input<typeof servingSchema>;
18
+ /**
19
+ * Nutrient values as entered. `basis` says whether they are per 100 units of
20
+ * the serving unit or for one serving; the API stores them per 100.
21
+ */
22
+ export declare const nutritionInputSchema: z.ZodObject<{
23
+ basis: z.ZodDefault<z.ZodEnum<{
24
+ PER_100: "PER_100";
25
+ PER_SERVING: "PER_SERVING";
26
+ }>>;
27
+ calories: z.ZodNumber;
28
+ protein: z.ZodNumber;
29
+ carbs: z.ZodNumber;
30
+ fat: z.ZodNumber;
31
+ sugar: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
32
+ fiber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
33
+ solubleFiber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
34
+ insolubleFiber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
35
+ }, z.core.$strict>;
36
+ export type NutritionInput = z.input<typeof nutritionInputSchema>;
37
+ export type NutritionBody = z.output<typeof nutritionInputSchema>;
38
+ export declare const createFoodSchema: z.ZodObject<{
39
+ name: z.ZodString;
40
+ nameHi: z.ZodOptional<z.ZodNullable<z.ZodString>>;
41
+ namePa: z.ZodOptional<z.ZodNullable<z.ZodString>>;
42
+ englishAlias: z.ZodOptional<z.ZodNullable<z.ZodString>>;
43
+ serving: z.ZodObject<{
44
+ label: z.ZodString;
45
+ quantity: z.ZodNumber;
46
+ unit: z.ZodEnum<{
47
+ g: "g";
48
+ ml: "ml";
49
+ }>;
50
+ }, z.core.$strict>;
51
+ nutrition: z.ZodObject<{
52
+ basis: z.ZodDefault<z.ZodEnum<{
53
+ PER_100: "PER_100";
54
+ PER_SERVING: "PER_SERVING";
55
+ }>>;
56
+ calories: z.ZodNumber;
57
+ protein: z.ZodNumber;
58
+ carbs: z.ZodNumber;
59
+ fat: z.ZodNumber;
60
+ sugar: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
61
+ fiber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
62
+ solubleFiber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
63
+ insolubleFiber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
64
+ }, z.core.$strict>;
65
+ metadataValueIds: z.ZodDefault<z.ZodArray<z.ZodUUID>>;
66
+ notes: z.ZodOptional<z.ZodNullable<z.ZodString>>;
67
+ }, z.core.$strict>;
68
+ export type CreateFoodInput = z.input<typeof createFoodSchema>;
69
+ export type CreateFoodBody = z.output<typeof createFoodSchema>;
70
+ export declare const updateFoodSchema: z.ZodObject<{
71
+ version: z.ZodNumber;
72
+ name: z.ZodOptional<z.ZodString>;
73
+ nameHi: z.ZodOptional<z.ZodNullable<z.ZodString>>;
74
+ namePa: z.ZodOptional<z.ZodNullable<z.ZodString>>;
75
+ englishAlias: z.ZodOptional<z.ZodNullable<z.ZodString>>;
76
+ serving: z.ZodOptional<z.ZodObject<{
77
+ label: z.ZodString;
78
+ quantity: z.ZodNumber;
79
+ unit: z.ZodEnum<{
80
+ g: "g";
81
+ ml: "ml";
82
+ }>;
83
+ }, z.core.$strict>>;
84
+ nutrition: z.ZodOptional<z.ZodObject<{
85
+ basis: z.ZodDefault<z.ZodEnum<{
86
+ PER_100: "PER_100";
87
+ PER_SERVING: "PER_SERVING";
88
+ }>>;
89
+ calories: z.ZodNumber;
90
+ protein: z.ZodNumber;
91
+ carbs: z.ZodNumber;
92
+ fat: z.ZodNumber;
93
+ sugar: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
94
+ fiber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
95
+ solubleFiber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
96
+ insolubleFiber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
97
+ }, z.core.$strict>>;
98
+ metadataValueIds: z.ZodOptional<z.ZodArray<z.ZodUUID>>;
99
+ notes: z.ZodOptional<z.ZodNullable<z.ZodString>>;
100
+ }, z.core.$strict>;
101
+ export type UpdateFoodInput = z.input<typeof updateFoodSchema>;
102
+ export type UpdateFoodBody = z.output<typeof updateFoodSchema>;
103
+ /**
104
+ * `metadata` filters by value ids: a food matches when it has at least one of
105
+ * the listed values of each type that appears in the list.
106
+ */
107
+ export declare const foodListQuerySchema: z.ZodObject<{
108
+ page: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
109
+ pageSize: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
110
+ search: z.ZodOptional<z.ZodString>;
111
+ metadata: z.ZodPreprocess<z.ZodOptional<z.ZodArray<z.ZodUUID>>, unknown>;
112
+ source: z.ZodOptional<z.ZodEnum<{
113
+ ADMIN: "ADMIN";
114
+ COACH: "COACH";
115
+ CLIENT_SUGGESTION: "CLIENT_SUGGESTION";
116
+ IMPORT: "IMPORT";
117
+ }>>;
118
+ sort: z.ZodDefault<z.ZodEnum<{
119
+ name: "name";
120
+ updatedAt: "updatedAt";
121
+ calories: "calories";
122
+ protein: "protein";
123
+ }>>;
124
+ order: z.ZodDefault<z.ZodEnum<{
125
+ asc: "asc";
126
+ desc: "desc";
127
+ }>>;
128
+ }, z.core.$strip>;
129
+ export type FoodListQuery = z.output<typeof foodListQuerySchema>;
130
+ export declare const similarFoodQuerySchema: z.ZodObject<{
131
+ name: z.ZodString;
132
+ }, z.core.$strip>;
133
+ export type SimilarFoodQuery = z.output<typeof similarFoodQuerySchema>;
134
+ export declare const nutrientValuesResponseSchema: z.ZodObject<{
135
+ calories: z.ZodNumber;
136
+ protein: z.ZodNumber;
137
+ carbs: z.ZodNumber;
138
+ fat: z.ZodNumber;
139
+ sugar: z.ZodNullable<z.ZodNumber>;
140
+ fiber: z.ZodNullable<z.ZodNumber>;
141
+ solubleFiber: z.ZodNullable<z.ZodNumber>;
142
+ insolubleFiber: z.ZodNullable<z.ZodNumber>;
143
+ }, z.core.$strip>;
144
+ export type NutrientValuesResponse = z.output<typeof nutrientValuesResponseSchema>;
145
+ export declare const servingResponseSchema: z.ZodObject<{
146
+ label: z.ZodString;
147
+ quantity: z.ZodNumber;
148
+ unit: z.ZodEnum<{
149
+ g: "g";
150
+ ml: "ml";
151
+ }>;
152
+ }, z.core.$strip>;
153
+ export type ServingResponse = z.output<typeof servingResponseSchema>;
154
+ /** A metadata value as attached to a food. */
155
+ export declare const foodMetadataRefSchema: z.ZodObject<{
156
+ id: z.ZodUUID;
157
+ typeKey: z.ZodString;
158
+ key: z.ZodString;
159
+ label: z.ZodString;
160
+ isActive: z.ZodBoolean;
161
+ }, z.core.$strip>;
162
+ export type FoodMetadataRef = z.output<typeof foodMetadataRefSchema>;
163
+ export declare const userRefSchema: z.ZodObject<{
164
+ id: z.ZodUUID;
165
+ fullName: z.ZodNullable<z.ZodString>;
166
+ }, z.core.$strip>;
167
+ export type UserRef = z.output<typeof userRefSchema>;
168
+ export declare const foodResponseSchema: z.ZodObject<{
169
+ id: z.ZodUUID;
170
+ name: z.ZodString;
171
+ nameHi: z.ZodNullable<z.ZodString>;
172
+ namePa: z.ZodNullable<z.ZodString>;
173
+ englishAlias: z.ZodNullable<z.ZodString>;
174
+ serving: z.ZodObject<{
175
+ label: z.ZodString;
176
+ quantity: z.ZodNumber;
177
+ unit: z.ZodEnum<{
178
+ g: "g";
179
+ ml: "ml";
180
+ }>;
181
+ }, z.core.$strip>;
182
+ per100: z.ZodObject<{
183
+ calories: z.ZodNumber;
184
+ protein: z.ZodNumber;
185
+ carbs: z.ZodNumber;
186
+ fat: z.ZodNumber;
187
+ sugar: z.ZodNullable<z.ZodNumber>;
188
+ fiber: z.ZodNullable<z.ZodNumber>;
189
+ solubleFiber: z.ZodNullable<z.ZodNumber>;
190
+ insolubleFiber: z.ZodNullable<z.ZodNumber>;
191
+ }, z.core.$strip>;
192
+ perServing: z.ZodObject<{
193
+ calories: z.ZodNumber;
194
+ protein: z.ZodNumber;
195
+ carbs: z.ZodNumber;
196
+ fat: z.ZodNumber;
197
+ sugar: z.ZodNullable<z.ZodNumber>;
198
+ fiber: z.ZodNullable<z.ZodNumber>;
199
+ solubleFiber: z.ZodNullable<z.ZodNumber>;
200
+ insolubleFiber: z.ZodNullable<z.ZodNumber>;
201
+ }, z.core.$strip>;
202
+ formulaCalories: z.ZodNumber;
203
+ metadata: z.ZodArray<z.ZodObject<{
204
+ id: z.ZodUUID;
205
+ typeKey: z.ZodString;
206
+ key: z.ZodString;
207
+ label: z.ZodString;
208
+ isActive: z.ZodBoolean;
209
+ }, z.core.$strip>>;
210
+ notes: z.ZodNullable<z.ZodString>;
211
+ source: z.ZodEnum<{
212
+ ADMIN: "ADMIN";
213
+ COACH: "COACH";
214
+ CLIENT_SUGGESTION: "CLIENT_SUGGESTION";
215
+ IMPORT: "IMPORT";
216
+ }>;
217
+ createdBy: z.ZodObject<{
218
+ id: z.ZodUUID;
219
+ fullName: z.ZodNullable<z.ZodString>;
220
+ }, z.core.$strip>;
221
+ suggestionId: z.ZodNullable<z.ZodUUID>;
222
+ importBatchId: z.ZodNullable<z.ZodUUID>;
223
+ version: z.ZodNumber;
224
+ createdAt: z.ZodISODateTime;
225
+ updatedAt: z.ZodISODateTime;
226
+ }, z.core.$strip>;
227
+ export type FoodResponse = z.output<typeof foodResponseSchema>;
228
+ export declare const similarFoodResponseSchema: z.ZodObject<{
229
+ id: z.ZodUUID;
230
+ name: z.ZodString;
231
+ serving: z.ZodObject<{
232
+ label: z.ZodString;
233
+ quantity: z.ZodNumber;
234
+ unit: z.ZodEnum<{
235
+ g: "g";
236
+ ml: "ml";
237
+ }>;
238
+ }, z.core.$strip>;
239
+ per100: z.ZodObject<{
240
+ calories: z.ZodNumber;
241
+ protein: z.ZodNumber;
242
+ carbs: z.ZodNumber;
243
+ fat: z.ZodNumber;
244
+ sugar: z.ZodNullable<z.ZodNumber>;
245
+ fiber: z.ZodNullable<z.ZodNumber>;
246
+ solubleFiber: z.ZodNullable<z.ZodNumber>;
247
+ insolubleFiber: z.ZodNullable<z.ZodNumber>;
248
+ }, z.core.$strip>;
249
+ similarity: z.ZodNumber;
250
+ }, z.core.$strip>;
251
+ export type SimilarFoodResponse = z.output<typeof similarFoodResponseSchema>;
252
+ export declare const foodStatsResponseSchema: z.ZodObject<{
253
+ totalFoods: z.ZodNumber;
254
+ categories: z.ZodArray<z.ZodObject<{
255
+ valueId: z.ZodUUID;
256
+ key: z.ZodString;
257
+ label: z.ZodString;
258
+ count: z.ZodNumber;
259
+ }, z.core.$strip>>;
260
+ pendingSuggestions: z.ZodNumber;
261
+ }, z.core.$strip>;
262
+ export type FoodStatsResponse = z.output<typeof foodStatsResponseSchema>;
263
+ export declare const MAX_METADATA_LABEL_LENGTH = 60;
264
+ export declare const metadataTypeResponseSchema: z.ZodObject<{
265
+ id: z.ZodUUID;
266
+ key: z.ZodString;
267
+ label: z.ZodString;
268
+ selection: z.ZodEnum<{
269
+ single: "single";
270
+ multiple: "multiple";
271
+ }>;
272
+ parentTypeId: z.ZodNullable<z.ZodUUID>;
273
+ isRequired: z.ZodBoolean;
274
+ isSystem: z.ZodBoolean;
275
+ isActive: z.ZodBoolean;
276
+ sortOrder: z.ZodNumber;
277
+ }, z.core.$strip>;
278
+ export type MetadataTypeResponse = z.output<typeof metadataTypeResponseSchema>;
279
+ export declare const metadataTypeListResponseSchema: z.ZodArray<z.ZodObject<{
280
+ id: z.ZodUUID;
281
+ key: z.ZodString;
282
+ label: z.ZodString;
283
+ selection: z.ZodEnum<{
284
+ single: "single";
285
+ multiple: "multiple";
286
+ }>;
287
+ parentTypeId: z.ZodNullable<z.ZodUUID>;
288
+ isRequired: z.ZodBoolean;
289
+ isSystem: z.ZodBoolean;
290
+ isActive: z.ZodBoolean;
291
+ sortOrder: z.ZodNumber;
292
+ }, z.core.$strip>>;
293
+ export declare const metadataValueResponseSchema: z.ZodObject<{
294
+ id: z.ZodUUID;
295
+ typeId: z.ZodUUID;
296
+ key: z.ZodString;
297
+ label: z.ZodString;
298
+ parentValueId: z.ZodNullable<z.ZodUUID>;
299
+ isActive: z.ZodBoolean;
300
+ sortOrder: z.ZodNumber;
301
+ createdAt: z.ZodISODateTime;
302
+ updatedAt: z.ZodISODateTime;
303
+ }, z.core.$strip>;
304
+ export type MetadataValueResponse = z.output<typeof metadataValueResponseSchema>;
305
+ export declare const metadataValueListResponseSchema: z.ZodArray<z.ZodObject<{
306
+ id: z.ZodUUID;
307
+ typeId: z.ZodUUID;
308
+ key: z.ZodString;
309
+ label: z.ZodString;
310
+ parentValueId: z.ZodNullable<z.ZodUUID>;
311
+ isActive: z.ZodBoolean;
312
+ sortOrder: z.ZodNumber;
313
+ createdAt: z.ZodISODateTime;
314
+ updatedAt: z.ZodISODateTime;
315
+ }, z.core.$strip>>;
316
+ export declare const metadataValueListQuerySchema: z.ZodObject<{
317
+ includeInactive: z.ZodOptional<z.ZodPipe<z.ZodEnum<{
318
+ true: "true";
319
+ false: "false";
320
+ }>, z.ZodTransform<boolean, "true" | "false">>>;
321
+ }, z.core.$strip>;
322
+ export type MetadataValueListQuery = z.output<typeof metadataValueListQuerySchema>;
323
+ /** The key is made from the label, so "High Protein" is stored as `high-protein`. */
324
+ export declare const createMetadataValueSchema: z.ZodObject<{
325
+ label: z.ZodString;
326
+ parentValueId: z.ZodOptional<z.ZodNullable<z.ZodUUID>>;
327
+ sortOrder: z.ZodDefault<z.ZodNumber>;
328
+ }, z.core.$strict>;
329
+ export type CreateMetadataValueInput = z.input<typeof createMetadataValueSchema>;
330
+ export type CreateMetadataValueBody = z.output<typeof createMetadataValueSchema>;
331
+ /** The key never changes, so renaming a value keeps every link to it. */
332
+ export declare const updateMetadataValueSchema: z.ZodObject<{
333
+ label: z.ZodOptional<z.ZodString>;
334
+ parentValueId: z.ZodOptional<z.ZodNullable<z.ZodUUID>>;
335
+ sortOrder: z.ZodOptional<z.ZodNumber>;
336
+ isActive: z.ZodOptional<z.ZodBoolean>;
337
+ }, z.core.$strict>;
338
+ export type UpdateMetadataValueInput = z.input<typeof updateMetadataValueSchema>;
339
+ export type UpdateMetadataValueBody = z.output<typeof updateMetadataValueSchema>;
@@ -0,0 +1,253 @@
1
+ import { z } from 'zod';
2
+ import { utcTimestampSchema } from '../common/date-time.schema.js';
3
+ import { paginationQuerySchema } from '../common/pagination.schema.js';
4
+ import { safeTextSchema } from '../common/safe-text.schema.js';
5
+ import { FOOD_METADATA_SELECTIONS, FOOD_SORT_FIELDS, FOOD_SOURCES, NUTRITION_BASES, SERVING_UNITS, SORT_ORDERS, } from '../enums/index.js';
6
+ import { nutrientAmountSchema, nutritionIssues, quantitySchema, toPer100, } from './nutrition.js';
7
+ // Each request has two types. `*Input` is what a client sends, where fields with
8
+ // a default are optional. `*Body` is what the API receives after validation.
9
+ export const MAX_FOOD_NAME_LENGTH = 120;
10
+ export const MAX_FOOD_NOTES_LENGTH = 1000;
11
+ export const MAX_FOOD_METADATA_VALUES = 50;
12
+ export const MAX_FOOD_SEARCH_LENGTH = 100;
13
+ export const foodIdParamSchema = z.uuid();
14
+ export const metadataTypeIdParamSchema = z.uuid();
15
+ export const metadataValueIdParamSchema = z.uuid();
16
+ const foodNameSchema = safeTextSchema(MAX_FOOD_NAME_LENGTH);
17
+ const optionalNameSchema = safeTextSchema(MAX_FOOD_NAME_LENGTH)
18
+ .nullable()
19
+ .optional();
20
+ export const servingSchema = z.strictObject({
21
+ /** As people say it, e.g. "1 cup (200g)". */
22
+ label: safeTextSchema(MAX_FOOD_NAME_LENGTH),
23
+ /** The weight or volume of one serving in `unit`, e.g. 200. */
24
+ quantity: quantitySchema(),
25
+ unit: z.enum(SERVING_UNITS),
26
+ });
27
+ /**
28
+ * Nutrient values as entered. `basis` says whether they are per 100 units of
29
+ * the serving unit or for one serving; the API stores them per 100.
30
+ */
31
+ export const nutritionInputSchema = z.strictObject({
32
+ basis: z.enum(NUTRITION_BASES).default('PER_100'),
33
+ calories: nutrientAmountSchema(),
34
+ protein: nutrientAmountSchema(),
35
+ carbs: nutrientAmountSchema(),
36
+ fat: nutrientAmountSchema(),
37
+ sugar: nutrientAmountSchema().nullable().optional(),
38
+ fiber: nutrientAmountSchema().nullable().optional(),
39
+ solubleFiber: nutrientAmountSchema().nullable().optional(),
40
+ insolubleFiber: nutrientAmountSchema().nullable().optional(),
41
+ });
42
+ const uniqueIds = (ids) => new Set(ids).size === ids.length;
43
+ const metadataValueIdsSchema = z
44
+ .array(z.uuid())
45
+ .max(MAX_FOOD_METADATA_VALUES)
46
+ .refine(uniqueIds, { error: 'Each value may be listed once' });
47
+ /** Adds an issue for every nutrition rule the values break, measured per 100. */
48
+ function checkNutrition(nutrition, servingQuantity, ctx) {
49
+ if (nutrition.basis === 'PER_SERVING' && servingQuantity === undefined) {
50
+ // An update that changes values per serving without a serving uses the
51
+ // stored serving, so the API checks it.
52
+ return;
53
+ }
54
+ const per100 = nutrition.basis === 'PER_SERVING' && servingQuantity !== undefined
55
+ ? toPer100(nutrition, servingQuantity)
56
+ : nutrition;
57
+ for (const issue of nutritionIssues(per100)) {
58
+ ctx.addIssue({
59
+ code: 'custom',
60
+ message: issue.message,
61
+ path: ['nutrition', issue.path],
62
+ });
63
+ }
64
+ }
65
+ const foodFields = {
66
+ name: foodNameSchema,
67
+ nameHi: optionalNameSchema,
68
+ namePa: optionalNameSchema,
69
+ englishAlias: safeTextSchema(200).nullable().optional(),
70
+ serving: servingSchema,
71
+ nutrition: nutritionInputSchema,
72
+ /** Category, subcategory, tags and other labels, by value id. */
73
+ metadataValueIds: metadataValueIdsSchema.default([]),
74
+ notes: safeTextSchema(MAX_FOOD_NOTES_LENGTH).nullable().optional(),
75
+ };
76
+ // Strict objects reject unknown fields, so an attempt to set `source` or
77
+ // `createdBy` through the API fails loudly.
78
+ export const createFoodSchema = z
79
+ .strictObject(foodFields)
80
+ .superRefine((food, ctx) => checkNutrition(food.nutrition, food.serving.quantity, ctx));
81
+ export const updateFoodSchema = z
82
+ .strictObject({
83
+ // The version last read, so that a stale edit is refused rather than lost.
84
+ version: z.number().int().min(1),
85
+ name: foodNameSchema.optional(),
86
+ nameHi: optionalNameSchema,
87
+ namePa: optionalNameSchema,
88
+ englishAlias: safeTextSchema(200).nullable().optional(),
89
+ serving: servingSchema.optional(),
90
+ /** Replaces every nutrient value; omitted nutrients become unknown. */
91
+ nutrition: nutritionInputSchema.optional(),
92
+ /** Replaces every metadata value. */
93
+ metadataValueIds: metadataValueIdsSchema.optional(),
94
+ notes: safeTextSchema(MAX_FOOD_NOTES_LENGTH).nullable().optional(),
95
+ })
96
+ .superRefine((food, ctx) => {
97
+ if (food.nutrition) {
98
+ checkNutrition(food.nutrition, food.serving?.quantity, ctx);
99
+ }
100
+ })
101
+ .refine(({ version: _version, ...changes }) => Object.values(changes).some((value) => value !== undefined), { error: 'Provide at least one field to change' });
102
+ const MAX_FILTER_VALUES = 50;
103
+ /** Accepts `a,b` or a repeated parameter, as query strings carry either. */
104
+ const idListQuerySchema = z.preprocess((value) => {
105
+ if (typeof value === 'string') {
106
+ return value === ''
107
+ ? undefined
108
+ : value
109
+ .split(',')
110
+ .map((part) => part.trim())
111
+ .filter(Boolean);
112
+ }
113
+ // Anything other than a string or a list of strings is left for the schema
114
+ // to reject.
115
+ return value;
116
+ }, z.array(z.uuid()).max(MAX_FILTER_VALUES).optional());
117
+ const searchQuerySchema = z
118
+ .string()
119
+ .trim()
120
+ .min(1)
121
+ .max(MAX_FOOD_SEARCH_LENGTH)
122
+ .optional();
123
+ /**
124
+ * `metadata` filters by value ids: a food matches when it has at least one of
125
+ * the listed values of each type that appears in the list.
126
+ */
127
+ export const foodListQuerySchema = paginationQuerySchema.extend({
128
+ search: searchQuerySchema,
129
+ metadata: idListQuerySchema,
130
+ source: z.enum(FOOD_SOURCES).optional(),
131
+ sort: z.enum(FOOD_SORT_FIELDS).default('name'),
132
+ order: z.enum(SORT_ORDERS).default('asc'),
133
+ });
134
+ export const similarFoodQuerySchema = z.object({
135
+ name: safeTextSchema(MAX_FOOD_NAME_LENGTH),
136
+ });
137
+ // Responses
138
+ export const nutrientValuesResponseSchema = z.object({
139
+ calories: z.number(),
140
+ protein: z.number(),
141
+ carbs: z.number(),
142
+ fat: z.number(),
143
+ sugar: z.number().nullable(),
144
+ fiber: z.number().nullable(),
145
+ solubleFiber: z.number().nullable(),
146
+ insolubleFiber: z.number().nullable(),
147
+ });
148
+ export const servingResponseSchema = z.object({
149
+ label: z.string(),
150
+ quantity: z.number(),
151
+ unit: z.enum(SERVING_UNITS),
152
+ });
153
+ /** A metadata value as attached to a food. */
154
+ export const foodMetadataRefSchema = z.object({
155
+ id: z.uuid(),
156
+ typeKey: z.string(),
157
+ key: z.string(),
158
+ label: z.string(),
159
+ isActive: z.boolean(),
160
+ });
161
+ export const userRefSchema = z.object({
162
+ id: z.uuid(),
163
+ fullName: z.string().nullable(),
164
+ });
165
+ export const foodResponseSchema = z.object({
166
+ id: z.uuid(),
167
+ name: z.string(),
168
+ nameHi: z.string().nullable(),
169
+ namePa: z.string().nullable(),
170
+ englishAlias: z.string().nullable(),
171
+ serving: servingResponseSchema,
172
+ /** Per 100 g, or per 100 ml for a serving in ml. */
173
+ per100: nutrientValuesResponseSchema,
174
+ perServing: nutrientValuesResponseSchema,
175
+ /** Calories from the macros per 100 units: (protein + carbs) × 4 + fat × 9. */
176
+ formulaCalories: z.number(),
177
+ metadata: z.array(foodMetadataRefSchema),
178
+ notes: z.string().nullable(),
179
+ source: z.enum(FOOD_SOURCES),
180
+ createdBy: userRefSchema,
181
+ suggestionId: z.uuid().nullable(),
182
+ importBatchId: z.uuid().nullable(),
183
+ version: z.number().int(),
184
+ createdAt: utcTimestampSchema,
185
+ updatedAt: utcTimestampSchema,
186
+ });
187
+ export const similarFoodResponseSchema = z.object({
188
+ id: z.uuid(),
189
+ name: z.string(),
190
+ serving: servingResponseSchema,
191
+ per100: nutrientValuesResponseSchema,
192
+ /** 0 to 1; 1 is the same name. */
193
+ similarity: z.number(),
194
+ });
195
+ export const foodStatsResponseSchema = z.object({
196
+ totalFoods: z.number().int(),
197
+ categories: z.array(z.object({
198
+ valueId: z.uuid(),
199
+ key: z.string(),
200
+ label: z.string(),
201
+ count: z.number().int(),
202
+ })),
203
+ pendingSuggestions: z.number().int(),
204
+ });
205
+ // Metadata
206
+ export const MAX_METADATA_LABEL_LENGTH = 60;
207
+ export const metadataTypeResponseSchema = z.object({
208
+ id: z.uuid(),
209
+ key: z.string(),
210
+ label: z.string(),
211
+ selection: z.enum(FOOD_METADATA_SELECTIONS),
212
+ parentTypeId: z.uuid().nullable(),
213
+ isRequired: z.boolean(),
214
+ isSystem: z.boolean(),
215
+ isActive: z.boolean(),
216
+ sortOrder: z.number().int(),
217
+ });
218
+ export const metadataTypeListResponseSchema = z.array(metadataTypeResponseSchema);
219
+ export const metadataValueResponseSchema = z.object({
220
+ id: z.uuid(),
221
+ typeId: z.uuid(),
222
+ key: z.string(),
223
+ label: z.string(),
224
+ parentValueId: z.uuid().nullable(),
225
+ isActive: z.boolean(),
226
+ sortOrder: z.number().int(),
227
+ createdAt: utcTimestampSchema,
228
+ updatedAt: utcTimestampSchema,
229
+ });
230
+ export const metadataValueListResponseSchema = z.array(metadataValueResponseSchema);
231
+ export const metadataValueListQuerySchema = z.object({
232
+ includeInactive: z
233
+ .enum(['true', 'false'])
234
+ .transform((value) => value === 'true')
235
+ .optional(),
236
+ });
237
+ const sortOrderSchema = z.number().int().min(0).max(10000);
238
+ /** The key is made from the label, so "High Protein" is stored as `high-protein`. */
239
+ export const createMetadataValueSchema = z.strictObject({
240
+ label: safeTextSchema(MAX_METADATA_LABEL_LENGTH),
241
+ /** Required for a type with a parent, such as a subcategory's category. */
242
+ parentValueId: z.uuid().nullable().optional(),
243
+ sortOrder: sortOrderSchema.default(0),
244
+ });
245
+ /** The key never changes, so renaming a value keeps every link to it. */
246
+ export const updateMetadataValueSchema = z
247
+ .strictObject({
248
+ label: safeTextSchema(MAX_METADATA_LABEL_LENGTH).optional(),
249
+ parentValueId: z.uuid().nullable().optional(),
250
+ sortOrder: sortOrderSchema.optional(),
251
+ isActive: z.boolean().optional(),
252
+ })
253
+ .refine((changes) => Object.values(changes).some((value) => value !== undefined), { error: 'Provide at least one field to change' });
@@ -0,0 +1,5 @@
1
+ export * from './client-food.dto.js';
2
+ export * from './food.dto.js';
3
+ export * from './food-import.dto.js';
4
+ export * from './food-suggestion.dto.js';
5
+ export * from './nutrition.js';
@@ -0,0 +1,5 @@
1
+ export * from './client-food.dto.js';
2
+ export * from './food.dto.js';
3
+ export * from './food-import.dto.js';
4
+ export * from './food-suggestion.dto.js';
5
+ export * from './nutrition.js';
@@ -0,0 +1,75 @@
1
+ import { z } from 'zod';
2
+ import { type ServingUnit } from '../enums/index.js';
3
+ /**
4
+ * Nutrition rules shared by the backend and the apps, so a value the web form
5
+ * accepts is a value the API accepts, and both calculate derived values the
6
+ * same way.
7
+ *
8
+ * Library nutrients are stored per 100 of the serving unit (per 100 g, or per
9
+ * 100 ml for a serving measured in ml). Per-serving values are always derived.
10
+ */
11
+ /** The largest nutrient amount per 100 units. Pure fat is 100 g; pure fat calories are 900. */
12
+ export declare const MAX_NUTRIENT_PER_100 = 9999.99;
13
+ export declare const MAX_SERVING_QUANTITY = 10000;
14
+ export declare const NUTRIENT_DECIMALS = 2;
15
+ /** Rounds to two decimals, the precision every nutrient is stored at. */
16
+ export declare function roundNutrient(value: number): number;
17
+ export declare function hasAtMostTwoDecimals(value: number): boolean;
18
+ /** A non-negative amount with at most two decimals. */
19
+ export declare const nutrientAmountSchema: (max?: number) => z.ZodNumber;
20
+ /** A positive quantity with at most two decimals, such as 200 (g) or 1.5 (bowls). */
21
+ export declare const quantitySchema: (max?: number) => z.ZodNumber;
22
+ /** The nutrients a library food records. Calories and the three macros are required. */
23
+ export interface NutrientValues {
24
+ calories: number;
25
+ protein: number;
26
+ carbs: number;
27
+ fat: number;
28
+ sugar?: number | null;
29
+ fiber?: number | null;
30
+ solubleFiber?: number | null;
31
+ insolubleFiber?: number | null;
32
+ }
33
+ /** The same values with every optional nutrient present, `null` when unknown. */
34
+ export interface CompleteNutrientValues {
35
+ calories: number;
36
+ protein: number;
37
+ carbs: number;
38
+ fat: number;
39
+ sugar: number | null;
40
+ fiber: number | null;
41
+ solubleFiber: number | null;
42
+ insolubleFiber: number | null;
43
+ }
44
+ /** Converts values for one serving of `servingQuantity` units into values per 100 units. */
45
+ export declare function toPer100(perServing: NutrientValues, servingQuantity: number): CompleteNutrientValues;
46
+ /** Converts values per 100 units into values for one serving of `servingQuantity` units. */
47
+ export declare function perServing(per100: NutrientValues, servingQuantity: number): CompleteNutrientValues;
48
+ /** Calories from the macros, using Biki's formula: (protein + carbs) × 4 + fat × 9. */
49
+ export declare function formulaCalories(protein: number, carbs: number, fat: number): number;
50
+ export interface NutritionIssue {
51
+ path: string;
52
+ message: string;
53
+ }
54
+ /**
55
+ * The checks that hold for any food's values per 100 units: the macros cannot
56
+ * weigh more than the food, and sugar, fibre and its parts cannot exceed what
57
+ * contains them. The database enforces the same rules.
58
+ */
59
+ export declare function nutritionIssues(per100: NutrientValues): NutritionIssue[];
60
+ /**
61
+ * The form of a food name used to find duplicates: case, accents of width,
62
+ * punctuation and spacing are ignored, so "Paneer (Full-Fat)" and
63
+ * "paneer full fat" match. Letters in any script are kept.
64
+ */
65
+ export declare function normalizeFoodName(name: string): string;
66
+ /** The stable key for a metadata value label: "High Protein" becomes "high-protein". */
67
+ export declare function slugifyMetadataKey(label: string): string;
68
+ /**
69
+ * Reads the weight or volume from a serving label such as "1 cup (200g)" or
70
+ * "1 glass (250ml)". Returns null when the label states neither.
71
+ */
72
+ export declare function parseServingLabel(label: string): {
73
+ quantity: number;
74
+ unit: ServingUnit;
75
+ } | null;