Skip to content

Commit

Permalink
refactored the construction of regex pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofsajdak committed Jan 2, 2016
1 parent b676216 commit a062c76
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,7 @@ exports.register = function (server, opts, next) {

server.ext('onRequest', (request, reply) => {
if (request.method === 'get') {
const resourcesPattern = _.keys(schemas).join('|');
const uuidPattern = `[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[34][0-9a-fA-F]{3}-[89ab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}`
var relatedLinkPattern = `\/(${resourcesPattern})\/(${uuidPattern})\/([^/?#]+)`;
const match = request.url.pathname.match(new RegExp(relatedLinkPattern));
const match = request.url.pathname.match(generateRelatedMatchPattern());
if (match) {
const type = match[1];
const parentId = match[2];
Expand Down Expand Up @@ -284,6 +281,12 @@ exports.register = function (server, opts, next) {
}
})

const generateRelatedMatchPattern = function() {
const resourcesPattern = _.keys(schemas).join('|')
const uuidPattern = `[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[34][0-9a-fA-F]{3}-[89ab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}`
return new RegExp(`\/(${resourcesPattern})\/(${uuidPattern})\/([^/?#]+)`)
};

server.ext('onPreResponse', (request, reply) => {
function globallySetContentTypeJsonApi(request) {
request.response = request.response || {}
Expand Down

0 comments on commit a062c76

Please sign in to comment.