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 🙏

© 2025 – Pkg Stats / Ryan Hefner

directus-extension-expandable-blocks-api

v0.2.1

Published

API endpoint extension for expandable blocks

Readme

Directus Expandable Blocks API

A focused Directus API extension for tracking item usage across M2A (Many-to-Any) relationships. This extension provides functionality that the native Directus API cannot offer - specifically tracking where items are used throughout your content structure.

Purpose

"The API should only do what the native Directus API cannot."

This extension focuses exclusively on relation/usage tracking - finding where content items are referenced across different collections through M2A junction tables.

Key Features

  • Usage Tracking: Track where items are used across M2A relationships
  • Junction ID Support: Returns junction table IDs for proper relationship management
  • Batch Operations: Optimized queries for loading multiple items efficiently
  • Performance Focused: Reduced database queries through batch loading
  • Minimal Surface Area: Only essential endpoints, reducing maintenance burden

Installation

# Install dependencies
npm install

# Build the extension
npm run build

# Copy to your Directus extensions folder
cp -r dist /path/to/directus/extensions/expandable-blocks-api

API Endpoints

Health Check

GET /expandable-blocks/health

Returns the API status and version information.

Response:

{
  "status": "ok",
  "version": "1.0.0",
  "description": "Expandable Blocks API - Relation/Usage Tracking"
}

Detail with Usage Tracking

POST /expandable-blocks/:collection/detail

Load items with comprehensive usage information across M2A relationships. This is the core functionality that native Directus API cannot provide.

Request Body:

{
  "ids": [1, 2, 3],
  "fields": "id,name,status"  // Optional field selection
}

Response:

{
  "data": [
    {
      "id": 1,
      "name": "Item Name",
      "status": "published",
      "usage_locations": [
        {
          "id": 10,
          "collection": "pages",
          "title": "Homepage",
          "status": "published",
          "field": "content",
          "sort": 1,
          "path": "/home",
          "edit_url": "/admin/content/pages/10",
          "junction_id": 101  // Critical for relationship management
        }
      ],
      "usage_summary": {
        "total_count": 1,
        "by_collection": { "pages": 1 },
        "by_status": { "published": 1 },
        "by_field": { "content": 1 }
      }
    }
  ],
  "meta": {
    "collection": "content_blocks",
    "total_count": 1,
    "timestamp": "2025-01-27T10:00:00.000Z"
  }
}

Usage Tracking

The API tracks usage across multiple junction tables:

  • pages_m2a: Tracks items used in pages
  • expandable_expandable: Tracks items used in expandable blocks
  • Additional junction tables can be added as needed

Each usage location includes:

  • junction_id: The ID in the junction table (critical for updates/deletes)
  • collection: Where the item is used
  • title: Human-readable reference
  • status: Current status of the parent item
  • edit_url: Direct link to edit the parent item

Performance Optimizations

  • Batch Loading: All usage data is loaded in batch queries
  • Optimized Queries: Uses efficient SQL joins
  • Minimal Overhead: Only loads what's necessary
  • Single Round-Trip: Reduces database queries significantly

Requirements

  • Directus v11.0.0 or higher
  • Expandable Blocks interface extension
  • Node.js 16 or higher

Development

# Install dependencies
npm install

# Development mode with auto-reload
npm run dev

# Run tests
npm test

# Build for production
npm run build

Architecture

src/
├── index.ts                 # Main endpoint definition (2 endpoints only)
├── handlers/
│   └── DetailHandler.ts     # Core usage tracking logic
├── factories/
│   └── ServiceFactory.ts    # Service instantiation
├── middleware/
│   ├── error-handler.ts     # Error handling
│   ├── rate-limit.ts        # Rate limiting
│   └── security-headers.ts  # Security headers
└── types/                   # TypeScript definitions

Philosophy

This extension follows the principle of doing only what Directus cannot do natively. By focusing exclusively on relation/usage tracking, we maintain:

  • Less code = Less maintenance = Fewer bugs
  • Clear purpose = Easier to understand
  • Focused functionality = Better performance
  • Minimal API surface = Reduced security risk

License

MIT License - see LICENSE file for details

Support

For issues, feature requests, or questions:

Credits

Developed by SmartLabs AT for the Directus community.