@sonic-commerce/storefront-types
v0.54.0
Published
TypeScript types for the Upceler storefront API (/v1).
Maintainers
Readme
@sonic-commerce/storefront-types
TypeScript types for the Upceler storefront API (/v1).
Types only — no runtime code, nothing to import at run time, nothing added to your bundle. Generated from the API's OpenAPI document, which is itself generated from the Zod schemas that serialize every response, so these types describe what the server actually sends.
Install
npm i -D @sonic-commerce/storefront-types
npm i openapi-fetchopenapi-fetch (~6 kB) turns the
paths type into a checked client. It is optional — the named types work with
fetch, axios, or anything else.
Use
Every store is served from one API host. The store is named by the
x-store-id header, not by the hostname.
import createClient from "openapi-fetch";
import type { paths, Product } from "@sonic-commerce/storefront-types";
// Server-side only — the Store ID is not meant for a browser bundle.
const api = createClient<paths>({
baseUrl: "https://api.example.com/v1",
headers: {
"x-store-id": process.env.SONIC_STORE_ID!,
"x-publishable-key": process.env.PUBLISHABLE_KEY!,
},
});
// Path, query params and response are all typed and checked.
const { data, error } = await api.GET("/products", {
params: { query: { limit: 20, sort: "-created_at" } },
});
const products: Product[] | undefined = data?.data;| Header | Send it | What it is |
|---|---|---|
| x-store-id | every request | The Store ID (K7M2-QX9P), from the admin under Settings → Store details. Keep it on your server |
| x-publishable-key | every request | The client key issued in the admin |
| x-shopper-ip | login, registration, cart creation, password reset, order tracking | The shopper's IP address. These endpoints are rate-limited per shopper and answer 400 without it |
| Idempotency-Key | POST /carts/{id}/complete | A unique value you generate. A retry with the same key returns the first response instead of placing a second order |
// Your server is the caller, so say which shopper it is acting for.
await api.POST("/carts", {
params: { header: { "x-shopper-ip": shopperIp } },
body: {},
});A missing or unknown Store ID or publishable key is answered 404 Not found.
A store with its own custom domain can use https://<that domain>/v1
instead; there the hostname names the store and x-store-id / x-shopper-ip
are not needed. Per-store subdomains are not served.
Every named schema in the spec is exported as a type alias — Product, Cart,
Order, Store, Category, Collection, AuthSession, the request bodies,
and so on — alongside the raw paths, components and operations that
openapi-typescript emits.
import type { Cart, CartTotals, AddCartItemBody } from "@sonic-commerce/storefront-types";Option filters
opt.<option> keys are named after the store's options, so they are not in the
typed query parameters. Spread them in — TypeScript does not check spread
properties against the parameter list:
import type { ProductFilters } from "@sonic-commerce/storefront-types";
const options = { "opt.size": "m,l", "opt.colour": "red" };
const { data: filters } = await api.GET("/products/filters", {
params: { query: { ...options, "price[lte]": 18500 } },
});
const sidebar: ProductFilters | undefined = filters?.data;
const { data: page } = await api.GET("/products", {
params: { query: { ...options, "price[lte]": 18500, sort: "price" } },
});Take keys from ProductFilters (option.key, value.key) rather than
building them from labels. Counts are over the current result and null above
5,000 matching products; see the API guide for the rules.
One openapi-fetch quirk
openapi-fetch (0.17.0, checked) drops properties typed as exactly null from
the response types it infers. Two fields are affected: shipping_total on
CartTotals and on OrderTotals, both deliberately null because shipping is
not modelled — an absent charge, not a zero one.
They are still there at run time. If you need them in the type, annotate with the named schema rather than the inferred response:
import type { Cart } from "@sonic-commerce/storefront-types";
const { data } = await api.GET("/carts/{id}", { params: { path: { id } } });
const cart = data?.data as Cart | undefined; // shipping_total presentProduct descriptions
description is a plain-text summary (max 500 characters). body, on the
product detail response only, is the detailed description as structured JSON —
see "Product descriptions" in the integrator's guide for the node types. Render
it as elements and text, never through innerHTML.
Stock by location
GET /locations lists the store's active locations (address, coordinates,
opening hours, pickup/delivery). On product detail each variant has
locations: [{ location_id, quantity }], the stock at each one. quantity on
the variant is the store-wide total. See "Stock and locations" in the
integrator's guide.
Versioning
The package version tracks the API version: each version describes the /v1
surface as of the backend release of the same number.
While the API is pre-1.0, breaking changes ship in a minor bump (0.x →
0.x+1), per the project's versioning policy. Pin with ~ to the version you
installed:
npm i -D --save-prefix='~' @sonic-commerce/storefront-types~ accepts patches and stays off the next minor. Switch to ^ once the API
reaches 1.0.
Other languages
The OpenAPI 3.1 document these types are generated from is served live at
GET /v1/openapi.json. Any generator that reads OpenAPI can target it.
