demf-csrf-consumer-parcel
v3.0.0
Published
This Parcel consumes the same-origin runtime contract (`window.__DEMF_AUTH__`) published by the Sidebar MF.
Readme
Digital Enabler CSRF Consumer Parcel
A single-spa parcel that combines its Vue/Vuetify UI with CSRF interceptors on the
Axios instance provided by its parent microfrontend. It does not create an Axios
client. The Sidebar remains the token provider through window.__DEMF_AUTH__.
Runtime Contract
Mount props:
| Prop | Required | Default | Purpose |
| --- | --- | --- | --- |
| axiosInstance | Yes | None | The exact Axios instance used by the parent's API calls |
| domElement | Yes | None | A unique host element for this parcel mount |
| bridgeKey | No | __DEMF_AUTH__ | Global Sidebar bridge name |
| setWithCredentials | No | true | Enable cookie credentials on the client |
| realm | No | None | Realm displayed in the UI |
| palette | No | {} | Vuetify theme colors |
| demf-csrf-consumer-parcel-config | No | {} | Configuration displayed in the UI |
On mount, the parcel installs request and response interceptors. For POST, PUT,
PATCH and DELETE it waits for the bridge's whenReady() and sets x-csrf-token.
GET requests do not receive the header. A mutating request that fails with HTTP 403
triggers refresh() and is retried at most once if a token is returned.
Multiple mounts on the same Axios instance share one interceptor pair. Options must match; conflicting mounts are rejected. Each host has independent ownership, and the last unmount removes the interceptors. Cookie defaults are not restored. Use distinct Axios instances for clients that need different options.
Each mount uses the original single-spa-vue template structure with Vue, Vuetify,
router, i18n and Pinia. Lifecycle instances and visual props are separate for each
host, without a nested v-app. The UI receives only visual props, not Axios or DOM
references. A failed UI mount releases its interceptor ownership. On unmount,
the Vue app is destroyed and ownership is released even if UI cleanup fails.
The internal lifecycle name matches the host ID for compatibility with the root's
single-spa-vue shim; anonymous hosts receive unique IDs.
Visibility is independent of lifecycle: hidden, display: none, v-show, and
closing an internal dialog do not unmount the parcel or disable its consumer.
demf-kpis-list leaves its mount-point div visible inside its scrollable page.
To hide it later, add the hidden attribute to that div while keeping it mounted.
This optional setting is not applied by the integration.
The original consumer behavior is preserved: an absent bridge results in an empty header, readiness has no timeout, and any mutating 403 may trigger a refresh even if the actual cause is insufficient permissions. Do not use this client for untrusted endpoints because the CSRF header is not filtered by destination.
Parent Integration
Use the Parcel Template's mountRootParcel pattern and a visible ref inside the
parent's Vue template. Do not append the host outside the root's layout, where
document-level overflow restrictions may hide it. Do not register the parcel as
a root layout application.
<div ref="csrfParcelMountPoint"></div>import axios from 'axios'
const [config, { mountRootParcel }] = await Promise.all([
window.importShim('demf-csrf-consumer-parcel'),
window.importShim('single-spa'),
])
const parcel = mountRootParcel(config, {
domElement: this.$refs.csrfParcelMountPoint,
axiosInstance: axios,
realm: props.realm,
palette: props.palette,
})
await parcel.mountPromiseAwait mountPromise before starting API requests or enabling mutating actions.
Root parcels are not automatically unmounted with their callers: the parent must
explicitly await parcel.unmount() before destroying the host, including cleanup
if its own mount fails. Never continue with unprotected API calls if loading fails.
demf-kpis-list implements this integration in its entry module and gates its
initial requests with the csrfReady prop until the embedded parcel is mounted.
Import Map And Deployment
For local development, configure the root's localMicrofrontends:
'demf-csrf-consumer-parcel': 'http://localhost:9999/mf-app.js'For production, map that same name to the deployed ESM mf-app.js URL in the root
import map or microfrontend catalog. The previous template package name
demf-csrf-confumer-parcel was a typo and is no longer the consumer's import name.
This repository does not modify the root configuration or publish a catalog entry.
Development And Verification
From app/:
npm install
npm run dev
npm testThe dev script builds in watch mode and previews the ESM bundle on port 9999.
npm test builds the bundle and exercises it with Axios and a simulated Sidebar
bridge using Node's built-in test runner and a Happy DOM environment: UI mounting,
isolated visual props, hidden hosts, headers, credentials, readiness, refresh,
retry limits, isolated clients, shared ownership, option conflicts and remount.
No backend is required. Happy DOM is a development-only dependency.
Guided MF Migration
From app/, run the two migration tools in order, for one microfrontend at a time:
npm run migrate:axios -- --target "../path-to-microfrontend/app" --dry-run
npm run migrate:axios -- --target "../path-to-microfrontend/app"
npm run migrate:csrf -- --target "../path-to-microfrontend/app" --dry-run
npm run migrate:csrf -- --target "../path-to-microfrontend/app"Omit --target to be prompted for the path. migrate:mf remains an alias for
migrate:csrf; it no longer creates the Axios client.
Axios (migrate-mf-axios.mjs) supports MFs with src/main.js, src/App.vue,
a singleSpaVue lifecycle, getMergedConfig, and Axios declared as a dependency. It creates one
src/api/axios.js instance with withCredentials: true and a 30-second timeout.
At mount, its base URL is configured from the merged MF config's api value and
the single-spa realm prop. Standard API URL prefixes containing apiUrl/realm
are converted to relative paths; standard default Axios imports are redirected
to the shared instance and duplicate withCredentials: true call options are
removed. It does not mount the parcel or add CSRF readiness gates. Backups use
the .axios-backup-* suffix.
Parcel (migrate-mf-csrf-parcel.mjs) requires the existing default-exported
client in src/api/axios.js and a Vue Options API App.vue with one <v-main>
or a single dynamic <component ... /> wrapper. It modifies only main.js and
App.vue: it passes that client to mountRootParcel, preserves its mount-time
configuration, adds the host, gates startup hooks/rendered content on CSRF
readiness, and cleans up the parcel before Vue unmounts. It does not create or
rewrite the Axios module, request imports, endpoints, or credential options.
Backups use the .csrf-backup-* suffix. A manually prepared client must already
be configured and used by the MF's API calls; unsupported mount wrappers require
manual integration.
Both tools preview changes and ask for confirmation before writing. --dry-run
performs compatibility checks without writing files, so the Axios step must be
applied before the parcel's dry-run if the client does not exist yet.
The Axios tool stops without changes for unsupported Axios imports, separate
axios.create() clients, an existing client, or an existing CSRF integration.
The parcel tool stops for a missing client, an existing CSRF integration, or an
App.vue/lifecycle structure it cannot safely recognize. The Axios tool only
rewrites recognized apiUrl/realm URL prefixes; review dynamic API URLs in
composables and validate the MF build after migration. The root import map must separately expose
demf-csrf-consumer-parcel and single-spa through window.importShim.
Run npm run test:migrations to verify the two CLI tools against temporary MFs.
