import {z} from 'zod' export type GetApiV2TimeseriesUserIdCaloriesBasalGroupedArgs = {user_id: string, cursor?: (string | null) | undefined, next_cursor?: (string | null) | undefined, provider?: string | undefined, start_date: string, end_date?: (string | null) | undefined}; export type GetApiV2TimeseriesUserIdCaloriesBasalGroupedOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const clientFacingSource = z.object({provider: z.string(), type: z.string().optional(), app_id: z.string().nullable().optional(), device_id: z.string().nullable().optional(), sport: z.string().nullable().optional(), workout_id: z.string().nullable().optional()}); export const clientFacingCaloriesBasalTimeseries = z.object({id: z.number().int().nullable().optional(), timezone_offset: z.number().int().nullable().optional(), type: z.string().nullable().optional(), unit: z.literal("kcal"), timestamp: z.string(), value: z.number()}); export const groupedCaloriesBasal = z.object({source: clientFacingSource, data: z.array(clientFacingCaloriesBasalTimeseries)}); export const groupedCaloriesBasalResponse = z.object({groups: z.record(z.string(), z.array(groupedCaloriesBasal)), next: z.string().nullable().optional(), next_cursor: z.string().nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Calories Basal * * GET /v2/timeseries/{user_id}/calories_basal/grouped */ export const getApiV2TimeseriesUserIdCaloriesBasalGrouped = async (args: GetApiV2TimeseriesUserIdCaloriesBasalGroupedArgs, options?: GetApiV2TimeseriesUserIdCaloriesBasalGroupedOptions) => { const {baseUrl = "https://api.us.junction.com", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.next_cursor != null) query.append("next_cursor", String(args.next_cursor)) if (args.provider != null) query.append("provider", String(args.provider)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (apiKeyAuth !== undefined) headers.set("x-vital-api-key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/timeseries/${encodeURIComponent(String(args.user_id))}/calories_basal/grouped${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(groupedCaloriesBasalResponse, await res.json()) };