Skip to content

Develop #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,5 @@ Fixing lints
- v1.22 Boolean|File|Image support
- v1.22 Boolean|File|Image support
- v1.23 Bug fix for lrd doc block #76
- v1.27 A few fixes on width and added request_methods

58 changes: 34 additions & 24 deletions config/request-docs.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

return [
// change it to true will make lrd to throw exception if rules in request class need to be changed
// keep it false
'debug' => false,
'document_name' => 'LRD',
// change it to true will make lrd to throw exception if rules in request class need to be changed
// keep it false
'debug' => false,
'document_name' => 'LRD',

/*
* Route where request docs will be served from
Expand All @@ -15,6 +15,7 @@
//Example
// \App\Http\Middleware\NotFoundWhenProduction::class,
],

/**
* Path to to static HTML if using command line.
*/
Expand All @@ -23,32 +24,41 @@
/**
* Sorting route by and there is two types default(route methods), route_names.
*/
'sort_by' => 'default',
'sort_by' => 'route_names',

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

'hide_matching' => [
"#^telescope#",
"#^docs#",
"#^request-docs#",
'#^telescope#',
'#^docs#',
'#^request-docs#',
'#^api-docs#',
'#^sanctum#',
'#^_ignition#',
],

'request_methods' => [
'rules',
'onCreate',
'onUpdate',
],

"open_api" => [
'open_api' => [
// default version that this library provides
"version" => "3.0.0",
'version' => '3.0.0',
// changeable
"document_version" => "1.0.0",
'document_version' => '1.0.0',
// license that you want to display
"license" => "Apache 2.0",
"license_url" => "https://www.apache.org/licenses/LICENSE-2.0.html",
"server_url" => env('APP_URL', 'http://localhost'),
'license' => 'Apache 2.0',
'license_url' => 'https://www.apache.org/licenses/LICENSE-2.0.html',
'server_url' => env('APP_URL', 'http://localhost'),

// for now putting default responses for all. This can be changed later based on specific needs
"responses" => [
'responses' => [
'200' => [
'description' => 'Successful operation',
'content' => [
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
Expand All @@ -58,7 +68,7 @@
],
'400' => [
'description' => 'Bad Request',
'content' => [
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
Expand All @@ -68,7 +78,7 @@
],
'401' => [
'description' => 'Unauthorized',
'content' => [
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
Expand All @@ -78,7 +88,7 @@
],
'403' => [
'description' => 'Forbidden',
'content' => [
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
Expand All @@ -88,7 +98,7 @@
],
'404' => [
'description' => 'Not Found',
'content' => [
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
Expand All @@ -98,7 +108,7 @@
],
'422' => [
'description' => 'Unprocessable Entity',
'content' => [
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
Expand All @@ -108,7 +118,7 @@
],
'500' => [
'description' => 'Internal Server Error',
'content' => [
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
Expand All @@ -118,7 +128,7 @@
],
'default' => [
'description' => 'Unexpected error',
'content' => [
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
Expand All @@ -127,5 +137,5 @@
],
],
],
]
],
];
31 changes: 19 additions & 12 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<script src="https://unpkg.com/vue-markdown@2.2.4/dist/vue-markdown.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sql-formatter/3.1.0/sql-formatter.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="{{asset('./vendor/request-docs/dist/app.js')}}"></script>

<style>
[v-cloak] {
display: none;
Expand Down Expand Up @@ -107,7 +107,7 @@
</div>
</nav>
<div id="app" v-cloak class="w-full flex lg:pt-10">
<aside class="text-sm ml-1.5 text-grey-darkest break-all bg-gray-200 pl-2 h-screen sticky top-1 overflow-auto">
<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">
<section class="pt-5 pl-2 pr-2 pb-5 border mb-10 rounded bg-white shadow">
<div class="font-sans">
<h2 class="text-sm break-normal text-black break-normal font-sans pb-1 pt-1 text-black">
Expand All @@ -130,7 +130,7 @@ class="my-prism-editor"
</section>
<h1 class="font-medium mx-3 mt-3" style="width: max-content;min-width:350px;">Routes List</h1>
<hr class="border-b border-gray-300">
<table class="table-fixed text-sm mt-5" style="width: max-content">
<table class="table-fixed text-sm mt-5 mb-5" style="width: max-content">
<tbody>
@foreach ($docs as $index => $doc)
<tr v-if="!docs[{{$index}}]['isHidden']">
Expand Down Expand Up @@ -187,8 +187,8 @@ class="inline-flex text-xs"
<hr class="border-b border-gray-300">
<br>
@foreach ($docs as $index => $doc)
<section class="pt-5 pl-2 pr-2 pb-5 border mb-10 rounded bg-white shadow" v-if="!docs[{{$index}}]['isHidden']">
<div class="font-sans" id="{{$doc['httpMethod'] .'-'. $doc['uri']}}">
<section class="pt-5 pl-2 pr-2 pb-5 border-2 mb-10 rounded bg-white shadow" v-if="!docs[{{$index}}]['isHidden']">
<div class="border-2 rounded" id="{{$doc['httpMethod'] .'-'. $doc['uri']}}">
<h1 class="text-sm break-normal text-black bg-indigo-50 break-normal font-sans pb-1 pt-1 text-black">
<span class="w-20
font-medium
Expand All @@ -197,6 +197,8 @@ class="inline-flex text-xs"
justify-center
px-2
py-1
ml-2
mr-2
text-xs
font-bold
leading-none
Expand All @@ -214,8 +216,6 @@ class="inline-flex text-xs"
</span>
</h1>
</div>
<hr class="border-b border-grey-light">

<table class="table-fixed text-sm mt-5">
<tbody>
<tr>
Expand Down Expand Up @@ -245,12 +245,15 @@ class="inline-flex text-xs"
</tbody>
</table>
<div v-if="docs[{{$index}}]['docBlock']" class="border-2 mr-4 mt-4 p-4 rounded text-sm">
<h3 class="font-bold">LRD Docs</h3>
<vue-markdown>{!! $doc['docBlock'] !!}</vue-markdown>
<h3 class="font-bold">Description</h3>
<hr>
<vue-markdown class="mt-2">{!! $doc['docBlock'] !!}</vue-markdown>
</div>
<br>
@if (!empty($doc['rules']))
<table class="table-fixed align-left text-sm mt-5">
<div class="border-2 mr-4 mt-4 p-4 rounded">
<h3 class="font-bold">Attributes</h3>
<hr>
<table class="table-fixed align-left text-sm mt-3">
<thead class="border">
<tr class="border">
<th class="border border-gray-300 pl-2 pr-16 pt-1 pb-1 w-min text-left bg-gray-200">No.</th>
Expand Down Expand Up @@ -319,7 +322,7 @@ class="inline-flex text-xs"
<div class="font-mono">
@foreach ($rules as $rule)
@foreach (explode('|', $rule) as $r)
@if (!in_array($r, ['required', 'integer', 'string', 'boolean', 'array', 'nullable', 'bail', 'file', 'image']))
@if (!in_array($r, ['required', 'integer', 'string', 'boolean', 'array', 'nullable', 'bail', 'file', 'image', 'numeric']))
{{$r}}
@if (!$loop->last)
<span class="text-gray-900 font-bold">|</span>
Expand All @@ -333,6 +336,7 @@ class="inline-flex text-xs"
@endforeach
</tbody>
</table>
</div>
@endif
<button
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"
Expand Down Expand Up @@ -559,6 +563,9 @@ class="my-prism-editor"
validations.max = 100
}
}
if (rule.match(/numeric/)) {
validations.isInteger = true
}
})

if (validations.isString) {
Expand Down
30 changes: 15 additions & 15 deletions src/LaravelRequestDocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public function sortDocs(array $docs, $sortBy = 'default'): array
foreach ($methods as $method) {
foreach ($docs as $key => $doc) {
if (in_array($method, $doc['methods'])) {
$sorted[] = $doc;
if (!in_array($doc, $sorted)) {
$sorted[] = $doc;
}
}
}
}
Expand Down Expand Up @@ -119,6 +121,7 @@ public function appendRequestRules(array $controllersInfo)
$reflectionMethod = new ReflectionMethod($controller, $method);
$params = $reflectionMethod->getParameters();
$customRules = $this->customParamsDocComment($reflectionMethod->getDocComment());
$controllersInfo[$index]['rules'] = [];

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

if ($requestClass && method_exists($requestClass, 'rules')) {
try {
$controllersInfo[$index]['rules'] = $this->flattenRules($requestClass->rules());
} catch (Throwable $e) {
// disabled. This only works when the rules are defined as 'required|integer' and that too in single line
// doesn't work well when the same rule is defined as array ['required', 'integer'] or in multiple lines such as
// If your rules are not populated using this library, then fix your rule to only throw validation errors and not throw exceptions
// such as 404, 500 inside the request class.
$controllersInfo[$index]['rules'] = $this->rulesByRegex($requestClassName);

if (config('request-docs.debug')) {
throw $e;
foreach (config('request-docs.request_methods') as $requestMethod) {
if ($requestClass && method_exists($requestClass, $requestMethod)) {
try {
$controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->flattenRules($requestClass->$requestMethod()));
} catch (Throwable $e) {
$controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->rulesByRegex($requestClassName, $requestMethod));
if (config('request-docs.debug')) {
throw $e;
}
}
}
}
Expand Down Expand Up @@ -211,9 +211,9 @@ public function flattenRules($mixedRules)
return $rules;
}

public function rulesByRegex($requestClassName)
public function rulesByRegex($requestClassName, $methodName)
{
$data = new ReflectionMethod($requestClassName, 'rules');
$data = new ReflectionMethod($requestClassName, $methodName);
$lines = file($data->getFileName());
$rules = [];

Expand Down