fusion-stack-laravel-rust 0.2.1

Fusion Stack for Laravel for Rust: versioned, documented JSON APIs with standard errors
Documentation

fusion-stack-laravel-rust

Fusion Stack for Laravel for Rust: date-based API versioning, deprecation headers, request IDs and one JSON:API error format. It sends exactly what the fusion-stack/laravel PHP package sends, so @fusion-stack/client, @fusion-stack/nuxt and the docs work unchanged against either backend.

Developed in fusion-stack/framework. Open issues and pull requests there.

cargo add fusion-stack-laravel-rust

The library is named fusion_stack. It needs Rust 1.99 or newer and laravel-rust 0.1.2 or a later 0.1 release.

Setup

Register FusionStack as a service provider, which renders every error as a JSON:API error document, and add it as middleware on your API routes:

use std::sync::Arc;

use fusion_stack::versioning::{Deprecated, Versions};
use fusion_stack::{Config, FusionStack};
use laravel::prelude::*;

let api = FusionStack::new(
    Config::new().versions(
        Versions::new("2026-11-30")
            .version("2026-06-01", vec![])
            .version("2026-11-30", vec![Arc::new(RenameCustomerNameToFullName)])
            .deprecated("2026-06-01", Deprecated::new().sunset("2027-06-01")),
    ),
);

Application::configure()
    .with_providers({
        let api = api.clone();
        move |providers| {
            providers.add(api);
        }
    })
    .with_routing(move |routing, _| {
        let api = api.clone();
        routing.api(move |route| {
            route.prefix("/v1").middleware(api).group(|route| {
                route.get("/customers/{customer}", customers::show);
            });
        });
    });

Errors

Every error is sent as application/vnd.api+json with a stable code and the request ID in meta.request_id. Return an ApiError from a handler with into_error():

use fusion_stack::errors::{ApiError, ApiErrorCode};

return Err(ApiError::new(ApiErrorCode::Conflict, "The order has already shipped.")
    .pointer("/data/attributes/status")
    .into_error());

Validation errors from the framework's Validate derive become validation_failed errors with a source.pointer for each field. Implement ErrorCode for your own codes.

Versioning

Clients send Api-Version: YYYY-MM-DD. Handlers always see the latest shape: each VersionChange upgrades older requests and downgrades responses for older versions. Changes can also add, remove or retire routes, or keep a frozen handler for older versions with legacy().

use fusion_stack::versioning::{VersionChange, document};
use serde_json::{Map, Value};

struct RenameCustomerNameToFullName;

impl VersionChange for RenameCustomerNameToFullName {
    fn description(&self) -> &str {
        "customer.name renamed to customer.full_name"
    }

    fn resources(&self) -> &[&'static str] {
        &["customers"]
    }

    fn transform_response(&self, document: &mut Value) {
        document::rename_attribute(document, self.resources(), "full_name", "name");
    }
}

Unknown versions get a 400 invalid_api_version, retired ones a 410 api_version_retired, and deprecated ones the Deprecation, Sunset and Link headers.

Not yet in this crate

Query parameters, idempotency, the response cache, API keys and scopes, rate limit tiers, version usage tracking, webhooks, the OpenAPI document and the console commands are in the PHP package and planned here.

License

MIT.