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)?
I would actually recommend working within the same Code Repository where possible, but this may in some cases be unavoidable
Modify package.json
to publish the package properly.
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.
Update the main
field to "dist/Functions.bundle.js"
and the types
field to "src/dist/index.d.ts"
.
(Optional) Set a description
.
Commit the package.json
changes and publish a tag on this repository.
Validate that the checks pass.
Import your source repository.
Go to Settings > Artifacts.
Click + Add
Select your source repository and import it as a backing repository.
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).
Import any exported classes from your package and use them in your code as normal.
All code must live in index.ts in the source repository for this to work.
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)
You need to ensure that you have imported all Ontology objects and relations that the source repository relies on into the destination repository.
I also ran into a typing issues in my destination repository.
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}';`
Solution:
Create a folder called typings
.
Create a file within typings
called index.d.ts
.
Declare the module as shown below in the file created in (2) index.d.ts
.
declare module '{module_name}';
Add the path to the file created in (2)index.d.ts
in tsconfig.json
under the typeRoots
element.
"typeRoots": [
"./typings",
"./node_modules/@types/"
]