RestApi
The default policy. Generates conventional REST endpoints — one URL per operation, JSON request/response bodies wrapped in a model key.
- MIME:
application/json - Serializer:
RestApiSerializer - Routing: Path-based (
/models,/models/:id)
import crate.protocols.restApi.policy;
// These are equivalent:auto crateRouter = router.crateSetup;auto crateRouter = router.crateSetup!RestApi;
crateRouter.add(userCrate);Generated Endpoints
Section titled “Generated Endpoints”| Method | Path | Operation | Status |
|---|---|---|---|
GET | /users | List all | 200 |
GET | /users/:id | Get one | 200 |
POST | /users | Create | 200 |
PUT | /users/:id | Replace | 200 |
PATCH | /users/:id | Update fields | 200 |
DELETE | /users/:id | Delete | 204 |
Request Format
Section titled “Request Format”Requests wrap data in a model key:
{ "user": { "name": "Alice", "email": "alice@example.com" }}Response Format
Section titled “Response Format”Responses use the same wrapping:
{ "user": { "_id": "abc123", "name": "Alice", "email": "alice@example.com" }}List responses use the plural key:
{ "users": [ { "_id": "abc123", "name": "Alice" }, { "_id": "def456", "name": "Bob" } ]}ETag Support
Section titled “ETag Support”GET requests for single items include an ETag header (from the item’s hash field or CRC32 of the JSON). Clients can send If-None-Match to receive a 304 Not Modified when the item hasn’t changed.
Pagination, Sorting & Filtering
Section titled “Pagination, Sorting & Filtering”The REST policy does not parse query parameters for pagination or sorting out of the box, but these features are available through middleware. The IQuery interface supports sort, limit, skip, and where — middleware can parse query parameters and apply them to the query.
Not Yet Supported
Section titled “Not Yet Supported”- Sparse fieldsets: No field selection via query params
- HATEOAS links: No self/relationship links in responses
- Bulk operations: No batch create/update/delete
Custom Base URL
Section titled “Custom Base URL”auto crateRouter = router.crateSetup!(RestApiPolicy!(PolicyConfig("/api/v1")));// Endpoints at /api/v1/users, /api/v1/users/:id, etc.