Generate Typescript Interfaces for Foundry Object Types

Placeholder Image

With the release of the Ontology SDK, this post is out of date. A future post on how to use the Ontology SDK will show the current best way to create Typescript types fo object types.

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.

Screenshot of an object type interface definition printed to the console.

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:


export $TOKEN=your foundry token
export $HOSTNAME=your hostname  # often in the form something.palantirfoundry.com
export $ONTOLOGYRID=your ontology rid

Alternately, create a <code>.env</code> file and add your token there:


# ./.env
TOKEN=your foundry token
HOSTNAME=your hostname
ONTOLOGYRID=your ontology rid

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.


$ deno --allow-net run generateObjectTypings -o objectType1ApiName objectType2ApiName ...

It will print the interface definitions:


interface ObjectType1 {
  propPrimaryKey: someType;
  prop2?: someType;
  prop3?: someType;
  ...
}

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.
Screenshot of an object type interface definition that extends an OntologyObject interface definition.
  • 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.