Skip to content

Astro Helpers

Use @bnomei/emdash-mika/astro from Astro pages and endpoints. Do not import these request-bound helpers from browser code.

Important helpers include:

  • createMika() / createMikaAstroClient();
  • createMikaPurchaseModel();
  • createMikaPurchaseOptions();
  • formatMikaMoney();
  • formatMikaPrice();
  • formatMikaVariant();
  • formatMikaSellable();
  • mikaReturnTo();
  • mikaSafeReturnTo();
  • mikaHiddenInput();
  • mikaReturnToInput();
  • mikaRedirectInputs();
  • isMikaPurchasable();
  • mikaMaxPurchaseQuantity();
  • firstAvailableMikaPurchaseOption();
  • firstAvailableMikaPurchaseIndex().

createMika(Astro) is an alias for createMikaAstroClient(Astro). It builds a request-scoped operation facade from Astro.request, Astro.url, optional Astro.session, and Astro.currentLocale.

The returned object has operation groups catalog, stock, cart, wishlist, checkout, magicLink, account, subscription, download, and order, plus routes, a builder for EmDash plugin JSON read routes. Webhook operations are excluded by default; pass { includeWebhook: true } only inside trusted server endpoints that need Mika.webhook.receive(...).

Pass { api } directly only for tests, fixtures, or one-off override wiring. In normal host pages, rely on the plugin/default API configured through mikaPlugin() and the native plugin entrypoint.

Use mikaHiddenInput() for hidden input attrs in Astro forms. It serializes string, number, and boolean values, and turns null or undefined into an empty string.

Use mikaReturnToInput() for posted returnTo fields. It wraps mikaSafeReturnTo() and accepts the same origin and fallback options.

Use mikaRedirectInputs() for checkout start forms:

const redirectInputs = mikaRedirectInputs(
{
successPath: "/checkout/success",
cancelPath: "/checkout/cancel",
returnTo: "/cart",
},
{
successFallback: "/checkout/success",
cancelFallback: "/checkout/cancel",
returnToFallback: "/cart",
},
);

Redirect fallbacks are caller-owned. Mika core does not bake in the ⓐ template route constants.

Do not use browser clients for protected account or mutation flows unless the host has designed the route and policy boundary.

  • ../emdash-mika/src/astro.ts
  • ../emdash-mika/src/api/operation-facade.ts
  • ../emdash-mika/src/api/form-contracts.ts
  • ../emdash-mika/src/api/redirect-policy.ts