Skip to main content

Module envelope

Module envelope 

Source
Expand description

Generic response envelopes for Ghost API endpoints.

Ghost wraps every response in a resource-keyed JSON object. Browse endpoints include a meta block for pagination; read, create, and update endpoints return a one-element array without meta. These two envelope shapes are modelled by BrowseEnvelope and SingleEnvelope respectively.

Both types use a custom Deserialize implementation that locates the resource array by scanning the JSON object for the first array-valued field (skipping meta). This means a single generic type works for every Ghost resource — posts, pages, tags, authors, and any future resource — without requiring per-resource newtype wrappers.

§Examples

use ghost_io_api::models::envelope::{BrowseEnvelope, SingleEnvelope};
use serde::Deserialize;
use serde_json::json;

#[derive(Debug, Deserialize)]
struct Widget { id: String, name: String }

// Browse: items + pagination
let browse_json = json!({
    "widgets": [
        {"id": "w1", "name": "Alpha"},
        {"id": "w2", "name": "Beta"}
    ],
    "meta": {
        "pagination": {
            "page": 1, "limit": 15, "pages": 1, "total": 2
        }
    }
});
let env: BrowseEnvelope<Widget> = serde_json::from_value(browse_json).unwrap();
assert_eq!(env.items.len(), 2);
assert_eq!(env.meta.pagination.total, 2);

// Single: one item in the array, no meta
let read_json = json!({ "widgets": [{"id": "w1", "name": "Alpha"}] });
let env: SingleEnvelope<Widget> = serde_json::from_value(read_json).unwrap();
assert_eq!(env.item.id, "w1");

Structs§

BrowseEnvelope
Response envelope for Ghost browse endpoints.
SingleEnvelope
Response envelope for Ghost read, create, and update endpoints.