Skip to content

Commit

Permalink
Merge pull request #30 from holidayextras/depsVersionUpForNode16
Browse files Browse the repository at this point in the history
Update to node 16 and hapi 21
  • Loading branch information
putvande authored May 3, 2023
2 parents 233b108 + fd78b6e commit 2a07da5
Show file tree
Hide file tree
Showing 9 changed files with 6,631 additions and 1,802 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8
16
52 changes: 26 additions & 26 deletions lib/pluginJsonapi.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
'use strict'

var Boom = require('boom')
var _ = require('lodash')
var pluginName = 'pluginJsonapi'
var utilities = require('./utilities')
const Boom = require('@hapi/boom')
const _ = require('lodash')
const pluginName = 'pluginJsonapi'
const utilities = require('./utilities')

// some paths sinply don't want to be jsonapi'd
var ignorePaths = [
const ignorePaths = [
'/', // Lout documentation and AWS healthcheck
'/css/{path*}' // Lout documentation
]

// certain status codes shouldn't be jsonapi'd
var ignoreStatusCodes = [
const ignoreStatusCodes = [
201, // Created (used on POST)
204 // No Content (used on DELETE)
]

var proxyableKeys = [
const proxyableKeys = [
'contentVersion',
'lang',
'ticketRates',
'roomRates'
]

var pluginJsonApi = module.exports = {}
const pluginJsonApi = module.exports = {}

pluginJsonApi.errorHandler = function errorHandler (request, reply, error) {
request.log(['error', __filename, pluginName], {
Expand All @@ -36,7 +36,7 @@ pluginJsonApi.errorHandler = function errorHandler (request, reply, error) {
}

pluginJsonApi.alsoMakeItSo = function alsoMakeItSo (request, reply) {
// eslint-disable-line consistent-return
// eslint-disable-line consistent-return
try {
// If there was no error and it's not one of our ignoredPaths or ignored status codes, jsonapi it
if (request.response.isBoom || _.includes(ignorePaths, _.get(request, 'route.path')) || _.includes(ignoreStatusCodes, request.response.statusCode)) {
Expand All @@ -45,24 +45,24 @@ pluginJsonApi.alsoMakeItSo = function alsoMakeItSo (request, reply) {
}

// Make the response a bit more accessible
var result = _.get(request, 'response.source')
const result = _.get(request, 'response.source')
// check a bind configuration is present
if (!_.get(request, 'route.settings.bind.resourceName')) {
throw new Error('configuration bind.resourceName not found on handler')
}

// Get the current resource from the result
var resources = result[request.route.settings.bind.resourceName]
const resources = result[request.route.settings.bind.resourceName]
if (!resources) {
// no resources, return early
return reply.continue
}

var stateObject = {
request: request,
resources: resources,
const stateObject = {
request,
resources,
linked: result.linked,
proxyableKeys: proxyableKeys
proxyableKeys
}

/*
Expand All @@ -73,17 +73,17 @@ pluginJsonApi.alsoMakeItSo = function alsoMakeItSo (request, reply) {
* resolveLinkedData() - resolve the sub resources which we have collected previously
*/
return utilities.collectIncludes(stateObject)
.then(utilities.collectProxyableValues)
.then(utilities.getSubResources)
.then(utilities.getResources)
.then(utilities.resolveLinkedData)
.then(function () {
.then(utilities.collectProxyableValues)
.then(utilities.getSubResources)
.then(utilities.getResources)
.then(utilities.resolveLinkedData)
.then(function () {
// all done
return reply.continue
})
.fail(function (error) {
return pluginJsonApi.errorHandler(request, reply, error)
})
return reply.continue
})
.fail(function (error) {
return pluginJsonApi.errorHandler(request, reply, error)
})
} catch (error) {
return pluginJsonApi.errorHandler(request, reply, error)
}
Expand All @@ -110,7 +110,7 @@ const register = function (server, options) {
}

const pkg = require('../package.json')
const {version, name} = pkg
const { version, name } = pkg

pluginJsonApi.plugin = {
register,
Expand Down
88 changes: 49 additions & 39 deletions lib/utilities.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint valid-jsdoc:0 */
'use strict'

var _ = require('lodash')
var Q = require('q')
var path = require('path')
var qs = require('qs')
var url = require('url')
const _ = require('lodash')
const Q = require('q')
const path = require('path')
const qs = require('qs')
const url = require('url')

var utilities = module.exports = {}
const utilities = module.exports = {}

// collect the includes for the sub resources
utilities.collectIncludes = function collectIncludes (stateObject) {
Expand All @@ -20,7 +20,7 @@ utilities.collectIncludes = function collectIncludes (stateObject) {
}

utilities.collectProxyableValues = function collectProxyableValues (stateObject) {
var queryData = _.get(stateObject, 'request.data.query')
const queryData = _.get(stateObject, 'request.data.query')

stateObject.proxiedValues = _.reduce(queryData, function (matchedValues, queryValue, queryKey) {
if (_.includes(stateObject.proxyableKeys, queryKey)) {
Expand All @@ -35,8 +35,8 @@ utilities.collectProxyableValues = function collectProxyableValues (stateObject)
// From a type and an array of ids or filters, create a href to find things on
// There may also be context to pass on to the new url
utilities._buildHref = function _buildHref (hrefParameters, context, query = {}) {
var ids = _.isArray(hrefParameters.ids) ? hrefParameters.ids.join(',') : hrefParameters.ids
var queryString = ''
let ids = _.isArray(hrefParameters.ids) ? hrefParameters.ids.join(',') : hrefParameters.ids
let queryString = ''
if (_.has(context, hrefParameters.type)) {
// build the url parts of context
query.context = {}
Expand Down Expand Up @@ -112,7 +112,7 @@ utilities._addSubResourceRequest = function _addSubResourceRequest (subResourceR
utilities._getResourcesLinks = function _getResourcesLinks (links) {
// Loop each resource in the links and where we have a 'type' and 'id' or 'filter', build a href
_.forEach(links, function (link) {
var hrefParameters = {
const hrefParameters = {
type: link.type
}

Expand Down Expand Up @@ -142,7 +142,7 @@ utilities._getResourcesLinks = function _getResourcesLinks (links) {
// get the resources
utilities.getResources = function getResources (stateObject) {
// Controllers should return an array of resources but double check here
var resourceArray = (_.isArray(stateObject.resources)) ? stateObject.resources : [stateObject.resources]
const resourceArray = (_.isArray(stateObject.resources)) ? stateObject.resources : [stateObject.resources]

// Loop the resources and pad out any required hrefs
_.forEach(resourceArray, function (resource) {
Expand All @@ -166,7 +166,7 @@ utilities.getResources = function getResources (stateObject) {
// get the sub resources which are selected from the 'include' in the query string
utilities.getSubResources = function getSubResources (stateObject) {
// Controllers should return an array of resources but double check here
var resourceArray = (_.isArray(stateObject.resources)) ? stateObject.resources : [stateObject.resources]
const resourceArray = (_.isArray(stateObject.resources)) ? stateObject.resources : [stateObject.resources]

if (!stateObject.includes) {
// no resources to include
Expand Down Expand Up @@ -196,8 +196,8 @@ utilities.getSubResources = function getSubResources (stateObject) {

// request the sub resources.
utilities._requestSubResource = async function _requestSubResource (request, info, type, proxiedValues) {
var hrefParameters = {
type: type,
const hrefParameters = {
type,
ids: info.ids
}

Expand All @@ -213,26 +213,36 @@ utilities._requestSubResource = async function _requestSubResource (request, inf

// Where will we find this resource?
// if a `context` is available, proxy it to sub resources
var subResourceUrl = utilities._buildHref(hrefParameters, request.url.query.context, proxiedValues)
const subResourceUrl = utilities._buildHref(hrefParameters, request.data?.query?.context, proxiedValues)

var headerHost = _.get(request, 'auth.artifacts.host')
let headerHost = _.get(request, 'auth.artifacts.host')
if (_.get(request, 'auth.artifacts.port')) {
headerHost += ':' + request.auth.artifacts.port
}

var headers = {
const headers = {
host: headerHost
}
if (request.headers && request.headers.referrer) {
headers.referrer = request.headers.referrer
}

const auth = {
...request.auth
}
if (!auth.credentials) {
auth.credentials = {}
}
if (!auth.strategy) {
auth.strategy = {}
}

const response = await request.server.inject({
url: subResourceUrl,
credentials: request.auth.credentials,
artifacts: request.auth.artifacts,
headers: headers
auth,
headers
})

// Append the result to our linked property if it responded without an error
if (!response.result.error) {
return response.result
Expand All @@ -249,37 +259,37 @@ utilities.resolveLinkedData = function resolveLinkedData (stateObject) {
return Q.resolve()
}

var result = _.get(stateObject.request, 'response.source')
var proxyDeferreds = []
const result = _.get(stateObject.request, 'response.source')
const proxyDeferreds = []

// send the sub resources
_.forEach(stateObject.subResourceRequests, function (info, type) {
proxyDeferreds.push(utilities._requestSubResource(stateObject.request, info, type, stateObject.proxiedValues))
})

return Q.all(proxyDeferreds)
.then(function (subResources) {
if (!result.linked) {
result.linked = {}
}
.then(function (subResources) {
if (!result.linked) {
result.linked = {}
}

// Promise chain returns an array, so...
_.forEach(subResources, function (subResource) {
// Promise chain returns an array, so...
_.forEach(subResources, function (subResource) {
// Shard off the meta and merge that with the meta for the same type at the root ensuring
// It's an object if singular resource, an array if multiple
_.merge(result.meta, subResource.meta)
_.merge(result.meta, subResource.meta)

// Before we delete them, boost any linked data onto the primary resource
_.merge(result.linked, subResource.linked)
// Before we delete them, boost any linked data onto the primary resource
_.merge(result.linked, subResource.linked)

// Lose the meta and linked before we merge in the actual resource
delete subResource.meta
delete subResource.linked
// Lose the meta and linked before we merge in the actual resource
delete subResource.meta
delete subResource.linked

// and finaly boost the primary resource as linked data itself
_.merge(result.linked, subResource)
})
// and finaly boost the primary resource as linked data itself
_.merge(result.linked, subResource)
})

return Q.resolve()
})
return Q.resolve()
})
}
Loading

0 comments on commit 2a07da5

Please sign in to comment.