Skip to content

Commit

Permalink
Allow wider paramter types for type guards
Browse files Browse the repository at this point in the history
  • Loading branch information
rhart92 committed Oct 26, 2023
1 parent 241fcf1 commit 0931cb8
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,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 +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)
Expand Down

0 comments on commit 0931cb8

Please sign in to comment.