Putnami
DocsGitHub

Licensed under FSL-1.1-MIT

Getting Started
Concepts
How To
Build A Web App
Build An Api Service
Share Code Between Projects
Configure Your App
Add Persistence
Add Authentication
Add Background Jobs
Principles
Tooling & Workspace
Workspace Overview
Cli
Jobs & Commands
SDK
Error Handling
Extensions
Typescript
Go
Python
Docker
Ci
Frameworks
Typescript
OverviewWebReact RoutingForms And ActionsStatic FilesApiErrors And ResponsesConfigurationLoggingHttp And MiddlewareDependency InjectionPlugins And LifecycleSessionsAuthPersistenceEventsStorageCachingWebsocketsTestingHealth ChecksTelemetryProto GrpcSmart Client
Go
OverviewHttpDependency InjectionPlugins And LifecycleConfigurationSecurityPersistenceErrorsEventsStorageCachingLoggingTelemetryGrpcService ClientsValidationOpenapiTesting
Platform
  1. DocsSeparator
  2. How ToSeparator
  3. Share Code Between Projects

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. Run bunx putnami serve @sample/ts-dependency-injection from the workspace root.

Steps

1) Create a shared package

putnami projects create @workspace/shared --template typescript-library

This 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.

On this page

  • Share code between projects
  • Steps
  • 1) Create a shared package
  • 2) Export a shared service
  • 3) Use it in an app
  • Result