Insightcn

Astro

Use Insightcn in Astro applications.

Setup

Install core:

npm install @insightcn/sdk

Usage

Create src/middleware.ts to track all registry requests automatically:

import { defineMiddleware } from "astro:middleware";
import { captureRegistryEvent } from "@insightcn/sdk";

export const onRequest = defineMiddleware(async (context, next) => {
  await captureRegistryEvent(
    context.request,
    import.meta.env.INSIGHTCN_API_KEY
  );

  return next();
});

This intercepts requests to /r/registry.json and tracks installs server-side. No client-side JavaScript is injected.

API Route

For custom tracking in API routes:

// src/pages/api/registry/[...slug].ts
import type { APIRoute } from "astro";
import { captureRegistryEvent } from "@insightcn/sdk";

export const GET: APIRoute = async ({ params, request }) => {
  const slug = params.slug;
  const registryUrl = `https://your-registry.com/r/${slug}.json`;
  const registryResponse = await fetch(registryUrl);
  const data = await registryResponse.json();

  await captureRegistryEvent(request, "your-api-key", "installed");

  return new Response(JSON.stringify(data), {
    headers: { "Content-Type": "application/json" },
  });
};

RSS Feed Endpoint

// src/pages/api/rss.xml.ts
import type { APIRoute } from "astro";
import { generateRegistryRssFeed } from "@insightcn/sdk";

export const GET: APIRoute = async () => {
  const rssFeed = await generateRegistryRssFeed({
    baseUrl: "https://your-registry.com",
    rss: {
      title: "My Shadcn Registry",
      description: "Custom components for shadcn/ui",
      endpoint: "/api/rss.xml",
    },
    github: {
      owner: "your-org",
      repo: "your-registry",
      token: import.meta.env.GITHUB_TOKEN,
    },
  });

  if (!rssFeed) {
    return new Response("No items found", { status: 404 });
  }

  return new Response(rssFeed, {
    headers: {
      "Content-Type": "application/rss+xml",
      "Cache-Control": "public, max-age=3600",
    },
  });
};

Environment Variables

INSIGHTCN_API_KEY=your-api-key
GITHUB_TOKEN=your-github-token

On this page