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

add support for pagination hook #75

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/hooks/helpers/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ function parseNestedPath(path, params) {

function parsePath(hook, config = {removePathFromCacheKey: false, parseNestedRoutes: false}) {
const q = hook.params.query || {};
const paginate = hook.params.paginate === false ? 'off' : 'on'; // if `.paginate` is underfined it means pagination hook is enabled, that's why we are using strict check with false
const remove = config.removePathFromCacheKey;
const parseNestedRoutes = config.parseNestedRoutes;
let path = remove && hook.id ? '' : `${hook.path}`;
let path = remove && hook.id ? `paginate:${paginate}:` : `paginate:${paginate}:${hook.path}`;

if (!remove && parseNestedRoutes) {
path = parseNestedPath(path, hook.params);
path = `paginate:${paginate}:${parseNestedPath(path, hook.params)}`;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could move this to after the processing since parseNestedPath would ignore the value right?


if (hook.id) {
Expand Down
70 changes: 43 additions & 27 deletions src/routes/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,53 @@ function routes(app) {
}

// Gets the value of a key in the redis client
client.get(`${target}`, (err, reply) => {
if (err) {
res.status(HTTP_SERVER_ERROR).json({
message: 'something went wrong' + err.message
});
} else {
// If the key existed
if (reply) {
// Clear existing cached key
h.clearSingle(target).then(r => {
res.status(HTTP_OK).json({
message: `cache cleared for key (${hasQueryString ?
'with' : 'without'} params): ${target}`,
status: HTTP_OK
});
const clearSingleKey = (key) => new Promise((resolve) => {
client.get(key, (err, reply) => {
if (err) {
res.status(HTTP_SERVER_ERROR).json({
message: 'something went wrong' + err.message
});
} else {
/**
* Empty reply means the key does not exist.
* Must use HTTP_OK with express as HTTP's RFC stats 204 should not
* provide a body, message would then be lost.
*/
res.status(HTTP_OK).json({
message: `cache already cleared for key (${hasQueryString ?
'with' : 'without'} params): ${target}`,
status: HTTP_NO_CONTENT
});
// If the key existed
if (reply) {
// Clear existing cached key
h.clearSingle(key)
.then(r => {
resolve({
message: `cache cleared for key (${hasQueryString ? 'with' : 'without'} params): ${key}`,
status: HTTP_OK
});
});
} else {
/**
* Empty reply means the key does not exist.
* Must use HTTP_OK with express as HTTP's RFC stats 204 should not
* provide a body, message would then be lost.
*/
resolve({
message: `cache already cleared for key (${hasQueryString ? 'with' : 'without'} params): ${key}`,
status: HTTP_NO_CONTENT
});
}
}

}
});
});

Promise.all([
clearSingleKey(`paginate:on:${target}`),
clearSingleKey(`paginate:off:${target}`),
])
.then(([withPagResult, witoutPagResult]) => {
if (withPagResult.status === HTTP_OK) {
res.status(HTTP_OK).json(withPagResult);
} else if (witoutPagResult.status === HTTP_OK) {
res.status(HTTP_OK).json(witoutPagResult);
} else if (withPagResult.status !== HTTP_OK) {
res.status(HTTP_OK).json(withPagResult);
} else {
res.status(HTTP_OK).json(witoutPagResult);
}
});
} else {
res.status(HTTP_NOT_FOUND).end();
}
Expand Down