diff --git a/resources/js/index.ts b/resources/js/index.ts index e7aa8a9..1fced16 100644 --- a/resources/js/index.ts +++ b/resources/js/index.ts @@ -187,7 +187,7 @@ export class Component return reason }) - .then((json: ComponentResponse) => { // todo this then() shouldn't execute if previous catch() executes + .then((json: ComponentResponse) => { if (json?.metadata?.errors) { this.errors = json.metadata.errors } @@ -227,7 +227,11 @@ export class Component let result = null; try { result = callback(); - } catch (e) { } + } catch (e) { + this.pausedRequests = false; + + throw e + } this.pausedRequests = false; diff --git a/src/TypehintConverter.php b/src/TypehintConverter.php index 5e6a24b..2bb71d0 100644 --- a/src/TypehintConverter.php +++ b/src/TypehintConverter.php @@ -32,15 +32,17 @@ class TypehintConverter }; } - public function convertType(string $php): string + public function convertType(string $php, string $target = 'property'): string { if (class_exists($php)) { if (is_subclass_of($php, Model::class) && ($model = $php::first())) { - return $this->convertModel($model); + return $target === 'parameter' + ? $this->convertModel($model) . '|string|number' // Models can be resolved from IDs + : $this->convertModel($model); } - if (is_subclass_of($php, Collection::class) && ($model = $php::first())) { - return 'array'; + if (is_subclass_of($php, Collection::class)) { + return 'any'; // Later maybe typed arrays? } return 'any'; @@ -200,7 +202,7 @@ class TypehintConverter if (isset($object->$property) && gettype($object->$property) === $type->getName()) { $results[] = $this->typeFromValue($object->$property); } else { - $results[] = $this->convertType($type->getName()); + $results[] = $this->convertType($type->getName(), 'property'); } } @@ -226,14 +228,14 @@ class TypehintConverter $types[] = 'null'; } - $parameters[$parameter->getName()] = join(' | ', array_map(fn (ReflectionNamedType $type) => $this->convertType($type->getName()), $types)); + $parameters[$parameter->getName()] = join(' | ', array_map(fn (ReflectionNamedType $type) => $this->convertType($type->getName(), 'parameter'), $types)); } $parameters = collect($parameters)->map(fn (string $type, string $name) => "{$name}: {$type}")->join(', '); $return = match ($type = $reflection->getReturnType()) { null => 'any', - default => $this->convertType($type), + default => $this->convertType($type, 'return'), }; return "{$method}(" . $parameters . "): AirwirePromise<{$return}>;"; diff --git a/tests/Airwire/TypehintsTest.php b/tests/Airwire/TypehintsTest.php index 3dff0ec..8bf9b0f 100644 --- a/tests/Airwire/TypehintsTest.php +++ b/tests/Airwire/TypehintsTest.php @@ -163,7 +163,7 @@ class TypehintComponent extends Component #[Wired] public Product $model; - #[Wired] #[Encode(method: 'getKey')] // todo add the same feature for Decode + #[Wired] #[Encode(method: 'getKey')] // todo add the same feature for Decode (but then we may have to update the type generator) public Product $model2; #[Wired]