From cd093195bb4dc04f06e6db1ce2cbc27630eda844 Mon Sep 17 00:00:00 2001 From: Kevin Nguyen Date: Wed, 9 Oct 2019 05:48:29 +0700 Subject: [PATCH] Added Type Definition for @tryghost/content-api 1.2 (#38926) --- types/tryghost__content-api/index.d.ts | 240 ++++++++++++++++++ .../tryghost__content-api-tests.ts | 12 + types/tryghost__content-api/tsconfig.json | 26 ++ types/tryghost__content-api/tslint.json | 1 + 4 files changed, 279 insertions(+) create mode 100644 types/tryghost__content-api/index.d.ts create mode 100644 types/tryghost__content-api/tryghost__content-api-tests.ts create mode 100644 types/tryghost__content-api/tsconfig.json create mode 100644 types/tryghost__content-api/tslint.json diff --git a/types/tryghost__content-api/index.d.ts b/types/tryghost__content-api/index.d.ts new file mode 100644 index 0000000000..0b836c03f5 --- /dev/null +++ b/types/tryghost__content-api/index.d.ts @@ -0,0 +1,240 @@ +// Type definitions for @tryghost/content-api 1.2 +// Project: https://github.com/TryGhost/Ghost-SDK/tree/master/packages/content-api +// Definitions by: Kevin Nguyen +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export type ArrayOrValue = T | T[]; +export type Nullable = T | null; + +export interface Pagination { + page: number; + limit: number; + pages: number; + total: number; + next: Nullable; + prev: Nullable; +} + +export interface Identification { + slug: string; + id: string; +} + +export interface Metadata { + meta_title?: Nullable; + meta_description?: Nullable; +} + +export interface Excerpt { + excerpt?: string; + custom_excerpt?: string; +} + +export interface CodeInjection { + codeinjection_head?: Nullable; + codeinjection_foot?: Nullable; +} + +/** Metadata for Facebook */ +export interface Facebook { + og_image?: Nullable; + og_title?: Nullable; + og_description?: Nullable; +} + +export interface Twitter { + twitter_image?: Nullable; + twitter_title?: Nullable; + twitter_description?: Nullable; +} + +export interface SocialMedia extends Facebook, Twitter {} + +export interface Setting extends Metadata, CodeInjection, SocialMedia { + title?: string; + description?: string; + logo?: string; + icon?: string; + cover_image?: string; + facebook?: string; + twitter?: string; + lang?: string; + timezone?: string; + ghost_head?: Nullable; + ghost_foot?: Nullable; + navigation?: Array<{ + label: string; + url: string; + }>; + url?: string; +} + +export interface Author extends Identification, Metadata { + name?: string; + profile_image?: Nullable; + cover_image?: Nullable; + bio?: Nullable; + website?: Nullable; + location?: Nullable; + facebook?: Nullable; + twitter?: Nullable; + url?: Nullable; + count?: { + posts: number; + }; +} + +export type TagVisibility = 'public' | 'draft' | 'scheduled'; + +export interface Tag extends Identification, Metadata { + name?: string; + description?: Nullable; + feature_image?: Nullable; + visibility?: TagVisibility; + url?: string; + count?: { + posts: number; + }; +} + +export interface PostOrPage extends Identification, Excerpt, CodeInjection, Metadata, SocialMedia { + // Identification + uuid?: string; + comment_id?: string; + + // Post or Page + title?: string; + html?: string | null; + plaintext?: Nullable; + + // Image + feature_image?: string | null; + featured?: boolean; + + // Dates + created_at?: string; + updated_at?: Nullable; + published_at?: Nullable; + + // Custom Template for posts and pages + custom_template?: string | null; + + // Post or Page + page?: boolean; + + // Tags - Only shown when using Include param + tags?: Tag[]; + primary_tag?: Nullable; + + // Authors - Only shown when using Include Param + authors?: Author[]; + primary_author?: Nullable; + + url?: string; + canonical_url?: Nullable; +} + +export type GhostData = PostOrPage | Author | Tag | Setting; + +export type IncludeParam = 'authors' | 'tags' | 'count.posts'; + +export type FieldParam = string; + +export type FormatParam = 'html' | 'plaintext'; + +export type FilterParam = string; + +export type LimitParam = number | string; + +export type PageParam = number; + +export type OrderParam = string; + +export interface Params { + include?: ArrayOrValue; + fields?: ArrayOrValue; + formats?: ArrayOrValue; + filters?: ArrayOrValue; + limit?: ArrayOrValue; + page?: ArrayOrValue; + order?: ArrayOrValue; +} + +export interface BrowseFunction { + (options?: Params, memberToken?: Nullable): Promise; +} + +export interface ReadFunction { + (data: GhostData, options?: Params, memberToken?: Nullable): Promise; +} + +export interface PostObject { + posts: PostOrPage[]; + meta: { pagination: Pagination }; +} + +export interface AuthorsObject { + authors: Author[]; + meta: { pagination: Pagination }; +} + +export interface TagsObject { + tags: Tag[]; + meta: { pagination: Pagination }; +} + +export interface PagesObject { + pages: PostOrPage[]; + meta: { pagination: Pagination }; +} + +export interface SettingsObject { + settings: Setting; + meta: {}; +} + +export interface GhostError { + errors: Array<{ + message: string; + errorType: string; + }>; +} + +export interface GhostContentAPIOptions { + url: string; + /** + * Version of GhostContentAPI + * + * Supported Versions: 'v2', 'canary' + */ + version: 'v2' | 'canary'; + key: string; + /** @deprecated since version v2 */ + host?: string; + /** @default "ghost" */ + ghostPath?: string; +} + +export interface GhostAPI { + posts: { + browse: BrowseFunction; + read: ReadFunction; + }; + authors: { + browse: BrowseFunction; + read: ReadFunction; + }; + tags: { + browse: BrowseFunction; + read: ReadFunction; + }; + pages: { + browse: BrowseFunction; + read: ReadFunction; + }; + settings: { + browse: BrowseFunction; + }; +} + +export default function GhostContentAPI(options: GhostContentAPIOptions): GhostAPI; diff --git a/types/tryghost__content-api/tryghost__content-api-tests.ts b/types/tryghost__content-api/tryghost__content-api-tests.ts new file mode 100644 index 0000000000..def77e444f --- /dev/null +++ b/types/tryghost__content-api/tryghost__content-api-tests.ts @@ -0,0 +1,12 @@ +import GhostContentAPI, { PostOrPage } from '@tryghost/content-api'; + +const api = GhostContentAPI({ url: 'test', version: 'v2', key: '' }); // $ExpectType GhostAPI + +let pages: PostOrPage[]; +const pagesBrowsePromise = api.pages.browse(); // $ExpectType Promise + +pagesBrowsePromise.then(pageObject => { + pages = pageObject.pages; + + api.pages.read(pages[0], { include: 'authors' }); // $ExpectType Promise +}); diff --git a/types/tryghost__content-api/tsconfig.json b/types/tryghost__content-api/tsconfig.json new file mode 100644 index 0000000000..5dcc334786 --- /dev/null +++ b/types/tryghost__content-api/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "@tryghost/content-api": ["tryghost__content-api"] + } + }, + "files": [ + "index.d.ts", + "tryghost__content-api-tests.ts" + ] +} diff --git a/types/tryghost__content-api/tslint.json b/types/tryghost__content-api/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/tryghost__content-api/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }