Type behaviors and build content types from them

Open
#272 0 comments 0 reactions 0 assignees View on GitHub

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

04 type: enhancement

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/types repeats the fields of its behaviors by hand. EventContent is exactly the fields of plone.eventbasic, plone.eventrecurrence, plone.eventlocation, plone.eventcontact and plone.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

  1. BehaviorMap, the fields interfaces of the core behaviors (from their schemas), BehaviorName and BehaviorFields.
  2. Build the core content interfaces from their behaviors, with a type test that checks they keep exactly the same fields.
  3. A how-to guide for extending behaviors and content types.
  4. Use BehaviorName where 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from plone/aurora

All issues in plone/aurora

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.