Repository navigation
chore: replace lodash forEach with native Object.entries/values - #27409
innerdvations merged 7 commits into
Conversation
8bac642 to
1ddfc7e
Compare
Fewer lodash calls, smaller bundle, no behavior change. Co-Authored-By: Claude Sonnet 5 <[email protected]>
1ddfc7e to
4ba868b
Compare
|
While converting `_.forEach` in `content-api/index.ts`, found a pre-existing bug in `getRoutesMap`: when `plugin.routes` is an array, the computed `plugin.routes.map(transformPrefix).filter(filterContentAPI)` result was discarded instead of being assigned to `routesMap[`plugin::${pluginName}`]`, so array-style plugin routes never appeared in the content-API routes map. Fixed in 4ba868b, with a regression test in `packages/core/core/src/services/content-api/tests/index.test.ts`. |
8a537d3 to
dde5f55
Compare
…utes map getRoutesMap computed the filtered routes for array-style plugin.routes but never assigned them to routesMap, so those plugins never appeared under plugin::<name>. Co-Authored-By: Claude Sonnet 5 <[email protected]>
dde5f55 to
b9f1e2a
Compare
…ns-config and export it Co-Authored-By: Claude Sonnet 5 <[email protected]>
…pes service Co-Authored-By: Claude Sonnet 5 <[email protected]>
Head branch was pushed to by a user without write access
|
@innerdvations did fix the type errors, please trigger CI again |
innerdvations
left a comment
There was a problem hiding this comment.
Lodash → native loops and the getRoutesMap array-route fix look good.
Please drop the i18n Struct typing commit (and the @strapi/types peer / yarn.lock change). model was already any, so Object.entries(model.attributes) type-checks without Struct — that commit is not needed to fix the loop swap. Keep the PluginDeclaration move; that one is the real typing follow-on from Object.entries(userPluginsConfig).
If you still want the annotations, use import type { Struct } from '@strapi/types' only and leave @strapi/types as a devDependency (same as graphql/documentation). Do not add it as a peer or a runtime dependency — Struct is a type-only export.
AI-assisted comment.
| import _ from 'lodash'; | ||
| import { pick, pipe, has, prop, isNil, cloneDeep, isArray } from 'lodash/fp'; | ||
| import { errors, contentTypes as contentTypeUtils } from '@strapi/utils'; | ||
| import { Struct } from '@strapi/types'; |
There was a problem hiding this comment.
Prefer reverting this file to native loops + any (as in the earlier commit). Object.entries(model.attributes) does not need Struct when model is any.
If you keep the annotations, this should be import type { Struct } from '@strapi/types' like the rest of this plugin — Struct is export type * as Struct.
AI-assisted comment.
There was a problem hiding this comment.
_.forEach resolved model.attributes attr type to any. Object.entries resolves it to unknown.
@strapi/types is already declared as a dev dependency, it was missing only the peer one, which ESLint reported as missing.
Struct.ComponentSchema | Struct.ContentTypeSchema is already used for the model type in this workspace. It was only missed for a few calls down.
PS: hate it when AI get these things wrong )
| "peerDependencies": { | ||
| "@strapi/admin": "^5.0.0", | ||
| "@strapi/content-manager": "^5.0.0", | ||
| "@strapi/types": "^5.0.0", |
There was a problem hiding this comment.
Please do not add @strapi/types as a peer. It is already a devDependency, and with import type there is no runtime/package contract change. Revert this peer and the matching yarn.lock hunk.
AI-assisted comment.
There was a problem hiding this comment.
Most of the time, @strapi/types get's exposed through plugins public API. Without it as a peer dependency, type check will fail in a isolated dependencies environment.
Probably what we want is to mark the peer dependency as optional.
There was a problem hiding this comment.
Theoretically all strapi packages/plugins should be dev+peer, and added as "dependencies" on @strapi/strapi or @strapi/core
Co-Authored-By: Claude Sonnet 5 <[email protected]>
|
@innerdvations should be good now |
What does it do?
_.forEach(object, ...)lodash calls with nativefor...ofloops overObject.entries(object)/Object.values(object). Also drops the now-unusedlodashdefault import in two files that had no other_.usage left.content-api'sgetRoutesMap, when a plugin'sroutesis a flat array, the computed routes were discarded instead of being assigned toroutesMap, so those plugins' routes never appeared underplugin::<name>. Added a regression test.Why is it needed?
(1) reduces reliance on lodash in favor of native JS where a straightforward native equivalent exists, with no behavior change. (2) is a genuine correctness fix uncovered during that cleanup.
How to test it?
yarn test:unit,yarn test:ts,yarn lintpackages/core/core/src/services/content-api/__tests__/index.test.tscovers both the array and record shapes ofplugin.routesingetRoutesMap.Related issue(s)/PR(s)
None