Skip to content

Instantly share code, notes, and snippets.

@k1sul1
Created April 28, 2020 15:44
Show Gist options
  • Save k1sul1/455e9dfa00db778b435c7ef16a01a869 to your computer and use it in GitHub Desktop.
Save k1sul1/455e9dfa00db778b435c7ef16a01a869 to your computer and use it in GitHub Desktop.
Mostly autogenerated type defs for WordPress REST API
// Below: Autogenerated types from Quicktype. They have been modified by hand.
// ACF fields in standard endpoints do not exist out of the box, you have to add them yourself.
// k1sul1/k1kit does that for you automagically.
// To parse this data:
//
// import { Convert, RawStoneData, Texts, Taxonomies, PostTypes, PostStatuses } from "./file";
//
// const taxonomies = Convert.toTaxonomies(json);
// const tags = Convert.toTags(json);
// const postTypes = Convert.toPostsTypes(json);
// const users = Convert.toUsers(json);
// const pages = Convert.toPages(json);
// const comments = Convert.toComments(json);
// const media = Convert.toMedia(json);
// const taxonomy = Convert.toTerm(json);
// const post = Convert.toPosts(json);
// const postStatuses = Convert.toPostsStatuses(json);
//
// These functions will throw an error if the JSON doesn't
// match the expected interface, even if the JSON is valid.
export interface Picture {
ID: number;
id: number;
title: string;
filename: string;
filesize: number;
url: string;
link: string;
alt: string;
author: string;
description: string;
caption: string;
name: string;
status: string;
uploaded_to: number;
date: String;
modified: String;
menu_order: number;
mime_type: PictureMIMEType;
type: any;
subtype: any;
icon: string;
width: number;
height: number;
sizes: PictureSizes;
}
type PictureMIMEType = string;
export interface PictureSizes {
thumbnail: string;
"thumbnail-width": number;
"thumbnail-height": number;
medium: string;
"medium-width": number;
"medium-height": number;
medium_large: string;
"medium_large-width": number;
"medium_large-height": number;
large: string;
"large-width": number;
"large-height": number;
"1536x1536": string;
"1536x1536-width": number;
"1536x1536-height": number;
"2048x2048": string;
"2048x2048-width": number;
"2048x2048-height": number;
"post-thumbnail": string;
"post-thumbnail-width": number;
"post-thumbnail-height": number;
}
export interface WpFeaturedmedia {
id: number;
date: String;
slug: string;
type: string;
link: string;
title: GUID;
author: number;
acf: boolean;
blocks: boolean;
seo: boolean;
caption: GUID;
alt_text: string;
media_type: string;
mime_type: string;
media_details: WpFeaturedmediaMediaDetails;
source_url: string;
_links: WpFeaturedmediaLinks;
}
export interface WpFeaturedmediaLinks {
self: RESTLink[];
collection: RESTLink[];
about: RESTLink[];
author: Author[];
replies: Author[];
}
export interface RESTLink {
href: string;
}
export interface Author {
embeddable: boolean;
href: string;
}
export interface GUID {
rendered: string;
}
export interface WpFeaturedmediaMediaDetails {
width: number;
height: number;
file: string;
sizes: PurpleSizes;
image_meta?: ImageMeta;
}
export interface ImageMeta {
aperture: string;
credit: string;
camera: string;
caption: string;
created_timestamp: string;
copyright: string;
focal_length: string;
iso: string;
shutter_speed: string;
title: string;
orientation: string;
keywords: any[];
}
export interface PurpleSizes {
thumbnail: Thumbnail;
medium: Medium;
medium_large?: Large;
large?: Large;
"1536x1536"?: AcfImageSize;
"2048x2048"?: AcfImageSize;
"post-thumbnail"?: AcfImageSize;
full: Full;
}
export interface AcfImageSize {
width: boolean;
height: boolean;
crop: boolean;
file: string;
mime_type: string;
source_url: string;
}
// export enum The1536X1536MIMEType {
// ImageSVGXML = "image/svg+xml",
// }
export interface Full {
file: string;
width: number;
height: number;
mime_type: string;
source_url: string;
}
export interface Large {
width: string;
height: string;
crop: boolean;
file: string;
mime_type: string;
source_url: string;
}
export interface Medium {
width: Height;
height: Height;
crop?: boolean;
file: string;
mime_type: string;
source_url: string;
}
export type Height = number | string;
export interface Thumbnail {
width: Height;
height: Height;
crop?: string;
file: string;
mime_type: string;
source_url: string;
}
export interface EmbeddedWpTerm {
id: number;
link: string;
name: string;
slug: string;
taxonomy: string;
_links: WpTermLinks;
}
export interface WpTermLinks {
self: RESTLink[];
collection: RESTLink[];
about: RESTLink[];
"wp:post_type": RESTLink[];
curies: Cury[];
}
export interface Cury {
name: string;
href: string;
templated: boolean;
}
// export enum Href {
// HTTPSAPIWOrgRel = "https://api.w.org/{rel}",
// }
export enum CuryName {
Wp = "wp",
}
export interface RESTLinks {
self: RESTLink[];
collection: RESTLink[];
about: RESTLink[];
"version-history": VersionHistory[];
"predecessor-version": PredecessorVersion[];
"wp:featuredmedia"?: Author[];
"wp:attachment": RESTLink[];
"wp:term"?: LinksWpTerm[];
curies: Cury[];
author?: Author[];
replies?: Author[];
}
export interface PredecessorVersion {
id: number;
href: string;
}
export interface VersionHistory {
count: number;
href: string;
}
export interface LinksWpTerm {
taxonomy: string;
embeddable: boolean;
href: string;
}
export interface AcfClass {
features: number[];
price: string;
sizes: SizeElement[];
colours: false | ColourElement
}
export interface Content {
rendered: string;
protected: boolean;
}
export interface Term {
id: number;
count: number;
description: string;
link: string;
name: string;
slug: string;
taxonomy: string;
meta: any[];
_links: WpTermLinks;
parent?: number;
acf: {
image?: Picture
}
}
export interface Taxonomies {
// category: Category;
// post_tag: Category;
[key: string]: Taxonomy;
}
export interface Taxonomy {
name: string;
slug: string;
description: string;
types?: string[];
hierarchical: boolean;
rest_base: string;
_links: CategoryLinks;
taxonomies?: string[];
}
export interface PostType {
name: string;
slug: string;
description: string;
types?: string[];
hierarchical: boolean;
rest_base: string;
_links: CategoryLinks;
taxonomies?: string[];
}
export interface CategoryLinks {
collection: RESTLink[];
"wp:items": RESTLink[];
curies: Cury[];
}
export interface PostTypes {
// post: PostType;
// page: Category;
// attachment: Category;
// wp_block: Category;
// libreform: Category;
[key: string]: PostType;
}
export interface Pages {
id: number;
date: String;
date_gmt: String;
guid: GUID;
modified: String;
modified_gmt: String;
slug: string;
status: string;
type: string;
link: string;
title: GUID;
content: Content;
excerpt: Content;
author: number;
featured_media: number;
parent: number;
menu_order: number;
comment_status: string;
ping_status: string;
template: string;
meta: any[];
acf: boolean;
blocks: Block[];
seo: boolean;
_links: RESTLinks;
}
export interface Block {
blockName: null | string;
attrs: any[];
innerBlocks: any[];
innerHTML: string;
innerContent: string[];
}
export interface Comments {
id: number;
post: number;
parent: number;
author: number;
author_name: SlugEnum;
author_url: string;
date: String;
date_gmt: String;
content: GUID;
link: string;
status: string;
type: string;
author_avatar_urls: { [key: string]: string };
meta: any[];
_links: CommentLinks;
_embedded: CommentEmbedded;
}
export interface CommentEmbedded {
author: Users[];
up: EmbeddedUp[];
}
export interface Users {
id: number;
name: SlugEnum;
url: string;
description: string;
link: string;
slug: SlugEnum;
avatar_urls: { [key: string]: string };
meta?: any[];
_links: UserLinks;
}
export interface UserLinks {
self: RESTLink[];
collection: RESTLink[];
}
export enum SlugEnum {
Vagrant = "vagrant",
}
export interface EmbeddedUp {
id: number;
date: String;
slug: string;
type: string;
link: string;
title: GUID;
excerpt: Content;
author: number;
featured_media: number;
acf: boolean;
blocks: boolean;
seo: boolean;
_links: RESTLinks;
}
export interface CommentLinks {
self: RESTLink[];
collection: RESTLink[];
author: Author[];
up: LinksUp[];
}
export interface LinksUp {
embeddable: boolean;
post_type: string;
href: string;
}
export interface Media {
id: number;
date: String;
date_gmt: String;
guid: GUID;
modified: String;
modified_gmt: String;
slug: string;
status: string;
type: string;
link: string;
title: GUID;
author: number;
comment_status: string;
ping_status: string;
template: string;
meta: any[];
acf: boolean;
blocks: boolean;
seo: boolean;
description: GUID;
caption: GUID;
alt_text: string;
media_type: string;
mime_type: string;
media_details: MediaMediaDetails;
post: null;
source_url: string;
_links: WpFeaturedmediaLinks;
_embedded: MediaEmbedded;
}
export interface MediaEmbedded {
author: Users[];
}
export interface MediaMediaDetails {
width: number;
height: number;
file: string;
sizes: FluffySizes;
}
export interface FluffySizes {
thumbnail: Thumbnail;
medium: Large;
medium_large: Large;
large: Large;
"1536x1536": AcfImageSize;
"2048x2048": AcfImageSize;
"post-thumbnail": AcfImageSize;
full: Full;
}
export interface Post {
id: number;
date: String;
date_gmt: String;
guid: GUID;
modified: String;
modified_gmt: String;
slug: string;
status: string;
type: string;
link: string;
title: GUID;
content: Content;
excerpt: Content;
author: number;
featured_media: number;
comment_status: string;
ping_status: string;
sticky: boolean;
template: string;
format: string;
meta: any[];
categories: number[];
tags: any[];
acf: boolean;
blocks: boolean;
seo: boolean;
_links: RESTLinks;
_embedded: PostEmbedded;
}
export interface PostEmbedded {
author: Users[];
"wp:term": Array<EmbeddedWpTerm[]>;
}
export interface PostStatuses {
publish: PostStatus;
"acf-disabled": PostStatus;
}
export interface PostStatus {
name: string;
public: boolean;
queryable: boolean;
slug: string;
date_floating: boolean;
_links: {
archives: RESTLink[];
};
}
// Converts JSON strings to/from your types
export class Convert {
public static toTaxonomies(json: string): Taxonomies {
return JSON.parse(json);
}
public static taxonomiesToJson(value: Taxonomies): string {
return JSON.stringify(value);
}
public static toTags(json: string): Term[] {
return JSON.parse(json);
}
public static tagsToJson(value: Term[]): string {
return JSON.stringify(value);
}
public static toPostTypes(json: string): PostTypes {
return JSON.parse(json);
}
public static postTypesToJson(value: PostTypes): string {
return JSON.stringify(value);
}
public static toUsers(json: string): Users[] {
return JSON.parse(json);
}
public static usersToJson(value: Users[]): string {
return JSON.stringify(value);
}
public static toPages(json: string): Pages[] {
return JSON.parse(json);
}
public static pagesToJson(value: Pages[]): string {
return JSON.stringify(value);
}
public static toComments(json: string): Comments[] {
return JSON.parse(json);
}
public static commentsToJson(value: Comments[]): string {
return JSON.stringify(value);
}
public static toMedia(json: string): Media[] {
return JSON.parse(json);
}
public static mediaToJson(value: Media[]): string {
return JSON.stringify(value);
}
public static toTaxonomy(json: string): Taxonomy[] {
return JSON.parse(json);
}
public static taxonomyToJson(value: Taxonomy[]): string {
return JSON.stringify(value);
}
public static toPosts(json: string): Post[] {
return JSON.parse(json);
}
public static postToJson(value: Post[]): string {
return JSON.stringify(value);
}
public static toPostStatuses(json: string): PostStatuses {
return JSON.parse(json);
}
public static postStatusesToJson(value: PostStatuses): string {
return JSON.stringify(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment