Defining which parts my module I’m responsible for, and which ones the user should take care

问题内容:

I’m creating a simple wrapper of my endpoints using the request module, and one of my endpoints is used to upload files and a JSON, for that, i’m using multipart/form-data:

function doSomething(files, theJSON) {
  request.post('https://m.com/endpoint', {
    formData: {
      files,
      theJSON
    }
  })
}

But I’m with a doubt about the responsibility of providing the right arguments.

The files must be a array of read streams, and the theJSON, must be a json. I have two options, here:

1- Parse the args:

function doSomething(files, theJSON) {
  request.post('https://m.com/endpoint', {
    formData: {
      files: files.map(file => fs.createReadStream(file),
      theJSON: JSON.stringify(theJSON)
    }
  })
}

2- Let the user be responsible for the right type.

Which one would be the best one?? Is there a right choice?

问题评论:

答案:

答案1:

Whatever you do, document it.


There is some precedent for providing short-cuts for very common tasks, like uploading a file. In this case, you can conditionally check for strings and treat it as a path, otherwise assume it is a stream:

files: files.map(file =>
    typeof file === 'string' ?
    fs.createReadStream(file) :
    file
)

答案评论:

原文地址:

https://stackoverflow.com/questions/47756983/defining-which-parts-my-module-im-responsible-for-and-which-ones-the-user-shou

添加评论

友情链接:蝴蝶教程