Skip to content

Commit baddc3b

Browse files
authored
Feature/fix url default value (#17)
* Set default URL value to be the ClickUp API v2 URL. Fixes #13 * Fix typos * Guard against Laravel setting configs as empty strings
1 parent ee8d134 commit baddc3b

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

src/Api/Client.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ public function request($path, $data = [], $method = 'GET'): ?array
219219
*/
220220
public function setConfigs(array $configs): self
221221
{
222-
$this->configs = $configs;
222+
// Replace empty strings with nulls in config values
223+
$this->configs = array_map(function($value) {
224+
return $value === "" ? null : $value;
225+
}, $configs);
223226

224227
return $this;
225228
}
@@ -241,8 +244,9 @@ public function setToken($token): self
241244
/**
242245
* URL to ClickUp
243246
*
244-
* If path is passed in, then append it to the end. By default, it will use the url
247+
* If path is passed in, then append it to the end. By default, it will use the url
245248
* in the configs, but if an url is passed in as second parameter, then it is used.
249+
* If no url is found, it will use the hard-coded v2 ClickUp API URL.
246250
*
247251
* @param string|null $path
248252
* @param string|null $url
@@ -251,6 +255,8 @@ public function setToken($token): self
251255
*/
252256
public function uri($path = null, $url = null): string
253257
{
254-
return rtrim(($url ?: $this->configs['url']), '/') . (Str::startsWith($path, '?') ? null : '/') . ltrim($path, '/');
258+
$url = $url ?? $this->configs['url'] ?? 'https://api.clickup.com/api/v2';
259+
260+
return rtrim($url, '/') . (Str::startsWith($path, '?') ? null : '/') . ltrim($path, '/');
255261
}
256262
}

tests/Api/ClientTest.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function it_can_be_constructed()
8181
/**
8282
* @test
8383
*/
84-
public function it_expects_the_first_argument_tobe_an_array()
84+
public function it_expects_the_first_argument_to_be_an_array()
8585
{
8686
$this->expectException(TypeError::class);
8787

@@ -91,7 +91,7 @@ public function it_expects_the_first_argument_tobe_an_array()
9191
/**
9292
* @test
9393
*/
94-
public function it_expects_the_second_argument_tobe_a_guzzle()
94+
public function it_expects_the_second_argument_to_be_a_guzzle()
9595
{
9696
$this->expectException(TypeError::class);
9797

@@ -150,6 +150,34 @@ public function it_allows_setting_the_configs()
150150
$this->assertInstanceOf(Client::class, $return);
151151
}
152152

153+
/**
154+
* @test
155+
*/
156+
public function it_sets_url_to_default_value_if_not_passed_in()
157+
{
158+
$this->client->setConfigs(
159+
[
160+
'url' => null,
161+
]
162+
);
163+
164+
$this->assertEquals('https://api.clickup.com/api/v2/', $this->client->uri());
165+
}
166+
167+
/**
168+
* @test
169+
*/
170+
public function it_sets_url_to_default_value_if_its_an_empty_string()
171+
{
172+
$this->client->setConfigs(
173+
[
174+
'url' => "",
175+
]
176+
);
177+
178+
$this->assertEquals('https://api.clickup.com/api/v2/', $this->client->uri());
179+
}
180+
153181
/**
154182
* @test
155183
*/
@@ -201,8 +229,8 @@ public function it_builds_correct_uri()
201229
$this->assertEquals('http://some/place/resource', $this->client->uri('/resource'), 'no double slash');
202230
$this->assertEquals('http://some/place/resource/', $this->client->uri('resource/'), 'leaves end slash');
203231
$this->assertEquals(
204-
'http://some/place?paramater=value',
205-
$this->client->uri('?paramater=value'),
232+
'http://some/place?parameter=value',
233+
$this->client->uri('?parameter=value'),
206234
'query string'
207235
);
208236
$this->assertEquals(

0 commit comments

Comments
 (0)