Share code between projects
You will create a shared library, export a service from it, and use it from an app via dependency injection.
Companion sample:
samples/ts/05-dependency-injection— a runnable project with explicit provider registration,endpoint().inject(), and scoped services. Runbunx putnami serve @sample/ts-dependency-injectionfrom the workspace root.
Steps
1) Create a shared package
putnami projects create @workspace/shared --template typescript-libraryThis scaffolds a TypeScript library at packages/workspace/shared/ with exports and a test suite.
2) Export a shared service
Replace packages/workspace/shared/src/index.ts:
export class GreetingService {
message() {
return 'Hello from shared code';
}
}No decorators or runtime imports needed — services are plain classes.
3) Use it in an app
In your app package.json, add the shared package as a dependency:
"@workspace/shared": "workspace:*"Register the service in your application entry point (src/main.ts):
import { application, api, http } from '@putnami/application';
import { GreetingService } from '@workspace/shared';
export const app = () =>
application()
.use(http())
.provide(GreetingService)
.use(api());Then inject it in a route, for example src/api/hello/get.ts:
import { endpoint } from '@putnami/application';
import { GreetingService } from '@workspace/shared';
export const GET = endpoint()
.inject({ greeting: GreetingService })
.handle(({ greeting }) => ({ message: greeting.message() }));Result
You now have shared code used by more than one project. The library is built, tested, and linked through workspace dependencies.