Skip to content

Commit 129483e

Browse files
Merge pull request #126 from rakutentech/feature/v2-bugs
Feature/v2 bugs and features
2 parents 25cd509 + e59050e commit 129483e

File tree

8 files changed

+151
-16
lines changed

8 files changed

+151
-16
lines changed

.github/workflows/releaser.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
6+
name: "CI Node"
7+
8+
jobs:
9+
test:
10+
name: Test
11+
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
node-versions: [18]
17+
18+
steps:
19+
- name: Cancel Previous Runs
20+
uses: styfle/[email protected]
21+
with:
22+
access_token: ${{ github.token }}
23+
- name: Checkout
24+
uses: actions/checkout@v2
25+
26+
- uses: actions/setup-node@v3
27+
with:
28+
node-version: ${{ matrix.node-versions }}
29+
30+
- name: NPM Install
31+
working-directory: ./ui
32+
run: npm install
33+
34+
- name: Lint, build/export
35+
working-directory: ./ui
36+
run: |
37+
npm run lint
38+
npm run export
39+
40+
- name: Create Pull Request
41+
uses: peter-evans/create-pull-request@v4
42+
43+
44+

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ You can publish the config file with:
5858

5959
```bash
6060
php artisan vendor:publish --tag=request-docs-config
61+
php artisan route:cache
6162
```
6263

6364
# Usage
@@ -111,7 +112,7 @@ Example of using it in controller
111112
/**
112113
* @lrd:start
113114
* Hello markdown
114-
* ## Free text to write documentation in markdown
115+
* Free `code` or *text* to write documentation in markdown
115116
* @lrd:end
116117
*/
117118
public function index(MyIndexRequest $request): Resource
@@ -125,7 +126,10 @@ You write extra params with rules with @LRDparam in comment line as one line
125126
```php
126127
/**
127128
* @LRDparam username string|max:32
129+
* // either space or pipe
128130
* @LRDparam nickaname string|nullable|max:32
131+
* // override the default response codes
132+
* @LRDparam responses 200,422
129133
*/
130134
public function index(MyIndexRequest $request): Resource
131135
{
@@ -175,6 +179,6 @@ Fixing lints
175179
- v1.23 Bug fix for lrd doc block #76
176180
- v1.27 A few fixes on width and added request_methods
177181
- v2.0 UI Renewal to React static
178-
- @QAParam is now @LRDparam
182+
- `@QAParam` is now `@LRDparam`
179183
- No special changes for users, upgrade to v2.x as usual
180184

config/request-docs.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131

3232
// https://github.com/rakutentech/laravel-request-docs/pull/92
3333
// When rules are put in other method than rules()
34-
'request_methods' => [
35-
'rules',
36-
'onCreate',
37-
'onUpdate',
34+
'rules_methods' => [
35+
'rules'
3836
],
37+
// Can be overridden as // @LRDResponses 200|400|401
38+
'default_responses' => [ "200", "400", "401", "403", "404", "405", "422", "429", "500", "503"],
3939

4040
// No need to touch below
4141
// open api config

src/LaravelRequestDocs.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ public function appendRequestRules(array $controllersInfo): array
193193
continue;
194194
}
195195
$params = $reflectionMethod->getParameters();
196-
$customRules = $this->customParamsDocComment($reflectionMethod->getDocComment());
196+
$docComment = $reflectionMethod->getDocComment();
197+
$customRules = $this->customParamsDocComment($docComment);
198+
$customResponses = $this->customResponsesDocComment($docComment);
199+
$controllersInfo[$index]['responses'] = $customResponses;
197200
$controllersInfo[$index]['rules'] = [];
198201

199202
foreach ($params as $param) {
@@ -214,7 +217,7 @@ public function appendRequestRules(array $controllersInfo): array
214217
//throw $th;
215218
}
216219

217-
foreach (config('request-docs.request_methods') as $requestMethod) {
220+
foreach (config('request-docs.rules_methods') as $requestMethod) {
218221
if ($requestClass && method_exists($requestClass, $requestMethod)) {
219222
try {
220223
$controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->flattenRules($requestClass->$requestMethod()));
@@ -326,14 +329,38 @@ private function customParamsDocComment($docComment): array
326329
if (Str::contains($comment, '@LRDparam')) {
327330
$comment = trim(Str::replace(['@LRDparam', '*'], '', $comment));
328331

329-
$comment = explode(' ', $comment);
332+
$comment = $this->multiexplode([' ', '|'], $comment);
330333

331334
if (count($comment) > 0) {
332335
$params[$comment[0]] = array_values(array_filter($comment, fn($item) => $item != $comment[0]));
333336
}
334337
}
335338
}
339+
return $params;
340+
}
341+
private function customResponsesDocComment($docComment): array
342+
{
343+
$params = [];
344+
345+
foreach (explode("\n", $docComment) as $comment) {
346+
if (Str::contains($comment, '@LRDresponses')) {
347+
$comment = trim(Str::replace(['@LRDresponses', '*'], '', $comment));
348+
349+
$comment = $this->multiexplode([' ', '|'], $comment);
336350

351+
$params = $comment;
352+
}
353+
}
354+
if (count($params) == 0) {
355+
$params = config('request-docs.default_responses') ?? [];
356+
}
337357
return $params;
338358
}
359+
360+
private function multiexplode($delimiters, $string)
361+
{
362+
$ready = str_replace($delimiters, $delimiters[0], $string);
363+
$launch = explode($delimiters[0], $ready);
364+
return $launch;
365+
}
339366
}

tests/LRDTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function testGetDocs()
1010
{
1111
$docs = $this->lrd->getDocs();
1212

13-
$docSize = 9;
13+
$docSize = 10;
1414
$firstDoc = $docs[0];
1515
$this->assertCount($docSize, $firstDoc);
1616
$this->assertArrayHasKey('uri', $firstDoc);

ui/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ npm install
88
npm run dev
99
```
1010

