Defining TypeScript Types For WordPress Data
Posts Schema
WordPress conveniently provides detailed information about their REST API at https://developer.wordpress..org making the use of their API a straightforward process. TypeScript interfaces were created that describe a blog post based on the schema defined on the WordPress developer website. The following table is extracted from the WordPress website and provides the schema for a blog post.
datestring or null, datetime (details) |
The date the object was published, in the site's timezone.
Context: |
date_gmtstring or null, datetime (details) |
The date the object was published, as GMT.
Context: |
guidobject |
The globally unique identifier for the object.
Read only Context: |
idinteger |
Unique identifier for the object.
Read only
Context: |
linkstring, uri |
URL to the object.
Read only
Context: |
modifiedstring, datetime (details) |
The date the object was last modified, in the site's timezone.
Read only Context: |
modified_gmtstring, datetime (details) |
The date the object was last modified, as GMT.
Read only Context: |
slugstring |
An alphanumeric identifier for the object unique to its type.
Context: |
statusstring |
A named status for the object.
Context:
One of: |
typestring |
Type of Post for the object.
Read only
Context: |
passwordstring |
A password to protect access to the content and excerpt.
Context: |
permalink_templatestring |
Permalink template for the object.
Read only Context: |
generated_slugstring |
Slug automatically generated from the object title.
Read only Context: |
titleobject |
The title for the object.
Context: |
contentobject |
The content for the object.
Context: |
authorinteger |
The ID for the author of the object.
Context: |
excerptobject |
The excerpt for the object.
Context: |
featured_mediainteger |
The ID of the featured media for the object.
Context: |
comment_statusstring |
Whether or not comments are open on the object.
Context: One of: |
ping_statusstring |
Whether or not the object can be pinged.
Context: One of: |
formatstring |
The format for the object.
Context:
One of: |
metaobject |
Meta fields.
Context: |
stickyboolean |
Whether or not the object should be treated as sticky.
Context: |
templatestring |
The theme file to use to display the object.
Context: |
categoriesarray |
The terms assigned to the object in the category taxonomy.
Context: |
tagsarray |
The terms assigned to the object in the post_tag taxonomy.
Context: |
(WordPress, n.d.).
Defining Interfaces
Interfaces for the schema detailed within the above table were created that can be used throughout the applications development. This provides static type checking, assisting with the catching of errors at compilation time.
The following screenshot shows the created interfaces based on the content found in the table shown above. These types were used throughout the application,
These interfaces can now be used throughout the code to provide static type checking. The BlogListedItem interface uses a generic type so that the type of the author can be specified. Multiple examples of the usage of this type can be found in the following image
Conclusion
By mapping the WordPress schemas the WordPress developer website provides to custom built interfaces static type checking can be performed throughout the application to help catch compilation errors. Additionally, many runtime errors such as the creation of a component with improper data is also identified through the usage of these interfaces.
References
WordPress (n.d.). Posts | REST API Handbook. [online] WordPress Developer Resources. Available at: https://developer.wordpress.org/rest-api/reference/posts/#schema [Accessed 7 Apr. 2021].


Comments
Post a Comment