Skip to content

Commit d376d1c

Browse files
committed
fixed valid versions / token bug
1 parent b9454e7 commit d376d1c

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

src/Endpoints/Endpoint.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,16 @@ class Endpoint
1717
const SEARCH = 'search';
1818

1919
public Notion $notion;
20-
private Collection $validVersions;
21-
2220

2321
protected ?StartCursor $startCursor = null;
2422
protected int $pageSize = 100;
2523

2624
public function __construct(Notion $notion)
2725
{
28-
$this->validVersions = collect(["v1"]);
2926
$this->notion = $notion;
30-
}
3127

32-
/**
33-
* Checks if given version for notion-api is valid
34-
*
35-
* @param string $version
36-
*/
37-
public function checkValidVersion(string $version): void
38-
{
39-
if (!$this->validVersions->contains($version)) {
40-
throw WrapperException::instance("invalid version for notion-api", ['invalidVersion' => $version]);
28+
if ($this->notion->getConnection() === null) {
29+
throw WrapperException::instance("Connection could not be established, please check your token.");
4130
}
4231
}
4332

src/Notion.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace FiveamCode\LaravelNotionApi;
44

5+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
6+
use Illuminate\Support\Collection;
57
use Illuminate\Support\Facades\Http;
68
use Illuminate\Http\Client\PendingRequest;
79
use FiveamCode\LaravelNotionApi\Endpoints\Pages;
@@ -18,8 +20,9 @@ class Notion
1820
private Endpoint $endpoint;
1921
private string $version;
2022
private string $token;
21-
private PendingRequest $connection;
23+
private ?PendingRequest $connection = null;
2224

25+
private Collection $validVersions;
2326

2427
/**
2528
* Notion constructor.
@@ -30,11 +33,16 @@ public function __construct(string $version = null, string $token = null)
3033
{
3134
if ($token !== null) {
3235
$this->setToken($token);
33-
} else {
34-
$this->setToken(config('laravel-notion-api.notion-api-token'));
36+
} elseif ($token === null) {
37+
38+
// check if Notion integration token is set in config
39+
$token = config('laravel-notion-api.notion-api-token');
40+
41+
if ($token !== null)
42+
$this->setToken($token);
3543
}
3644

37-
$this->endpoint = new Endpoint($this);
45+
$this->validVersions = collect(["v1"]);
3846

3947
if ($version !== null) {
4048
$this->setVersion($version);
@@ -61,7 +69,7 @@ private function connect(): Notion
6169
*/
6270
public function setVersion(string $version): Notion
6371
{
64-
$this->endpoint->checkValidVersion($version);
72+
$this->checkValidVersion($version);
6573
$this->version = $version;
6674
return $this;
6775
}
@@ -148,10 +156,22 @@ public function getVersion(): string
148156
}
149157

150158
/**
151-
* @return PendingRequest
159+
* @return PendingRequest|null
152160
*/
153-
public function getConnection(): PendingRequest
161+
public function getConnection(): ?PendingRequest
154162
{
155163
return $this->connection;
156164
}
165+
166+
/**
167+
* Checks if given version for notion-api is valid
168+
*
169+
* @param string $version
170+
*/
171+
public function checkValidVersion(string $version): void
172+
{
173+
if (!$this->validVersions->contains($version)) {
174+
throw WrapperException::instance("invalid version for notion-api", ['invalidVersion' => $version]);
175+
}
176+
}
157177
}

0 commit comments

Comments
 (0)