Skip to content

Commit

Permalink
Merge pull request #199 from pluginpal/feature/fix-single-type=issue
Browse files Browse the repository at this point in the history
fix: issue in the router endpoint while querying a single type that d…
  • Loading branch information
boazpoolman authored Oct 5, 2024
2 parents 9152602 + 9e21d15 commit 18b135b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-glasses-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pluginpal/webtools-core": patch
---

Issue in the router endpoint when querying a single type which does not have localizations enabled.
14 changes: 11 additions & 3 deletions packages/core/server/content-api/services/by-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default () => ({
excludeDrafts = true;
}

const entity = await strapi.entityService.findMany(contentTypeUid, {
const entities = await strapi.entityService.findMany(contentTypeUid, {
...query,
filters: {
...query?.filters,
Expand All @@ -42,12 +42,20 @@ export default () => ({
limit: 1,
});

if (!entity[0]) {
/**
* If we're querying a single type, which does not have localizations enabled,
* Strapi will return a single entity instead of an array. Which is slightly weird,
* because the API we're querying is called `findMany`. That's why we need to check
* if the result is an array or not and handle it accordingly.
*/
const entity = Array.isArray(entities) ? entities[0] : entities;

if (!entity) {
return {};
}

return {
entity: entity[0],
entity,
contentType: urlAliasEntity.contenttype as Common.UID.ContentType,
};
},
Expand Down

0 comments on commit 18b135b

Please sign in to comment.