11+
**Open in Browser** http://localhost:3000?api=http://localhost:3000/sample.json
12+
13+
14+
### Developing with Laravel
15+
1116
#### Step 1
1217

1318
**Optional** Enable CORS on Laravel to allow localhost:3000

ui/src/components/ApiAction.tsx

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ interface IAPIInfo {
3030
httpMethod: string;
3131
rules: IAPIRule;
3232
docBlock: string;
33+
responses: string[];
3334
}
3435

3536
interface Props {
@@ -67,6 +68,22 @@ export default function ApiAction(props: Props) {
6768
const [responseHeaders, setResponseHeaders] = useState("");
6869
const [activeTab, setActiveTab] = useState('info');
6970

71+
const responsesText: any = {
72+
"200": "OK",
73+
"201": "Created",
74+
"202": "Accepted",
75+
"204": "No Content",
76+
"400": "Bad Request",
77+
"401": "Unauthorized",
78+
"403": "Forbidden",
79+
"404": "Not Found",
80+
"405": "Method Not Allowed",
81+
"422": "Unprocessable Entity",
82+
"429": "Too Many Requests",
83+
"500": "Internal Server Error",
84+
"503": "Service Unavailable",
85+
}
86+
7087
const setGetCurlCommand = (queries: string) => {
7188
let curl = `curl -X ${method} "${host}/${lrdDocsItem.uri}${queries}"`
7289

@@ -93,6 +110,14 @@ export default function ApiAction(props: Props) {
93110
}
94111
setCurlCommand(curl)
95112
}
113+
const explode = (str: string, maxLength: number) => {
114+
let buff = "";
115+
const numOfLines = Math.floor(str.length/maxLength);
116+
for(let i = 0; i<numOfLines+1; i++) {
117+
buff += str.substr(i*maxLength, maxLength); if(i !== numOfLines) { buff += "\\<br/>"; }
118+
}
119+
return buff;
120+
}
96121

97122
const handleSendRequest = () => {
98123
// update localstorage
@@ -147,7 +172,6 @@ export default function ApiAction(props: Props) {
147172
setTimeTaken(timeTaken)
148173
setResponseStatus(response.status)
149174
setResponseHeaders(JSON.stringify(Object.fromEntries(response.headers), null, 2))
150-
showResponse()
151175
setSendingRequest(false)
152176
return response.json();
153177
}).then((data) => {
@@ -174,10 +198,12 @@ export default function ApiAction(props: Props) {
174198
delete data._lrd
175199
}
176200
setResponseData(JSON.stringify(data, null, 2))
201+
showResponse()
177202
}).catch((error) => {
178203
setError("Response error: " + error)
179204
setResponseStatus(500)
180205
setSendingRequest(false)
206+
showResponse()
181207
})
182208

183209
}
@@ -352,10 +378,24 @@ export default function ApiAction(props: Props) {
352378
<th>Curl</th>
353379
<td>
354380
<small>
355-
<pre className='p-2 bg-base-300'>{curlCommand}</pre>
381+
<pre className='m-1 p-2 bg-base-300'>
382+
<div className='' dangerouslySetInnerHTML={{__html: explode(curlCommand, 70)}} />
383+
</pre>
356384
</small>
357385
</td>
358386
</tr>
387+
<tr>
388+
<th>Responses</th>
389+
<td>
390+
{lrdDocsItem.responses.map((response) => (
391+
<div key={shortid.generate()}>
392+
<div className={`response response-${response}`}>
393+
- {response} &nbsp; {responsesText[response]}
394+
</div>
395+
</div>
396+
))}
397+
</td>
398+
</tr>
359399
</tbody>
360400
</table>
361401
</div>

ui/src/global.css

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,30 @@ td {
7171
.method-HEAD {
7272
@apply text-info;
7373
}
74-
.badge-200 {
74+
.badge-200,.badge-201,.badge-202,.response-204 {
7575
@apply bg-success;
7676
}
77-
.badge-422 {
77+
.badge-422,.badge-429 {
7878
@apply bg-warning;
7979
}
80-
.badge-400 {
80+
.badge-400,.badge-401,.badge-402,.badge-403,.badge-404,.badge-405 {
8181
@apply bg-error;
8282
}
83-
.badge-500 {
83+
.badge-500,.badge-501,.badge-502,.badge-503 {
8484
@apply bg-error;
8585
}
86+
.response {
87+
@apply w-10/12 rounded bg-base-300 pl-3 pt-1 pb-1 mb-2 font-bold;
88+
}
89+
.response-200,.response-201,.response-202,.response-204 {
90+
@apply bg-success text-green-800;
91+
}
92+
.response-422,.response-429 {
93+
@apply bg-yellow-500 text-yellow-800;
94+
}
95+
.response-400,.response-401,.response-402,.response-403,.response-404,.response-405 {
96+
@apply bg-red-300 text-red-800;
97+
}
98+
.response-500,.response-501,.response-502,.response-503 {
99+
@apply bg-red-400 text-red-800;
100+
}

0 commit comments

Comments
 (0)