Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core Data: TypeScript definitions for entity records. #38666

Merged
merged 74 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
8d9ab6d
Add types for core data entity records
adamziel Feb 9, 2022
190ccdf
Use string enums instead of numeric ones
adamziel Feb 9, 2022
6d9687b
Put each type in a separate file
adamziel Feb 10, 2022
de331e0
Rond 2 of autogenerating types
adamziel Feb 10, 2022
1300f77
Extract enum types
adamziel Feb 10, 2022
60d1c91
Extract common interfaces
adamziel Feb 10, 2022
9cb67ca
Add type definitions for posts and template parts
adamziel Feb 10, 2022
08081f7
Add Raw Data typings
adamziel Feb 10, 2022
cd9b910
Extract AvatarUrls to common.js
adamziel Feb 10, 2022
2449089
Try context-based entity types
adamziel Feb 10, 2022
5f35431
Experimenting with different ways of contextualizing data types
adamziel Feb 11, 2022
566c954
Remove EntityInContext – it isn't really needed
adamziel Feb 11, 2022
5969c12
Rename RawDataIsString to RawDataOverride
adamziel Feb 11, 2022
4d8ea95
Use Entity and EntityWithEdits without distinguishing between differe…
adamziel Feb 15, 2022
4ff84bb
Make all the fields contextual
adamziel Feb 16, 2022
b73646b
Refactir WithEdits to EditedRecord
adamziel Feb 16, 2022
a20c2a1
Rename "Edited" to "Updatable"
adamziel Feb 17, 2022
539f7c2
Flatten Nevers and WithoutNevers to OmitNevers
adamziel Feb 17, 2022
1c420c0
Remove dedicated updatable fields in favor of an Updateble type wrapper
adamziel Feb 17, 2022
942f152
Remove extra wrappers around types
adamziel Feb 17, 2022
f13eda1
Add missing definitions to NavMenu type
adamziel Feb 17, 2022
433be4f
Use RawField in Page and Post types
adamziel Feb 17, 2022
62963d9
Export UpdatableRecord type
adamziel Feb 17, 2022
6554342
Rename RawField to RenderedText
adamziel Feb 17, 2022
ec99125
Export updatable types
adamziel Feb 17, 2022
4292622
Rename UpdatableRecord to Updatable
adamziel Feb 17, 2022
dde6773
Remove atomic updatable types
adamziel Feb 18, 2022
e9ee4d0
Adjust Comment status and User locale modeling
adamziel Feb 18, 2022
dcdabe5
Remove the NestedWidget name, declare the type inline in Sidebar
adamziel Feb 18, 2022
31e1ea9
Make User.password optional
adamziel Feb 18, 2022
d81c259
Introduce StringWhenUpdatable type to model WP templates
adamziel Feb 18, 2022
e6193b2
Document the Updatable type
adamziel Feb 18, 2022
a95ba80
Document the RenderedText using @dmsnell's proposal
adamziel Feb 18, 2022
0e4c63e
Document types in common.ts
adamziel Feb 18, 2022
09fe23a
Flatten the content prop of WpTemplate and WpTemplatePart into a stri…
adamziel Feb 18, 2022
4974607
Type the remaining optional fields more strictly
adamziel Feb 18, 2022
c086bab
Wrap Type with OmitNevers
adamziel Feb 18, 2022
b4b0dd7
Wrap the type user with OmitNevers
adamziel Feb 18, 2022
f153a3a
Use consistent kebab case in type files
adamziel Feb 18, 2022
ee55c45
Wrap comment with OmitNevers
adamziel Feb 18, 2022
9ba701f
Add the missing OmitNevers to types with contextual fields
adamziel Feb 18, 2022
b5560c1
Add README.md
adamziel Feb 18, 2022
8bd4641
Rename common.ts to helpers.ts
adamziel Feb 18, 2022
184bbe4
Model WpTemplate.content and WpTemplatePart.content as RenderableText
adamziel Feb 21, 2022
185ecfd
Link to the REST API docs when explaining contexts
adamziel Feb 21, 2022
0486076
Explain the ContextualFields without meandering on the implementation…
adamziel Feb 21, 2022
493050e
Use the correct capitalization of the word javascript
adamziel Feb 21, 2022
222e2fa
Rename CommentStatus to CommentingStatus in context of comments conta…
adamziel Feb 21, 2022
fccae67
Use Post as an example illustrating the usage of ContextualField
adamziel Feb 21, 2022
d8522e8
Focus on the goal of the ContextualField in its documentation
adamziel Feb 21, 2022
3fe2297
Use a cleaner explanation of the Updatable type wrapper
adamziel Feb 21, 2022
c544fef
Explain why the types do not provide full type safety
adamziel Feb 21, 2022
1b7963e
Remove the TODO comments from the README and a section about extendab…
adamziel Feb 21, 2022
b855db9
Add extensible type prefix
adamziel Feb 21, 2022
5b2145d
a -> an
adamziel Feb 22, 2022
b23a08a
Use export type and import type declarations
adamziel Feb 22, 2022
c98911e
Note that Comment.id is still a field even after the Comment type has…
adamziel Feb 22, 2022
c02ddb3
Use CommentingStatus and PingStatus in the Attachment type
adamziel Feb 22, 2022
a0650ac
Use a correct snippet of code to illustrate interface extending in RE…
adamziel Feb 22, 2022
fec0432
Use namespaced base types for extenders
adamziel Feb 22, 2022
df1080c
Document the BaseTypes namespace
adamziel Feb 22, 2022
d1c4dfb
Lint
adamziel Feb 22, 2022
97bc553
Lint the docstring in base-types.ts
adamziel Feb 22, 2022
80b7610
Rename the BaseTypes namespace to WPBaseTypes
adamziel Feb 22, 2022
db67911
Add more commentary to the Extending section
adamziel Feb 22, 2022
4509bd2
Add a comma
adamziel Feb 22, 2022
466dd5f
Clarify the warning about type safety
adamziel Feb 22, 2022
a84b420
Restore the first sentence of the warning
adamziel Feb 22, 2022
0ab8ed7
Link to the types readme in core-data readme
adamziel Feb 22, 2022
2ce3ce0
Merge branch 'trunk' into ts/add-core-data-types
adamziel Feb 22, 2022
dde0888
Do not publish the data types
adamziel Feb 22, 2022
669f289
Rename WPBaseTypes to CoreBaseEntityTypes
adamziel Feb 22, 2022
adfd612
Rename CoreBaseEntityTypes to BaseEntityTypes
adamziel Feb 22, 2022
0fbd96f
Fix a typo (extends -> extend)
adamziel Feb 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions packages/core-data/src/types/attachment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* Internal dependencies
*/
import { EntityRecordWithRawData, OpenOrClosed, PostStatus } from './common';

