↓ Skip to main content
An ink and watercolour drawing of a small hand-cranked wooden press with a teal wheel on a workbench. A pile of colourful wooden stars, circles, triangles and half-moons lies by the chute on the left, and three identical plain square blocks stand in a row on the right.

Writing Automation Scripts in TypeScript

Maximo Automation Scripts - This article is part of a series.
Part 32: This Article
TypeScript gives a script team types, unit tests and an editor that knows the API. Manage runs JavaScript at the ES5 level, from one source text per script, and reads its results from undeclared globals. A build in between has to guarantee a short list of things. This article explains the list, and why each rule is there.

We build the Manage-side scripts of our own products this way. Every rule below comes from a measurement on our test system, on the MAS 9.2 series, or from a build that failed without it.

What Manage runs
#

Article 2 showed the JavaScript engine of Manage. For a build, five facts decide everything:

  • ES5 syntax. let, arrow functions, template strings, classes, default parameters, destructuring, spread, for…of, generators, ** and ?. do not compile.
  • The ES5 library. Map, Set, Symbol, Array.prototype.includes and find, Object.assign and Promise are missing at run time.
  • One source text per script. Manage has no module loader. Whatever a script needs has to be in its own source.
  • Globals in and out. Manage puts requestBody, userInfo and the other variables into the global scope, and reads responseBody from there after the run.
  • No strict mode. With "use strict", the assignment to responseBody throws a ReferenceError, because strict mode forbids assigning a name that was never declared.

Java classes come through Java.type. MXServer, for example, is no global.

The build, rule by rule
#

From TypeScript sources to one ES5 script From TypeScript sources to one ES5 script
From TypeScript sources to one ES5 script
RuleWhy
Declare the injected globals in TypeScriptthe compiler and the editor then know requestBody, responseBody and userInfo
A small typed facade for the Maximo classes the scripts usetypes for MboSet, Mbo and UserInfo, written by us, with no IBM package
Bundle each script into one fileManage loads one source text, with no module loader
Type check against the ES5 library onlyMap or includes fail at compile time, before Manage ever sees them
Parse every output file as ES5any newer syntax fails the build; bundlers emit some of it even from ES5-style sources
Fail on "use strict"it turns the responseBody assignment into a ReferenceError
Check that responseBody is assigned at top levela wrapper function would hide the assignment from Manage
A version header in a comment, such as /*! MYSCRIPT 1.4.0 */an administrator can read the version in the Automation Scripts application

The parse step is the one that catches the most. Our bundler has no ES5 target, and it writes some ES2015 forms on its own, such as the shorthand { key } for { key: key }. A build that only type checks lets them through, and Manage then refuses the script at install. So the sources keep to var and function, and the build parses its own output before anything else sees it.

A script entry then looks like this:

declare var requestBody: string | undefined;
declare var responseBody: string;

function answer(body: string | undefined): string {
  return JSON.stringify({ received: body ? body.length : 0 });
}

responseBody = answer(typeof requestBody === "undefined" ? undefined : requestBody);

The typeof test matters. On a GET, requestBody does not exist at all, and reading it would throw.

Testing outside and inside Manage
#

The facade makes the scripts testable without Manage. A fake MXServer answers getMboSet with in-memory sets, and the tests run with an ordinary test runner. That covers the logic: the rules, the edge cases, the error paths.

Inside Manage, the script handler of article 22 calls the installed script with a request, and the answer shows whether the built file behaves like the source. Article 30 shows that second way of testing.

When it pays, and what it costs
#

TypeScript pays for larger scripts, for helpers shared between scripts, and for a team that wants reviews and tests in a pipeline. A short object script on one event stays simpler in Jython, as article 2 recommends.

The costs are real:

  • A build step. The source of truth moves out of Manage. An edit in the Automation Scripts application is overwritten by the next install.
  • Line numbers. An error message names a line of the built file. Keep the build readable, without minification, so that the line still points at code a person can read.
  • The ES5 discipline. Sources look older than modern TypeScript, because the target is older.

The full build, with its checks and its tests, is described in detail in our member area.

What comes next
#

This ends Part 8. Part 9 is the reference: where IBM’s script documentation is wrong, what the script engine does that nobody documented, and a quick reference card.

IBM’s own pages are the reference to keep next to this series:

The complete list of corrections, with the evidence for each one, is in our member area. Members get access on request, so get in touch if you want it. If you want help with the scripts in your own Manage, our services page shows how we work.

IBM and Maximo are trademarks of International Business Machines Corporation, registered in many jurisdictions worldwide.

Maximo Automation Scripts - This article is part of a series.
Part 32: This Article