You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The type key present on the Scribe config file tells the type of docs setup you want. There are two options: static (that generates a simple HTML file) and laravel (that generates a blade view served via Laravel routes). For our scenario we will configure using the laravel type.
[config/scribe.php]
'type' => 'laravel',
Defining API basic info
You can set some basic information about the API, like the title, description, introdution text and base url. You can do this by editing the keys of config array as the example that follows.
[config/scribe.php]
'title' => 'Book API',
'description' => 'API that allows the basic CRUD operation for books.',
'intro_text' => <<<INTRO
This documentation will provide all the information you need to work with our API.
<aside>
As you scroll, you'll see code examples for working with the API in the available programming languages in the dark area to the right (or as part of the content on mobile).
You can switch the language used with the tabs at the top right (or from the nav menu at the top left on mobile).
</aside>
INTRO,
'base_url' => '{{config("app.url")}}:{{config("app.port")}}',
Example requests
You can define the programming languages that will be used to show examples of requests to the endpoints of the API. The examples will be shown in each of these languages defined in the config array in the area on the right of the page.
Now, let's do a test run. Run the command to generate your docs.
php artisan scribe:generate
After this, make sure that your application is running and access the API documentaton endpoint.
http://localhost:9999/api-docs
You can navigate through the documentation.
From now on, for any changes that you make that affect the documentation somehow, you will have to run the generation code again, so keep that in mind.
Now you have to implement the Scribe annotations on the dockblocks of your controllers and requests classes as needed.
An example for a controller:
<?phpnamespaceApp\Http\Controllers\Api;
useThrowable;
useIlluminate\Http\JsonResponse;
useIlluminate\Http\Request;
useIlluminate\Http\Response;
useIlluminate\Support\Facades\Log;
useApp\Domain\Auth\DataTransferObjects\LoginDto;
useApp\Domain\Auth\Exceptions\IncorrectPasswordException;
useApp\Domain\Auth\Exceptions\InvalidUserException;
useApp\Domain\Auth\Services\Interfaces\AuthServiceInterface;
useApp\Http\Controllers\Controller;
useApp\Http\Requests\Api\LoginRequest;
useApp\Http\Resources\AuthenticatedUserResource;
useApp\Traits\Http\ApiResponses;
/** * @group Authentication * * Endpoints for managing API authentication */class AuthController extends Controller
{
use ApiResponses;
publicfunction__construct(privateAuthServiceInterface$authService)
{
}
/** * Login * * This endpoint lets you login an API user, generating an access token for him. * * @responseField access_token string The access token that will be used to authenticate API requests. * @responseField token_type string The type of token generated. * @responseField expires_at string The date and time in which the token will expire. * * @response status=200 scenario=success { * "data": { * "access_token": "1|laravel10_falemaisUI8A7aHrlN0XCyKApJCfO2uzK9Gc4X8DWZtFJbCY4d735783", * "token_type": "Bearer", * "expires_at": "2024-02-01 12:27:37" * } * } * * @response status=400 scenario="user with email provided not found" { * "message": "Could not found a valid user with the email: [email protected]." * } * * @response status=400 scenario="password incorrect" { * "message": "The password provided for this user is incorrect." * } * * @response status=500 scenario="unexpected error" { * "message": "Internal Server Error." * } * */publicfunctionlogin(LoginRequest$request): JsonResponse
{
try {
$dto = LoginDto::from([
'email' => $request->input('email'),
'password' => $request->input('password')
]);
$result = $this->authService->login($dto);
return$this->sendSuccessResponse(
data: $result->toArray(),
code: Response::HTTP_OK
);
} catch (Throwable$exception) {
Log::error(
'[AuthController] Failed to login with the credentials provided.',
[
'error_message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'data' => [
'received_data' => $request->all() ?? null
],
'stack_trace' => $exception->getTrace()
]
);
$exceptionTypes = [InvalidUserException::class, IncorrectPasswordException::class];
$errorMessage = in_array(get_class($exception), $exceptionTypes)
? $exception->getMessage()
: 'An error has occurred. Could not login with the credentials provided as requested.';
return$this->sendErrorResponse(
message: $errorMessage,
code: $exception->getCode()
);
}
}
}
An example for a request class (in this case we do not use annotations, but include a method bodyParameters):
<?phpnamespaceApp\Http\Requests\Api;
useIlluminate\Foundation\Http\FormRequest;
class LoginRequest extends FormRequest
{
/** * Determine if the user is authorized to make this request. */publicfunctionauthorize(): bool
{
returntrue;
}
/** * Get the validation rules that apply to the request. * * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> */publicfunctionrules(): array
{
return [
'email' => ['required', 'string', 'min:6', 'max:70', 'email'],
'password' => ['required', 'string', 'min:6']
];
}
/** * Define body params to be used by the API documentation */publicfunctionbodyParameters(): array
{
return [
'email' => [
'description' => 'The e-mail of the user.',
'example' => '[email protected]'
],
'password' => [
'description' => 'The password of the user.',
'example' => 'LfMJvB5b9xZbF76Q4tFT'
]
];
}
}