The Wayback Machine - https://web.archive.org/web/20210108213027/https://github.com/JKHeadley/rest-hapi/issues/140
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor functions to use default parameters/options object #140

Open
JKHeadley opened this issue Jul 21, 2018 · 1 comment
Open

Refactor functions to use default parameters/options object #140

JKHeadley opened this issue Jul 21, 2018 · 1 comment

Comments

@JKHeadley
Copy link
Owner

@JKHeadley JKHeadley commented Jul 21, 2018

See: https://gist.github.com/ericelliott/f3c2a53a1d4100539f71

Advantages

See: https://medium.com/javascript-scene/you-might-not-need-typescript-or-static-types-aa7cb670a77b

  • autocomplete and type inference with most IDEs
  • doesn't require passing Log objects for every call (although it's encouraged)
  • other non-required parameters (such as query) can be omitted
  • much easier to add/modify parameter list without making breaking changes (huge for project scalability)

Considerations

  • major breaking change
  • need to assert parameters/options exist and follow correct format (should do this anyway)

Examples

Ex1:

function _list(model, query, Log) {

  • current:
function _list(model, query, Log)
  • proposed:
function _list({ model: {}, query: {}, Log: RestHapi.getLogger('list') })
  • usage:
let results = await RestHapi.list({ model: mongoose.model('user') })

Ex2:

function _addOne(

  • current:
function _addOne(ownerModel, ownerId, childModel, childId, associationName, payload, Log)
  • proposed:
function _addOne({
ownerModel: {}, 
ownerId: '', 
childModel: {}, 
childId: '', 
associationName: '', 
payload: {}, 
Log: RestHapi.getLogger('addOne')

Ex3:

query = await model.routeOptions.list.pre(query, request, Log)

  • current:
query = await model.routeOptions.list.pre(query, request, Log)
  • proposed:
query = await model.routeOptions.list.pre({ query, request, Log })

This means middleware functions could be defined like so:

    routeOptions: {
      list: {
        pre: function (params) {
          let { query, request, Log } = params
          /* do work */
          return query
        }
      }
    },
@JKHeadley
Copy link
Owner Author

@JKHeadley JKHeadley commented Sep 13, 2019

Handler portion resolved with #200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.