Skip to content

Events and responses

Use NativePHP listeners such as #[On] or $this->on(). These events come through NativePHP's bridge; they are not ordinary Laravel global events.

Completion example

php
use Native\Mobile\Attributes\On;
use Victorycodedev\NativephpFetch\Events\FetchRequestCompleted;
use Victorycodedev\NativephpFetch\FetchResponse;

#[On(FetchRequestCompleted::class)]
public function completed(
    string $requestId,
    int $status,
    array $headers,
    string $body,
): void {
    if ($requestId !== $this->requestId) {
        return;
    }

    $response = FetchResponse::from($requestId, $status, $headers, $body);

    if ($response->successful()) {
        $this->users = $response->json('data', []);
    }
}

Event reference

EventData
FetchRequestStartedrequestId, method, url
FetchRequestCompletedrequestId, status, headers, body
FetchRequestFailedrequestId, message, nullable code
FetchRequestCancelledrequestId
FetchRequestRetryingrequestId, attempt, maxAttempts, delayMs, reason, nullable status
FetchUploadProgressrequestId, bytesSent, bytesTotal, progress
FetchDownloadProgressrequestId, bytesReceived, nullable bytesTotal, nullable progress
FetchDownloadCompletedrequestId, status, headers, path, bytesReceived

Terminal events

Each request ends with exactly one terminal event: completed, failed, cancelled, or download-completed. HTTP statuses do not automatically make a normal request fail; inspect the response status in FetchRequestCompleted.

Transport error codes

CodeMeaning
timeoutThe per-attempt timeout elapsed.
offlineThe device is offline or has no network route.
dns_failureHostname resolution failed.
connection_failedA connection could not be established.
tls_failureTLS or certificate validation failed.
network_errorAnother native transport error occurred.
http_errorA download or exhausted retry received a failed status.
write_failedA download could not be written or committed.

Only an explicit cancel() emits the cancelled event. Timeouts emit failure code timeout.

Released under the MIT License.