import {z} from 'zod' export type LocationAddress = {street_address?: (string | null) | undefined, postal_code?: (string | null) | undefined, city?: (string | null) | undefined, country_code?: (string | null) | undefined}; export type LocationGeo = {latitude?: (number | null) | undefined, longitude?: (number | null) | undefined}; 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 ProfileType = 'residential' | 'business' | 'undefined'; export type Location = {id?: (string | null) | undefined, eliq_id?: (number | null) | undefined, name?: (string | null) | undefined, user_id?: (string | null) | undefined, spot_price_region_id?: (string | null) | undefined, address?: LocationAddress | undefined, geo?: LocationGeo | undefined, meters?: (Array | null) | undefined, created_at_timestamp?: (string | null) | undefined, location_profile_type?: ProfileType | undefined}; export type UpdateApiLocationsIdArgs = {id: string, body: Location}; export type UpdateApiLocationsIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const locationAddress = z.object({street_address: z.string().nullable().optional(), postal_code: z.string().nullable().optional(), city: z.string().nullable().optional(), country_code: z.string().nullable().optional()}); export const locationGeo = z.object({latitude: z.number().nullable().optional(), longitude: z.number().nullable().optional()}); 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 profileType = z.enum(["residential", "business", "undefined"]); export const location = z.object({id: z.string().nullable().optional(), eliq_id: z.number().int().nullable().optional(), name: z.string().nullable().optional(), user_id: z.string().nullable().optional(), spot_price_region_id: z.string().nullable().optional(), address: locationAddress.optional(), geo: locationGeo.optional(), meters: z.array(meter).nullable().optional(), created_at_timestamp: z.string().nullable().optional(), location_profile_type: profileType.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 location * * PUT /locations/{id} */ export const updateApiLocationsId = async (args: UpdateApiLocationsIdArgs, options: UpdateApiLocationsIdOptions) => { 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(/\/+$/, '')}/locations/${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(location, await res.json()) };