From 0931cb8e787c183709f0da9d2d89b68fc9556018 Mon Sep 17 00:00:00 2001 From: Ryan Hart Date: Thu, 26 Oct 2023 10:49:18 -0400 Subject: [PATCH 1/2] Allow wider paramter types for type guards --- src/helpers.ts | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 38a1f15b..f2e97913 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -89,27 +89,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 +136,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) From c06f83ac7a3c9ef62dca2e3d0e204fb1c55ac8a0 Mon Sep 17 00:00:00 2001 From: Ryan Hart Date: Thu, 26 Oct 2023 11:00:43 -0400 Subject: [PATCH 2/2] Add rich text type guards --- src/helpers.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/helpers.ts b/src/helpers.ts index f2e97913..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" @@ -167,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" +}