From d239af39c63a5740b013ed93755ca5447f0e3cfa Mon Sep 17 00:00:00 2001 From: b-gran Date: Thu, 26 Jan 2017 00:27:47 -0500 Subject: [PATCH] Convert to ES6 modules --- src/APIMethod.js | 9 +-- src/ERMOperation.js | 8 +-- src/RESTPathGenerator.js | 6 +- src/api/applyQueryToContext.js | 8 +-- src/api/db/createObject.js | 4 +- src/api/db/deleteItem.js | 8 +-- src/api/db/deleteItems.js | 6 +- src/api/db/getCount.js | 6 +- src/api/db/getItem.js | 11 ++- src/api/db/getItems.js | 11 ++- src/api/db/getShallow.js | 11 ++- src/api/db/modifyObject.js | 10 +-- src/api/getContext.js | 12 ++-- src/api/prepareQuery.js | 10 +-- src/api/shared.js | 10 +-- src/buildQuery.js | 8 +-- src/errorHandler.js | 6 +- src/express-restify-mongoose.js | 87 ++++++++++++----------- src/middleware/access.js | 8 ++- src/middleware/ensureContentType.js | 8 ++- src/middleware/onError.js | 6 +- src/middleware/outputFn.js | 4 +- src/middleware/prepareOutput.js | 11 +-- src/middleware/prepareQuery.js | 11 +-- src/operations.js | 31 +++++--- src/resource_filter.js | 8 +-- test/integration/resource_filter.js | 2 +- test/unit/APIMethod.js | 4 +- test/unit/RESTPathGenerator.js | 2 +- test/unit/api/prepareQuery.js | 2 +- test/unit/buildQuery.js | 2 +- test/unit/errorHandler.js | 2 +- test/unit/middleware/access.js | 2 +- test/unit/middleware/ensureContentType.js | 2 +- test/unit/middleware/onError.js | 2 +- test/unit/middleware/outputFn.js | 2 +- test/unit/middleware/prepareOutput.js | 2 +- test/unit/resourceFilter.js | 2 +- 38 files changed, 183 insertions(+), 161 deletions(-) diff --git a/src/APIMethod.js b/src/APIMethod.js index 3c2d7637..aeded2ff 100644 --- a/src/APIMethod.js +++ b/src/APIMethod.js @@ -1,5 +1,6 @@ -const _ = require('lodash') -const ERMOperation = require('./ERMOperation') +import _ from 'lodash' +import ERMOperation from './ERMOperation' +import getErrorHandler from './errorHandler' const privates = new WeakMap() @@ -44,7 +45,7 @@ class APIMethod { * @return {function(*=, *=, *=)} */ getMiddleware (initialState) { - const errorHandler = require('./errorHandler')(initialState.options) + const errorHandler = getErrorHandler(initialState.options) return (req, res, next) => { // Grab the current operation state from the request @@ -64,4 +65,4 @@ class APIMethod { } } -module.exports = APIMethod +export default APIMethod diff --git a/src/ERMOperation.js b/src/ERMOperation.js index 87597561..85d9f593 100644 --- a/src/ERMOperation.js +++ b/src/ERMOperation.js @@ -2,9 +2,9 @@ * @module './ERMOperation' */ -const ImmutableRecord = require('immutable-record') -const _ = require('lodash') -const Model = require('mongoose').Model +import ImmutableRecord from 'immutable-record' +import _ from 'lodash' +import { Model } from 'mongoose' /** * Underlying record for the Operation class @@ -196,4 +196,4 @@ function isModel (model) { return Object.getPrototypeOf(model) === Model } -module.exports = ERMOperation +export default ERMOperation diff --git a/src/RESTPathGenerator.js b/src/RESTPathGenerator.js index 1aa1ad52..8d8d102e 100644 --- a/src/RESTPathGenerator.js +++ b/src/RESTPathGenerator.js @@ -2,7 +2,7 @@ * Generate RESTful URI paths for the an Express router. */ -const _ = require('lodash') +import _ from 'lodash' /** * Given an array of URI paths, joins them together using forward slashes. @@ -101,5 +101,5 @@ function ensureString (value, valueName = 'value') { return null } -module.exports = RESTPathGenerator -module.exports.joinPathsAsURL = joinPathsAsURL +export default RESTPathGenerator +export { joinPathsAsURL } diff --git a/src/api/applyQueryToContext.js b/src/api/applyQueryToContext.js index 70a6ab00..4f301248 100644 --- a/src/api/applyQueryToContext.js +++ b/src/api/applyQueryToContext.js @@ -1,5 +1,5 @@ -const getQueryBuilder = require('../buildQuery') -const cloneMongooseQuery = require('./shared').cloneMongooseQuery +import getQueryBuilder from '../buildQuery' +import { cloneMongooseQuery } from './shared' /** * Given ERM query options, a Mongoose context (a ModelQuery), and a Mongo @@ -17,8 +17,8 @@ const cloneMongooseQuery = require('./shared').cloneMongooseQuery * * @return {Promise} */ -module.exports = function applyQueryToContext (queryOptions, mongooseContext, queryStringObject) { +export default function applyQueryToContext (queryOptions, mongooseContext, queryStringObject) { const buildQuery = getQueryBuilder(queryOptions) return buildQuery(cloneMongooseQuery(mongooseContext), queryStringObject) -} +}; diff --git a/src/api/db/createObject.js b/src/api/db/createObject.js index 4d24aba5..f71984a7 100644 --- a/src/api/db/createObject.js +++ b/src/api/db/createObject.js @@ -1,4 +1,4 @@ -const APIMethod = require('../../APIMethod') +import APIMethod from '../../APIMethod' /** * Given the body of an object, query options, and an access level (all in state), creates @@ -58,4 +58,4 @@ function doCreateObject (ermInstance, req) { }) } -module.exports = new APIMethod(doCreateObject) +export default new APIMethod(doCreateObject) diff --git a/src/api/db/deleteItem.js b/src/api/db/deleteItem.js index ebb1de6a..edd13910 100644 --- a/src/api/db/deleteItem.js +++ b/src/api/db/deleteItem.js @@ -1,6 +1,6 @@ -const http = require('http') -const APIMethod = require('../../APIMethod') -const Promise = require('bluebird') +import http from 'http' +import APIMethod from '../../APIMethod' +import Promise from 'bluebird' /** * Delete a single object. @@ -33,4 +33,4 @@ function doDeleteItem (state, req) { } } -module.exports = new APIMethod(doDeleteItem) +export default new APIMethod(doDeleteItem) diff --git a/src/api/db/deleteItems.js b/src/api/db/deleteItems.js index a726fa59..aae54fc0 100644 --- a/src/api/db/deleteItems.js +++ b/src/api/db/deleteItems.js @@ -1,5 +1,5 @@ -const APIMethod = require('../../APIMethod') -const applyQueryToContext = require('../applyQueryToContext') +import APIMethod from '../../APIMethod' +import applyQueryToContext from '../applyQueryToContext' /** * Delete all of the items specified by a query in an Express request. @@ -14,4 +14,4 @@ function doDeleteItems (state, req) { .then(() => state.set('statusCode', 204)) } -module.exports = new APIMethod(doDeleteItems) +export default new APIMethod(doDeleteItems) diff --git a/src/api/db/getCount.js b/src/api/db/getCount.js index 0b34387a..bc00fa04 100644 --- a/src/api/db/getCount.js +++ b/src/api/db/getCount.js @@ -1,5 +1,5 @@ -const APIMethod = require('../../APIMethod') -const applyQueryToContext = require('../applyQueryToContext') +import APIMethod from '../../APIMethod' +import applyQueryToContext from '../applyQueryToContext' function doGetCount (state, req) { return applyQueryToContext(state.options, state.context.count(), state.query) @@ -11,4 +11,4 @@ function doGetCount (state, req) { }) } -module.exports = new APIMethod(doGetCount) +export default new APIMethod(doGetCount) diff --git a/src/api/db/getItem.js b/src/api/db/getItem.js index 66360447..363859b1 100644 --- a/src/api/db/getItem.js +++ b/src/api/db/getItem.js @@ -1,8 +1,7 @@ -const isDistinctExcluded = require('./../shared').isDistinctExcluded -const http = require('http') - -const APIMethod = require('../../APIMethod') -const applyQueryToContext = require('../applyQueryToContext') +import { isDistinctExcluded } from './../shared' +import http from 'http' +import APIMethod from '../../APIMethod' +import applyQueryToContext from '../applyQueryToContext' /** * Retrieve a single document based on a request. Use the query and context filter specified in @@ -29,4 +28,4 @@ function doGetItem (state, req) { }) } -module.exports = new APIMethod(doGetItem) +export default new APIMethod(doGetItem) diff --git a/src/api/db/getItems.js b/src/api/db/getItems.js index efaad84e..e4e4eee0 100644 --- a/src/api/db/getItems.js +++ b/src/api/db/getItems.js @@ -1,8 +1,7 @@ -const isDistinctExcluded = require('./../shared').isDistinctExcluded -const _ = require('lodash') - -const APIMethod = require('../../APIMethod') -const applyQueryToContext = require('../applyQueryToContext') +import { isDistinctExcluded } from './../shared' +import _ from 'lodash' +import APIMethod from '../../APIMethod' +import applyQueryToContext from '../applyQueryToContext' /** * Get all of the items matching a query inside some consumer-provided context. @@ -60,4 +59,4 @@ function getTotalCountHeader (state, req) { return applyQueryToContext(state.options, state.context.count(), noSkipOrLimit) } -module.exports = new APIMethod(doGetItems) +export default new APIMethod(doGetItems) diff --git a/src/api/db/getShallow.js b/src/api/db/getShallow.js index 0fa0842f..1dd0caab 100644 --- a/src/api/db/getShallow.js +++ b/src/api/db/getShallow.js @@ -1,8 +1,7 @@ -const http = require('http') -const applyQueryToContext = require('../applyQueryToContext') -const _ = require('lodash') - -const APIMethod = require('../../APIMethod') +import http from 'http' +import applyQueryToContext from '../applyQueryToContext' +import _ from 'lodash' +import APIMethod from '../../APIMethod' /** * Given an object, replaces all of its Object-type properties with the value true. @@ -48,4 +47,4 @@ function doGetShallow (state, req) { }) } -module.exports = new APIMethod(doGetShallow) +export default new APIMethod(doGetShallow) diff --git a/src/api/db/modifyObject.js b/src/api/db/modifyObject.js index c8e1fb54..6b1bbc9c 100644 --- a/src/api/db/modifyObject.js +++ b/src/api/db/modifyObject.js @@ -1,7 +1,7 @@ -const APIMethod = require('../../APIMethod') -const moredots = require('moredots') -const _ = require('lodash') -const http = require('http') +import APIMethod from '../../APIMethod' +import moredots from 'moredots' +import _ from 'lodash' +import http from 'http' /** * Given a document and the document's model, depopulate any populated fields in the document. @@ -122,4 +122,4 @@ function performUpdateAndSave (state, req, updates) { return document.save() } -module.exports = new APIMethod(doModifyObject) +export default new APIMethod(doModifyObject) diff --git a/src/api/getContext.js b/src/api/getContext.js index 7e620421..2596b11a 100644 --- a/src/api/getContext.js +++ b/src/api/getContext.js @@ -1,8 +1,8 @@ -const http = require('http') -const _ = require('lodash') -const APIMethod = require('../APIMethod') -const Promise = require('bluebird') -const cloneMongooseQuery = require('./shared').cloneMongooseQuery +import http from 'http' +import _ from 'lodash' +import APIMethod from '../APIMethod' +import Promise from 'bluebird' +import { cloneMongooseQuery } from './shared' /** * @@ -42,4 +42,4 @@ function getContext (state, req) { }) } -module.exports = new APIMethod(getContext) +export default new APIMethod(getContext) diff --git a/src/api/prepareQuery.js b/src/api/prepareQuery.js index 8f635da8..dd0f4617 100644 --- a/src/api/prepareQuery.js +++ b/src/api/prepareQuery.js @@ -1,6 +1,6 @@ -const _ = require('lodash') -const Promise = require('bluebird') -const isCoordinates = require('is-coordinates') +import _ from 'lodash' +import Promise from 'bluebird' +import isCoordinates from 'is-coordinates' function parseQueryOptions (queryOptions) { if (queryOptions.select && _.isString(queryOptions.select)) { @@ -117,7 +117,7 @@ const QUERY_OPTIONS_WHITELIST = Object.freeze( * @param {boolean} allowRegex - whether or not regular expressions are allowed in the query string * @return {function(Object): function(Object): Promise} */ -module.exports = function (allowRegex) { +export default function (allowRegex) { return function (queryStringObject = {}) { return new Promise((resolve, reject) => { const baseQueryOptions = {} @@ -154,4 +154,4 @@ module.exports = function (allowRegex) { return resolve(parsedOptions) }) } -} +}; diff --git a/src/api/shared.js b/src/api/shared.js index eee31a64..fb601a5b 100644 --- a/src/api/shared.js +++ b/src/api/shared.js @@ -1,4 +1,4 @@ -const _ = require('lodash') +import _ from 'lodash' /** * Given a operation state, returns true if the 'distinct' option in the state's query @@ -7,12 +7,12 @@ const _ = require('lodash') * @param {ERMOperation} state * @return {boolean} */ -module.exports.isDistinctExcluded = function (state) { +export function isDistinctExcluded (state) { return state.options.filter.isExcluded(state.query.distinct, { access: state.accessLevel, excludedMap: state.excludedMap }) -} +}; /** * Given a mongoose query, clones the query so that changes to the query can be made without @@ -21,10 +21,10 @@ module.exports.isDistinctExcluded = function (state) { * @param {ModelQuery} mongooseQuery * @return {*} */ -module.exports.cloneMongooseQuery = function (mongooseQuery) { +export function cloneMongooseQuery (mongooseQuery) { if (!mongooseQuery || !_.isFunction(mongooseQuery.toConstructor)) { return mongooseQuery } return mongooseQuery.toConstructor()() -} +}; diff --git a/src/buildQuery.js b/src/buildQuery.js index d364d3b1..5e6dc41c 100644 --- a/src/buildQuery.js +++ b/src/buildQuery.js @@ -1,17 +1,17 @@ -const Promise = require('bluebird') +import Promise from 'bluebird' /** * Given global (ERM-instance-wide) query-building options, returns a function that extends a * partial query with local (request-specific) query options. * - * @param {Object} globalOptions - ERM-instance query options + * @param {Object} globalOptions - ERM-cinstance query options * @param {Number} globalOptions.limit - * @param {String} globalOptions.readPreference - * @param {Boolean} globalOptions.lean - * * @return {function(ModelQuery, Object): Promise} */ -module.exports = function (globalOptions) { +export default function (globalOptions) { // Note: requestSpecificQueryOptions should be the result of a prepareQuery() call return function (query, requestSpecificQueryOptions) { const promise = new Promise((resolve, reject) => { @@ -64,4 +64,4 @@ module.exports = function (globalOptions) { return promise } -} +}; diff --git a/src/errorHandler.js b/src/errorHandler.js index 11f6e036..4eded544 100644 --- a/src/errorHandler.js +++ b/src/errorHandler.js @@ -1,6 +1,6 @@ -const http = require('http') +import http from 'http' -module.exports = function (options) { +export default function (options) { return function (req, res, next) { return function (err) { req.erm = req.erm || {} @@ -14,4 +14,4 @@ module.exports = function (options) { options.onError(err, req, res, next) } } -} +}; diff --git a/src/express-restify-mongoose.js b/src/express-restify-mongoose.js index 99a726dd..a1826ecf 100644 --- a/src/express-restify-mongoose.js +++ b/src/express-restify-mongoose.js @@ -1,9 +1,18 @@ -const util = require('util') -const _ = require('lodash') - -const Filter = require('./resource_filter') -const RESTPathGenerator = require('./RESTPathGenerator') -const ERMOperation = require('./ERMOperation') +import util from 'util' +import _ from 'lodash' +import Filter from './resource_filter' +import RESTPathGenerator from './RESTPathGenerator' +import ERMOperation from './ERMOperation' + +import getContext from './api/getContext' +import access from './middleware/access' +import ensureContentType from './middleware/ensureContentType' +import onError from './middleware/onError' +import outputFn from './middleware/outputFn' +import prepareQuery from './middleware/prepareQuery' +import prepareOutput from './middleware/prepareOutput' + +import operations from './operations' let customDefaults = null let excludedMap = {} @@ -36,13 +45,9 @@ const restify = function (app, model, opts = {}) { let options = {} _.assign(options, getDefaults(), opts) - const getContext = require('./api/getContext') - const access = require('./middleware/access') - const ensureContentType = require('./middleware/ensureContentType')(options) - const onError = require('./middleware/onError') - const outputFn = require('./middleware/outputFn') - const prepareQuery = require('./middleware/prepareQuery')(options) - const prepareOutput = require('./middleware/prepareOutput')(options, excludedMap) + const ensureContentTypeMiddleware = ensureContentType(options) + const prepareQueryMiddleware = prepareQuery(options) + const prepareOutputMiddleware = prepareOutput(options, excludedMap) if (!_.isArray(options.private)) { throw new Error('"options.private" must be an array of fields') @@ -103,7 +108,7 @@ const restify = function (app, model, opts = {}) { const initialOperationState = ERMOperation.initialize(model, options, excludedMap) - const ops = require('./operations')(initialOperationState) + const ops = operations(initialOperationState) const restPaths = new RESTPathGenerator(options.prefix, options.version, options.name) if (_.isUndefined(app.delete)) { @@ -122,7 +127,7 @@ const restify = function (app, model, opts = {}) { function deprecatePrepareQuery (text) { return util.deprecate( - prepareQuery, + prepareQueryMiddleware, `express-restify-mongoose: in a future major version, ${text} ` + `Use PATCH instead.` ) @@ -131,35 +136,35 @@ const restify = function (app, model, opts = {}) { // Retrieval app.get( - restPaths.allDocuments, prepareQuery, options.preMiddleware, contextMiddleware, + restPaths.allDocuments, prepareQueryMiddleware, options.preMiddleware, contextMiddleware, options.preRead, accessMiddleware, ops.getItems, - prepareOutput + prepareOutputMiddleware ) app.get( - restPaths.allDocumentsCount, prepareQuery, options.preMiddleware, contextMiddleware, + restPaths.allDocumentsCount, prepareQueryMiddleware, options.preMiddleware, contextMiddleware, options.preRead, accessMiddleware, ops.getCount, - prepareOutput + prepareOutputMiddleware ) app.get( - restPaths.singleDocument, prepareQuery, options.preMiddleware, contextMiddleware, + restPaths.singleDocument, prepareQueryMiddleware, options.preMiddleware, contextMiddleware, options.preRead, accessMiddleware, ops.getItem, - prepareOutput + prepareOutputMiddleware ) app.get( - restPaths.singleDocumentShallow, prepareQuery, options.preMiddleware, contextMiddleware, + restPaths.singleDocumentShallow, prepareQueryMiddleware, options.preMiddleware, contextMiddleware, options.preRead, accessMiddleware, ops.getShallow, - prepareOutput + prepareOutputMiddleware ) // Creation app.post( - restPaths.allDocuments, prepareQuery, ensureContentType, options.preMiddleware, + restPaths.allDocuments, prepareQueryMiddleware, ensureContentTypeMiddleware, options.preMiddleware, options.preCreate, accessMiddleware, ops.createObject, - prepareOutput + prepareOutputMiddleware ) // Modification @@ -167,48 +172,50 @@ const restify = function (app, model, opts = {}) { app.post( restPaths.singleDocument, deprecatePrepareQuery('the POST method to update resources will be removed.'), - ensureContentType, options.preMiddleware, contextMiddleware, + ensureContentTypeMiddleware, options.preMiddleware, contextMiddleware, options.preUpdate, accessMiddleware, ops.modifyObject, - prepareOutput + prepareOutputMiddleware ) app.put( restPaths.singleDocument, deprecatePrepareQuery(`the PUT method will replace rather than update a resource.`), - ensureContentType, options.preMiddleware, contextMiddleware, + ensureContentTypeMiddleware, options.preMiddleware, contextMiddleware, options.preUpdate, accessMiddleware, ops.modifyObject, - prepareOutput + prepareOutputMiddleware ) app.patch( restPaths.singleDocument, - prepareQuery, ensureContentType, options.preMiddleware, contextMiddleware, + prepareQueryMiddleware, ensureContentTypeMiddleware, options.preMiddleware, contextMiddleware, options.preUpdate, accessMiddleware, ops.modifyObject, - prepareOutput + prepareOutputMiddleware ) // Deletion app.delete( restPaths.allDocuments, - prepareQuery, options.preMiddleware, contextMiddleware, + prepareQueryMiddleware, options.preMiddleware, contextMiddleware, options.preDelete, ops.deleteItems, - prepareOutput + prepareOutputMiddleware ) app.delete( restPaths.singleDocument, - prepareQuery, options.preMiddleware, contextMiddleware, + prepareQueryMiddleware, options.preMiddleware, contextMiddleware, options.preDelete, ops.deleteItem, - prepareOutput + prepareOutputMiddleware ) return restPaths.allDocuments } -module.exports = { - defaults: function (options) { - customDefaults = options - }, - serve: restify +function defaults (options) { + customDefaults = options +} + +export { + defaults, + restify as serve } diff --git a/src/middleware/access.js b/src/middleware/access.js index acb5ce45..f926313d 100644 --- a/src/middleware/access.js +++ b/src/middleware/access.js @@ -1,3 +1,5 @@ +import getErrorHandler from '../errorHandler' + /** * Returns Express middleware that calls the "access" function in options and adds the result to * the request object (as "req.access") @@ -8,8 +10,8 @@ * @param {function(req: Object, done: function?)} options.access - tells us what the access level for this route is, based on the request * @return {function(req, res, next)} */ -module.exports = function (options) { - const errorHandler = require('../errorHandler')(options) +export default function (options) { + const errorHandler = getErrorHandler(options) return function (req, res, next) { const handler = function (err, access) { @@ -33,4 +35,4 @@ module.exports = function (options) { handler(null, options.access(req)) } } -} +}; diff --git a/src/middleware/ensureContentType.js b/src/middleware/ensureContentType.js index 46e72d9f..d81773ea 100644 --- a/src/middleware/ensureContentType.js +++ b/src/middleware/ensureContentType.js @@ -1,5 +1,7 @@ -module.exports = function (options) { - const errorHandler = require('../errorHandler')(options) +import getErrorHandler from '../errorHandler' + +export default function (options) { + const errorHandler = getErrorHandler(options) return function ensureContentType (req, res, next) { const ct = req.headers['content-type'] @@ -14,4 +16,4 @@ module.exports = function (options) { next() } -} +}; diff --git a/src/middleware/onError.js b/src/middleware/onError.js index e76ee669..dedd00e0 100644 --- a/src/middleware/onError.js +++ b/src/middleware/onError.js @@ -1,6 +1,6 @@ -const serializeError = require('serialize-error') +import serializeError from 'serialize-error' -module.exports = function (isExpress) { +export default function (isExpress) { return function (err, req, res, next) { const serializedErr = serializeError(err) @@ -21,4 +21,4 @@ module.exports = function (isExpress) { res.send(req.erm.statusCode, serializedErr) } } -} +}; diff --git a/src/middleware/outputFn.js b/src/middleware/outputFn.js index 56e49b1d..34d8bd41 100644 --- a/src/middleware/outputFn.js +++ b/src/middleware/outputFn.js @@ -1,4 +1,4 @@ -module.exports = function (isExpress) { +export default function (isExpress) { return function output (req, res) { if (isExpress) { if (req.erm.result) { @@ -10,4 +10,4 @@ module.exports = function (isExpress) { res.send(req.erm.statusCode, req.erm.result) } } -} +}; diff --git a/src/middleware/prepareOutput.js b/src/middleware/prepareOutput.js index 484fb28b..4dad6381 100644 --- a/src/middleware/prepareOutput.js +++ b/src/middleware/prepareOutput.js @@ -1,8 +1,9 @@ -const _ = require('lodash') -const async = require('async') +import _ from 'lodash' +import async from 'async' +import getErrorHandler from '../errorHandler' -module.exports = function (options, excludedMap) { - const errorHandler = require('../errorHandler')(options) +export default function (options, excludedMap) { + const errorHandler = getErrorHandler(options) return function (req, res, next) { let postMiddleware @@ -56,4 +57,4 @@ module.exports = function (options, excludedMap) { } }) } -} +}; diff --git a/src/middleware/prepareQuery.js b/src/middleware/prepareQuery.js index 3e01e1b0..f62f44e4 100644 --- a/src/middleware/prepareQuery.js +++ b/src/middleware/prepareQuery.js @@ -1,6 +1,9 @@ -module.exports = function (options) { - const prepareQueryAsPromise = require('../api/prepareQuery')(options.allowRegex) - const errorHandler = require('../errorHandler')(options) +import getErrorHandler from '../errorHandler' +import getPrepareQuery from '../api/prepareQuery' + +export default function (options) { + const prepareQueryAsPromise = getPrepareQuery(options.allowRegex) + const errorHandler = getErrorHandler(options) return function (req, res, next) { prepareQueryAsPromise(req.query) @@ -12,4 +15,4 @@ module.exports = function (options) { return errorHandler(req, res, next)(err) }) } -} +}; diff --git a/src/operations.js b/src/operations.js index 6d5c4939..ca82fc7c 100644 --- a/src/operations.js +++ b/src/operations.js @@ -1,13 +1,22 @@ -module.exports = function (ermInstance) { - const getItems = require('./api/db/getItems').getMiddleware(ermInstance) - const getCount = require('./api/db/getCount').getMiddleware(ermInstance) - const getShallow = require('./api/db/getShallow').getMiddleware(ermInstance) - const deleteItems = require('./api/db/deleteItems').getMiddleware(ermInstance) - const getItem = require('./api/db/getItem').getMiddleware(ermInstance) - const deleteItem = require('./api/db/deleteItem').getMiddleware(ermInstance) - const createObject = require('./api/db/createObject').getMiddleware(ermInstance) - const modifyObject = require('./api/db/modifyObject').getMiddleware(ermInstance) +import getItems from './api/db/getItems' +import getCount from './api/db/getCount' +import getShallow from './api/db/getShallow' +import deleteItems from './api/db/deleteItems' +import getItem from './api/db/getItem' +import deleteItem from './api/db/deleteItem' +import createObject from './api/db/createObject' +import modifyObject from './api/db/modifyObject' - return { getItems, getCount, getItem, getShallow, createObject, modifyObject, deleteItems, deleteItem } -} +export default function (ermInstance) { + return { + getItems: getItems.getMiddleware(ermInstance), + getCount: getCount.getMiddleware(ermInstance), + getItem: getItem.getMiddleware(ermInstance), + getShallow: getShallow.getMiddleware(ermInstance), + createObject: createObject.getMiddleware(ermInstance), + modifyObject: modifyObject.getMiddleware(ermInstance), + deleteItems: deleteItems.getMiddleware(ermInstance), + deleteItem: deleteItem.getMiddleware(ermInstance) + } +}; diff --git a/src/resource_filter.js b/src/resource_filter.js index a883dd84..89d16205 100644 --- a/src/resource_filter.js +++ b/src/resource_filter.js @@ -1,6 +1,6 @@ -const _ = require('lodash') -const detective = require('mongoose-detective') -const weedout = require('weedout') +import _ from 'lodash' +import detective from 'mongoose-detective' +import weedout from 'weedout' /** * A wrapper around a Mongoose model that exposes utilities for filtering document keys @@ -203,4 +203,4 @@ Filter.prototype.filterObject = function (resource, opts = {}) { return filtered } -module.exports = Filter +export default Filter diff --git a/test/integration/resource_filter.js b/test/integration/resource_filter.js index 73b8e673..32078ae7 100644 --- a/test/integration/resource_filter.js +++ b/test/integration/resource_filter.js @@ -1,6 +1,6 @@ 'use strict' -const Filter = require('../../lib/resource_filter') +const Filter = require('../../lib/resource_filter').default const db = require('./setup')() const assert = require('assert') const ObjectId = require('mongoose').Types.ObjectId diff --git a/test/unit/APIMethod.js b/test/unit/APIMethod.js index 0dd56269..00df61ae 100644 --- a/test/unit/APIMethod.js +++ b/test/unit/APIMethod.js @@ -1,7 +1,7 @@ const assert = require('assert') const _ = require('lodash') -const APIMethod = require('../../src/APIMethod') -const ERMOperation = require('../../src/ERMOperation') +const APIMethod = require('../../src/APIMethod').default +const ERMOperation = require('../../src/ERMOperation').default /** * The Promise-based API operation we're going to use for the tests. diff --git a/test/unit/RESTPathGenerator.js b/test/unit/RESTPathGenerator.js index 55a1b17d..eeb27455 100644 --- a/test/unit/RESTPathGenerator.js +++ b/test/unit/RESTPathGenerator.js @@ -1,5 +1,5 @@ const assert = require('assert') -const RESTPathGenerator = require('../../src/RESTPathGenerator') +const RESTPathGenerator = require('../../src/RESTPathGenerator').default const joinPathsAsURL = require('../../src/RESTPathGenerator').joinPathsAsURL describe('RESTPathGenerator', () => { diff --git a/test/unit/api/prepareQuery.js b/test/unit/api/prepareQuery.js index f0c5e292..f2762f12 100644 --- a/test/unit/api/prepareQuery.js +++ b/test/unit/api/prepareQuery.js @@ -2,7 +2,7 @@ const assert = require('assert') const _ = require('lodash') describe('prepareQuery', () => { - const prepareQuery = require('../../../lib/api/prepareQuery') + const prepareQuery = require('../../../lib/api/prepareQuery').default /** * This test of the prepareQuery() middleware passes if prepareQuery() successfully prepares diff --git a/test/unit/buildQuery.js b/test/unit/buildQuery.js index ea91405a..f0967bbc 100644 --- a/test/unit/buildQuery.js +++ b/test/unit/buildQuery.js @@ -2,7 +2,7 @@ const assert = require('assert') const sinon = require('sinon') describe('buildQuery', () => { - const buildQuery = require('../../lib/buildQuery') + const buildQuery = require('../../lib/buildQuery').default let query = { where: sinon.spy(), diff --git a/test/unit/errorHandler.js b/test/unit/errorHandler.js index cd6e188f..f4ea1091 100644 --- a/test/unit/errorHandler.js +++ b/test/unit/errorHandler.js @@ -3,7 +3,7 @@ const CastError = require('mongoose/lib/error/cast') const sinon = require('sinon') describe('errorHandler', () => { - const errorHandler = require('../../lib/errorHandler') + const errorHandler = require('../../lib/errorHandler').default it('is a function', () => { assert.equal(typeof errorHandler, 'function') diff --git a/test/unit/middleware/access.js b/test/unit/middleware/access.js index 95496851..d5f39203 100644 --- a/test/unit/middleware/access.js +++ b/test/unit/middleware/access.js @@ -2,7 +2,7 @@ const assert = require('assert') const sinon = require('sinon') describe('access', () => { - const access = require('../../../lib/middleware/access') + const access = require('../../../lib/middleware/access').default let next = sinon.spy() diff --git a/test/unit/middleware/ensureContentType.js b/test/unit/middleware/ensureContentType.js index 83f1429c..83f63e3b 100644 --- a/test/unit/middleware/ensureContentType.js +++ b/test/unit/middleware/ensureContentType.js @@ -2,7 +2,7 @@ const assert = require('assert') const sinon = require('sinon') describe('ensureContentType', () => { - const ensureContentType = require('../../../lib/middleware/ensureContentType') + const ensureContentType = require('../../../lib/middleware/ensureContentType').default let onError = sinon.spy() let next = sinon.spy() diff --git a/test/unit/middleware/onError.js b/test/unit/middleware/onError.js index 63b32cfa..a5c7169a 100644 --- a/test/unit/middleware/onError.js +++ b/test/unit/middleware/onError.js @@ -1,7 +1,7 @@ const sinon = require('sinon') describe('onError', () => { - const onError = require('../../../lib/middleware/onError') + const onError = require('../../../lib/middleware/onError').default const req = { erm: { diff --git a/test/unit/middleware/outputFn.js b/test/unit/middleware/outputFn.js index d3012ce9..6a836c4c 100644 --- a/test/unit/middleware/outputFn.js +++ b/test/unit/middleware/outputFn.js @@ -1,7 +1,7 @@ const sinon = require('sinon') describe('outputFn', () => { - const outputFn = require('../../../lib/middleware/outputFn') + const outputFn = require('../../../lib/middleware/outputFn').default let res = { sendStatus: function () {}, diff --git a/test/unit/middleware/prepareOutput.js b/test/unit/middleware/prepareOutput.js index 6e7c07e5..76a85146 100644 --- a/test/unit/middleware/prepareOutput.js +++ b/test/unit/middleware/prepareOutput.js @@ -1,7 +1,7 @@ const sinon = require('sinon') describe('prepareOutput', () => { - const prepareOutput = require('../../../lib/middleware/prepareOutput') + const prepareOutput = require('../../../lib/middleware/prepareOutput').default let onError = sinon.spy() let outputFn = sinon.spy() diff --git a/test/unit/resourceFilter.js b/test/unit/resourceFilter.js index 60d9f61e..200677d6 100644 --- a/test/unit/resourceFilter.js +++ b/test/unit/resourceFilter.js @@ -1,7 +1,7 @@ const assert = require('assert') describe('resourceFilter', () => { - const Filter = require('../../lib/resource_filter') + const Filter = require('../../lib/resource_filter').default describe('getExcluded', () => { const filter = new Filter({})