nuqs Server
APIs exported from @octopus-energy/measurements/storage/nuqs/server
Use this entry point in Server Components, route handlers, or
getServerSideProps. The parser configs work with both
createSearchParamsCache (App Router) and createLoader (Pages Router) from
nuqs/server.
import {
createPreferencesSearchParams,
createTimeframeSearchParams,
groupByScope,
} from "@octopus-energy/measurements/storage/nuqs/server";When returning a resolved Timeframe from getServerSideProps, serialize it with
serializeTimeframe before passing it as a prop.
createTimeframeSearchParams
Creates a server-safe nuqs parser config for useTimeframe state. period and
interval have stable parser defaults; date has no parser default. Resolve
dynamic date defaults after parsing search params.
createTimeframeSearchParamsProp
Type
Use the same stable parser defaults on the server and client when an omitted URL param should mean a custom app default.
createPreferencesSearchParams
Creates a server-safe nuqs parser config for usePreferences state.
createPreferencesSearchParamsProp
Type
Shared parser default and option types are documented in nuqs Shared Types.
groupByScope
Reshapes a flat { "scope.field": value } object into a nested
{ scope: { field: value } } structure. Use this when server-side code parses
scoped search params and needs to inspect each scope separately.
import { groupByScope } from "@octopus-energy/measurements/storage/nuqs/server";groupByScopeProp
Type
const cache = createSearchParamsCache(
createTimeframeSearchParams({ scopes: ["electricity", "gas"] }),
);
const { electricity, gas } = groupByScope(await cache.parse(searchParams), ["electricity", "gas"]);On the client, use createNuqsAdapter(state, setState, { scope }) for scoped
read/write state. groupByScope is the server-side read-only counterpart.
Search params
| Param | Type | Default | Description |
|---|---|---|---|
period | "day" | "week" | "month" | "year" | "custom" | "week" | Selected timeframe period |
date | YYYY-MM-DD calendar date | none | Anchor date, distinct from an ISO timestamp |
interval | "day" | "week" | "month" | "week" | Custom range interval; preserved even when the current period is not custom |
unit | "energy" | "cost" | "energy" | Display unit |
standingCharge | boolean | true | Show standing charge line |
tax | boolean | true | Include tax in cost values |
view | "chart" | "table" | "chart" | Render mode |
These defaults are package parser defaults. Custom stable parser defaults change what an omitted URL param means.
Related
- nuqs Shared Types: shared parser default and option types
- nuqs Client: client adapter and parser configs
- Storage: core adapter interface