-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core-data] Document and add types for dynamic actions and selectors. (…
…#67668) * [core-data] Document and add types for dynamic actions and selectors. * Now that things are typed, we don't expect an error * Put definitions first to allow it be overridden * Use namespaces to avoid direct imports * Remove unnecessary `ts-expect-error` * Add notice for new entities * Use existing Type instead of new PostType * Dynamically create entity selectors and actions * Remove unnecessary comment * Export base type as UnstableBase * Add template related types to base * Get rid of one more @ts-expect-error * Fix Site, Status and Revision types * Add GlobalStyles * Disable plural for global styles * Add a note about "GlobalStyles" * Fix type for gmt_offset * Export and use TemplatePartArea
- Loading branch information
1 parent
4b39807
commit 72a9996
Showing
8 changed files
with
274 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { GetRecordsHttpQuery, State } from './selectors'; | ||
import type * as ET from './entity-types'; | ||
|
||
export type WPEntityTypes< C extends ET.Context = 'edit' > = { | ||
Comment: ET.Comment< C >; | ||
GlobalStyles: ET.GlobalStylesRevision< C >; | ||
Media: ET.Attachment< C >; | ||
Menu: ET.NavMenu< C >; | ||
MenuItem: ET.NavMenuItem< C >; | ||
MenuLocation: ET.MenuLocation< C >; | ||
Plugin: ET.Plugin< C >; | ||
PostType: ET.Type< C >; | ||
Revision: ET.PostRevision< C >; | ||
Sidebar: ET.Sidebar< C >; | ||
Site: ET.Settings< C >; | ||
Status: ET.PostStatusObject< C >; | ||
Taxonomy: ET.Taxonomy< C >; | ||
Theme: ET.Theme< C >; | ||
UnstableBase: ET.UnstableBase< C >; | ||
User: ET.User< C >; | ||
Widget: ET.Widget< C >; | ||
WidgetType: ET.WidgetType< C >; | ||
}; | ||
|
||
/** | ||
* A simple utility that pluralizes a string. | ||
* Converts: | ||
* - "post" to "posts" | ||
* - "taxonomy" to "taxonomies" | ||
* - "media" to "mediaItems" | ||
* - "status" to "statuses" | ||
* | ||
* It does not pluralize "GlobalStyles" due to lack of clarity about it at time of writing. | ||
*/ | ||
type PluralizeEntity< T extends string > = T extends 'GlobalStyles' | ||
? never | ||
: T extends 'Media' | ||
? 'MediaItems' | ||
: T extends 'Status' | ||
? 'Statuses' | ||
: T extends `${ infer U }y` | ||
? `${ U }ies` | ||
: `${ T }s`; | ||
|
||
/** | ||
* A simple utility that singularizes a string. | ||
* | ||
* Converts: | ||
* - "posts" to "post" | ||
* - "taxonomies" to "taxonomy" | ||
* - "mediaItems" to "media" | ||
* - "statuses" to "status" | ||
*/ | ||
type SingularizeEntity< T extends string > = T extends 'MediaItems' | ||
? 'Media' | ||
: T extends 'Statuses' | ||
? 'Status' | ||
: T extends `${ infer U }ies` | ||
? `${ U }y` | ||
: T extends `${ infer U }s` | ||
? U | ||
: T; | ||
|
||
export type SingularGetters = { | ||
[ Key in `get${ keyof WPEntityTypes }` ]: ( | ||
state: State, | ||
id: number | string, | ||
query?: GetRecordsHttpQuery | ||
) => WPEntityTypes[ Key extends `get${ infer E }` ? E : never ] | undefined; | ||
}; | ||
|
||
export type PluralGetters = { | ||
[ Key in `get${ PluralizeEntity< keyof WPEntityTypes > }` ]: ( | ||
state: State, | ||
query?: GetRecordsHttpQuery | ||
) => Array< | ||
WPEntityTypes[ Key extends `get${ infer E }` | ||
? SingularizeEntity< E > | ||
: never ] | ||
> | null; | ||
}; | ||
|
||
type ActionOptions = { | ||
throwOnError?: boolean; | ||
}; | ||
|
||
type DeleteRecordsHttpQuery = Record< string, any >; | ||
|
||
export type SaveActions = { | ||
[ Key in `save${ keyof WPEntityTypes }` ]: ( | ||
data: Partial< | ||
WPEntityTypes[ Key extends `save${ infer E }` ? E : never ] | ||
>, | ||
options?: ActionOptions | ||
) => Promise< void >; | ||
}; | ||
|
||
export type DeleteActions = { | ||
[ Key in `delete${ keyof WPEntityTypes }` ]: ( | ||
id: number | string, | ||
query?: DeleteRecordsHttpQuery, | ||
options?: ActionOptions | ||
) => Promise< void >; | ||
}; | ||
|
||
export let dynamicActions: SaveActions & DeleteActions; | ||
|
||
export let dynamicSelectors: SingularGetters & PluralGetters; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { Context, OmitNevers } from './helpers'; | ||
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records'; | ||
|
||
export type TemplatePartArea = { | ||
area: string; | ||
label: string; | ||
icon: string; | ||
description: string; | ||
}; | ||
|
||
export type TemplateType = { | ||
title: string; | ||
description: string; | ||
slug: string; | ||
}; | ||
|
||
declare module './base-entity-records' { | ||
export namespace BaseEntityRecords { | ||
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */ | ||
export interface Base< C extends Context > { | ||
/** | ||
* Site description. | ||
*/ | ||
description: string; | ||
|
||
/** | ||
* GMT offset for the site. | ||
*/ | ||
gmt_offset: string; | ||
|
||
/** | ||
* Home URL. | ||
*/ | ||
home: string; | ||
|
||
/** | ||
* Site title | ||
*/ | ||
name: string; | ||
|
||
/** | ||
* Site icon ID. | ||
*/ | ||
site_icon?: number; | ||
|
||
/** | ||
* Site icon URL. | ||
*/ | ||
site_icon_url: string; | ||
|
||
/** | ||
* Site logo ID. | ||
*/ | ||
site_logo?: number; | ||
|
||
/** | ||
* Site timezone string. | ||
*/ | ||
timezone_string: string; | ||
|
||
/** | ||
* Site URL. | ||
*/ | ||
url: string; | ||
|
||
/** | ||
* Default template part areas. | ||
*/ | ||
default_template_part_areas?: Array< TemplatePartArea >; | ||
|
||
/** | ||
* Default template types | ||
*/ | ||
default_template_types?: Array< TemplateType >; | ||
} | ||
} | ||
} | ||
|
||
export type Base< C extends Context = 'edit' > = OmitNevers< | ||
_BaseEntityRecords.Base< C > | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { Context, OmitNevers } from './helpers'; | ||
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records'; | ||
|
||
declare module './base-entity-records' { | ||
export namespace BaseEntityRecords { | ||
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */ | ||
export interface PostStatusObject< C extends Context > { | ||
/** | ||
* The title for the status. | ||
*/ | ||
name: string; | ||
|
||
/** | ||
* Whether posts with this status should be private. | ||
*/ | ||
private: boolean; | ||
|
||
/** | ||
* Whether posts with this status should be protected. | ||
*/ | ||
protected: boolean; | ||
|
||
/** | ||
* Whether posts of this status should be shown in the front end of the site. | ||
*/ | ||
public: boolean; | ||
|
||
/** | ||
* Whether posts with this status should be publicly-queryable. | ||
*/ | ||
queryable: boolean; | ||
|
||
/** | ||
* Whether to include posts in the edit listing for their post type. | ||
*/ | ||
show_in_list: boolean; | ||
|
||
/** | ||
* An alphanumeric identifier for the status. | ||
*/ | ||
slug: string; | ||
|
||
/** | ||
* Whether posts of this status may have floating published dates. | ||
*/ | ||
date_floating: boolean; | ||
} | ||
} | ||
} | ||
|
||
export type PostStatusObject< C extends Context = 'edit' > = OmitNevers< | ||
_BaseEntityRecords.Type< C > | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
72a9996
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in 72a9996.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12626411034
📝 Reported issues:
/test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js
/test/e2e/specs/widgets/editing-widgets.spec.js