type MediaType = 'image' | 'file';

export interface Attachment< RawType > extends EntityRecordWithRawData {
/**
* The date the post was published, in the site's timezone.
*/
date: string | null;
/**
* The date the post was published, as GMT.
*/
date_gmt?: string | null;
/**
* The globally unique identifier for the post.
*/
guid?: RawType;
/**
* Unique identifier for the post.
*/
id: number;
/**
* URL to the post.
*/
link: string;
/**
* The date the post was last modified, in the site's timezone.
*/
modified?: string;
/**
* The date the post was last modified, as GMT.
*/
modified_gmt?: string;
/**
* An alphanumeric identifier for the post unique to its type.
*/
slug: string;
/**
* A named status for the post.
*/
status?: PostStatus;
/**
* Type of post.
*/
type: string;
/**
* Permalink template for the post.
*/
permalink_template?: string;
/**
* Slug automatically generated from the post title.
*/
generated_slug?: string;
/**
* The title for the post.
*/
title: RawType;
/**
* The ID for the author of the post.
*/
author: number;
/**
* Whether or not comments are open on the post.
*/
comment_status?: OpenOrClosed;
/**
* Whether or not the post can be pinged.
*/
ping_status?: OpenOrClosed;
/**
* Meta fields.
*/
meta?: {
[ k: string ]: string;
};
/**
* The theme file to use to display the post.
*/
template?: string;
/**
* Alternative text to display when attachment is not displayed.
*/
alt_text: string;
/**
* The attachment caption.
*/
caption: RawType;
/**
* The attachment description.
*/
description?: RawType;
/**
* Attachment type.
*/
media_type: MediaType;
/**
* The attachment MIME type.
*/
mime_type: string;
/**
* Details about the media file, specific to its type.
*/
media_details: {
[ k: string ]: string;
};
/**
* The ID for the associated post of the attachment.
*/
post?: number;
/**
* URL to the original attachment file.
*/
source_url: string;
/**
* List of the missing image sizes of the attachment.
*/
missing_image_sizes?: string[];
}
77 changes: 77 additions & 0 deletions packages/core-data/src/types/comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Internal dependencies
*/
import { AvatarUrls, EntityRecordWithRawData } from './common';

