Skip to content
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ matchHeaders = ["X-UserName"]
matchProps = ["id"]
# Create a whitelist of properties (query/body) to base cache on.
# Or set to false to match no properties. Default is to match all.
ignorePath = ".*?\/file.*"
# Ignores caching for all url containing provided regexp expression.
# This exemple will ignore all paths with /file in url ex. https://example.org/exmaple/file/test.png
ignoreProps = ["nonce"]
# Create a list of properties to ignore when otherwise matching all.
contentType = "application/json"
Expand Down
16 changes: 16 additions & 0 deletions src/app-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {parse} from 'url';
import querystring from 'querystring';
import {join} from 'path';
import config from 'config';

// Utility methods
function stripSpecialChars (val) {
Expand Down Expand Up @@ -61,9 +62,24 @@ function getReqHeaders (req, match) {
}

export function shouldIgnore ({url}) {
const mapping = getMapping(url);
if (mapping && mapping.ignorePath && url.match(new RegExp(mapping.ignorePath, 'i'))) {
return true;
}
return url === '' || url === '/' || url.startsWith('/__');
}

export function getMapping (url) {
const reqUrl = url.startsWith('/') ? url.substr(1) : url;
const key = reqUrl.split('/')[0];
const mappings = config.has('mappings') ? config.get('mappings') : {};
const mapmap = new Map();
Object.keys(mappings).forEach(key => mapmap.set(key, { key, ...mappings[key] }));
if (mappings.has(key)) {
return mappings.get(key);
}
}

export function resolveMockPath (req, dataRoot) {
// Mock data directory associated with the API call
let path = join(req.conf.dir, req.method);
Expand Down
9 changes: 3 additions & 6 deletions src/mappings.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import config from 'config';
import {shouldIgnore} from './app-utils';
import {shouldIgnore, getMapping} from './app-utils';

const mappings = config.has('mappings') ? config.get('mappings') : {};

const mapmap = new Map();

Object.keys(mappings).forEach(key => mapmap.set(key, { key, ...mappings[key] }));

const middleware = () => (req, res, next) => {
if (shouldIgnore(req)) {
return next();
}
// remove a leading slash if there is any
const reqUrl = req.url.startsWith('/') ? req.url.substr(1) : req.url;
const key = reqUrl.split('/')[0];
if (mappings.has(key)) {
const mapping = mappings.get(key);
const mapping = getMapping(req.url);
if (mapping) {
const conf = {
key: key,
dir: mapping.dir || key,
Expand Down