-
Notifications
You must be signed in to change notification settings - Fork 440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature request] Pass default handler to customHandlers #54
Comments
In your second case defaultHandler would then just return this.get('/posts', function(db, request, defaultHandler) {
var payload = db.contacts;
payload.meta = { page: 1, timestamp: (new Date()).toString() };
return payload;
}); (btw it's db instead of store now) |
just to clarify, because the default handler changes based on the second param to this[verb] (undefined, array, string etc.) |
Yes, with In my second example it's not that simple as |
still confused...
bey default handler here are you referring to the first one that you wrote within your own |
might be easy if you just gist me the entire |
I have a few routes that follow the same patterns, so I created a function that generates functions. import config from '../config/environment';
var defaultPageSize = config.APP.defaultPageSize;
function servePaginatedAndCoalesced(type, { filterField }) {
let normalizedType = type.underscore();
let normalizedCollectionType = type.pluralize().underscore();
return function(store, request) {
let results = store.findAll(normalizedType);
let ids = request.queryParams.ids;
if (ids) {
return { [normalizedCollectionType]: results.filter(e => ids.indexOf(''+e.id) > -1) };
} else {
let total = results.length;
let page = 1;
let filter = request.queryParams.filter;
if (filter && filter.length > 1) {
filter = filter.toLowerCase();
results = results.filter(s => s[filterField].toLowerCase().indexOf(filter) > -1);
}
let payload = {};
let pagedResults = results.slice(0, defaultPageSize);
payload.meta = { total, page };
payload[normalizedCollectionType] = pagedResults;
return payload;
}
};
}
this.get('/schools', servePaginatedAndCoalesced('school', { filterField: 'name' }));
this.get('/teachers', servePaginatedAndCoalesced('teacher', { filterField: 'surname' }));
this.get('/staff_members', servePaginatedAndCoalesced('staff_member', { filterField: 'surname' }));
this.get('/students', servePaginatedAndCoalesced('student', { filterField: 'surname' }));
// And some more The real backend will a full featured elastic search filtering, but in mirage I am just creating some endpoints that:
The reason why I wanted to receive the defaultHandler is to avoid to implement the point 2 myself. |
So I think one solution to this would be to define routes that differentiate on query params, for example: this.get('/schools?ids=:ids', function(store, request) {
let ids = request.queryParmas.ids;
// query logic
});
this.get('/schools'); // default Others have requested this too, e.g. to split up query logic across routes. Would this suit your purposes here? Or are you requesting a generic handler build-in to Mirage that understands |
That is good enough for most use cases 👍 |
Filing this under the "Calling super / expose default route handler" note in the roadmap |
I'll expose my use case to see if you see this as something it might worth to add.
I have some index routes where I need to perform pagination (and also filtering if a
filter=abc
query param is received) but also they need to handle coalescedRequests like/posts?ids=1,3,5
.I've defined a custom hander like this one:
I thought that a situation where you want to use the default behavior or a slightly modified version of the default behavior, receiving the default handler as a third parameter in that function could be useful.
P.e:
or
Thoughts? I've located the part of the code that has to be changed and seems fairly minimal.
The text was updated successfully, but these errors were encountered: