UNPKG

@adonisjs/inertia

Version:

Official Inertia.js adapter for AdonisJS

52 lines (51 loc) 1.95 kB
import { t as InertiaHeaders } from "../../../headers-DafWEpBh.js"; import { ApiRequest, ApiResponse } from "@japa/api-client"; function ensureIsInertiaResponse() { if (!this.header("x-inertia")) throw new Error("Not an Inertia response. Make sure to use \"withInertia()\" method when making the request"); } function ensureHasAssert(assertLib) { if (!assertLib) throw new Error("Response assertions are not available. Make sure to install the @japa/assert plugin"); } function inertiaApiClient(app) { return async () => { const inertiaConfig = app.config.get("inertia"); ApiRequest.macro("withInertia", function() { this.header(InertiaHeaders.Inertia, "true"); this.header(InertiaHeaders.Version, String(inertiaConfig.assetsVersion ?? "1")); return this; }); ApiRequest.macro("withInertiaPartialReload", function(component, data) { this.withInertia(); this.header(InertiaHeaders.PartialComponent, component); this.header(InertiaHeaders.PartialOnly, data.join(",")); return this; }); ApiResponse.getter("inertiaComponent", function() { ensureIsInertiaResponse.call(this); return this.body().component; }); ApiResponse.getter("inertiaProps", function() { ensureIsInertiaResponse.call(this); return this.body().props; }); ApiResponse.macro("assertInertiaComponent", function(component) { ensureIsInertiaResponse.call(this); ensureHasAssert(this.assert); this.assert.equal(this.body().component, component); return this; }); ApiResponse.macro("assertInertiaProps", function(props) { ensureIsInertiaResponse.call(this); ensureHasAssert(this.assert); this.assert.deepEqual(this.body().props, props); return this; }); ApiResponse.macro("assertInertiaPropsContains", function(props) { ensureIsInertiaResponse.call(this); ensureHasAssert(this.assert); this.assert.containSubset(this.body().props, props); return this; }); }; } export { inertiaApiClient };