Skip to Content
Octopus Energy Packages

React Query

TanStack Query option factories for measurements data

Use this entry point when integrating measurements data with TanStack Query.

import {
  anchorDateQueryOptions,
  marketSupplyPointsQueryOptions,
  measurementsQueryOptions,
} from "@octopus-energy/measurements/react-query";

Hydration-safe query data

Query functions return serialization-safe data for SSR hydration where needed; client-side consumers get transformed values through each option factory's select function.

measurementsQueryOptions

Creates TanStack Query options for fetching normalized measurement records. The query error type is MeasurementsError, so after query.error is checked for nullishness UI code can read query.error.code directly.

measurementsQueryOptions

Prop

Type

MeasurementsQueryOptionsConfig

Prop

Type


marketSupplyPointsQueryOptions

Creates TanStack Query options for fetching market supply points for a property. The query function returns a resolved MarketSupplyPoint[] array and types query errors as MeasurementsError.

import { useQuery } from "@tanstack/react-query";
import { marketSupplyPointsQueryOptions } from "@octopus-energy/measurements/react-query";

const { data: marketSupplyPoints } = useQuery(
  marketSupplyPointsQueryOptions(client, {
    accountNumber,
    propertyId,
    utilityMarkets,
  }),
);
marketSupplyPointsQueryOptions

Prop

Type

MarketSupplyPointsQueryOptionsConfig

Prop

Type


anchorDateQueryOptions

Creates TanStack Query options for resolving the best anchor date. Resolve market supply points first, then pass them to anchorDateQueryOptions.

Client-side usage

The select transform means useQuery exposes anchorDate as a Date.

const { data: marketSupplyPoints } = useQuery(
  marketSupplyPointsQueryOptions(client, {
    accountNumber,
    propertyId,
    utilityMarkets,
  }),
);

const { data: anchorDate } = useQuery(
  anchorDateQueryOptions(client, {
    marketSupplyPoints,
    propertyId,
    period: "week",
    timezone: "Europe/London",
  }),
);

Server-side prefetching

The query function and cache use a YYYY-MM-DD calendar value for dehydration. Parse the result returned by fetchQuery before using it as a Date.

import { QueryClient } from "@tanstack/react-query";
import { parseCalendarDate } from "@octopus-energy/measurements/timeframe";

const queryClient = new QueryClient();
const anchorDateValue = await queryClient.fetchQuery(
  anchorDateQueryOptions(client, {
    marketSupplyPoints,
    propertyId,
    timezone: "Europe/London",
  }),
);
const anchorDate = parseCalendarDate(anchorDateValue);

if (!anchorDate) throw new Error("Invalid anchor-date calendar value");

anchorDateQueryOptions is disabled until marketSupplyPoints are available. Its query errors are typed as MeasurementsError. resolveAnchorDate still keeps its fallback behavior for request/schema/pagination failures and returns yesterday instead of putting the anchor-date query into an error state.

anchorDateQueryOptions

Prop

Type

AnchorDateQueryOptionsConfig

Prop

Type

On this page