Creating a Typescript Library

Hello, Foundry Community!

I successfully created a TypeScript library within Foundry to use across multiple projects and would like to share the process with you. Although this isn’t officially supported yet, it works well!

1. creating the library:
1.1 Create: src/index.ts

import { Function} from "@foundry/functions-api";

// Uncomment the import statement below to start importing object types
// import { Objects, ExampleDataAircraft } from "@foundry/ontology-api";
import { Integer, FunctionsMap } from "@foundry/functions-api";


export class UtilsTests {
    constructor() {
        
    }

    @Function()
    public addOne(n: Integer): Integer {
        return n + 1;
    }
}

1.2 Update package.json

  "name": "my-test-package",
  "version": "1.0.2",
  "description": "Testing imports from other packages",
  "main": "dist/Functions.bundle.js",
  "types": "src/dist/index.d.ts",

1.3. Commit your changes
1.4 Create a Tag
Ensure your tag matches the version: 1.0.2

2. Using the Library in Another Project repository

2.1 go to libraries tab and install your created library
2.2. Crete file: src/typings/index.d.ts
put inside only the following line:
declare module 'my-test-package';

2.3. Update src/index.ts

import { Function, FunctionsMap, Integer } from "@foundry/functions-api";
import { UtilsTests } from "my-test-package/src"

export class MyFunctions2 {

    @Function()
    public add_test(n: Integer): Integer {
        const utils = new UtilsTests();
        return utils.addOne(n);
    }
}

2.4. Commit Your Changes
2.5. Live Preview in Functions
Navigate to Functions → Live Preview and test the function to ensure everything works correctly.

I hope this guide inspires others to explore building reusable TypeScript libraries in Foundry! If you found this helpful, please give it a like and share your thoughts or improvements in the comments. Your feedback will help improve the process and encourage more sharing within the community!

Let’s build together :rocket:

16 Likes