Options

Sometimes you may want to prompt the user to input configuration options for your code block. For instance, if you write a code block that outputs a thumbnail image and you want the user to specify the width and height.

Here's how you define options in codeblox.json

{
  "name": "create-thumbnail",
  "version": "1.16.0",
  "description": "Creates a thumbnail",
  "keywords": [],
  "input": "image",
  "output": "image",
  "options": {
    "Width": "number",
    "Height": "number"
  }
}

The above example uses "number" type, but options can also be of type "text" or an array of values. This will determine the input type the user is shown to provide options.

Options will be accessible on the options parameter, under the option's name (options.Width).

Options may not be provided by the user, so you should have fallback values in your code.

Here are examples of all three types of options allowed:

{
    ...,
    "options": {
        "Number Option": "number",
        "Text Option": "text",
        "Select Option": ["Option 1", "Option 2", "Option 3"]
    }
}

Here's an example:

const codeblox = require('codeblox')
const Jimp = require('jimp')

module.exports.default = codeblox((input, options, callback) => {
  const imgBuffer = new Buffer(input, "binary");
  Jimp.read(imgBuffer, (err, image) => {
    if (err) {
      return callback(err)
    }
    image
      .scaleToFit(options.Width || 50, options.Height || 50)
      .getBuffer(Jimp.AUTO, (err, data) => {
        callback(err, data.toString('binary'))
      })
  })
})

results matching ""

    No results matching ""