To integrate Compose, create a dedicated module and service to manage the Compose client and app definitions. This ensures the client starts during the NestJS lifecycle and allows for easy dependency injection of other services into your internal apps.
// 1. src/compose/compose.service.ts
import { Injectable, OnModuleInit } from '@nestjs/common';
import { Compose } from '@composehq/sdk';
@Injectable()
export class ComposeService implements OnModuleInit {
onModuleInit() {
const nav = new Compose.Navigation(['view-users', 'create-user']);
const viewUsersApp = new Compose.App({
route: 'view-users',
navigation: nav,
handler: ({ page, ui }) => {
page.add(() => ui.header('View Users', { size: 'lg' }));
const users = []; // Replace with real DB call
page.add(() => ui.table('users-table', users));
},
});
const createUserApp = new Compose.App({
route: 'create-user',
navigation: nav,
handler: ({ page, ui }) => {
page.add(() => ui.header('Create User', { size: 'lg' }));
page.add(() =>
ui.form(
'create-user-form',
[ui.textInput('name'), ui.emailInput('email')],
{
onSubmit: (form) => {
// Handle form submission
page.toast('User created successfully', { appearance: 'success' });
page.link('view-users');
},
},
),
);
},
});
const client = new Compose.Client({
apiKey: 'YOUR_API_KEY',
apps: [viewUsersApp, createUserApp],
});
client.connect();
}
}
// 2. src/compose/compose.module.ts
import { Module } from '@nestjs/common';
import { ComposeService } from './compose.service';
@Module({
providers: [ComposeService],
exports: [ComposeService],
})
export class ComposeModule {}
// 3. src/app.module.ts
import { Module } from '@nestjs/common';
import { ComposeModule } from './compose/compose.module';
@Module({
imports: [ComposeModule],
// ... other controllers and providers
})
export class AppModule {}