-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathApiDocsController.php
More file actions
79 lines (67 loc) · 2.32 KB
/
Copy pathApiDocsController.php
File metadata and controls
79 lines (67 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace BookStack\Api;
use BookStack\Http\ApiController;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class ApiDocsController extends ApiController
{
protected static array $gettingStartedSectionsById = [
'authentication' => 'Authentication',
'request-format' => 'Request Format',
'listing-endpoints' => 'Listing Endpoints',
'error-handling' => 'Error Handling',
'rate-limits' => 'Rate Limits',
'content-security' => 'Content Security',
];
/**
* Load the docs page for the API.
*/
public function display()
{
$docs = ApiDocsGenerator::generateConsideringCache();
$this->setPageTitle(trans('settings.users_api_tokens_docs'));
return view('api-docs.index', [
'docs' => $docs,
'gettingStartedSections' => static::$gettingStartedSectionsById,
]);
}
/**
* Show a JSON view of the API docs data.
*/
public function json()
{
$docs = ApiDocsGenerator::generateConsideringCache();
return response()->json($docs);
}
/**
* Download the API docs as a cleaner HTML file or as JSON with embedded HTML guidance.
* Provide a ?format=json query parameter to download as JSON.
*/
public function download(Request $request)
{
$format = $request->query('format') ?? 'html';
$docs = ApiDocsGenerator::generateConsideringCache();
$downloadName = Str::slug(strtolower(setting('app-name')) . '-api-docs');
if ($format === 'json') {
$docs->prepend(
view('api-docs.parts.getting-started')->render(),
'getting-started-guide'
);
return $this->createDownload()->directly(json_encode($docs), "{$downloadName}.json");
}
$responseData = view('api-docs.download', [
'docs' => $docs,
'gettingStartedSections' => static::$gettingStartedSectionsById,
])->render();
return $this->createDownload()->directly($responseData, "{$downloadName}.html");
}
/**
* Redirect to the API docs page.
* Required as a controller method, instead of the Route::redirect helper,
* to ensure the URL is generated correctly.
*/
public function redirect()
{
return redirect('/api/docs');
}
}