Type behaviors and build content types from them
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 45/100
- Issue type
- Feature
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- backend
Research direction
Start with the ContentTypeMap pattern in @plone/types and inspect the core behavior schemas and existing content interfaces. Add a type test that verifies the generated content interfaces keep exactly the same fields, then check the extension cases with the TypeScript compiler. Done means core behaviors and content types are typed as proposed and the how-to guide and BehaviorName usage are covered.
Written by the indexing model from the issue text.
Description
Summary
Type Plone behaviors in @plone/types, so the fields every behavior adds are typed once, content types are built from their behaviors, and add-ons can extend both.
It came up while designing the content types control panel (#269). The control panel itself works with plain behavior names, but content typing, forms and add-on content types will need this.
Today
- Behavior names are plain
strings. - Each content type in
@plone/typesrepeats the fields of its behaviors by hand.EventContentis exactly the fields ofplone.eventbasic,plone.eventrecurrence,plone.eventlocation,plone.eventcontactandplone.eventattendees, written out. - An add-on that enables a behavior on a type has no way to type that behavior's fields.
Proposal
Each behavior's fields are a named, exported interface, and an augmentable BehaviorMap maps behavior names to them, following the ContentTypeMap pattern:
export interface EventBasicFields {
start: string;
end: string;
whole_day: boolean;
open_end: boolean;
sync_uid: string | null;
}
export interface BehaviorMap {
'plone.eventbasic': EventBasicFields;
'plone.eventlocation': EventLocationFields;
// every core behavior; the ones that add no fields map to `{}`
}
/** Known names autocomplete; names that only the backend knows still work. */
export type BehaviorName = keyof BehaviorMap | (string & {});
/** The fields of one or more behaviors. */
export type BehaviorFields<N extends keyof BehaviorMap> = UnionToIntersection<
BehaviorMap[N]
>;
export interface EventContent
extends ContentBase,
BehaviorFields<
| 'plone.eventbasic'
| 'plone.eventrecurrence'
| 'plone.eventlocation'
| 'plone.eventcontact'
| 'plone.eventattendees'
> {
'@type': 'Event';
}
Field types describe what plone.restapi sends: dates as ISO strings, relations as related item summaries, images as Image, and so on, as ContentTypeMap does today.
Extending it
All four cases below have been checked with the TypeScript compiler.
declare module '@plone/types' {
// 1. An add-on behavior.
interface SponsorsFields {
sponsors: string[];
sponsor_logos: Image[];
}
interface BehaviorMap {
'collective.sponsors': SponsorsFields;
}
// 2. A site that overrides a core behavior's schema with an extra field.
// Every content type using the behavior gets it.
interface EventBasicFields {
capacity: number;
}
// 3. A project that enables a behavior on a core type. A later declaration
// of an interface can add `extends` clauses.
interface EventContent extends BehaviorFields<'collective.sponsors'> {}
// 4. A field added to the type itself, through the web or the schema editor.
interface EventContent {
registration_required?: boolean;
}
}
Limits
- Merging can only add. A site that disables a core behavior on a type keeps its fields in the types. Types describe what the project knows at build time; at runtime the backend is the source of truth.
- No changing what exists. Redeclaring an existing behavior with another type, or changing an existing field's type, is a compile error. A field's REST type is fixed by its field type, so this is acceptable.
Plan
BehaviorMap, the fields interfaces of the core behaviors (from their schemas),BehaviorNameandBehaviorFields.- Build the core content interfaces from their behaviors, with a type test that checks they keep exactly the same fields.
- A how-to guide for extending behaviors and content types.
- Use
BehaviorNamewhere behavior names appear, e.g.@plone/controlpanels(#269).
Later
@types/{type} gives each type's full schema, including the behavior of every field. A script could generate these declare module declarations from a live site, so typings match a project's real configuration, including removed behaviors.
- Dominant language
- JavaScript
- Stars
- 10
- Forks
- 9
- Avg merge
- 4d 7h
- Merged PRs (30d)
- 54
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from plone/aurora
-
01 type: bug 41 lvl: easy
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
Edit content TabList: selected tab's icon/label are hidden by its own selection indicator - a11yOpen01 type: bug 99 tag: UX Accessibility
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 2 days
-
01 type: bug 30 needs: triage
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 2 days
-
01 type: bug 99 tag: UX Accessibility
Difficulty 1/5 Under an hour Newbie friendliness 76/100
Maintainers usually reply within 2 days
-
01 type: bug 30 needs: triage
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Maintainers usually reply within 2 days
Similar issues
-
accessibility bug dark-mode dashboards
Difficulty 1/5 Under an hour Newbie friendliness 84/100
quarto-dev/quarto-cli#15447 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
521xueweihan/HelloGitHub#4655 ·
-
[19.0] pos_sale: settling an order with a 0 down payment line crashes (reading 'reward_id') and leaves a wrong amount duePossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Maintainers usually reply within 1 day
-
bug CRIT-3
Difficulty 1/5 Under an hour Newbie friendliness 70/100
NASA-AMMOS/anms#426 ·