import {z} from 'zod' export type GetApiV3UsersBiosensingBodytemperatureArgs = {from?: string | undefined, to?: string | undefined}; export type GetApiV3UsersBiosensingBodytemperatureOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const bodyTemperatureSample = z.object({temperature_celsius: z.number().optional(), recording_time_delta_milliseconds: z.number().int().optional()}); export const bodyTemperaturePeriod = z.object({source_device_id: z.string().optional(), measurement_type: z.enum(["TM_UNKNOWN", "TM_SKIN_TEMPERATURE", "TM_CORE_TEMPERATURE"]).optional(), sensor_location: z.enum(["SL_UNKNOWN", "SL_DISTAL", "SL_PROXIMAL"]).optional(), start_time: z.string().optional(), end_time: z.string().optional(), modified_time: z.string().optional(), samples: z.array(bodyTemperatureSample).optional()}); export const getApiV3UsersBiosensingBodytemperatureResponse = z.array(bodyTemperaturePeriod); 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 }; /** * Body temperature data * * GET /v3/users/biosensing/bodytemperature */ export const getApiV3UsersBiosensingBodytemperature = async (args: GetApiV3UsersBiosensingBodytemperatureArgs, options?: GetApiV3UsersBiosensingBodytemperatureOptions) => { const {baseUrl = "https://www.polaraccesslink.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/users/biosensing/bodytemperature${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(getApiV3UsersBiosensingBodytemperatureResponse, await res.json()) };