Skip to content

Commit 608d5cd

Browse files
committed
Add documentation
1 parent 2396f15 commit 608d5cd

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ To use this library, you describe the special behavior (if any) that resources o
7777

7878
- <a name="before-save"></a>`beforeSave` (optional): a function called on each resource provided by the client (i.e. in a `POST` or `PATCH` request) before it's sent to the adapter for saving. You can transform the data here as necessary or pre-emptively reject the request. [Usage details](https://github.com/ethanresnick/json-api-example/blob/master/src/resource-descriptions/people.js#L25).
7979

80+
- <a name="before-delete"></a>`beforeDelete` (optional): a function called on each resource to be deleted, allowing you to throw an error if the action is not allowed. See [Handling Errors](#handling-errors) for more information.
81+
8082
- <a name="labels"></a>`labelMappers` (optional): this lets you create urls (or, in REST terminology, resources) that map to different database items over time. For example, you could have an `/events/upcoming` resource or a `/users/me` resource. In those examples, "upcoming" and "me" are called the labels and, in labelMappers, you provide a function that maps each label to the proper database id(s) at any given time. The function can return a Promise if needed.
8183

8284
- <a name="parentType"></a>`parentType` (optional): this allows you to designate one resource type being a sub-type of another (its `parentType`). This is often used when you have two resource types that live in the same database table/collection, and their type is determined with a discriminator key. See the [`schools` type](https://github.com/ethanresnick/json-api-example/blob/master/src/resource-descriptions/schools.js#L2) in the example repository.
@@ -107,5 +109,31 @@ This library gives you a Front controller (shown in the example) that can handle
107109

108110
In the example above, routing is handled with Express's built-in `app[VERB]` methods, and the three parameters are set properly using express's built-in `:param` syntax. If you're looking for something more robust, you might be interested in [Express Simple Router](https://github.com/ethanresnick/express-simple-router). For authentication, check out [Express Simple Firewall](https://github.com/ethanresnick/express-simple-firewall).
109111

112+
## Handling Errors
113+
114+
This library provides an error contructor and a handler that you can use to return JSON API-compliant errors to the user. For example, here is how you would handle 404s.
115+
116+
```javascript
117+
var API = require("json-api");
118+
var APIError = API.types.Error;
119+
120+
// Your route definitions would go here, but if none match...
121+
122+
app.use(function(req, res, next) {
123+
var err = new APIError(404, undefined, "Not Found");
124+
Front.sendError(err, req, res);
125+
});
126+
```
127+
128+
You can also throw an APIError inside `beforeSave`, `beforeRender`, and `beforeDelete` transforms to cancel the request.
129+
130+
```javascript
131+
beforeDelete: function(resource, req, res, superFn) {
132+
if (req.user.role !== "admin") {
133+
throw APIError(403, undefined, "You are not allowed to delete this resource.");
134+
}
135+
}
136+
```
137+
110138
## Database Adapters
111139
An adapter handles all the interaction with the database. It is responsible for turning requests into standard [`Resource`](https://github.com/ethanresnick/json-api/blob/master/src/types/Resource.js) or [`Collection`](https://github.com/ethanresnick/json-api/blob/master/src/types/Collection.js) objects that the rest of the library will use. See the built-in [MongooseAdapter](https://github.com/ethanresnick/json-api/blob/master/src/db-adapters/Mongoose/MongooseAdapter.js) for an example.

0 commit comments

Comments
 (0)