palantir-foundryfoundry-code-repositoriesfoundry-functions

How can I use functions defined in other Code Repository?


I have a set of functions defined in a Code Repository (A). Code Repository A has the following structure with my class objectController defined in 'objectController.ts' :

How can I use the functions defined in objectController.ts in another Code Repository (B)?


Solution

  • I would actually recommend working within the same Code Repository where possible, but this may in some cases be unavoidable

    Steps for setting up the source repository:

    1. Modify package.json to publish the package properly.

      1. Set a new name field to define what the package name should be in other repos. It might make sense to prefix this with something use case specific to avoid conflicts.

      2. Update the main field to "dist/Functions.bundle.js" and the types field to "src/dist/index.d.ts".

      3. (Optional) Set a description.

    2. Commit the package.json changes and publish a tag on this repository.

    3. Validate that the checks pass.

    Steps in the destination repository:

    1. Import your source repository.

      1. Go to Settings > Artifacts.

      2. Click + Add

      3. Select your source repository and import it as a backing repository.

    2. Go to package.json. Under dependencies, add the package name you set in the Source repository above (step 1.1), and set the version to the tag that you published (step 2).

    3. Import any exported classes from your package and use them in your code as normal.

      1. Note : You'll have to restart Code Assist after step (2) above in order to have the newly added package show up properly.

    Caveats

    1. All code must live in index.ts in the source repository for this to work.

    2. Unit tests in the destination repository that use the imported package will fail. (This may actually be fixable, but I didn't get it to work)

    3. You need to ensure that you have imported all Ontology objects and relations that the source repository relies on into the destination repository.

    4. I also ran into a typing issues in my destination repository.

      1. Error Message:

         Errors: 
         src/index.ts(6,21): error TS7016: Could not find a declaration file for module '{module_name}'. '{redacted}/repo/functions-typescript/node_modules/{module_name}/dist/Functions.bundle.js' implicitly has an 'any' type.
        
        Try `npm install @types/{module_name}` if it exists or add a new declaration (.d.ts) file containing `declare module '{module_name}';`
        
      2. Solution:

        1. Create a folder called typings.

        2. Create a file within typings called index.d.ts.

        3. Declare the module as shown below in the file created in (2) index.d.ts.

           declare module '{module_name}';
          
        4. Add the path to the file created in (2)index.d.ts in tsconfig.json under the typeRoots element.

           "typeRoots": [
                "./typings",
                "./node_modules/@types/"
           ]