Skip to content

Commit

Permalink
fix(handler): enable handlers to attach filters to the request, fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSwitch authored Jan 23, 2025
1 parent 68ecdd8 commit d6323b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/format_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,21 @@ async function format_request(options, dareInstance) {
// Loop through the joins array
if (joins.length) {
// Loop through the joins and pass through the formatter
const a = joins.map(join_object => {
const a = joins.map(async join_object => {
// Set the parent
join_object.parent = options;

// Format join...
return format_request(join_object, dareInstance);
const formatedObject = await format_request(join_object, dareInstance);

// If this is present
if (formatedObject) {
// The handler may have assigned filters when their previously wasn't any
formatedObject.has_filter ||= Boolean(formatedObject.filter);
}

return formatedObject;

});

// Add Joins
Expand Down
37 changes: 37 additions & 0 deletions test/specs/format_request.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';
import assert from 'node:assert/strict';
import Dare from '../../src/index.js';
/*
* Format Request
Expand Down Expand Up @@ -827,6 +828,42 @@ describe('format_request', () => {
expect(commentsJoin).to.not.have.property('filter');
});

it('should allow nested children to assign filters', async () => {
const removed = {removed: false};

dare.options.method = method;
dare.options.models = {
users: {
schema: {},
get(options) {
// Add a filter to users to only show user who haven't been removed
options.filter = removed;
},
},
comments: {
schema: {
// Join definition to users model
user_id: ['users.id'],
},
},
};

/**
* Run a setup whereby we call `users` tree from within the `comments` tree, but with no `users` filter
*/
const comments = await dare.format_request({
method,
table: 'comments',
fields: ['name', {users: ['name']}],
});

/**
* Test that `has_filter` and `filter` have been assigned to the `users` tree
*/
assert.strictEqual(comments._joins.at(0).has_filter, true);
assert.deepStrictEqual(comments._joins.at(0).filter, removed);
});

it('should await the response from a promise', () => {
const msg = 'snap';
dare.options.method = method;
Expand Down

0 comments on commit d6323b7

Please sign in to comment.