Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Requires PHP 8.4+, Laravel 13, and Node.js for the Skir compiler.
- Scaffold application-owned controllers and Form Requests from generated manifests. See [Scaffolding](docs/scaffolding.md).
- Route procedures to attributed or invokable Laravel controllers with native dependency injection, middleware, authorization, and Form Requests. See [Routing](docs/routing.md).
- Keep generated providers and manual handlers available for non-controller layouts. See [Routing](docs/routing.md#generated-procedure-providers).
- Inspect an endpoint's procedures through its opt-in Studio. See [Studio](docs/routing.md#studio).
- Browse and call an endpoint's procedures in Skir's RPC Studio, opt-in per endpoint. See [Studio](docs/routing.md#studio).
- Speak the standard SkirRPC protocol, so clients generated for any Skir language can call your endpoints. See [Wire protocol](docs/protocol.md).

## Quick start
Expand Down
1 change: 1 addition & 0 deletions config/skir-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
return [
'studio_enabled' => env('SKIR_SERVER_STUDIO_ENABLED', false),
'studio_query_key' => env('SKIR_SERVER_STUDIO_QUERY_KEY', 'studio'),
'studio_app_js_url' => env('SKIR_SERVER_STUDIO_APP_JS_URL', 'https://cdn.jsdelivr.net/npm/skir-studio/dist/skir-studio-standalone.js'),

'manifests' => [
base_path('app/Skir/skirout/skir-server-manifest.json'),
Expand Down
11 changes: 11 additions & 0 deletions docs/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,11 @@ Studio is controlled by these settings in `config/skir-server.php`:
```php
'studio_enabled' => env('SKIR_SERVER_STUDIO_ENABLED', false),
'studio_query_key' => env('SKIR_SERVER_STUDIO_QUERY_KEY', 'studio'),
'studio_app_js_url' => env('SKIR_SERVER_STUDIO_APP_JS_URL', 'https://cdn.jsdelivr.net/npm/skir-studio/dist/skir-studio-standalone.js'),
```

Studio is Skir's [RPC Studio](https://skir.build/docs/skirrpc#rpc-studio): a page to browse an endpoint's methods, fill in a request, send it and read the response. The endpoint serves a small HTML page that loads the Studio app from `studio_app_js_url`; point that setting at your own copy of the script when the app must not come from a CDN.

Studio is disabled by default. Enable it on an individual endpoint with `studio()`:

```php
Expand All @@ -331,6 +334,14 @@ Route::skirRpc('/api/skir', [

Calling `studio()` explicitly enables Studio for that endpoint, regardless of `studio_enabled`. With the default query key, open `/api/skir?studio` with a GET request. Only the query parameter's presence matters; its value is ignored. Studio renders the procedures registered on that route. Endpoints with providers keep separate procedure lists; endpoints registered without providers all share the application-wide `SkirServer` and list the same procedures.

### Method list

The Studio app reads the endpoint's methods from a GET request to `/api/skir?list`. The answer follows the upstream Skir services: every method with its name, number, and the type descriptors of its request and response. The list is available only while Studio is enabled for the endpoint; otherwise it answers `404`.

The PHP runtime types carry no record names, so the names in the list are derived. A method's request and response are named `<Method>Request` and `<Method>Response`, and a nested struct or enum is named after the field or variant that holds it. Structs and enums with the same shape share one definition. These names only label the Studio forms; they do not affect the wire format.

Studio sends its calls from the browser as `{"method": ..., "request": ...}` POST requests, with the `Authorization` header you enter in the app. Outer route middleware applies to the Studio page, the method list, and every call.

Set `studio_enabled` to `true` to enable Studio for every SkirRPC route by default. Calling `studio(false)` on a route overrides that global setting and keeps Studio disabled for that endpoint.

Use `studio_query_key` to change the query parameter. For example, setting it to `skir-studio` makes the Studio URL `/api/skir?skir-studio`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Studio is disabled by default. Enable it per route with `studio()` or override a
`studio(false)`. Package configuration uses `studio_enabled` and `studio_query_key`; the query key
only selects the Studio response and is never access control.

Studio is the official Skir RPC Studio app, loaded from `studio_app_js_url`. It reads the endpoint's
methods from `GET <endpoint>?list`, which answers `404` unless Studio is enabled, and sends calls as
readable JSON POST requests.

```php
Route::skirRpc('/api/skir', [
Skir::controller(AccountController::class),
Expand Down Expand Up @@ -42,7 +46,7 @@ paths applicable to the change:
- regressions for explicit `null`, boundary request shapes, and any corrected failure contract.

Assert status, Skir error code/details, response content type/body, authorization boundary, and side
effects. For Studio, assert both rendered procedure metadata and protection on the outer route. Use
effects. For Studio, assert the `?list` method metadata and protection on the outer route. Use
genuine encoded request/response bodies.

When a test fails, first identify whether the mismatch belongs to route protection, method
Expand Down
23 changes: 22 additions & 1 deletion src/Http/Controllers/SkirRpcController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
use Skir\Server\RegisteredProcedure;
use Skir\Server\Routing\SkirEndpointRegistry;
use Skir\Server\SkirContext;
use Skir\Server\Studio\MethodList;
use Skir\Server\Studio\StudioRenderer;
use Symfony\Component\HttpFoundation\Response;

final readonly class SkirRpcController
{
private const string STUDIO_APP_JS_URL = 'https://cdn.jsdelivr.net/npm/skir-studio/dist/skir-studio-standalone.js';

public function __construct(
private SkirEndpointRegistry $endpoints,
private StudioRenderer $studioRenderer,
private MethodList $methodList,
private Container $container,
private SkirMethodRequestScope $requestScope,
private Router $router,
Expand All @@ -37,13 +41,21 @@ public function __invoke(Request $request): Response
try {
$server = $this->endpoints->server($request->route());

if ($this->isMethodListRequest($request)) {
if (! $this->studioEnabled($request)) {
abort(Response::HTTP_NOT_FOUND);
}

return response()->json($this->methodList->toArray($server));
}

if ($this->isStudioRequest($request)) {
if (! $this->studioEnabled($request)) {
abort(Response::HTTP_NOT_FOUND);
}

return response(
$this->studioRenderer->render($server, '/'.$request->path()),
$this->studioRenderer->render((string) config('skir-server.studio_app_js_url', self::STUDIO_APP_JS_URL)),
Response::HTTP_OK,
)->header('content-type', 'text/html; charset=UTF-8');
}
Expand Down Expand Up @@ -109,6 +121,15 @@ private function procedureMiddleware(PreparedProcedure $prepared): array
return $prepared->middleware;
}

private function isMethodListRequest(Request $request): bool
{
if (! $this->usesQueryString($request)) {
return false;
}

return $this->rpcBody($request) === 'list';
}

private function isStudioRequest(Request $request): bool
{
if (! $this->usesQueryString($request)) {
Expand Down
43 changes: 43 additions & 0 deletions src/Studio/MethodList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Skir\Server\Studio;

use Skir\Server\RegisteredProcedure;
use Skir\Server\SkirServer;

/** What an endpoint answers to `?list`: its methods, in the format of the upstream Skir services. */
final class MethodList
{
/**
* @return array{
* methods: list<array{
* method: string,
* number: int,
* request: array<string, mixed>,
* response: array<string, mixed>
* }>
* }
*/
public function toArray(SkirServer $server): array
{
return [
'methods' => array_values(array_map(
fn (RegisteredProcedure $procedure): array => [
'method' => $procedure->descriptor->name,
'number' => $procedure->descriptor->number,
'request' => (new TypeDescriptor)->describe(
$procedure->descriptor->requestType,
"{$procedure->descriptor->name}Request",
),
'response' => (new TypeDescriptor)->describe(
$procedure->descriptor->responseType,
"{$procedure->descriptor->name}Response",
),
],
$server->procedures(),
)),
];
}
}
Loading
Loading