Resource
Resource is a library for building API's with Frank, and other Nest Project web frameworks.
Example
struct Repository: Resource {
let name = "Resource"
let description = "Library for building REST API's with Frank"
}
if requested as JSON would return:
{
"name": "Resource",
"description": "Library for building REST API's with Frank"
}
Resource will also embed any resources returned in the relations()
dictionary.
For example:
public struct User: Resource {
let prettyName = "Daniel Tomlinson"
let username = "danieltomlinson"
let favouriteRepository = Repository()
public func attributes() -> [String : Any] {
return ["pretty_name": prettyName, "username": username]
}
public func relations() -> [String : [Resource]] {
return ["favourite_repository": [favouriteRepository]]
}
}
if requested as JSON would return:
{
"pretty_name": "Daniel Tomlinson",
"username": "danieltomlinson",
"embed": {
"favourite_repository": {
"name": "Resource",
"description": "Library for building REST API's with Frank"
}
}
}
Content Types
Default
Resource comes with support for:
- JSON (application/json)
Custom
You can provide custom content types by overriding the encoders
function on a resource.
Coming Soon
- [] Better support for content negotiation