Ontology buildingFunctionsTypeScript v2 [Experimental]Getting started

Getting started with TypeScript v2 functions

TypeScript v2 allows users to take advantage of several key improvements over TypeScript v1, including a Node.js runtime and first-class OSDK support. Review the sections below to get started.

Create a TypeScript v2 functions repository

Navigate to a project of your choice and create a new code repository by selecting + New > Repository. Select the TypeScript v2 functions template to initialize your repository.

Create a TypeScript v2 function code repository.

Once the repository has been created, navigate to the typescript-functions/src/functions/helloWorld.ts file.

Explore the repository

Your repository will be initialized with a helloWorld.ts file containing a single function:

Copied!
1 2 3 function helloWorld(): string { return "Hello World!"; }

The helloWorld function returns a message of type string.

Export and test in live preview

To make your function available in live preview, export it using the export default syntax. This is a requirement to register the function and later make it available to other Foundry applications. Functions in the same file that are not the default export will remain private.

To export the helloWorld function, uncomment the export line:

Copied!
1 export default helloWorld;

To test your function in live preview, open the Functions helper and select Live Preview.

Open the functions helper.

Then, select the helloWorld function, and choose Run to execute the function.

Run your new function in the functions helper.

Add another function

Now, let's add a more complex function to this repository, test it, and publish it. Navigate to the findSumOfArray.ts file and export the function using export default, or copy and paste the code below:

Copied!
1 2 3 4 5 6 7 8 import { Integer } from "@osdk/functions"; function findSumOfArray(numbers: Integer[]): Integer { return numbers.reduce((acc, num) => acc + num, 0); } // To test this function in live preview and later publish to the platform, make it the default export of the file. export default findSumOfArray;

Commit and publish a function

Select Commit at the upper right corner of the window to commit your changes to the master branch of your repository. To view your function's checks, open the Checks tab at the top of the page. Here, after making a commit, you should see a running check. Select it, and wait until it succeeds.

Select the check to view progress.

After committing your work, you will see the Tag version option. This will publish all of the functions in your repository.

The available tag options.

Select Tag version to tag a release off of the master branch. Set the tag name based on the extent of your changes, and then select Tag and release.

Choose the version type to tag for the new release.

To view the progress as your functions are tagged and released, select the View pop-up or navigate to the Tags tab. Once Step 2: Release is completed, select the published functions to view them in the function registry.

Functions may not be immediately searchable by name in Workshop or the function registry while permissions propagate.

Both the tag and release checks passed, and the new function is published.

Use a new function

After the checks for your tag have passed, navigate back to the Code tab in Code Repositories and select the Functions helper. You should now be able to see your findSumOfArray function under the Published section. Select it and run the new function:

Run the new function in the functions helper.

Next steps

In this tutorial, you learned how to use Code Repositories to write, publish, and test a TypeScript v2 function from a repository. Next, we recommend learning how to author functions on objects.