When using Typescript to build an app that uses Foundry’s ontology APIs, you’ll need interface definitions for your object types. While it doesn’t take very long to create these manually, we use a small script to automatically generate object type definitions by calling Foundry’s get object type endpoint.
Setup
This script is written in Typescript and uses Deno, a Typescript runtime. After cloning the repo and installing Deno on your machine, you’ll need to create some environment variables.
Generate a Foundry API token, and store it in an environment variable called <code>TOKEN</code>. Also store the hostname of your stack and the ontology rid for the relevant ontology. You can find the ontology rid in the Advanced pane of the Ontology Manager app:
Alternately, create a <code>.env</code> file and add your token there:
Usage
To use the script, run the following command. It will print out the interface definitions for each object type you supply. You can then copy the definitions wherever you need them. All properties except the primary key will be typed as optional. Properties will remain in the same order as received in the response from Foundry’s API.
Use the <code>-o</code> or <code>--objectTypes</code> option before the object types.
It will print the interface definitions:
Options
- If you want to extend a generic OntologyObject interface, add the <code>-e</code> or <code>--extendOntologyObject</code> option. This option is incompatible with the <code>-t</code> option and will be overridden by that option.
- If you want types instead of interfaces, add the <code>-t</code> or <code>--types</code> option.
- If you want the original object types included as comments, add the <code>-c</code> or <code>--commentOntologyType</code> option.
- If you want to type date and timestamp fields as strings instead of Date, add the <code>-d</code> or <code>--dateAsString</code> option.