1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! # Responses: how your data looks in the API output
//!
//! `requests/` describes what comes **in**; this folder describes what goes
//! **out**. A resource turns a model into the JSON your API sends, so you
//! decide exactly which fields clients see (and never leak a password hash):
//!
//! ```ignore
//! #[resource(Post)]
//! pub struct PostResource;
//!
//! impl JsonResource for PostResource {
//! fn to_array(&self) -> JsonValue {
//! json!({
//! "id": self.id, // `self` reads the Post
//! "title": self.title,
//! "author": UserResource::when_loaded(&self.author), // only when loaded
//! "body": when(self.published, &self.body), // only when published
//! })
//! }
//! }
//! ```
//!
//! In a controller:
//!
//! | Return | JSON |
//! |-----------------------------------------|----------------------------------------------|
//! | `PostResource::make(post)` | `{"data": {...}}` |
//! | `PostResource::make(post).created()` | the same, with status `201 Created` |
//! | `PostResource::collection(posts)` | `{"data": [...]}` |
//! | `PostResource::collection(page)` | `{"data": [...], "links": {...}, "meta": {...}}` for `.paginate()` |
//! | `.additional(json!({ "meta": {...} }))` | extra top-level fields next to `data` |
//!
//! - Create a new one with `chy make:response Comment`. It is added to this
//! file for you.
//! - Optional fields: `when(condition, value)`, `merge_when(condition, json!({...}))`,
//! `OtherResource::when_loaded(&self.relation)`.
//! - Errors keep their own format (`{"error": {...}}`); you don't need a resource for them.
pub use PostResource;
pub use UserResource;