Documentation
¶
Overview ¶
Package jsonapi implements components to build JSON APIs with fire.
Index ¶
- func Fatal(err error) error
- type Action
- type Callback
- func Combine(callbacks ...Callback) Callback
- func DependentResourcesValidator(resources fire.Map) Callback
- func MatchingReferencesValidator(collection, reference string, matcher fire.Map) Callback
- func ModelValidator() Callback
- func ProtectedAttributesValidator(attributes fire.Map) Callback
- func VerifyReferencesValidator(references fire.Map) Callback
- type Context
- type Controller
- type Group
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Action ¶
type Action int
An Action describes the currently called action on the API.
const ( List Action Find Create Update Delete )
All the available actions.
type Callback ¶
A Callback can be an Authorizer or Validator an is called during execution of a controller.
Note: If the callback returns an error wrapped using Fatal() the API returns an InternalServerError status and the error will be logged. All other errors are serialized to an error object and returned.
func Combine ¶
Combine combines multiple callbacks to one.
Note: Execution will be stopped if a callback returns and error.
func DependentResourcesValidator ¶
DependentResourcesValidator counts documents in the supplied collections and returns an error if some get found. This callback is meant to protect resources from breaking relations when requested to be deleted.
Resources are defined by passing pairs of collections and fields where the field must be a database field of the target resource model:
DependentResourcesValidator(fire.Map{
"posts": "user_id",
"comments": "user_id",
})
func MatchingReferencesValidator ¶
MatchingReferencesValidator compares the model with a related model and checks if certain references are shared.
The target model is defined by passing its collection and the referencing field on the current model. The matcher is defined by passing pairs of database fields on the target and current model:
MatchingReferencesValidator("posts", "post_id", fire.Map{
"user_id": "user_id",
})
func ModelValidator ¶
func ModelValidator() Callback
ModelValidator uses the govalidator package to validate the model based on the "valid" struct tags.
func ProtectedAttributesValidator ¶
ProtectedAttributesValidator compares protected attributes against their default (during Create) or stored value (during Update) and returns and error if they have been changed.
Attributes are defined by passing pairs of fields and default values:
ProtectedAttributesValidator(fire.Map{
"title": "A fixed title",
})
func VerifyReferencesValidator ¶
VerifyReferencesValidator makes sure all references in the document are existing by counting on the related collections.
References are defined by passing pairs of fields and collections where the field must be a database field on the resource model:
VerifyReferencesValidator(fire.Map{
"post_id": "posts",
"user_id": "users",
})
type Context ¶
type Context struct {
// The current action in process.
Action Action
// The query that will be used during FindAll, FindOne, Update or Delete.
// On FindOne, Update and Delete, the "_id" key is preset to the document ID.
// On FindAll all field filters and relationship filters are preset.
Query bson.M
// The Model that will be saved during Create or Update.
Model model.Model
// The sorting that will be used during FindAll.
Sorting []string
// The store that is used to retrieve and persist the model.
Store *model.Store
// The underlying JSON API request.
Request *jsonapi.Request
// The underlying echo context.
Echo echo.Context
// contains filtered or unexported fields
}
A Context provides useful contextual information.
type Controller ¶
type Controller struct {
// The model that this controller should provide (e.g. &Foo{}).
Model model.Model
// FilterableFields defines the attributes that are filterable.
FilterableFields []string
// SortableFields defines the attributes that are sortable.
SortableFields []string
// The store that is used to retrieve and persist the model.
Store *model.Store
// The Authorizer is run on all actions. Will return an Unauthorized status
// if an user error is returned.
Authorizer Callback
// The Validator is run to validate Create, Update and Delete actions. Will
// return a Bad Request status if an user error is returned.
Validator Callback
// The NoList property can be set to true if the resource is only listed
// through relationships from other resources. This is useful for
// resources like comments that should never listed without a relationship.
NoList bool
// The ListLimit can be set to a value higher than 1 to enforce paginated
// responses and restrain the page size to be within one and the limit.
ListLimit int
// contains filtered or unexported fields
}
A Controller provides a JSON API based interface to a model.
Note: Controllers must not be modified after adding to an application.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
A Group manages access to multiple controllers and their interconnections.
func NewGroup ¶
NewGroup creates and returns a new group.
Note: You should pass the full URL prefix of the API to allow proper generation of resource links.
func (*Group) Add ¶
func (g *Group) Add(controllers ...*Controller)
Add will add a controller to the group.
func (*Group) Describe ¶
func (g *Group) Describe() fire.ComponentInfo
Describe implements the fire.Component interface.