-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(search): search pages and databases
Closes #62
- Loading branch information
1 parent
45eed99
commit 5c2a4bd
Showing
21 changed files
with
637 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Search | ||
|
||
## Introduction | ||
|
||
Searches all parent or child pages and databases that have been shared with an | ||
integration. | ||
|
||
Returns all pages or databases, excluding duplicated linked databases, that have | ||
titles that include the query param. If no query param is provided, then the | ||
response contains all pages or databases that have been shared with the | ||
integration. | ||
|
||
::: warning | ||
If you want to search a specific individual database, rather than across all | ||
databases, then [query a database](../how-to/query-database.md) instead. | ||
::: | ||
|
||
## Searching | ||
|
||
```php | ||
use Notion\Search\Query; | ||
|
||
$query = Query::title("Example"); // Search by page/database title... | ||
$query = Query::all(); // ... or search everything | ||
|
||
$result = $notion->search()->search($query); | ||
|
||
$result->hasMore; // bool | ||
$result->nextCursor; // CursorId when hasMore is true | ||
$result->results; // array of Page and/or Database objects | ||
``` | ||
|
||
## Query options | ||
|
||
```php | ||
use Notion\Search\Query; | ||
|
||
$query = Query::title("Example"); // Page or database title | ||
$query = $query->filterByPages(); // Return only pages | ||
$query = $query->filterByDatabases(); // Return only databases | ||
$query = $query->sortByLastEditedTime(SortDirection::Ascending); // Results order | ||
|
||
// Pagination | ||
$query = $query->changePageSize(10); | ||
$query = $query->changeNextCursor("70d73991-7e06-43d9-ad3c-3711213f1235") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Notion\Search; | ||
|
||
use Notion\Configuration; | ||
use Notion\Infrastructure\Http; | ||
|
||
/** @psalm-import-type ResultJson from \Notion\Search\Result */ | ||
class Client | ||
{ | ||
/** | ||
* @internal Use `\Notion\Notion::search()` instead | ||
*/ | ||
public function __construct( | ||
private readonly Configuration $config, | ||
) { | ||
} | ||
|
||
public function search(Query $query): Result | ||
{ | ||
$data = json_encode($query->toArray(), JSON_PRETTY_PRINT); | ||
$url = "https://api.notion.com/v1/search"; | ||
$request = Http::createRequest($url, $this->config) | ||
->withMethod("POST") | ||
->withHeader("Content-Type", "application/json"); | ||
$request->getBody()->write($data); | ||
|
||
/** @psalm-var ResultJson $body */ | ||
$body = Http::sendRequest($request, $this->config); | ||
|
||
return Result::fromArray($body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Notion\Search; | ||
|
||
/** @psalm-immutable */ | ||
class Filter | ||
{ | ||
private function __construct( | ||
public readonly FilterValue $value, | ||
public readonly FilterProperty $property, | ||
) { | ||
} | ||
|
||
/** @psalm-mutation-free */ | ||
public static function byPages(): self | ||
{ | ||
return new self(FilterValue::Page, FilterProperty::Object); | ||
} | ||
|
||
/** @psalm-mutation-free */ | ||
public static function byDatabases(): self | ||
{ | ||
return new self(FilterValue::Database, FilterProperty::Object); | ||
} | ||
|
||
/** | ||
* @internal | ||
* | ||
* @return array{ value: "page"|"database", property: "object" } | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
"value" => $this->value->value, | ||
"property" => $this->property->value, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Notion\Search; | ||
|
||
enum FilterProperty: string | ||
{ | ||
case Object = "object"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Notion\Search; | ||
|
||
enum FilterValue: string | ||
{ | ||
case Page = "page"; | ||
case Database = "database"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Notion\Search; | ||
|
||
/** @psalm-immutable */ | ||
class Query | ||
{ | ||
private function __construct( | ||
public readonly string|null $query = null, | ||
public readonly Filter|null $filter = null, | ||
public readonly Sort|null $sort = null, | ||
public readonly string|null $startCursor = null, | ||
public readonly int|null $pageSize = null, | ||
) { | ||
} | ||
|
||
public static function all(): self | ||
{ | ||
return new self(); | ||
} | ||
|
||
public static function title(string $query): self | ||
{ | ||
return new self($query); | ||
} | ||
|
||
public function filterByPages(): self | ||
{ | ||
return new self( | ||
$this->query, | ||
Filter::byPages(), | ||
$this->sort, | ||
$this->startCursor, | ||
$this->pageSize, | ||
); | ||
} | ||
|
||
public function filterByDatabases(): self | ||
{ | ||
return new self( | ||
$this->query, | ||
Filter::byDatabases(), | ||
$this->sort, | ||
$this->startCursor, | ||
$this->pageSize, | ||
); | ||
} | ||
|
||
public function sortByLastEditedTime(SortDirection $direction): self | ||
{ | ||
$sort = Sort::create()->byLastEditedTime(); | ||
|
||
return new self( | ||
$this->query, | ||
$this->filter, | ||
$direction === SortDirection::Ascending ? $sort->ascending() : $sort->descending(), | ||
$this->startCursor, | ||
$this->pageSize, | ||
); | ||
} | ||
|
||
public function changeStartCursor(string $startCursor): self | ||
{ | ||
return new self( | ||
$this->query, | ||
$this->filter, | ||
$this->sort, | ||
$startCursor, | ||
$this->pageSize, | ||
); | ||
} | ||
|
||
public function changePageSize(int $pageSize): self | ||
{ | ||
return new self( | ||
$this->query, | ||
$this->filter, | ||
$this->sort, | ||
$this->startCursor, | ||
$pageSize, | ||
); | ||
} | ||
|
||
/** | ||
* @internal | ||
* | ||
* @return array{ | ||
* query?: string, | ||
* filter?: array{ value: string, property: string }, | ||
* sort?: array{ direction: string, timestamp: string }, | ||
* start_cursor?: string, | ||
* page_size?: int | ||
* } | ||
*/ | ||
public function toArray(): array | ||
{ | ||
$array = []; | ||
|
||
if ($this->query !== null) { | ||
$array["query"] = $this->query; | ||
} | ||
|
||
if ($this->filter !== null) { | ||
$array["filter"] = $this->filter->toArray(); | ||
} | ||
|
||
if ($this->sort !== null) { | ||
$array["sort"] = $this->sort->toArray(); | ||
} | ||
|
||
if ($this->startCursor !== null) { | ||
$array["start_cursor"] = $this->startCursor; | ||
} | ||
|
||
if ($this->pageSize !== null) { | ||
$array["page_size"] = $this->pageSize; | ||
} | ||
|
||
return $array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Notion\Search; | ||
|
||
use Notion\Databases\Database; | ||
use Notion\Pages\Page; | ||
|
||
/** | ||
* @psalm-immutable | ||
* | ||
* @psalm-import-type PageJson from \Notion\Pages\Page | ||
* @psalm-import-type DatabaseJson from \Notion\Databases\Database | ||
* | ||
* @psalm-type ResultJson = array{ | ||
* object: string, | ||
* results: list{PageJson, DatabaseJson}, | ||
* next_cursor: string|null, | ||
* has_more: bool | ||
* } | ||
*/ | ||
class Result | ||
{ | ||
/** @param list{Page, Database} $results */ | ||
private function __construct( | ||
public readonly array $results, | ||
public readonly string|null $nextCursor, | ||
public readonly bool $hasMore, | ||
) { | ||
} | ||
|
||
/** | ||
* @psalm-param ResultJson $array | ||
*/ | ||
public static function fromArray(array $array): self | ||
{ | ||
$results = []; | ||
foreach ($array["results"] as $result) { | ||
if ($result["object"] === "page") { | ||
/** @psalm-var PageJson $result */ | ||
$results[] = Page::fromArray($result); | ||
} | ||
|
||
if ($result["object"] === "database") { | ||
/** @psalm-var DatabaseJson $result */ | ||
$results[] = Database::fromArray($result); | ||
} | ||
} | ||
|
||
/** @psalm-var list{Page, Database} $results */ | ||
return new self( | ||
$results, | ||
$array["next_cursor"], | ||
$array["has_more"], | ||
); | ||
} | ||
} |
Oops, something went wrong.