Skip to content

Commit 908fd36

Browse files
committed
Update doc
1 parent 327d079 commit 908fd36

28 files changed

+671
-448
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## Scribe ✍
1+
<h1 align="center">Scribe</h1>
2+
3+
<p align="center">
4+
<img src="logo-scribe.png"><br>
5+
</p>
26

37
Generate API documentation for humans from your Laravel codebase. [Here's what the output looks like](https://shalvah.me/TheCensorshipAPI/).
48

config/scribe.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* The value of the parameter. This will NOT be part of the generated documentation.
5555
* Use it to easily auth response calls by this package. Otherwise, we'll use a random value.
5656
*/
57-
'use_value' => env('SCRIBE_API_KEY'),
57+
'use_value' => env('SCRIBE_AUTH_KEY'),
5858

5959
/*
6060
* Any extra info for your users. For instance, you can describe where to find (or generate) their auth credentials.
@@ -88,10 +88,16 @@
8888

8989
/*
9090
* The base URL to be used in examples and the Postman collection.
91-
* By default, this will be the value of config('app.url').
91+
* If this is null, Scribe will use the value of config('app.url').
9292
*/
9393
'base_url' => null,
9494

95+
/*
96+
* The HTML <title> for the generated documentation, and the name of the generated Postman collection.
97+
* If this is null, Scribe will infer it from config('app.name').
98+
*/
99+
'title' => null,
100+
95101
/*
96102
* Generate a Postman collection in addition to HTML docs.
97103
* For 'static' docs, the collection will be generated to public/docs/collection.json.
@@ -104,11 +110,6 @@
104110
*/
105111
'enabled' => true,
106112

107-
/*
108-
* The name for the exported Postman collection. Default: config('app.name')." API"
109-
*/
110-
'name' => null,
111-
112113
/*
113114
* The description for the exported Postman collection.
114115
*/

docs/config.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Configuration
2+
[IN PROGRESS]
23

34
Before you can generate your documentation, you'll need to configure a few things in your `config/scribe.php`. If you aren't sure what an option does, it's best to leave it set to the default. If you don't have this config file, see the [installation instructions](index.html#installation).
45

@@ -75,9 +76,7 @@ If you are using a custom serializer with league/fractal, you can specify it he
7576
Leave this as null to use no serializer or return a simple JSON.
7677

7778
## `routes`
78-
The `routes` section is an array of items, describing what routes in your application that should have documentation generated for them. Each item in the array contains rules about what routes belong in that group, and what rules to apply to them. This allows you to apply different settings to different routes.
79-
80-
> Note: This package does not work with Closure-based routes. If you want your route to be captured by this package, you need a controller.
79+
The `routes` section is an array of items, describing what routes in your application that should be included in the generated documentation. Each item in the array contains rules about what routes belong in that group, and what rules to apply to them. This allows you to apply different settings to different routes.
8180

8281
Each item in the `routes` array (a route group) has keys which are explained below. We'll use this sample route definition for a Laravel app to demonstrate them:
8382

docs/documenting-api-information.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Adding general information about your API
2+
3+
## Authentication information
4+
You can add authentication information for your API using the `auth` section in `scribe.php`. Scribe will use this in three places:
5+
- Generating an "Authentication" section in your docs
6+
- Adding authentication parameters o your rxample requests (for endpoints marked as `authenticated`)
7+
- Setting authentication information for response calls.
8+
9+
Here's how you'd configure auth with a query parameter named `apiKey`:
10+
11+
```php
12+
'auth' => [
13+
'enabled' => true,
14+
'in' => 'query',
15+
'name' => 'apiKey',
16+
'use_value' => env('SCRIBE_API_KEY'),
17+
'extra_info' => 'You can retrieve your key by going to settings and clicking <b>Generate API key</b>.',
18+
],
19+
```
20+
21+
If `apiKey` were to be a body parameter, the config would be same. Just set `in` to `'body'`.
22+
23+
Here's an example with a bearer token (also applies to basic auth, if you change `in` to `'basic'`):
24+
25+
26+
```php
27+
'auth' => [
28+
'enabled' => true,
29+
'in' => 'bearer',
30+
'name' => 'hahaha', // <--- This value is ignored for bearer and basic auth
31+
'use_value' => env('SCRIBE_AUTH_KEY'),
32+
'extra_info' => 'You can retrieve your token by visiting your dashboard and clicking <b>Generate API token</b>.',
33+
],
34+
```
35+
36+
And here's an example with a custom header:
37+
38+
39+
```php
40+
'auth' => [
41+
'enabled' => true,
42+
'in' => 'header',
43+
'name' => 'Api-Key', // <--- The name of the header
44+
'use_value' => env('SCRIBE_AUTH_KEY'),
45+
'extra_info' => 'You can retrieve your token by visiting your dashboard and clicking <b>Generate API token</b>.',
46+
],
47+
```
48+
49+
You can set whatever you want as the `extra_info`. A good idea would be to tell your users where to get their auth key.
50+
51+
The `use_value` field is only used by Scribe for response calls. It won't be included in the generated output or examples.
52+
53+
For more information, see the [reference documentation on the auth section](config.html#auth).
54+
55+
56+
## Introductory text
57+
The `intro_text` key in `scribe.php` is where you can set the text shown to readers in the "Introduction" section. If your text is too long to be put in a config file, you can create a `prepend.md` containing the intro text and put it in the `resources/docs` folder.
58+
59+
## Title
60+
You can set the HTML <title> for the generated documentation, and the name of the generated Postman collection by setting the `title` key in `scribe.php`. If you leave it as null, Scribe will infer it from the value of `config('app.name')`.
61+
62+
## Logo
63+
Maybe you've got a pretty logo for your API or company, and you'd like to display that on your documentation page. No worries! To add a logo, set the `logo` key in `scribe.php` to the path of the logo. Here are your options:
64+
65+
- To point to an image on an external public URL, set `logo` to that URL.
66+
- To point to an image in your codebase, set `logo` to the `public_path()` of the image, if you're using `laravel` type docs. If you're using `static` type, pass in the relative path to the image from the `public/docs` directory. For example, if your logo is in public/images, use '../img/logo.png' for `static` type and 'img/logo.png' for `laravel` type.
67+
- To disable the logo, set `logo` to false.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Documenting parameters for an endpoint
2+
[IN PROGRESS]
3+
4+
## Specifying request parameters
5+
To specify a list of valid parameters your API route accepts, use the `@urlParam`, `@bodyParam` and `@queryParam` annotations.
6+
- The `@urlParam` annotation is used for describing parameters in your URl. For instance, in a Laravel route with this URL: "/post/{id}/{lang?}", you would use this annotation to describe the `id` and `lang` parameters. It takes the name of the parameter, an optional "required" label, and then its description.
7+
- The `@queryParam` annotation takes the name of the parameter, an optional "required" label, and then its description.
8+
- The `@bodyParam` annotation takes the name of the parameter, its type, an optional "required" label, and then its description.
9+
10+
Examples:
11+
12+
```php
13+
/**
14+
* @urlParam id required The ID of the post.
15+
* @urlParam lang The language.
16+
* @bodyParam user_id int required The id of the user. Example: 9
17+
* @bodyParam room_id string The id of the room.
18+
* @bodyParam forever boolean Whether to ban the user forever. Example: false
19+
* @bodyParam another_one number Just need something here.
20+
* @bodyParam yet_another_param object required Some object params.
21+
* @bodyParam yet_another_param.name string required Subkey in the object param.
22+
* @bodyParam even_more_param array Some array params.
23+
* @bodyParam even_more_param.* float Subkey in the array param.
24+
* @bodyParam book.name string
25+
* @bodyParam book.author_id integer
26+
* @bodyParam book[pages_count] integer
27+
* @bodyParam ids.* integer
28+
* @bodyParam users.*.first_name string The first name of the user. Example: John
29+
* @bodyParam users.*.last_name string The last name of the user. Example: Doe
30+
*/
31+
public function createPost()
32+
{
33+
// ...
34+
}
35+
36+
/**
37+
* @queryParam sort Field to sort by
38+
* @queryParam page The page number to return
39+
* @queryParam fields required The fields to include
40+
*/
41+
public function listPosts()
42+
{
43+
// ...
44+
}
45+
```
46+
47+
They will be included in the generated documentation text and example requests.
48+
49+
**Result:**
50+
51+
![](./../body_params_1.png)
52+
53+
![](./../body_params_2.png)
54+
55+
### Example parameters
56+
For each parameter in your request, this package will generate a random value to be used in the example requests. If you'd like to specify an example value, you can do so by adding `Example: your-example` to the end of your description. For instance:
57+
58+
```php
59+
/**
60+
* @queryParam location_id required The id of the location.
61+
* @queryParam user_id required The id of the user. Example: me
62+
* @queryParam page required The page number. Example: 4
63+
* @bodyParam user_id int required The id of the user. Example: 9
64+
* @bodyParam room_id string The id of the room.
65+
* @bodyParam forever boolean Whether to ban the user forever. Example: false
66+
*/
67+
```
68+
69+
You can also exclude a particular parameter from the generated examples (for all languages) by annotating it with `No-example`. For instance:
70+
```php
71+
/**
72+
* @queryParam location_id required The id of the location. Example: 1
73+
* @queryParam user_id required The id of the user. No-example
74+
* @queryParam page required The page number. Example: 4
75+
*/
76+
```
77+
Outputs:
78+
```bash
79+
curl -X GET -G "https://example.com/api?location_id=1&page=4"
80+
```
81+
82+
Note: You can also add the `@queryParam` and `@bodyParam` annotations to a `\Illuminate\Foundation\Http\FormRequest` subclass instead, if you are using one in your controller method
83+
84+
```php
85+
/**
86+
* @queryParam user_id required The id of the user. Example: me
87+
* @bodyParam title string required The title of the post.
88+
* @bodyParam body string required The content of the post.
89+
* @bodyParam type string The type of post to create. Defaults to 'textophonious'.
90+
* @bodyParam author_id int the ID of the author. Example: 2
91+
* @bodyParam thumbnail image This is required if the post type is 'imagelicious'.
92+
*/
93+
class MyRequest extends \Illuminate\Foundation\Http\FormRequest
94+
{
95+
96+
}
97+
98+
// in your controller...
99+
public function createPost(MyRequest $request)
100+
{
101+
// ...
102+
}
103+
```

docs/documenting-endpoint-headers.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Documenting headers for endpoints
2+
3+
To specify headers to be added to your endpoints, use the `apply.headers` section of the route group in `scribe.php`. For instance, if you have this config:
4+
5+
```php
6+
'routes' => [
7+
[
8+
'match' => [
9+
'domains' => ['*'],
10+
'prefixes' => ['v2/'],
11+
],
12+
'apply' => [
13+
'headers' => [ 'Api-Version' => 'v2']
14+
]
15+
]
16+
]
17+
```
18+
19+
All endpoints that start with `v2/` will have the header `Api-Version: v2` included in their example requests and response calls.

docs/documenting-endpoint-metadata.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Specifying metadata about an endpoint
2+
3+
## Endpoint title and description
4+
To set an endpoint's title and description, just write in the method's docblock. The first paragraph will be used as the title, the rest as the description. Custom formatting (such as `<aside>` tags) is also supported (see the [Pastel docs](http://github.com/knuckleswtf/pastel))
5+
6+
For instance, this:
7+
8+
```php
9+
/**
10+
* Add a word to the list.
11+
* This endpoint allows you to add a word to the list. It's a really useful endpoint,
12+
* and you should play around with it for a bit.
13+
* <aside class="notice">We mean it; you really should.😕</aside>
14+
*/
15+
public function store(Request $request)
16+
{
17+
//...
18+
}
19+
```
20+
21+
becomes:
22+
23+
![](images/endpoint-title-description.png)
24+
25+
## Grouping endpoints
26+
All endpoints are grouped for easy navigation.
27+
28+
To add all endpoints in a controller to a group, use `@group` in the controller docblock, followed by the group's title. You can also add a description below the group.
29+
30+
You can also specify an `@group` on a single method to override the group defined at the controller level.
31+
32+
```php
33+
/**
34+
* @group User management
35+
*
36+
* APIs for managing users
37+
*/
38+
class UserController extends Controller
39+
{
40+
41+
/**
42+
* Create a user.
43+
*/
44+
public function createUser()
45+
{
46+
47+
}
48+
49+
/**
50+
* Change a user's password.
51+
*
52+
* @group Account management
53+
*/
54+
public function changePassword()
55+
{
56+
57+
}
58+
}
59+
```
60+
61+
![Doc block result](images/endpoint-groups.png)
62+
63+
Grouping endpoints is optional. Any endpoints not in a group will be placed in a default group, "Endpoints".
64+
65+
## Indicating authentication status
66+
You can use the `@authenticated` annotation on a method to indicate if the endpoint is authenticated. A "Requires authentication" badge will be added to that route in the generated documentation.
67+
68+
If all the routes in a controller are authenticated, you can specify `@authenticated` in the controller doc block instead.
69+
70+
```php
71+
/**
72+
* Create a user
73+
* This endpoint lets you create a user.
74+
* @authenticated
75+
*
76+
*/
77+
public function create()
78+
{
79+
}
80+
```
81+
82+
![](images/endpoint-auth.png)

0 commit comments

Comments
 (0)