Advanced parser methods

Our advanced parser has five built-in methods, which can be used by calling the "dockstersHelpers" module. Down below is a list of the five methods that you can use in a Docksters (pre-)parser.


Make HTTP GET requests

dockstersHelpers.get()

This advanced parsing method allows you to make HTTP GET requests to external resources from within a Docksters (pre-)parser.

Required parameters

This method takes two arguments. The first argument is the URL of the resource you want to make request to. The second argument is a headers object, which is optional.

  • Name
    url
    Type
    string
    Description

    The url of the resource that you want to access.

Optional parameters

  • Name
    headers
    Type
    object
    Description

    An object that can contain request headers.

Example usage in JavaScript

const headers = {
  Authorization: 'Bearer ' + bearerToken,
}

let response = await dockstersHelpers.get(
  'https://jsonplaceholder.typicode.com/users',
  headers
)

console.log(response.json())

Above is an example of how this method can be used in a Docksters (pre-)parser. This example makes a GET request to an external API, to retrieve a list users. The first argument is the API endpoint. The second is a headers object, which contains an Authorization header, followed by a bearer token.


Make HTTP POST requests

dockstersHelpers.post()

This advanced parsing method allows you to make HTTP POST requests to external resources from within a Docksters (pre-)parser.

Required parameters

This method takes three arguments. The first argument is the URL of the resource you want to make request to. The second argument is a data object. The last argument is a headers object, which is optional.

  • Name
    url
    Type
    string
    Description

    The url of the resource that you want to access.

  • Name
    data
    Type
    object
    Description

    The data that you want to send to your resource.

Optional parameters

  • Name
    headers
    Type
    object
    Description

    An object that can contain request headers.

Example usage in JavaScript

const dataArray = {
  title: 'foo',
  body: 'bar',
  userId: 1,
}

const headers = {
  Content-type: 'application/json; charset=UTF-8'
}

let response = await dockstersHelpers.post(
  'https://jsonplaceholder.typicode.com/posts',
  dataArray,
  headers
)

console.log(response.json())

Above is an example of how this method can be used in a Docksters (pre-)parser. This example makes a POST request to an external API, to create a new post. The first argument is the API endpoint. The second is a data object, which contains the data we want to send. The third argument is a headers object, which contains the content type of the data we are sending.


Add tags to Docksters devices

dockstersHelpers.addTag()

This advanced parsing method allows you to add a tag to a device from within a Docksters device definition parser.

Required parameters

This method takes just one argument, which is the tag that you want to add to your device. You don't need to specify a device for this method, as the pre-parser/definition parser automaticaly detects a device based on your payload.

  • Name
    tag
    Type
    string
    Description

    The tag that you want to add to a Docksters device.

Example usage in JavaScript

// payloadStr = {
//  'deviceName': 'ExampleDevice1',
//  'tag': '12345'
// }

const decoded = JSON.parse(payloadStr)
const newTag = decoded.tag

dockstersHelpers.addTag(newTag)

Above is an example of how this method can be used in a Docksters device definition parser. This example adds a new tag to your device, which in this example will be dynamically retrieved from the data sent to the parser (payloadStr).


Remove tags to Docksters devices

dockstersHelpers.removeTag()

This advanced parsing method allows you to remove a tag from a device from within a Docksters device definition parser.

Required parameters

This method takes just one argument, which is the tag that you want to remove from your device. You don't need to specify a device for this method, as the pre-parser/definition parser automaticaly detects a device based on your payload.

  • Name
    tag
    Type
    string
    Description

    The tag that you want to remove from a Docksters device.

Example usage in JavaScript

dockstersHelpers.removeTag('12345')

Above is an example of how this method can be used in a Docksters device definition parser. This example removes a specific tag ("12345") from your device.


Validates tags

dockstersHelpers.isTagValid()

This advanced parsing method allows you verify a tag from within a Docksters device definition parser. The input should always be a string value and must not contain any restricted characters. If this tag is valid/allowed, the method will return a true boolean value. If the tag is not allowed, this method will return a false boolean value.

Required parameters

This method takes just one argument, which is the tag that you want to verify.

  • Name
    tag
    Type
    string
    Description

    The tag that you want to remove from a Docksters device.

Example usage in JavaScript

// Example 1 - valid tag
const isThisTagValid = dockstersHelpers.isTagValid('12345')
console.log(isThisTagValid) // this will return 'true'

// Example 2 - invalid tag
const isThisTagValid_2 = dockstersHelpers.isTagValid('@2!_`${4E+="-"')
console.log(isThisTagValid_2) // this will return 'false'

Above is an example of how this method can be used in a Docksters device definition parser. This example validates a tag ("12345"), and shows what happens when you use an invalid tag.