Skip to content

Commit

Permalink
UX Data Broker Transform Starter Template (#1573)
Browse files Browse the repository at this point in the history
* Create template.js

Added a starter template for a transform data resource. This template contains:
- JSDoc type annotations
- Destructured inputs
- Try/catch
- Error logging then throw (Enables error logging but still allows the data resource to fail)
- An example of what the properties object should look like

* Create readme.md

* Update readme.md

Fixed incorrect JSDoc optional syntax

* Update template.js

Fixed incorrect JSDoc optional syntax
  • Loading branch information
yansa-reece authored Oct 31, 2024
1 parent 4a04f68 commit 5014a5f
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
72 changes: 72 additions & 0 deletions UX Data Broker Transform/starter-template/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Transform Data Broker Template

This repository provides a starter template for creating Transform Data Brokers in ServiceNow’s UX framework. This template includes error handling, input validation, and JSDoc annotations for a streamlined setup.

## Features

- **JSDoc Type Annotations** for clarity and type-checking.
- **Destructured Inputs** for simplified parameter handling.
- **Error Logging & Propagation**: Logs errors while allowing the data resource to fail if needed.
- **Properties Example**: Provides an example of what the properties should look like

## Template Overview

```javascript
/**
* @param {{param1: string, param2: number, param3?: boolean}} inputs
* Inputs from the properties field above; param1 and param2 are mandatory.
* @returns {string} The value returned after transformation.
*/
function transform({ param1, param2, param3 }) {
const lib = "Data Broker";
const func = "<insert data broker name here>";
let res;

try {
if (!param1) throw new Error("Missing required param 'param1'");
if (!param2) throw new Error("Missing required param 'param2'");

// Add transformation logic here

return res;
} catch (e) {
gs.error(`${lib} ${func} - ${e}`);
throw new Error(e);
}
}

/**
* TIPS
* Make sure to flag mutates data if your data resource changes any data
* Properties structure (these are the inputs for your data resource):
[
{
"name": "param1",
"label": "Param 1",
"description": "An example of the first param as a string, mandatory",
"readOnly": false,
"fieldType": "string",
"mandatory": true,
"defaultValue": ""
},
{
"name": "param2",
"label": "Param 2",
"description": "An example of the second param as a number, mandatory",
"readOnly": false,
"fieldType": "number",
"mandatory": true,
"defaultValue": ""
},
{
"name": "param3",
"label": "Param 3",
"description": "An example of the third param as a boolean, optional",
"readOnly": false,
"fieldType": "boolean",
"mandatory": false,
"defaultValue": ""
}
]
*/
```
64 changes: 64 additions & 0 deletions UX Data Broker Transform/starter-template/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* This is a starter template for a transform data broker with:
* - JSDoc type annotations
* - Destructured inputs
* - Try/catch
* - Error logging then throw (Enables error logging but still allows the data resource to fail)
* @param {{param1: string, param2: number, param3?: boolean}} inputs inputs from the properties field above, param1 and param2 are mandatory
* @returns {string} the value returned
*/
function transform({ param1, param2, param3 }) {
const lib = "Data Broker";
const func = "<insert data broker name here>";

/** @type {string} */
let res;

try {
// Handle required param checks
if (!param1) throw new Error("Missing required param 'param1'");
if (!param2) throw new Error("Missing required param 'param2'");

// Add logic here

return res;
} catch (e) {
gs.error(`${lib} ${func} - ${e}`);
throw new Error(e);
}
}

/**
* TIPS
* Make sure to flag mutates data if your data resource changes any data
* Properties structure (these are the inputs for your data resource):
[
{
"name": "param1",
"label": "Param 1",
"description": "An example of the first param as a string, mandatory",
"readOnly": false,
"fieldType": "string",
"mandatory": true,
"defaultValue": ""
},
{
"name": "param2",
"label": "Param 2",
"description": "An example of the second param as a number, mandatory",
"readOnly": false,
"fieldType": "number",
"mandatory": true,
"defaultValue": ""
},
{
"name": "param3",
"label": "Param 3",
"description": "An example of the third param as a boolean, optional",
"readOnly": false,
"fieldType": "boolean",
"mandatory": false,
"defaultValue": ""
}
]
*/

0 comments on commit 5014a5f

Please sign in to comment.