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 from normal host pages, endpoints, and fixture routes so they use the same host API as createMikaPlugin({ api }) and createMikaActions({ api }).

mikaAccountExportIntegration({ entrypoint }) injects the exact /_emdash/api/plugins/mika/account/export/download endpoint ahead of EmDash’s catch-all. Use it for built-in export attachments with EmDash 0.22, whose plugin dispatcher JSON-wraps Response objects. Do not use it to publish general account or tool mutation routes.

The absolute entrypoint exports the live api and optional operationPolicy/onError, shared with plugin wiring. validateMikaAccountExportHttpWiring validates that module; handleMikaAccountExportHttp uses the real Astro session and locale. These exports are server-only. GET renders non-consuming token confirmation; same-origin POST delivers the built-in JSON attachment. The request capability deliverAccountExport carries bytes only to the server response boundary, never into DTOs or tool output.

Keep the injected endpoint package-owned and do not create a competing route. See Account Export Downloads for configuration, middleware, and SSR requirements.

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