@rekaz-io/storefront
v0.3.0
Published
Official TypeScript client and customer sessions for the Rekaz Storefront API
Readme
Rekaz Storefront API client
Use this TypeScript client to call the Rekaz Storefront API from your own storefront: the store's public configuration and catalog, customer sign-in, and the signed-in customer's carts, orders, bookings, and account. It has a method for each operation in the API reference. The package requires Node.js 24 or later.
pnpm add @rekaz-io/storefrontAPI client
The package has no root export. Import the API client from /api:
import { createStorefrontApiClient, isRekazProblemDetails } from "@rekaz-io/storefront/api";
const storefront = createStorefrontApiClient({
accessToken: customerAccessToken,
tenant: "my-tenant",
});
const { data, error } = await storefront.me.listOrders({ query: { limit: 20 } });
if (error) {
throw new Error(isRekazProblemDetails(error) ? error.detail : "The request failed.");
}Client options include baseUrl, headers, a Fetch-compatible fetch, and
accessToken. The token can be a string or a provider called before each
request. Keep customer tokens on the server; see
customer sessions. Import operation and error metadata from
@rekaz-io/storefront/api/catalog when you need the full catalog.
Hosted checkout runs at /checkout/{cartId} on the storefront's hostname (or
/{tenant}/checkout/{cartId} on the shared Rekaz storefront). Its server
exchanges the session token from the storefront's __Host-rekaz_customer_session
cookie through the urn:rekaz:customer-session grant, then calls Rekaz's Me
checkout endpoints with the resulting server-side bearer token. The cart ID
identifies the cart; the customer token and ownership check authorize it.
List operations use cursor pagination. Pass nextCursor unchanged with the same
filters and sort order; null marks the end. Request includeTotal only when the
total count is needed.
Requests return data, error, request, and response by default. Pass
throwOnError: true to throw on API errors. Operations that require an
Idempotency-Key document the header. Generate a key with
createIdempotencyKey() and reuse it when retrying the same operation.
Customer sessions
Public operations need only the store's tenant. Customer operations need an
access token, which must stay on your server. Import the session manager from
/session in server code. It signs customers in with a one-time code and keeps
them signed in with the __Host-rekaz_customer_session cookie (HttpOnly,
Secure, SameSite=Lax, 30 days). The cookie holds only an opaque session
token, never an access token.
import { createIdempotencyKey, createStorefrontApiClient } from "@rekaz-io/storefront/api";
import { createCustomerSessionManager } from "@rekaz-io/storefront/session";
const sessions = createCustomerSessionManager({ tenant: "my-tenant" });
// Sign-in: send the code, then exchange it and set the returned cookie.
const challenge = await sessions.startOtp({ idempotencyKey: createIdempotencyKey(), mobileNumber });
const { setCookie } = await sessions.exchangeOtp({ challengeId: challenge.challengeId, code });
// Each request that calls customer operations.
const session = await sessions.resolve(request.headers.get("Cookie"));
if (session.status === "authenticated") {
const storefront = createStorefrontApiClient({ accessToken: session.accessToken, tenant: "my-tenant" });
}resolveexchanges the session token for a 15-minute access token. Call it once per incoming request and reuse the token for that request's calls.- Set the
setCookiethatresolvereturns on your response. It keeps the cookie alive, or clears it when the session was revoked or expired. logoutrevokes the session at Rekaz and returns the cookie that clears it.- The cookie is sent with any request to your origin. Before resolving it for a request that changes data, check that the request came from your own pages.
- Failures throw
CustomerSessionError. Itscodeis Rekaz's error code, such asinvalid_otp_code, andretryablemarks outages worth retrying.
License
See LICENSE for usage terms.
