Skip to content

Commit

Permalink
Update VitePress documentation links and remove unused files
Browse files Browse the repository at this point in the history
  • Loading branch information
Thavarshan committed Sep 30, 2024
1 parent efb651f commit 86accb3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 156 deletions.
49 changes: 0 additions & 49 deletions docs/api-examples.md

This file was deleted.

16 changes: 16 additions & 0 deletions docs/api/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ Returns the reason phrase associated with the status code (e.g., "OK" for a 200
$statusMessage = $response->statusText();
```

### **`getStatusCode()`**

```php
public function getStatusCode(): int
```

Returns the HTTP status code of the response.

**Returns**: The status code as an integer.

### Example

```php
$statusCode = $response->getStatusCode();
```

### **`createFromBase()`**

```php
Expand Down
51 changes: 37 additions & 14 deletions docs/guide/async-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ FetchPHP allows you to make asynchronous HTTP requests using the `async()` funct
```php
use Fetch\Interfaces\Response as ResponseInterface;

$response = async(fn () => fetch('https://example.com', [
$data = null;

async(fn () => fetch('https://example.com', [
'method' => 'POST',
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode(['key' => 'value']),
]))
->then(fn (ResponseInterface $response) => $response->json()) // Success handler
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
->catch(fn (Throwable $e) => $e->getMessage()); // Error handler

echo $data;
```

In this example:
Expand All @@ -39,14 +43,18 @@ FetchPHP’s Fluent API can also be used to build asynchronous requests. This AP
```php
use Fetch\Interfaces\Response as ResponseInterface;

$response = async(fn () => fetch()
$data = null;

async(fn () => fetch()
->baseUri('https://example.com')
->withHeaders('Content-Type', 'application/json')
->withBody(json_encode(['key' => 'value']))
->withToken('fake-bearer-auth-token')
->post('/posts'))
->then(fn (ResponseInterface $response) => $response->json()) // Success handler
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
->catch(fn (Throwable $e) => $e->getMessage()); // Error handler

echo $data;
```

In this example:
Expand Down Expand Up @@ -84,6 +92,9 @@ $task->resume();

// Cancel the task if required
$task->cancel();

// Get only if completed properly
$result = $task->getResult();
```

In this example, a long-running asynchronous task is started, paused, resumed, and potentially canceled based on the task’s lifecycle needs.
Expand All @@ -95,13 +106,17 @@ In this example, a long-running asynchronous task is started, paused, resumed, a
```php
use Fetch\Interfaces\Response as ResponseInterface;

$response = async(fn () => fetch()
$data = null;

async(fn () => fetch()
->baseUri('https://example.com')
->withQueryParameters(['page' => 1])
->withToken('fake-bearer-auth-token')
->get('/resources'))
->then(fn (ResponseInterface $response) => $response->json()) // Success handler
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
->catch(fn (Throwable $e) => $e->getMessage()); // Error handler

echo $data;
```

This example demonstrates an asynchronous GET request where query parameters and a Bearer token are used to retrieve data from an API.
Expand All @@ -113,14 +128,18 @@ You can implement retry logic in asynchronous requests by utilizing the `retry()
```php
use Fetch\Interfaces\Response as ResponseInterface;

$response = async(fn () => fetch()
$data = null;

async(fn () => fetch()
->baseUri('https://example.com')
->withHeaders('Content-Type', 'application/json')
->withBody(json_encode(['key' => 'value']))
->retry(3, 1000) // Retry 3 times with a 1-second delay between retries
->post('/posts'))
->then(fn (ResponseInterface $response) => $response->json()) // Success handler
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
->catch(fn (Throwable $e) => $e->getMessage()); // Error handler

echo $data;
```

In this example:
Expand All @@ -136,11 +155,13 @@ FetchPHP makes it easy to handle errors in asynchronous requests using the `catc
```php
use Fetch\Interfaces\Response as ResponseInterface;

$response = async(fn () => fetch('https://nonexistent-url.com'))
->then(fn (ResponseInterface $response) => $response->json()) // Success handler
$data = null;

async(fn () => fetch('https://nonexistent-url.com'))
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
->catch(fn (Throwable $e) => "Error: " . $e->getMessage()); // Error handler

echo $response;
echo $data;
```

In this example, any errors that occur during the request are caught and handled gracefully.
Expand All @@ -152,11 +173,13 @@ Just like synchronous requests, asynchronous requests allow you to handle differ
### **Example: Handling JSON Response**

```php
$response = async(fn () => fetch('https://example.com/api/resource'))
->then(fn ($response) => $response->json())
$data = null;

async(fn () => fetch('https://example.com/api/resource'))
->then(fn ($response) => $data = $response->json())
->catch(fn (Throwable $e) => $e->getMessage());

echo $response;
echo $data;
```

- **json()**: Parses the response as JSON.
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ $response = fetch('https://example.com/nonexistent', [
if ($response->ok()) {
echo $response->json();
} else {
echo "HTTP Error: " . $response->status() . " - " . $response->statusText();
echo "HTTP Error: " . $response->getStatusCode() . " - " . $response->statusText();
}
```

Expand Down
24 changes: 18 additions & 6 deletions docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ FetchPHP also supports asynchronous requests using a syntax similar to JavaScrip
```php
use Fetch\Interfaces\Response as ResponseInterface;

$response = async(fn () => fetch('https://example.com', [
$data = null;

async(fn () => fetch('https://example.com', [
'method' => 'POST',
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode(['key' => 'value']),
]))
->then(fn (ResponseInterface $response) => $response->json()) // Success handler
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
->catch(fn (Throwable $e) => $e->getMessage()); // Error handler

echo $data;
```

This example asynchronously sends a POST request and processes the JSON response, or handles an error using `.catch()`.
Expand Down Expand Up @@ -84,14 +88,18 @@ You can also use the fluent API for asynchronous requests:
```php
use Fetch\Interfaces\Response as ResponseInterface;

$response = async(fn () => fetch()
$data = null;

async(fn () => fetch()
->baseUri('https://example.com')
->withHeaders('Content-Type', 'application/json')
->withBody(json_encode(['key' => 'value']))
->withToken('fake-bearer-auth-token')
->post('/posts'))
->then(fn (ResponseInterface $response) => $response->json()) // Success handler
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
->catch(fn (Throwable $e) => $e->getMessage()); // Error handler

echo $data;
```

This example asynchronously sends a POST request using the fluent API, handles the response, or catches any errors.
Expand Down Expand Up @@ -150,9 +158,13 @@ if ($response->ok()) {
```php
use Fetch\Interfaces\Response as ResponseInterface;

$response = async(fn () => fetch('https://nonexistent-url.com'))
->then(fn (ResponseInterface $response) => $response->json())
$data = null;

async(fn () => fetch('https://nonexistent-url.com'))
->then(fn (ResponseInterface $response) => $data = $response->json())
->catch(fn (\Throwable $e) => echo "Error: " . $e->getMessage());

echo $data;
```

## Proxy and Authentication Support
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/sync-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ if ($response->ok()) {
$data = $response->json();
echo "JSON Response: " . print_r($data, true);
} else {
echo "Error: " . $response->status() . " - " . $response->statusText();
echo "Error: " . $response->getStatusCode() . " - " . $response->statusText();
}
```

Expand Down
85 changes: 0 additions & 85 deletions docs/markdown-examples.md

This file was deleted.

0 comments on commit 86accb3

Please sign in to comment.