diff --git a/packages/serinus_inertia/example/client/package-lock.json b/packages/serinus_inertia/example/client/package-lock.json index a682278..c9f671e 100644 --- a/packages/serinus_inertia/example/client/package-lock.json +++ b/packages/serinus_inertia/example/client/package-lock.json @@ -55,7 +55,6 @@ "integrity": "sha512-CGOfOJqWjg2qW/Mb6zNsDm+u5vFQ8DxXfbM09z69p5Z6+mE1ikP2jUXw+j42Pf1XTYED2Rni5f95npYeuwMDQA==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@babel/code-frame": "^7.29.0", "@babel/generator": "^7.29.0", @@ -1461,7 +1460,6 @@ "integrity": "sha512-ilcTH/UniCkMdtexkoCN0bI7pMcJDvmQFPvuPvmEaYA/NSfFTAgdUSLAoVjaRJm7+6PvcM+q1zYOwS4wTYMF9w==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "csstype": "^3.2.2" } @@ -1503,7 +1501,6 @@ "integrity": "sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw==", "dev": true, "license": "MIT", - "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -1612,7 +1609,6 @@ } ], "license": "MIT", - "peer": true, "dependencies": { "baseline-browser-mapping": "^2.9.0", "caniuse-lite": "^1.0.30001759", @@ -1844,7 +1840,6 @@ "integrity": "sha512-XoMjdBOwe/esVgEvLmNsD3IRHkm7fbKIUGvrleloJXUZgDHig2IPWNniv+GwjyJXzuNqVjlr5+4yVUZjycJwfQ==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.8.0", "@eslint-community/regexpp": "^4.12.1", @@ -2548,7 +2543,6 @@ "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", "dev": true, "license": "MIT", - "peer": true, "engines": { "node": ">=12" }, @@ -2610,7 +2604,6 @@ "resolved": "https://registry.npmjs.org/react/-/react-19.2.4.tgz", "integrity": "sha512-9nfp2hYpCwOjAN+8TZFGhtWEwgvWHXqESH8qT89AT/lWklpLON22Lc8pEtnpsZz7VmawabSU0gCjnj8aC0euHQ==", "license": "MIT", - "peer": true, "engines": { "node": ">=0.10.0" } @@ -2620,7 +2613,6 @@ "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.2.4.tgz", "integrity": "sha512-AXJdLo8kgMbimY95O2aKQqsz2iWi9jMgKJhRBAxECE4IFxfcazB2LmzloIoibJI3C12IlY20+KFaLv+71bUJeQ==", "license": "MIT", - "peer": true, "dependencies": { "scheduler": "^0.27.0" }, @@ -2845,7 +2837,6 @@ "integrity": "sha512-w+N7Hifpc3gRjZ63vYBXA56dvvRlNWRczTdmCBBa+CotUzAPf5b7YMdMR/8CQoeYE5LX3W4wj6RYTgonm1b9DA==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "esbuild": "^0.27.0", "fdir": "^6.5.0", @@ -2967,7 +2958,6 @@ "integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==", "dev": true, "license": "MIT", - "peer": true, "funding": { "url": "https://github.com/sponsors/colinhacks" } diff --git a/packages/serinus_inertia/example/lib/app_module.dart b/packages/serinus_inertia/example/lib/app_module.dart index c212542..264b3a5 100644 --- a/packages/serinus_inertia/example/lib/app_module.dart +++ b/packages/serinus_inertia/example/lib/app_module.dart @@ -8,9 +8,9 @@ class AppModule extends Module { : super( imports: [ InertiaModule( - options: SerinusInertiaOptions( + options: InertiaOptions( version: '1.0.0', - assets: SerinusInertiaAssetOptions( + assets: InertiaAssetsOptions( entry: 'src/main.jsx', clientDirectory: 'client', includeReactRefresh: true, diff --git a/packages/serinus_inertia/lib/src/serinus_inertia.dart b/packages/serinus_inertia/lib/src/serinus_inertia.dart index 1371358..773e74f 100644 --- a/packages/serinus_inertia/lib/src/serinus_inertia.dart +++ b/packages/serinus_inertia/lib/src/serinus_inertia.dart @@ -42,9 +42,9 @@ const List _defaultManagedSsrBundleCandidates = [ ]; /// Module-level Vite asset configuration. -class SerinusInertiaAssetOptions { +class InertiaAssetsOptions { /// Creates a new Vite asset configuration. - const SerinusInertiaAssetOptions({ + const InertiaAssetsOptions({ this.entry = 'index.html', this.clientDirectory = 'client', this.manifestPath, @@ -93,7 +93,7 @@ class SerinusInertiaAssetOptions { } /// Returns a copy with updated fields. - SerinusInertiaAssetOptions copyWith({ + InertiaAssetsOptions copyWith({ String? entry, String? clientDirectory, String? manifestPath, @@ -103,7 +103,7 @@ class SerinusInertiaAssetOptions { bool? includeReactRefresh, String? fallbackScript, }) { - return SerinusInertiaAssetOptions( + return InertiaAssetsOptions( entry: entry ?? this.entry, clientDirectory: clientDirectory ?? this.clientDirectory, manifestPath: manifestPath ?? this.manifestPath, @@ -117,9 +117,9 @@ class SerinusInertiaAssetOptions { } /// Module-level SSR configuration. -class SerinusInertiaSsrOptions { +class InertiaSsrOptions { /// Creates a new SSR configuration. - const SerinusInertiaSsrOptions({ + const InertiaSsrOptions({ this.enabled = false, this.gateway, this.endpoint, @@ -251,7 +251,7 @@ class SerinusInertiaSsrOptions { } /// Returns a copy with updated fields. - SerinusInertiaSsrOptions copyWith({ + InertiaSsrOptions copyWith({ bool? enabled, SsrGateway? gateway, Uri? endpoint, @@ -268,7 +268,7 @@ class SerinusInertiaSsrOptions { Duration? startupTimeout, Duration? healthCheckInterval, }) { - return SerinusInertiaSsrOptions( + return InertiaSsrOptions( enabled: enabled ?? this.enabled, gateway: gateway ?? this.gateway, endpoint: endpoint ?? this.endpoint, @@ -289,9 +289,9 @@ class SerinusInertiaSsrOptions { } /// Module-level configuration for the Serinus Inertia integration. -class SerinusInertiaOptions { +class InertiaOptions { /// Creates a new options object. - const SerinusInertiaOptions({ + const InertiaOptions({ this.version = '', this.encryptHistory = false, this.clearHistory = false, @@ -299,7 +299,7 @@ class SerinusInertiaOptions { this.sharedProps, this.htmlBuilder, this.assets, - this.ssr = const SerinusInertiaSsrOptions(), + this.ssr = const InertiaSsrOptions(), }); /// The asset version sent with every page. @@ -321,23 +321,23 @@ class SerinusInertiaOptions { final InertiaHtmlBuilder? htmlBuilder; /// Optional Vite asset settings used by the default HTML wrapper. - final SerinusInertiaAssetOptions? assets; + final InertiaAssetsOptions? assets; /// SSR configuration for first visits. - final SerinusInertiaSsrOptions ssr; + final InertiaSsrOptions ssr; /// Returns a copy with updated fields. - SerinusInertiaOptions copyWith({ + InertiaOptions copyWith({ String? version, bool? encryptHistory, bool? clearHistory, String? elementId, InertiaSharedPropsBuilder? sharedProps, InertiaHtmlBuilder? htmlBuilder, - SerinusInertiaAssetOptions? assets, - SerinusInertiaSsrOptions? ssr, + InertiaAssetsOptions? assets, + InertiaSsrOptions? ssr, }) { - return SerinusInertiaOptions( + return InertiaOptions( version: version ?? this.version, encryptHistory: encryptHistory ?? this.encryptHistory, clearHistory: clearHistory ?? this.clearHistory, @@ -353,10 +353,10 @@ class SerinusInertiaOptions { /// Provider used by [InertiaModule] to expose Inertia defaults. class InertiaService extends Provider { /// Creates a new service with module-level [options]. - const InertiaService({this.options = const SerinusInertiaOptions()}); + const InertiaService({this.options = const InertiaOptions()}); /// The configured module options. - final SerinusInertiaOptions options; + final InertiaOptions options; } /// Manages an optional external SSR runtime for Serinus. @@ -377,7 +377,7 @@ class InertiaSsrProcessManager extends Provider _stderrSink = stderrSink ?? stderr; /// The configured module options. - final SerinusInertiaOptions options; + final InertiaOptions options; final StartSsrServerCallback _startSsrServer; final CheckSsrServerCallback _checkSsrServer; @@ -563,26 +563,22 @@ class InertiaSsrProcessManager extends Provider /// Serinus module that registers configurable Inertia defaults. class InertiaModule extends Module { /// Creates a new Inertia module with the provided [options]. - InertiaModule({SerinusInertiaOptions options = const SerinusInertiaOptions()}) + InertiaModule({InertiaOptions options = const InertiaOptions()}) : super( providers: [ InertiaService(options: options), InertiaSsrProcessManager(options: options), - Provider.forValue(options), + Provider.forValue(options), ], exports: [InertiaService], + isGlobal: true, ); } -/// Extracts a flat `Map` from Serinus [SerinusHeaders]. -Map extractSerinusHeaders(SerinusHeaders headers) { - return Map.from(headers.asFullMap()); -} - /// Builds an [InertiaRequest] from a Serinus [Request]. InertiaRequest inertiaRequestFromSerinus(Request request) { return InertiaRequest( - headers: extractSerinusHeaders(request.headers), + headers: request.headers.asFullMap(), url: request.uri.toString(), method: request.method.name.toUpperCase(), body: request.body, @@ -616,14 +612,17 @@ extension SerinusInertiaRequestContextExtension on RequestContext { /// Returns the configured Inertia service, or fallback defaults. InertiaService get inertiaService { - if (canUse()) { - return use(); + if (!canUse()) { + throw StateError( + 'InertiaService is not available in the current context. ' + 'Make sure InertiaModule is imported and registered in the application.', + ); } - return const InertiaService(); + return use(); } /// Returns the configured module options, or fallback defaults. - SerinusInertiaOptions get inertiaOptions => inertiaService.options; + InertiaOptions get inertiaOptions => inertiaService.options; /// Returns the current request as an [InertiaRequest]. InertiaRequest get inertiaRequest => request.inertiaRequest; diff --git a/packages/serinus_inertia/test/unit/serinus_inertia_test.dart b/packages/serinus_inertia/test/unit/serinus_inertia_test.dart index 43f7cf1..9cb04af 100644 --- a/packages/serinus_inertia/test/unit/serinus_inertia_test.dart +++ b/packages/serinus_inertia/test/unit/serinus_inertia_test.dart @@ -20,11 +20,8 @@ void main() { }), ); - expect(extractSerinusHeaders(headers), containsPair('x-inertia', 'true')); - expect( - extractSerinusHeaders(headers), - containsPair('x-inertia-version', 'v1'), - ); + expect(headers.asFullMap(), containsPair('x-inertia', 'true')); + expect(headers.asFullMap(), containsPair('x-inertia-version', 'v1')); }); }); @@ -71,6 +68,7 @@ void main() { 'X-Inertia-Partial-Data': 'user.name', }, uri: Uri.parse('http://localhost/dashboard'), + providers: {InertiaService: InertiaService()}, ); final payload = await context.inertia( @@ -96,7 +94,10 @@ void main() { }); test('returns bootstrap html for initial visits', () async { - final context = _context(uri: Uri.parse('http://localhost/dashboard')); + final context = _context( + uri: Uri.parse('http://localhost/dashboard'), + providers: {InertiaService: InertiaService()}, + ); final payload = await context.inertia( component: 'Dashboard', @@ -137,7 +138,7 @@ void main() { uri: Uri.parse('http://localhost/dashboard'), providers: { InertiaService: InertiaService( - options: SerinusInertiaOptions( + options: InertiaOptions( version: 'v1', elementId: 'root', sharedProps: (_) async => { @@ -172,8 +173,8 @@ void main() { uri: Uri.parse('http://localhost/dashboard'), providers: { InertiaService: InertiaService( - options: const SerinusInertiaOptions( - assets: SerinusInertiaAssetOptions( + options: const InertiaOptions( + assets: InertiaAssetsOptions( entry: 'src/main.jsx', clientDirectory: 'web', devServerUrl: 'http://localhost:5173', @@ -201,8 +202,8 @@ void main() { uri: Uri.parse('http://localhost/dashboard'), providers: { InertiaService: InertiaService( - options: SerinusInertiaOptions( - ssr: SerinusInertiaSsrOptions( + options: InertiaOptions( + ssr: InertiaSsrOptions( enabled: true, gateway: _FakeSsrGateway( const SsrResponse( @@ -237,8 +238,8 @@ void main() { var startCalls = 0; final manager = InertiaSsrProcessManager( - options: const SerinusInertiaOptions( - ssr: SerinusInertiaSsrOptions(enabled: true, manageProcess: true), + options: const InertiaOptions( + ssr: InertiaSsrOptions(enabled: true, manageProcess: true), ), checkSsrServer: ({required endpoint, healthEndpoint}) async => true, startSsrServer: (config, {inheritStdio = false}) async { @@ -260,8 +261,8 @@ void main() { late SsrServerConfig startedConfig; final manager = InertiaSsrProcessManager( - options: const SerinusInertiaOptions( - ssr: SerinusInertiaSsrOptions( + options: const InertiaOptions( + ssr: InertiaSsrOptions( enabled: true, manageProcess: true, runtime: 'bun', @@ -292,8 +293,8 @@ void main() { var stopCalls = 0; final manager = InertiaSsrProcessManager( - options: const SerinusInertiaOptions( - ssr: SerinusInertiaSsrOptions( + options: const InertiaOptions( + ssr: InertiaSsrOptions( enabled: true, manageProcess: true, waitUntilReady: false, @@ -319,8 +320,8 @@ void main() { final process = _FakeProcess(); final manager = InertiaSsrProcessManager( - options: const SerinusInertiaOptions( - ssr: SerinusInertiaSsrOptions( + options: const InertiaOptions( + ssr: InertiaSsrOptions( enabled: true, manageProcess: true, waitUntilReady: false,