Strapi: How to crawl Strapi Content-Types and get textual or numeric attributes?

exFabrica
4 min readApr 12, 2022

--

Welcome back!

In this new story, we’ll talk about the crawling of Strapi Content-Types.

The goal is to make a deep scan of Strapi internal service instance to get all Content-Types and proceed to a complete analysis. In first step, we need to create a new service and code all the logic inside. In a second step, we want to display the analysis results in javascript console (Time to time, we need to do that….) but from the main plugin homepage.

You noticed that all service modules use “strapi” instance.

module.exports = ({ strapi }) => {}

This instance is the Strapi core and reference a lot of very useful properties, in particular “contentTypes”. This latter is an array who contains all Content-Types defined in your Strapi instance.

Strapi core service instance
Attributes collection inside Strapi core service instance

If you remember the story on the internal plugin collection creation, in “Schema.json”, a section “attributes” is defined. This is this one you retrieve here.

It should be possible to retrieve all textual and numeric attributes, right?

For Sure!

So, let’s create a new service. Go in “api/src/plugins/awesome-help/server/services” and create a new file “parser.js”

Paste the code bellow:

“parse” is the main function of this service and only this one is exposed.

Inside “parse” function, the first call is on “getContentTypes” function to retrieve all Content-Types. It maps the “strapi.contentTypes” and iterates on it. Notice the test, we just get the collection and single Content-Types whiteout plugin Content-Types, because we want to get only Content-Types created by the content-manager/developer.

Now, we need to crawl the attributes. A Strapi Content-Type can be complex with, for example, simple textual or numeric attributes but it can be also a components or a dynamic zone who self-contains numeric or textual attributes or components!

A function pattern is advised to explore attributes, we need to create a recursive function.

A recursive function is a function that calls itself during its execution.

The purpose of “getStructure” is to switch from Strapi attributes tree structure to a simple flat collection.

Look at the “getStructure” function: on each item it receives, the function checks all attributes and finds if their types are “component” or “dynamicZone”. If this case is found, the function proceeds to a new “getStructure” call with current attribute parameter. If not, the function directly puts the attribute in collection.

Thereby the function explores all the attributes tree and flats the results without losing the tree path. In fact, when the function flat the attribute path, new properties are set inside the flatten item:

  • container,
  • componentName,
  • zoneName

With these properties we can easily rebuild the tree path to retrieve the position of attribute later.

The last step is to filter the new collection and remove all Content-Types with no attribute and return it! But to request this function from Admin website, we need to create a controller and a route.

Create the controller in “api/src/plugins/awesome-help/server/controllers/parser.js” and paste inside the code bellow:

parser controller

Create the controller in “api/src/plugins/awesome-help/server/routes/parser.js” and paste inside the code bellow:

parser route

Don’t forget to reference this new files into the index.js of each folder.

Back to Admin!

It’s time to create an API proxy and update the plugin homepage to call the proxy and get the results from “Parser” API.

Create the proxy in the folder “api/src/plugins/awesome-help/admin/src/api/parser-proxy/index.js” and paste inside the code bellow:

parser proxy

And update the homepage with the code bellow:

updated homepage

Launch the Strapi Admin website.

It’s time to click on the “Scan your Strapi internal structure” button and look at results (in console for now)!

Content-Types crawling results

The “result” array contains all FoodAdvisor Strapi Content-Types with the flatten attributes collection.

Mission complete!

The crawling of the Content-types is finished. We need to save all results inside our help collection. Don’t miss the next part: How to create a service to manage help documents?

Back to previous article: How to design a base UI and set setting?

Back to the table of contents: A Strapi V4 plugin from scratch to production

And don’t forget our GIT to get the code: https://github.com/ExFabrica/strapi-stories/tree/develop/part-4

See U!

--

--