import {z} from 'zod' export type GetApiLocationsArgs = {after_id?: string | undefined, limit?: number | undefined, created_after?: string | undefined}; export type GetApiLocationsOptions = {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 getApiLocationsResponse = z.array(location); 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 }; /** * Gets multiple locations * * GET /locations */ export const getApiLocations = async (args: GetApiLocationsArgs, options: GetApiLocationsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after_id != null) query.append("after_id", String(args.after_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.created_after != null) query.append("created_after", String(args.created_after)) const url = `${baseUrl.replace(/\/+$/, '')}/locations${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(getApiLocationsResponse, await res.json()) };