Generate Typescript Interfaces for Foundry Object Types

Taylor Gregoire-Wright
Ontologize
Published in
2 min readApr 28, 2023

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 TOKEN. 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 .env 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 -o or --objectTypes 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 -e or --extendOntologyObject option. This option is incompatible with the -t 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 -t or --types option.
  • If you want the original object types included as comments, add the -c or --commentOntologyType option.
  • If you want to type date and timestamp fields as strings instead of Date, add the -d or --ateAsString option.

--

--