Restgoose

Model-driven REST API framework using decorators

API reference

Restgoose.initialize()

This is the enrty point of Restgoose. It creates all the routes in the given context. Example:

const app = express();
app.use(Restgoose.initialize());

@rest(config)

The main decorator of Restgoose. It can be placed on top of a class extending Typegoose, or on top of a property in such a class (see Using @rest() on submodels).

Middlewares composition

Restgoose provides two helper function to compose your middlewares, easing the reusability of your middlewares.

The returned functions generated by or()/and() can themselves be used in another or()/and(), allowing complex logic :

or(and(middlewareA, middlewareB), and(middlewareA, middlewareC))

NOTE : middleware composition does NOT work with the fetch or persist hooks.

asFilter(fn)

Converts a postFetch or preSend function throwing errors to a filtering one.

It returns the entity if the function didn’t throw, or null if the function has thrown an error.

This is typically used for the ‘all’ methods. You only need to write you middleware once, always throw an error instead of null, then wrap it around with asFilter().

Example:

function myPostFetchFn(req, item) {
    if (/* some check */) {
        return item;
    }
    else {
        throw new RestError(403, 'Cannot access item'); 
    }
}
// asFilter(myPostFetchFn) will return a function with the same signature, 
// but will return null if an error is thrown
@rest({
   route: '/items',
   methods: [
       all({
           postFetch: asFilter(myPostFetchFn)
       }),
       one({
           postFetch: myPostFetchFn
       })
   ]
})
export class Item extends Typegoose {
   /* ... */
}