From ba873383d5416405798c66d0b47fed3717c14f6a Mon Sep 17 00:00:00 2001 From: Ryan Hart Date: Thu, 26 Oct 2023 11:46:27 -0400 Subject: [PATCH] Allow wider paramter types for type guards and add type guards for `RichTextItemResponse`. (#469) * Allow wider paramter types for type guards * Add rich text type guards --- src/helpers.ts | 66 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 38a1f15b..b5a086de 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -2,12 +2,15 @@ import { BlockObjectResponse, CommentObjectResponse, DatabaseObjectResponse, + EquationRichTextItemResponse, PageObjectResponse, PartialBlockObjectResponse, PartialCommentObjectResponse, PartialDatabaseObjectResponse, PartialPageObjectResponse, PartialUserObjectResponse, + RichTextItemResponse, + TextRichTextItemResponse, UserObjectResponse, } from "./api-endpoints" @@ -89,27 +92,45 @@ export async function collectPaginatedAPI( * @returns `true` if `response` is a full `BlockObjectResponse`. */ export function isFullBlock( - response: BlockObjectResponse | PartialBlockObjectResponse + response: + | PageObjectResponse + | PartialPageObjectResponse + | DatabaseObjectResponse + | PartialDatabaseObjectResponse + | BlockObjectResponse + | PartialBlockObjectResponse ): response is BlockObjectResponse { - return "type" in response + return response.object === "block" && "type" in response } /** * @returns `true` if `response` is a full `PageObjectResponse`. */ export function isFullPage( - response: PageObjectResponse | PartialPageObjectResponse + response: + | PageObjectResponse + | PartialPageObjectResponse + | DatabaseObjectResponse + | PartialDatabaseObjectResponse + | BlockObjectResponse + | PartialBlockObjectResponse ): response is PageObjectResponse { - return "url" in response + return response.object === "page" && "url" in response } /** * @returns `true` if `response` is a full `DatabaseObjectResponse`. */ export function isFullDatabase( - response: DatabaseObjectResponse | PartialDatabaseObjectResponse + response: + | PageObjectResponse + | PartialPageObjectResponse + | DatabaseObjectResponse + | PartialDatabaseObjectResponse + | BlockObjectResponse + | PartialBlockObjectResponse ): response is DatabaseObjectResponse { - return "title" in response + return response.object === "database" && "title" in response } /** @@ -118,10 +139,12 @@ export function isFullDatabase( */ export function isFullPageOrDatabase( response: - | DatabaseObjectResponse - | PartialDatabaseObjectResponse | PageObjectResponse | PartialPageObjectResponse + | DatabaseObjectResponse + | PartialDatabaseObjectResponse + | BlockObjectResponse + | PartialBlockObjectResponse ): response is DatabaseObjectResponse | PageObjectResponse { if (response.object === "database") { return isFullDatabase(response) @@ -147,3 +170,30 @@ export function isFullComment( ): response is CommentObjectResponse { return "created_by" in response } + +/** + * @returns `true` if `richText` is a `TextRichTextItemResponse`. + */ +export function isTextRichTextItemResponse( + richText: RichTextItemResponse +): richText is TextRichTextItemResponse { + return richText.type === "text" +} + +/** + * @returns `true` if `richText` is an `EquationRichTextItemResponse`. + */ +export function isEquationRichTextItemResponse( + richText: RichTextItemResponse +): richText is EquationRichTextItemResponse { + return richText.type === "equation" +} + +/** + * @returns `true` if `richText` is an `MentionRichTextItemResponse`. + */ +export function isMentionRichTextItemResponse( + richText: RichTextItemResponse +): richText is EquationRichTextItemResponse { + return richText.type === "mention" +}