Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow wider paramter types for type guards and add type guards for RichTextItemResponse. #469

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 58 additions & 8 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import {
BlockObjectResponse,
CommentObjectResponse,
DatabaseObjectResponse,
EquationRichTextItemResponse,
PageObjectResponse,
PartialBlockObjectResponse,
PartialCommentObjectResponse,
PartialDatabaseObjectResponse,
PartialPageObjectResponse,
PartialUserObjectResponse,
RichTextItemResponse,
TextRichTextItemResponse,
UserObjectResponse,
} from "./api-endpoints"

Expand Down Expand Up @@ -89,27 +92,45 @@ export async function collectPaginatedAPI<Args extends PaginatedArgs, Item>(
* @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
}

/**
Expand All @@ -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)
Expand All @@ -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"
}
Loading