#Access the Content Collections names and configs from within an integration?

1 messages · Page 1 of 1 (latest)

severe wigeon
#

Hi! I'm currently working on a free and open source CMS for Astro's Content Collections. It would be a web interface that would live parallel to your blog/website (e.g: at https://myblog.com/admin). Once authenticated, you'll be able to see all of your collections. And for each of them, you'll be able to create/edit/delete entries.

I would like to make that integration as simple as possible. Ideally, you would just have to run astro add name-of-my-integration and set the Astro output to hybrid or server. That's it.

I'm quite close to reaching that goal. My remaining problems are:

  1. I need to list the collections names (aka, the keys of collections from src/content/config.ts). This would be used to list the collections on the CMS dashboard.
  2. I need to get the config for each collection (to know whether it's a data or content collection, and get its Zod schema). This would be used to generate the forms to create or edit a collection's entry.

My current solution is to "pass" the entire collections object from src/content/config.ts to my integration using locals. I'm doing this using the following middleware:

import { defineMiddleware } from "astro:middleware";
import { collections } from "./content/config";

export const onRequest = defineMiddleware(
  ({ locals}, next) => {
    locals.collections = collections;
    return next();
  }
);

With that, I can use Astro.locals.collections to access the collection names and config from within my integration.

I was wondering if there is any other way of doing this? It's my first integration so it's possible I've overlooked something.
Alternatively, should I consider working on a PR to add getCollections() and getCollectionConfig(collection) to Astro's Collections API?

#

To better illustrate what this integration does, here's some screenshots. Let's start with the Astro blog starter.

#

You can connect to the CMS dashboard by going to https://myblog.com/admin. Once logged in, you see the Collections and the entries for those collections

#

I'll create a new blog post: