Skip to content
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

Querystring filter #24

Merged
merged 3 commits into from
Feb 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 53 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ module.exports = {
{ label: 'External URL', value: 'external', showFields: [ 'externalUrl' ] }
]
},
{
name: 'ignoreQueryString',
label: 'Ignore query string when matching.',
type: 'boolean',
def: false
},
{
name: '_newPage',
type: 'joinByOne',
Expand Down Expand Up @@ -100,6 +106,7 @@ module.exports = {
'redirectSlug',
'title',
'urlType',
'ignoreQueryString',
'_newPage',
'externalUrl',
'statusCode'
Expand Down Expand Up @@ -135,36 +142,55 @@ module.exports = {

// Check to see if a redirect exists before sending user on their way
self.expressMiddleware = function (req, res, next) {
var slug = req.url;
return self.find(req, { slug: 'redirect-' + slug }, {
title: 1,
slug: 1,
urlType: 1,
pageId: 1,
type: 1,
externalUrl: 1,
redirectSlug: 1,
statusCode: 1,
_newPage: 1
}).toObject(function (err, result) {
if (err) {
console.log(err);
}
if (result) {
var status = parseInt(result.statusCode);

if (isNaN(status) || !status) {
status = 302;

let slug = req.url;
let pathOnly = self.apos.utils.regExpQuote(slug.split('?')[0]);
let redirectRegEx = new RegExp(`redirect-${pathOnly}.*`);

let redirectResult = self.find(req, { slug: redirectRegEx }, {
title: 1,
slug: 1,
urlType: 1,
ignoreQueryString: 1,
pageId: 1,
type: 1,
externalUrl: 1,
redirectSlug: 1,
statusCode: 1,
_newPage: 1
}).toArray(function(err, results) {
if (err) {
console.log(err);
}

if (result.urlType === 'internal' && result._newPage) {
return req.res.redirect(status, result._newPage._url);
} else if (result.urlType === 'external' && result.externalUrl.length) {
return req.res.redirect(status, result.externalUrl);
let target;
if (results) {

if (results.some(result => result.redirectSlug == slug)) {
target = results.find(result => result.redirectSlug == slug);
} else if (results.some(result => result.redirectSlug == pathOnly && result.ignoreQueryString)) {
target = results.find(result => result.redirectSlug == pathOnly && result.ignoreQueryString);
}

if (target) {

let status = parseInt(target.statusCode);

if (isNaN(status) || !status) {
status = 302;
}

if (target.urlType === 'internal' && target._newPage) {
return req.res.redirect(status, target._newPage._url);
} else if (target.urlType === 'external' && target.externalUrl.length) {
return req.res.redirect(status, target.externalUrl);
}
}

}
}
return next();
});
return next();
});

};
}
};