22
33namespace FiveamCode \LaravelNotionApi ;
44
5+ use FiveamCode \LaravelNotionApi \Exceptions \WrapperException ;
6+ use Illuminate \Support \Collection ;
57use Illuminate \Support \Facades \Http ;
68use Illuminate \Http \Client \PendingRequest ;
79use 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