Hello,
V4.24.3 custom API module ‘npm run develop’ fails with type error:
" TypeError: Error creating endpoint GET /single-types: Cannot create proxy with a non-object as target or handler ││ at getContentTypeProxy "
Module files:
// src/api/common/routes/single-types.ts
export default {
routes: [
{
method: "GET",
path: "/single-types",
handler: "single-types.findSingleTypes",
config: {
policies: [],
middlewares: [],
},
},
],
};
// src/api/pages/controllers/single-types.ts
import { factories } from "@strapi/strapi";
import getSingleTypes from "../services/single-types";
export default factories.createCoreController(
"api::pages.pages",
({ strapi }) => ({
async findSingleTypes(ctx) {
try {
const { slug } = ctx.query;
const singleTypes = await getSingleTypes(strapi, slug);
ctx.body = singleTypes;
} catch (error) {
ctx.throw(500, "Internal Server Error");
}
},
}),
);
// src/api/pages/services/single-types.ts
import { Strapi } from "@strapi/strapi";
const getSingleTypes = async (strapi: Strapi, slug: string) => {
try {
const contentTypes = strapi.contentTypes;
if (!contentTypes) {
throw new Error("Content types are undefined.");
}
const singleTypes = Object.keys(contentTypes).filter(
(key) => contentTypes[key]?.kind === "singleType",
);
const results: any[] = [];
for (const type of singleTypes) {
const entries = await strapi.entityService.findMany(
`api::${type}.${type}`,
{
filters: { slug },
},
);
if (entries) {
const entriesArray = Array.isArray(entries)
? entries
: [entries];
results.push(...entriesArray);
}
}
return results;
} catch (error) {
console.error("Error fetching single type items:", error);
throw error;
}
};
export default getSingleTypes;
Thanks for taking a look!
Jacob