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-audit-middleware

v1.0.2

Published

Audit middleware for SAP CAP applications

Downloads

8

Readme

CAP Audit Middleware

A middleware library for audit logging in SAP Cloud Application Programming (CAP) applications.

Features

  • Entity-based or global audit logging
  • Configurable logging for CREATE, UPDATE, and DELETE operations
  • Multiple storage options:
    • Database storage (via CAP service)
    • JSON file storage
    • Webhook integration with external systems
  • Tracking of pre-operation and post-operation data
  • Filtering of unwanted or sensitive data
  • Customizable user resolution

Installation

npm install cap-audit-middleware

Usage

Basic Setup

const cds = require('@sap/cds');
const { AuditMiddleware, storage } = require('cap-audit-middleware');

//server.js
cds.on("bootstrap", async(app) =>  {
  // Configure the audit middleware
  setupWithDatabaseStorage();
});
async function setupWithDatabaseStorage() {
  const srv = await cds.connect.to('db');
  const auditMiddleware = new AuditMiddleware({
    storage: new storage.DatabaseStorage({
      db: srv,
      table: 'AuditLogs'
    }),
    // At least one entity must be specified in entities parameter
    entities: ['ProductService.Products', 'OrderService.Orders'],
    // Optional: log only specific operations
    operations: ['CREATE', 'UPDATE', 'DELETE'],
    // Optional: user resolution
    userResolver: (req) => req.user?.id || 'anonymous',
    // Optional: log request/response payload
    logPayload: true
  });
  
  // Initialize middleware with the service
  auditMiddleware.initialize(srv);
}

Storage Options

Database Storage

Stores audit logs using your CDS database.

const dbStorage = new storage.DatabaseStorage({
  db: srv,                           // CDS database service
  table: 'AuditLogs',                // Optional: table name (default: 'ServiceLogs')
  autoCreateTable: true              // Optional: create table if it doesn't exist (default: true)
});

You can use the AuditLogs aspect defined in the index.cds file:

using { sap.cap.auditLogs } from 'cap-audit-middleware';

entity MyAuditLogs : auditLogs.AuditLogs {
  // Add your custom fields
  timestamp : Timestamp;
  ipAddress : String;
}

JSON File Storage

Stores audit logs in a local JSON file.

const fileStorage = new storage.JsonFileStorage({
  filePath: './logs', // File path
  prettyPrint: true,                  // Optional: format JSON for readability
  appendMode: true                    // Optional: append mode (true) or overwrite (false)
});

Webhook Storage

Sends audit logs as HTTP requests to an external API.

const webhookStorage = new storage.WebhookStorage({
  url: 'https://example.com/audit-webhook', // Webhook URL
  headers: {                                // Optional: HTTP headers
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_API_KEY'
  },
  timeout: 3000                             // Optional: request timeout (ms)
});

Advanced Configuration

Before Log Hook

To modify or enrich audit data before it's stored:

const auditMiddleware = new AuditMiddleware({
  storage: myStorage,
  // ... other options ...
  beforeLog: async (logEntry, req) => {
    // Clean sensitive data
    if (logEntry.entity === 'Users' && logEntry.data) {
      delete logEntry.data.password;
    }
    // Add additional information
    logEntry.applicationName = 'MyCapApp';
    logEntry.environment = process.env.NODE_ENV;
  }
});

Example Use Cases

Audit Logging for Specific Entities Only

const auditMiddleware = new AuditMiddleware({
  storage: myStorage,
  entities: ['ProductService.Products', 'OrderService.Orders', 'CustomerService.Customers'],
  operations: ['CREATE', 'UPDATE', 'DELETE']
});

Audit Logging for Specific Operations Only

const auditMiddleware = new AuditMiddleware({
  storage: myStorage,
  operations: ['CREATE', 'DELETE'] // Only log create and delete operations
});

Filtering Sensitive Data

const auditMiddleware = new AuditMiddleware({
  storage: myStorage,
  beforeLog: (logEntry) => {
    // Clean sensitive fields from user data
    if (logEntry.entity === 'Users' && logEntry.data) {
      delete logEntry.data.password;
      delete logEntry.data.creditCardNumber;
    }
  }
});