export interface Comment< RawType > extends EntityRecordWithRawData {
/**
* Unique identifier for the comment.
*/
id: number;
/**
* The ID of the user object, if author was a user.
*/
author: number;
/**
* Email address for the comment author.
*/
author_email?: string;
/**
* IP address for the comment author.
*/
author_ip?: string;
/**
* Display name for the comment author.
*/
author_name: string;
/**
* URL for the comment author.
*/
author_url: string;
/**
* User agent for the comment author.
*/
author_user_agent?: string;
/**
* The content for the comment.
*/
content: RawType;
/**
* The date the comment was published, in the site's timezone.
*/
date: string;
/**
* The date the comment was published, as GMT.
*/
date_gmt?: string;
/**
* URL to the comment.
*/
link: string;
/**
* The ID for the parent of the comment.
*/
parent: number;
/**
* The ID of the associated post object.
*/
post?: number;
/**
* State of the comment.
*/
status?: string;
/**
* Type of the comment.
*/
type: string;
/**
* Avatar URLs for the comment author.
*/
author_avatar_urls: AvatarUrls;
/**
* Meta fields.
*/
meta?: {
[ k: string ]: string;
};
}
55 changes: 55 additions & 0 deletions packages/core-data/src/types/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* The raw data representation.
*/
export interface RawObject {
/**
* Data as it exists in the database.
*/
raw?: string;
/**
* Data transformed for display.
*/
rendered?: string;
}

export type RawString = string;
export type RawData = RawObject | RawString;

export type PostStatus = 'publish' | 'future' | 'draft' | 'pending' | 'private';

export type OpenOrClosed = 'open' | 'closed';
export type ActiveOrInactive = 'active' | 'inactive';
adamziel marked this conversation as resolved.
Show resolved Hide resolved

export type TemplateContent =
| {
/**
* Content for the template, as it exists in the database.
*/
raw?: string;
/**
* Version of the content block format used by the template.
*/
block_version?: number;
}
| string;

export interface EntityRecordWithRawData<
RawType extends RawData = RawObject
> {}

export interface AvatarUrls {
/**
* Avatar URL with image size of 24 pixels.
*/
'24'?: string;
/**
* Avatar URL with image size of 48 pixels.
*/
'48'?: string;
/**
* Avatar URL with image size of 96 pixels.
*/
'96'?: string;

[ k: string ]: string;
}
14 changes: 14 additions & 0 deletions packages/core-data/src/types/menu-location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface MenuLocation {
/**
* The name of the menu location.
*/
name: string;
/**
* The description of the menu location.
*/
description: string;
/**
* The ID of the assigned menu.
*/
menu: number;
}
adamziel marked this conversation as resolved.
Show resolved Hide resolved
32 changes: 32 additions & 0 deletions packages/core-data/src/types/nav_menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export interface NavMenu {
/**
* Unique identifier for the term.
*/
id: number;
/**
* HTML description of the term.
*/
description?: string;
/**
* HTML title for the term.
*/
name: string;
/**
* An alphanumeric identifier for the term unique to its type.
*/
slug: string;
/**
* Meta fields.
*/
meta?: {
[ k: string ]: string;
};
/**
* The locations assigned to the menu.
*/
locations?: string[];
/**
* Whether to automatically add top level pages to this menu.
*/
auto_add?: boolean;
}
80 changes: 80 additions & 0 deletions packages/core-data/src/types/nav_menu_item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Internal dependencies
*/

export interface NavMenuItem {
/**
* The title for the object.
*/
title: RawType;
/**
* Unique identifier for the object.
*/
id: number;
/**
* The singular label used to describe this type of menu item.
*/
type_label: string;
/**
* The family of objects originally represented, such as "post_type" or "taxonomy".
*/
type: 'taxonomy' | 'post_type' | 'post_type_archive' | 'custom';
/**
* A named status for the object.
*/
status: 'publish' | 'future' | 'draft' | 'pending' | 'private';
/**
* The ID for the parent of the object.
*/
parent: number;
/**
* Text for the title attribute of the link element for this menu item.
*/
attr_title: string;
/**
* Class names for the link element of this menu item.
*/
classes: string[];
/**
* The description of this menu item.
*/
description: string;
/**
* The DB ID of the nav_menu_item that is this item's menu parent, if any, otherwise 0.
*/
menu_order: number;
/**
* The type of object originally represented, such as "category", "post", or "attachment".
*/
object: string;
/**
* The database ID of the original object this menu item represents, for example the ID for posts or the term_id for categories.
*/
object_id: number;
/**
* The target attribute of the link element for this menu item.
*/
target: '_blank' | '';
/**
* The URL to which this menu item points.
*/
url: string;
/**
* The XFN relationship expressed in the link of this menu item.
*/
xfn: string[];
/**
* Whether the menu item represents an object that no longer exists.
*/
invalid: boolean;
/**
* The terms assigned to the object in the nav_menu taxonomy.
*/
menus?: number;
/**
* Meta fields.
*/
meta?: {
[ k: string ]: string;
};
}
14 changes: 14 additions & 0 deletions packages/core-data/src/types/navigation-area.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface NavigationArea {
/**
* The name of the navigation area.
*/
name: string;
/**
* The description of the navigation area.
*/
description: string;
/**
* The ID of the assigned navigation.
*/
navigation: number;
}
Loading