Skip to content

Commit

Permalink
Convert to ES6 modules
Browse files Browse the repository at this point in the history
  • Loading branch information
b-gran committed Jan 26, 2017
1 parent cb0819b commit d239af3
Show file tree
Hide file tree
Showing 38 changed files with 183 additions and 161 deletions.
9 changes: 5 additions & 4 deletions src/APIMethod.js
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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
Expand All @@ -64,4 +65,4 @@ class APIMethod {
}
}

module.exports = APIMethod
export default APIMethod
8 changes: 4 additions & 4 deletions src/ERMOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -196,4 +196,4 @@ function isModel (model) {
return Object.getPrototypeOf(model) === Model
}

module.exports = ERMOperation
export default ERMOperation
6 changes: 3 additions & 3 deletions src/RESTPathGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -101,5 +101,5 @@ function ensureString (value, valueName = 'value') {
return null
}

module.exports = RESTPathGenerator
module.exports.joinPathsAsURL = joinPathsAsURL
export default RESTPathGenerator
export { joinPathsAsURL }
8 changes: 4 additions & 4 deletions src/api/applyQueryToContext.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
};

4 changes: 2 additions & 2 deletions src/api/db/createObject.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -58,4 +58,4 @@ function doCreateObject (ermInstance, req) {
})
}

module.exports = new APIMethod(doCreateObject)
export default new APIMethod(doCreateObject)
8 changes: 4 additions & 4 deletions src/api/db/deleteItem.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -33,4 +33,4 @@ function doDeleteItem (state, req) {
}
}

module.exports = new APIMethod(doDeleteItem)
export default new APIMethod(doDeleteItem)
6 changes: 3 additions & 3 deletions src/api/db/deleteItems.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,4 +14,4 @@ function doDeleteItems (state, req) {
.then(() => state.set('statusCode', 204))
}

module.exports = new APIMethod(doDeleteItems)
export default new APIMethod(doDeleteItems)
6 changes: 3 additions & 3 deletions src/api/db/getCount.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -11,4 +11,4 @@ function doGetCount (state, req) {
})
}

module.exports = new APIMethod(doGetCount)
export default new APIMethod(doGetCount)
11 changes: 5 additions & 6 deletions src/api/db/getItem.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -29,4 +28,4 @@ function doGetItem (state, req) {
})
}

module.exports = new APIMethod(doGetItem)
export default new APIMethod(doGetItem)
11 changes: 5 additions & 6 deletions src/api/db/getItems.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
11 changes: 5 additions & 6 deletions src/api/db/getShallow.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -48,4 +47,4 @@ function doGetShallow (state, req) {
})
}

module.exports = new APIMethod(doGetShallow)
export default new APIMethod(doGetShallow)
10 changes: 5 additions & 5 deletions src/api/db/modifyObject.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -122,4 +122,4 @@ function performUpdateAndSave (state, req, updates) {
return document.save()
}

module.exports = new APIMethod(doModifyObject)
export default new APIMethod(doModifyObject)
12 changes: 6 additions & 6 deletions src/api/getContext.js
Original file line number Diff line number Diff line change
@@ -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'

/**
*
Expand Down Expand Up @@ -42,4 +42,4 @@ function getContext (state, req) {
})
}

module.exports = new APIMethod(getContext)
export default new APIMethod(getContext)
10 changes: 5 additions & 5 deletions src/api/prepareQuery.js
Original file line number Diff line number Diff line change
@@ -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)) {
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -154,4 +154,4 @@ module.exports = function (allowRegex) {
return resolve(parsedOptions)
})
}
}
};
10 changes: 5 additions & 5 deletions src/api/shared.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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()()
}
};
8 changes: 4 additions & 4 deletions src/buildQuery.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -64,4 +64,4 @@ module.exports = function (globalOptions) {

return promise
}
}
};
6 changes: 3 additions & 3 deletions src/errorHandler.js
Original file line number Diff line number Diff line change
@@ -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 || {}
Expand All @@ -14,4 +14,4 @@ module.exports = function (options) {
options.onError(err, req, res, next)
}
}
}
};
Loading

0 comments on commit d239af3

Please sign in to comment.