Skip to content

Commit

Permalink
2549 add V2 Typings to JS SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinpece-apptension authored and ViolanteCodes committed Nov 13, 2024
1 parent 3c4e53f commit a1f0c17
Showing 1 changed file with 76 additions and 46 deletions.
122 changes: 76 additions & 46 deletions lib/butter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export namespace Butter {
count: number;
}

interface Methods {
/**
* Cancels the ongoing fetch request by aborting the associated AbortController
*/
cancelRequest(): void;
}

interface Response<Data extends object | string = any> {
data?: Data;
status?: number;
Expand All @@ -78,12 +85,14 @@ export namespace Butter {
}

interface PostListParams<AuthorSlug extends string = string> {
preview?: 1 | 0;
author_slug?: AuthorSlug;
category_slug?: string;
exclude_body?: boolean;
limit?: number;
offset?: number;
page?: number;
page_size?: number;
author_slug?: AuthorSlug;
category_slug?: string;
preview?: 1 | 0;
tag_slug?: string;
}

Expand All @@ -96,29 +105,35 @@ export namespace Butter {
AuthorSlug extends string = string,
PostSlug extends string = string
> {
status: "published" | "draft" | "scheduled";
created: Date;
updated: Date;
published: Date;
scheduled: Date;
title: string;
author: Omit<Author<AuthorSlug>, "recent_posts">;
body?: string;
categories: Category[];
created: string;
featured_image: string | null;
featured_image_alt: string;
meta_description: string;
published: string | null;
scheduled: string | null;
seo_title: string;
slug: PostSlug;
status: "published" | "draft" | "scheduled";
summary: string;
seo_title: string;
meta_description: string;
featured_image: string;
featured_image_alt: string;
url: string;
author: Omit<Author<AuthorSlug>, "recent_posts">;
tags: Tag[];
categories: Category[];
body?: string;
title: string;
updated: string | null;
url: string;
}

interface PostMeta {
next_post: object | null;
previous_post: object | null;
}

interface PostRetrieveResponse<
AuthorSlug extends string = string,
PostSlug extends string = string
> {
meta: PostMeta;
data: Post<AuthorSlug, PostSlug>;
}

Expand All @@ -135,7 +150,7 @@ export namespace Butter {
data: Post[];
}

interface PostMethods {
interface PostMethods extends Methods {
/**
* Retrieve a post
* @param slug The post's slug
Expand Down Expand Up @@ -196,7 +211,7 @@ export namespace Butter {
data: Category[];
}

interface CategoryMethods {
interface CategoryMethods extends Methods {
/**
* Retrieve a category
* @param slug The category's slug
Expand Down Expand Up @@ -243,7 +258,7 @@ export namespace Butter {
data: Tag[];
}

interface TagMethods {
interface TagMethods extends Methods {
/**
* Retrieve a tag
* @param slug The tag's slug
Expand Down Expand Up @@ -300,7 +315,7 @@ export namespace Butter {
data: Author[];
}

interface AuthorMethods {
interface AuthorMethods extends Methods {
/**
* Retrieve an author
* @param slug The author's slug
Expand Down Expand Up @@ -333,7 +348,7 @@ export namespace Butter {
tag_slug?: string;
}

interface FeedMethods {
interface FeedMethods extends Methods {
/**
* Get a feed
* @param feedType The type of feed
Expand All @@ -354,40 +369,44 @@ export namespace Butter {
//////////

interface PageRetrieveParams {
preview?: 0 | 1;
levels?: number;
locale?: string;
preview?: 0 | 1;
}

type PageListParams<PageModel extends object = object> =
WithFieldsPrefix<PageModel> & {
preview?: 0 | 1;
levels?: number;
order?: `${"-" | ""}${"published" | "updated"}`;
limit?: number;
locale?: string;
offset?: number;
page?: number;
page_size?: number;
preview?: 0 | 1;
};

interface PageSearchParams<PageType extends string = string> {
page_type?: PageType;
locale?: string;
levels?: number;
limit?: number;
locale?: string;
offset?: number;
page?: number;
page_size?: number;
page_type?: PageType;
}

interface Page<
PageModel extends object = object,
PageType extends string = string,
PageSlug extends string = string
> {
fields: PageModel;
name: string;
page_type: PageType;
published: string | null;
slug: PageSlug;
name: string;
published: Date;
updated: Date;
scheduled: Date;
status: "published" | "draft" | "scheduled";
fields: PageModel;
updated: string | null;
}

interface PageRetrieveResponse<
Expand All @@ -414,7 +433,7 @@ export namespace Butter {
data: Page<PageModel, PageType>[];
}

interface PageMethods {
interface PageMethods extends Methods {
/**
* Retrieve a single page
* @param page_type The page type
Expand Down Expand Up @@ -471,19 +490,20 @@ export namespace Butter {

type ContentParams<ContentModel extends object = object> =
WithFieldsPrefix<ContentModel> & {
test?: 0 | 1;
levels?: number;
locale?: string;
order?: keyof OrderParam<ContentModel>;
page?: number;
page_size?: number;
levels?: number;
preview?: 0 | 1;
};

interface ContentResponse<ContentModels extends object = object> {
meta: Meta;
data: ContentArrays<ContentModels>;
}

interface ContentMethods {
interface ContentMethods extends Methods {
/**
* Retrieve content
* @param keys An array of the keys of the content to retrieve
Expand All @@ -503,26 +523,36 @@ export namespace Butter {
}

export class ButterStatic {
post: Butter.PostMethods;
category: Butter.CategoryMethods;
tag: Butter.TagMethods;
author: Butter.AuthorMethods;
category: Butter.CategoryMethods;
content: Butter.ContentMethods;
feed: Butter.FeedMethods;
page: Butter.PageMethods;
content: Butter.ContentMethods;
post: Butter.PostMethods;
tag: Butter.TagMethods;
constructor(
apiToken: string,
testMode?: boolean,
timeout?: number,
axiosHook?: (axios: AxiosInstance) => unknown
options: {
cache?: string
onError?: ((errors: any[], params?: object) => void) | null
onRequest?: ((apiEndpoint: string, params?: object) => void) | null
onResponse?: ((response: Response, params?: object) => void) | null
testMode?: boolean
timeout?: number
}
);
}

export const Butter: (
apiToken: string,
testMode?: boolean,
timeout?: number,
axiosHook?: (axios: AxiosInstance) => unknown
options?: {
cache?: string
onError?: ((errors: any[], params?: object) => void) | null
onRequest?: ((apiEndpoint: string, params?: object) => void) | null
onResponse?: ((response: Response, params?: object) => void) | null
testMode?: boolean
timeout?: number
}
) => ButterStatic;

export default Butter;

0 comments on commit a1f0c17

Please sign in to comment.