Skip to content

Commit a24387f

Browse files
Merge pull request rakutentech#96 from rakutentech/develop
Develop
2 parents 9e2e6cd + c07a7f4 commit a24387f

File tree

4 files changed

+69
-51
lines changed

4 files changed

+69
-51
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,5 @@ Fixing lints
175175
- v1.22 Boolean|File|Image support
176176
- v1.22 Boolean|File|Image support
177177
- v1.23 Bug fix for lrd doc block #76
178+
- v1.27 A few fixes on width and added request_methods
178179

config/request-docs.php

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

33
return [
4-
// change it to true will make lrd to throw exception if rules in request class need to be changed
5-
// keep it false
6-
'debug' => false,
7-
'document_name' => 'LRD',
4+
// change it to true will make lrd to throw exception if rules in request class need to be changed
5+
// keep it false
6+
'debug' => false,
7+
'document_name' => 'LRD',
88

99
/*
1010
* Route where request docs will be served from
@@ -15,6 +15,7 @@
1515
//Example
1616
// \App\Http\Middleware\NotFoundWhenProduction::class,
1717
],
18+
1819
/**
1920
* Path to to static HTML if using command line.
2021
*/
@@ -23,32 +24,41 @@
2324
/**
2425
* Sorting route by and there is two types default(route methods), route_names.
2526
*/
26-
'sort_by' => 'default',
27+
'sort_by' => 'route_names',
2728

2829
//Use only routes where ->uri start with next string Using Str::startWith( . e.g. - /api/mobile
2930
'only_route_uri_start_with' => '',
3031

3132
'hide_matching' => [
32-
"#^telescope#",
33-
"#^docs#",
34-
"#^request-docs#",
33+
'#^telescope#',
34+
'#^docs#',
35+
'#^request-docs#',
36+
'#^api-docs#',
37+
'#^sanctum#',
38+
'#^_ignition#',
39+
],
40+
41+
'request_methods' => [
42+
'rules',
43+
'onCreate',
44+
'onUpdate',
3545
],
3646

37-
"open_api" => [
47+
'open_api' => [
3848
// default version that this library provides
39-
"version" => "3.0.0",
49+
'version' => '3.0.0',
4050
// changeable
41-
"document_version" => "1.0.0",
51+
'document_version' => '1.0.0',
4252
// license that you want to display
43-
"license" => "Apache 2.0",
44-
"license_url" => "https://www.apache.org/licenses/LICENSE-2.0.html",
45-
"server_url" => env('APP_URL', 'http://localhost'),
53+
'license' => 'Apache 2.0',
54+
'license_url' => 'https://www.apache.org/licenses/LICENSE-2.0.html',
55+
'server_url' => env('APP_URL', 'http://localhost'),
4656

4757
// for now putting default responses for all. This can be changed later based on specific needs
48-
"responses" => [
58+
'responses' => [
4959
'200' => [
5060
'description' => 'Successful operation',
51-
'content' => [
61+
'content' => [
5262
'application/json' => [
5363
'schema' => [
5464
'type' => 'object',
@@ -58,7 +68,7 @@
5868
],
5969
'400' => [
6070
'description' => 'Bad Request',
61-
'content' => [
71+
'content' => [
6272
'application/json' => [
6373
'schema' => [
6474
'type' => 'object',
@@ -68,7 +78,7 @@
6878
],
6979
'401' => [
7080
'description' => 'Unauthorized',
71-
'content' => [
81+
'content' => [
7282
'application/json' => [
7383
'schema' => [
7484
'type' => 'object',
@@ -78,7 +88,7 @@
7888
],
7989
'403' => [
8090
'description' => 'Forbidden',
81-
'content' => [
91+
'content' => [
8292
'application/json' => [
8393
'schema' => [
8494
'type' => 'object',
@@ -88,7 +98,7 @@
8898
],
8999
'404' => [
90100
'description' => 'Not Found',
91-
'content' => [
101+
'content' => [
92102
'application/json' => [
93103
'schema' => [
94104
'type' => 'object',
@@ -98,7 +108,7 @@
98108
],
99109
'422' => [
100110
'description' => 'Unprocessable Entity',
101-
'content' => [
111+
'content' => [
102112
'application/json' => [
103113
'schema' => [
104114
'type' => 'object',
@@ -108,7 +118,7 @@
108118
],
109119
'500' => [
110120
'description' => 'Internal Server Error',
111-
'content' => [
121+
'content' => [
112122
'application/json' => [
113123
'schema' => [
114124
'type' => 'object',
@@ -118,7 +128,7 @@
118128
],
119129
'default' => [
120130
'description' => 'Unexpected error',
121-
'content' => [
131+
'content' => [
122132
'application/json' => [
123133
'schema' => [
124134
'type' => 'object',
@@ -127,5 +137,5 @@
127137
],
128138
],
129139
],
130-
]
140+
],
131141
];

resources/views/index.blade.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<script src="https://unpkg.com/vue-markdown@2.2.4/dist/vue-markdown.js"></script>
2424
<script src="https://cdnjs.cloudflare.com/ajax/libs/sql-formatter/3.1.0/sql-formatter.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
25-
<script src="{{asset('./vendor/request-docs/dist/app.js')}}"></script>
25+
2626
<style>
2727
[v-cloak] {
2828
display: none;
@@ -107,7 +107,7 @@
107107
</div>
108108
</nav>
109109
<div id="app" v-cloak class="w-full flex lg:pt-10">
110-
<aside class="text-sm ml-1.5 text-grey-darkest break-all bg-gray-200 pl-2 h-screen sticky top-1 overflow-auto">
110+
<aside class="text-sm ml-1.5 text-grey-darkest break-all bg-gray-200 pl-2 h-screen sticky top-1 overflow-auto w-10/12">
111111
<section class="pt-5 pl-2 pr-2 pb-5 border mb-10 rounded bg-white shadow">
112112
<div class="font-sans">
113113
<h2 class="text-sm break-normal text-black break-normal font-sans pb-1 pt-1 text-black">
@@ -130,7 +130,7 @@ class="my-prism-editor"
130130
</section>
131131
<h1 class="font-medium mx-3 mt-3" style="width: max-content;min-width:350px;">Routes List</h1>
132132
<hr class="border-b border-gray-300">
133-
<table class="table-fixed text-sm mt-5" style="width: max-content">
133+
<table class="table-fixed text-sm mt-5 mb-5" style="width: max-content">
134134
<tbody>
135135
@foreach ($docs as $index => $doc)
136136
<tr v-if="!docs[{{$index}}]['isHidden']">
@@ -187,8 +187,8 @@ class="inline-flex text-xs"
187187
<hr class="border-b border-gray-300">
188188
<br>
189189
@foreach ($docs as $index => $doc)
190-
<section class="pt-5 pl-2 pr-2 pb-5 border mb-10 rounded bg-white shadow" v-if="!docs[{{$index}}]['isHidden']">
191-
<div class="font-sans" id="{{$doc['httpMethod'] .'-'. $doc['uri']}}">
190+
<section class="pt-5 pl-2 pr-2 pb-5 border-2 mb-10 rounded bg-white shadow" v-if="!docs[{{$index}}]['isHidden']">
191+
<div class="border-2 rounded" id="{{$doc['httpMethod'] .'-'. $doc['uri']}}">
192192
<h1 class="text-sm break-normal text-black bg-indigo-50 break-normal font-sans pb-1 pt-1 text-black">
193193
<span class="w-20
194194
font-medium
@@ -197,6 +197,8 @@ class="inline-flex text-xs"
197197
justify-center
198198
px-2
199199
py-1
200+
ml-2
201+
mr-2
200202
text-xs
201203
font-bold
202204
leading-none
@@ -214,8 +216,6 @@ class="inline-flex text-xs"
214216
</span>
215217
</h1>
216218
</div>
217-
<hr class="border-b border-grey-light">
218-
219219
<table class="table-fixed text-sm mt-5">
220220
<tbody>
221221
<tr>
@@ -245,12 +245,15 @@ class="inline-flex text-xs"
245245
</tbody>
246246
</table>
247247
<div v-if="docs[{{$index}}]['docBlock']" class="border-2 mr-4 mt-4 p-4 rounded text-sm">
248-
<h3 class="font-bold">LRD Docs</h3>
249-
<vue-markdown>{!! $doc['docBlock'] !!}</vue-markdown>
248+
<h3 class="font-bold">Description</h3>
249+
<hr>
250+
<vue-markdown class="mt-2">{!! $doc['docBlock'] !!}</vue-markdown>
250251
</div>
251-
<br>
252252
@if (!empty($doc['rules']))
253-
<table class="table-fixed align-left text-sm mt-5">
253+
<div class="border-2 mr-4 mt-4 p-4 rounded">
254+
<h3 class="font-bold">Attributes</h3>
255+
<hr>
256+
<table class="table-fixed align-left text-sm mt-3">
254257
<thead class="border">
255258
<tr class="border">
256259
<th class="border border-gray-300 pl-2 pr-16 pt-1 pb-1 w-min text-left bg-gray-200">No.</th>
@@ -319,7 +322,7 @@ class="inline-flex text-xs"
319322
<div class="font-mono">
320323
@foreach ($rules as $rule)
321324
@foreach (explode('|', $rule) as $r)
322-
@if (!in_array($r, ['required', 'integer', 'string', 'boolean', 'array', 'nullable', 'bail', 'file', 'image']))
325+
@if (!in_array($r, ['required', 'integer', 'string', 'boolean', 'array', 'nullable', 'bail', 'file', 'image', 'numeric']))
323326
{{$r}}
324327
@if (!$loop->last)
325328
<span class="text-gray-900 font-bold">|</span>
@@ -333,6 +336,7 @@ class="inline-flex text-xs"
333336
@endforeach
334337
</tbody>
335338
</table>
339+
</div>
336340
@endif
337341
<button
338342
class="hover:bg-red-500 font-semibold hover:text-white mt-2 pl-5 pr-5 border-gray-700 hover:border-transparent shadow-inner border-2 rounded-full"
@@ -559,6 +563,9 @@ class="my-prism-editor"
559563
validations.max = 100
560564
}
561565
}
566+
if (rule.match(/numeric/)) {
567+
validations.isInteger = true
568+
}
562569
})
563570
564571
if (validations.isString) {

src/LaravelRequestDocs.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public function sortDocs(array $docs, $sortBy = 'default'): array
5353
foreach ($methods as $method) {
5454
foreach ($docs as $key => $doc) {
5555
if (in_array($method, $doc['methods'])) {
56-
$sorted[] = $doc;
56+
if (!in_array($doc, $sorted)) {
57+
$sorted[] = $doc;
58+
}
5759
}
5860
}
5961
}
@@ -119,6 +121,7 @@ public function appendRequestRules(array $controllersInfo)
119121
$reflectionMethod = new ReflectionMethod($controller, $method);
120122
$params = $reflectionMethod->getParameters();
121123
$customRules = $this->customParamsDocComment($reflectionMethod->getDocComment());
124+
$controllersInfo[$index]['rules'] = [];
122125

123126
foreach ($params as $param) {
124127
if (!$param->getType()) {
@@ -133,18 +136,15 @@ public function appendRequestRules(array $controllersInfo)
133136
//throw $th;
134137
}
135138

136-
if ($requestClass && method_exists($requestClass, 'rules')) {
137-
try {
138-
$controllersInfo[$index]['rules'] = $this->flattenRules($requestClass->rules());
139-
} catch (Throwable $e) {
140-
// disabled. This only works when the rules are defined as 'required|integer' and that too in single line
141-
// doesn't work well when the same rule is defined as array ['required', 'integer'] or in multiple lines such as
142-
// If your rules are not populated using this library, then fix your rule to only throw validation errors and not throw exceptions
143-
// such as 404, 500 inside the request class.
144-
$controllersInfo[$index]['rules'] = $this->rulesByRegex($requestClassName);
145-
146-
if (config('request-docs.debug')) {
147-
throw $e;
139+
foreach (config('request-docs.request_methods') as $requestMethod) {
140+
if ($requestClass && method_exists($requestClass, $requestMethod)) {
141+
try {
142+
$controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->flattenRules($requestClass->$requestMethod()));
143+
} catch (Throwable $e) {
144+
$controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->rulesByRegex($requestClassName, $requestMethod));
145+
if (config('request-docs.debug')) {
146+
throw $e;
147+
}
148148
}
149149
}
150150
}
@@ -211,9 +211,9 @@ public function flattenRules($mixedRules)
211211
return $rules;
212212
}
213213

214-
public function rulesByRegex($requestClassName)
214+
public function rulesByRegex($requestClassName, $methodName)
215215
{
216-
$data = new ReflectionMethod($requestClassName, 'rules');
216+
$data = new ReflectionMethod($requestClassName, $methodName);
217217
$lines = file($data->getFileName());
218218
$rules = [];
219219

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy