npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

cap-handler-framework

v1.1.0

Published

Handler framework for SAP CAP applications with auto-generation, multi-service support, TypeScript, and draft lifecycle

Readme

cap-handler-framework

Handler framework for SAP CAP applications with multi-service support, TypeScript, and full draft lifecycle.

✨ Features

  • Convention-based - Auto-maps methods like beforeCreate, onRead, afterUpdate
  • Multi-service - Support for unlimited CAP services
  • Draft lifecycle - Full support for draft entities (NEW, EDIT, SAVE, CANCEL, etc.)
  • Type-safe - Full TypeScript support with type definitions
  • Performance - ExpandTree optimization (50-80% fewer calls)
  • Auto-discovery - Handlers automatically discovered via cds-plugin
  • Dependency injection - Shared context for services and utilities
  • Factory pattern - Cross-handler communication support

📦 Installation

npm install cap-handler-framework

🚀 Quick Start

1. Create Handler

// srv/my-service/handlers/entities/BooksHandler.ts
import { BaseHandler, TypedRequest } from 'cap-handler-framework';

export default class BooksHandler extends BaseHandler {
  getEntityName() {
    return 'Books';
  }

  async beforeCreate(req: TypedRequest): Promise<void> {
    req.data.createdAt = new Date();
  }

  async onRead(req: TypedRequest, next: () => Promise<any>): Promise<any> {
    this.initializeExpandTree(req);
    const result = await next();
    
    if (this.isExpanded('author')) {
      await this.enrichAuthor(result);
    }
    
    return result;
  }
}

2. Start Service

cds watch

That's it! Handlers are automatically registered. ✅

📖 Documentation

🎯 Convention-Based Method Mapping

| CDS Event | Handler Method | |-----------|----------------| | before('CREATE') | beforeCreate(req) | | on('READ') | onRead(req, next) | | after('UPDATE') | afterUpdate(data, req) | | before('DELETE') | beforeDelete(req) |

🎨 Draft Support

export default class OrdersHandler extends BaseHandler {
  shouldHandleDrafts() {
    return true;
  }

  async beforeSaveDraft(req: TypedRequest): Promise<void> {
    // Validate before activation
    if (!req.data.customer_ID) {
      req.error(400, 'Customer is required');
    }
  }

  async afterPatchDraft(data: any, req: TypedRequest): Promise<void> {
    // Auto-compute on field change
    data.total = data.quantity * data.unitPrice;
  }
}

⚡ Bound Actions

async onBorrow(req: TypedRequest): Promise<any> {
  const { ID } = req.params[0];  // Entity key
  const { days } = req.data;      // Parameters
  
  // Your logic
  
  return updatedEntity;
}

🔌 External Services

// srv/my-service/handlers.config.json
{
  "externalServices": ["API_BUSINESS_PARTNER"]
}
const bpApi = this.getExternalService('API_BUSINESS_PARTNER');
const result = await bpApi.run(SELECT.from('A_BusinessPartner').where(...));

🏭 Cross-Handler Communication

import { HandlerFactory } from 'cap-handler-framework';

const factory = HandlerFactory.getInstance();
const otherHandler = factory.getTradeSlipsHandler();
await otherHandler.somePublicMethod(data);

📁 Project Structure

srv/
└── my-service/
    ├── my-service.cds
    ├── handlers.config.json (optional)
    └── handlers/
        ├── entities/
        │   └── BooksHandler.ts
        ├── proxies/
        │   └── ExternalServiceProxy.ts
        └── operations/
            └── customAction.ts

🎓 Learning Resources

  1. Start with Quick Reference (10 min)
  2. Follow Developer Guide tutorial (30 min)
  3. Explore example handlers in the repo

📝 License

MIT

🤝 Contributing

Contributions welcome! Please read our contributing guidelines first.

🐛 Issues

Found a bug? Report it here