fix: fixing the types for browse function results (#43795)

* fix: fixing the types for browse function results

* Added the missing reading_time to the PostOrPage type
* Introduces the BrowseResults (an array augmented with metadata)
* Included a Constructor function

BREAKING CHANGE: The following types are remapped as follows:
* `PostObject` --> `PostsOrPages`
* `AuthorsObject` --> `Authors`
* `TagsObject` --> `Tags`
* `Setting` --> `Settings`
* `SettingsObject` --> `SettingsResponse`

Partly inspired by this PR from Oliver Emery <https://github.com/thrymgjol>

* corrected line terminator

* code formatting corrected

Co-authored-by: Yashar Moradi <yashar.moradi@scoperty.de>
This commit is contained in:
Yashar Moradi 2020-04-13 20:49:20 +02:00 committed by GitHub
parent add4fc1a34
commit f5ad8554cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 33 deletions

View File

@ -3,6 +3,7 @@
// Definitions by: Kevin Nguyen <https://github.com/knguyen0125>
// Anton Van Eechaute <https://github.com/antonve>
// Yashar Moradi <https://github.com/maveric1977>
// Oliver Emery <https://github.com/thrymgjol>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export type ArrayOrValue<T> = T | T[];
@ -50,9 +51,10 @@ export interface Twitter {
twitter_description?: Nullable<string>;
}
export interface SocialMedia extends Facebook, Twitter {}
export interface SocialMedia extends Facebook, Twitter {
}
export interface Setting extends Metadata, CodeInjection, SocialMedia {
export interface Settings extends Metadata, CodeInjection, SocialMedia {
title?: string;
description?: string;
logo?: string;
@ -106,11 +108,11 @@ export interface PostOrPage extends Identification, Excerpt, CodeInjection, Meta
// Post or Page
title?: string;
html?: string | null;
html?: Nullable<string>;
plaintext?: Nullable<string>;
// Image
feature_image?: string | null;
feature_image?: Nullable<string>;
featured?: boolean;
// Dates
@ -119,11 +121,14 @@ export interface PostOrPage extends Identification, Excerpt, CodeInjection, Meta
published_at?: Nullable<string>;
// Custom Template for posts and pages
custom_template?: string | null;
custom_template?: Nullable<string>;
// Post or Page
page?: boolean;
// Reading time
reading_time?: number;
// Tags - Only shown when using Include param
tags?: Tag[];
primary_tag?: Nullable<Tag>;
@ -136,7 +141,7 @@ export interface PostOrPage extends Identification, Excerpt, CodeInjection, Meta
canonical_url?: Nullable<string>;
}
export type GhostData = PostOrPage | Author | Tag | Setting;
export type GhostData = PostOrPage | Author | Tag | Settings;
export type IncludeParam = 'authors' | 'tags' | 'count.posts';
@ -170,29 +175,21 @@ export interface ReadFunction<T> {
(data: { id: Nullable<string> } | { slug: Nullable<string> }, options?: Params, memberToken?: Nullable<string>): Promise<T>;
}
export interface PostObject {
posts: PostOrPage[];
interface BrowseResults<T> extends Array<T> {
meta: { pagination: Pagination };
}
export interface AuthorsObject {
authors: Author[];
meta: { pagination: Pagination };
export interface PostsOrPages extends BrowseResults<PostOrPage> {
}
export interface TagsObject {
tags: Tag[];
meta: { pagination: Pagination };
export interface Authors extends BrowseResults<Author> {
}
export interface PagesObject {
pages: PostOrPage[];
meta: { pagination: Pagination };
export interface Tags extends BrowseResults<Tag> {
}
export interface SettingsObject {
settings: Setting;
meta: {};
export interface SettingsResponse extends Settings {
meta: any;
}
export interface GhostError {
@ -219,24 +216,29 @@ export interface GhostContentAPIOptions {
export interface GhostAPI {
posts: {
browse: BrowseFunction<PostObject>;
browse: BrowseFunction<PostsOrPages>;
read: ReadFunction<PostOrPage>;
};
authors: {
browse: BrowseFunction<AuthorsObject>;
browse: BrowseFunction<Authors>;
read: ReadFunction<Author>;
};
tags: {
browse: BrowseFunction<TagsObject>;
browse: BrowseFunction<Tags>;
read: ReadFunction<Tag>;
};
pages: {
browse: BrowseFunction<PagesObject>;
browse: BrowseFunction<PostsOrPages>;
read: ReadFunction<PostOrPage>;
};
settings: {
browse: BrowseFunction<SettingsObject>;
browse: BrowseFunction<SettingsResponse>;
};
}
export default function GhostContentAPI(options: GhostContentAPIOptions): GhostAPI;
declare var GhostContentAPI: {
(options: GhostContentAPIOptions): GhostAPI;
new(options: GhostContentAPIOptions): GhostAPI;
};
export default GhostContentAPI;

View File

@ -1,12 +1,9 @@
import GhostContentAPI, { PostOrPage } from '@tryghost/content-api';
import GhostContentAPI from '@tryghost/content-api';
const api = GhostContentAPI({ url: 'test', version: 'v3', key: '' }); // $ExpectType GhostAPI
const api = new GhostContentAPI({ url: 'test', version: 'v3', key: '' }); // $ExpectType GhostAPI
let pages: PostOrPage[];
const pagesBrowsePromise = api.pages.browse(); // $ExpectType Promise<PagesObject>
pagesBrowsePromise.then(pageObject => {
pages = pageObject.pages;
const pagesBrowsePromise = api.pages.browse(); // $ExpectType Promise<PostsOrPages>
pagesBrowsePromise.then(pages => {
api.pages.read(pages[0], { include: 'authors' }); // $ExpectType Promise<PostOrPage>
});