Behind the scenes, getters and setters are registered as HTTP GET and HTTP POST routes, respectively, via Express. As a result, if necessary, custom middleware can be defined on an individual getter or setter.
Example Usage
api/books/getters.js
import rate_limiter_middleware from 'example-rate-limiter';
const getters = {
books: {
middleware: [
rate_limiter_middleware,
],
authorized: (input = {}, context = {}) => {
return !!context?.user;
},
get: (input = {}, context = {}) => {
return process.databases.mongodb.collection('books').find({
user_id: context?.user?._id,
category: input?.category,
}).toArray();
}
}
};
export default getters;
You can provide as many middleware functions as you'd like on a getter or setter, but it's important to be mindful of your middleware's effect on response performance. It's recommended to only use middleware when absolutely necessary to avoid surprise bottlenecks for your users.
API
Definition
middleware: [function];
Parameters
- middleware array[function]
- An array of Express middleware functions to execute in order before the getter or setter is run. Each middleware function receives (req, res, next) as arguments.