import {z} from 'zod' export type Meter = {id?: (string | null) | undefined, eliq_id?: (string | null) | undefined, location_id?: (string | null) | undefined, name?: (string | null) | undefined, model?: (string | null) | undefined, fuel?: (string | null) | undefined, type?: (string | null) | undefined, is_sub_meter?: (boolean | null) | undefined, category?: (string | null) | undefined, category_percentage?: (number | null) | undefined, supply_start_date?: (string | null) | undefined, supply_end_date?: (string | null) | undefined, created_at_timestamp?: (string | null) | undefined, sync_status?: (string | null) | undefined}; export type UpdateApiMetersIdArgs = {id: string, body: Meter}; export type UpdateApiMetersIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const meter = z.object({id: z.string().nullable().optional(), eliq_id: z.string().nullable().optional(), location_id: z.string().nullable().optional(), name: z.string().nullable().optional(), model: z.string().nullable().optional(), fuel: z.string().nullable().optional(), type: z.string().nullable().optional(), is_sub_meter: z.boolean().nullable().optional(), category: z.string().nullable().optional(), category_percentage: z.number().int().nullable().optional(), supply_start_date: z.string().nullable().optional(), supply_end_date: z.string().nullable().optional(), created_at_timestamp: z.string().nullable().optional(), sync_status: 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 }; /** * Creates or updates a meter. * * PUT /meters/{id} */ export const updateApiMetersId = async (args: UpdateApiMetersIdArgs, options: UpdateApiMetersIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/meters/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(meter, await res.json()) };