Job aid: the programming primer
Section titled “Job aid: the programming primer”ACE’s integration modules (M4 to M6) assume four small skills: reading and writing JSON, understanding an HTTP request and its answer, reading and editing simple TypeScript or JavaScript, and running commands in a terminal. This page is the optional primer for them, outside the course’s hours: a summary, a self-check, and where to practise. If the self-check on the course page went well, you do not need it.
Your tools
Section titled “Your tools”| Tool | What it is for in ACE | Install, then check |
|---|---|---|
| A code editor | Writing the Function and client files | Any editor that shows line numbers |
Node.js 20 or later, with npm | Running and building TypeScript and JavaScript | From the Node.js website or your package manager; node --version |
curl | Sending HTTP requests from the terminal | Built into most systems; curl --version |
jq | Reading JSON answers in the terminal | From its website or your package manager; jq --version |
zip | Packaging a Function | Built into most systems; zip -v |
Ten commands cover the course: cd folder (go into a folder), ls or dir (list it), mkdir -p a/b (make folders), cp -r a b (copy a folder), export NAME="value" (set a variable for this
terminal; in Windows PowerShell, $env:NAME="value"), echo $NAME (show it), npm install,
npm run build, npm start, and Ctrl+C (stop a running program).
JSON writes data as objects in braces, with "key": value pairs, and arrays in brackets.
Values are text in double quotes, numbers, true, false, null, or another object or array.
{ "success": true, "result": { "count": 3, "items": [ { "id": "a1" }, { "id": "b2" } ] } }You reach a value by following the keys from the outside in: result.count is 3, and
result.items[1].id is "b2" (arrays count from 0). In jq the same paths start with a dot:
jq '.result.items[1].id'.
A request has a method (GET reads, POST creates, PATCH and PUT change, DELETE
removes), an address, headers (name: value pairs, for example the credential), and for
writes a body, usually JSON. The answer has a status and a body.
| Status | Means |
|---|---|
200, 201 | It worked (201: something was created) |
400, 422 | Your request is wrong: fix it, do not resend it unchanged |
401 | No valid credential |
403 | A valid credential without access to this |
404 | Not found: a wrong address, name or id |
409 | Conflict: something changed since you read it |
429 | Too many requests: wait and retry |
5xx | The server side failed or is unavailable |
With curl: -H 'name: value' adds a header, -d '{...}' sends a body, -X POST sets the
method, -G --data-urlencode 'key=value' adds an encoded query parameter, -s stays quiet and
-i shows the status line.
TypeScript and JavaScript, just enough
Section titled “TypeScript and JavaScript, just enough”const total = (a: number, b: number) => a + b; // a function; ": number" is a typeconst answer = { success: true, result: { total: total(2, 3) } };if (!answer.success) { throw new Error('failed'); } // stop with an errorconst rate = Number('0.25') || 0; // text to a number, 0 if it is not onefor (const item of [1, 2, 3]) { console.log(item); } // a loopconst res = await fetch(url); // wait for an HTTP answerconst body = await res.json(); // read its JSON bodyTypeScript is JavaScript with types; npm run build turns it into JavaScript and reports type
mistakes before anything runs. In ACE you read and adjust programs printed in full; you do not
design them from nothing.
Practise
Section titled “Practise”- Send
curl -sto any public JSON address you like, and read one nested value withjq. - In an empty folder, write
hello.mjscontainingconsole.log(JSON.stringify({ ok: true }))and runnode hello.mjs. - Change it to print a variable you set with
export(read it asprocess.env.NAME).
If all three work, you are ready for Module 4.