mirror of
https://github.com/ambieco/scribe.git
synced 2026-04-24 02:20:05 +08:00
Use bound URL in doc examples (closes #478)
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
> Example request:
|
||||
|
||||
```bash
|
||||
curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ trim(config('app.docs_url') ?: config('app.url'), '/')}}/{{ ltrim($route['uri'], '/') }}" @if(count($route['headers']))\
|
||||
curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ trim(config('app.docs_url') ?: config('app.url'), '/')}}/{{ ltrim($route['boundUri'], '/') }}" @if(count($route['headers']))\
|
||||
@foreach($route['headers'] as $header => $value)
|
||||
-H "{{$header}}: {{$value}}"@if(! ($loop->last) || ($loop->last && count($route['bodyParameters']))) \
|
||||
@endif
|
||||
@@ -25,7 +25,7 @@ curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"
|
||||
```
|
||||
|
||||
```javascript
|
||||
const url = new URL("{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($route['uri'], '/') }}");
|
||||
const url = new URL("{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($route['boundUri'], '/') }}");
|
||||
@if(count($route['queryParameters']))
|
||||
|
||||
let params = {
|
||||
|
||||
@@ -64,6 +64,7 @@ class Generator
|
||||
'description' => $docBlock['long'],
|
||||
'methods' => $this->getMethods($route),
|
||||
'uri' => $this->getUri($route),
|
||||
'boundUri' => Utils::getFullUrl($route, $rulesToApply['bindings'] ?? []),
|
||||
'bodyParameters' => $bodyParameters,
|
||||
'cleanBodyParameters' => $this->cleanParams($bodyParameters),
|
||||
'queryParameters' => $queryParameters,
|
||||
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Http\Response;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Str;
|
||||
use Mpociot\ApiDoc\Tools\Traits\ParamHelpers;
|
||||
use Mpociot\ApiDoc\Tools\Utils;
|
||||
|
||||
/**
|
||||
* Make a call to the route and retrieve its response.
|
||||
@@ -66,7 +67,7 @@ class ResponseCallStrategy
|
||||
*/
|
||||
private function prepareRequest(Route $route, array $rulesToApply, array $bodyParams, array $queryParams)
|
||||
{
|
||||
$uri = $this->replaceUrlParameterBindings($route, $rulesToApply['bindings'] ?? []);
|
||||
$uri = Utils::getFullUrl($route, $rulesToApply['bindings'] ?? []);
|
||||
$routeMethods = $this->getMethods($route);
|
||||
$method = array_shift($routeMethods);
|
||||
$cookies = isset($rulesToApply['cookies']) ? $rulesToApply['cookies'] : [];
|
||||
@@ -83,34 +84,6 @@ class ResponseCallStrategy
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform parameters in URLs into real values (/users/{user} -> /users/2).
|
||||
* Uses bindings specified by caller, otherwise just uses '1'.
|
||||
*
|
||||
* @param Route $route
|
||||
* @param array $bindings
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function replaceUrlParameterBindings(Route $route, $bindings)
|
||||
{
|
||||
$uri = $route->uri();
|
||||
foreach ($bindings as $path => $binding) {
|
||||
// So we can support partial bindings like
|
||||
// 'bindings' => [
|
||||
// 'foo/{type}' => 4,
|
||||
// 'bar/{type}' => 2
|
||||
//],
|
||||
if (Str::is("*$path*", $uri)) {
|
||||
preg_match('/({.+?})/', $path, $parameter);
|
||||
$uri = str_replace("{$parameter['1']}", $binding, $uri);
|
||||
}
|
||||
}
|
||||
// Replace any unbound parameters with '1'
|
||||
$uri = preg_replace('/{(.+?)}/', 1, $uri);
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
|
||||
45
src/Tools/Utils.php
Normal file
45
src/Tools/Utils.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Mpociot\ApiDoc\Tools;
|
||||
|
||||
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Utils
|
||||
{
|
||||
public static function getFullUrl(Route $route, array $bindings = []): string
|
||||
{
|
||||
$uri = $route->uri();
|
||||
return self::replaceUrlParameterBindings($uri, $bindings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform parameters in URLs into real values (/users/{user} -> /users/2).
|
||||
* Uses bindings specified by caller, otherwise just uses '1'.
|
||||
*
|
||||
* @param string $uri
|
||||
* @param array $bindings
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected static function replaceUrlParameterBindings(string $uri, array $bindings)
|
||||
{
|
||||
foreach ($bindings as $path => $binding) {
|
||||
// So we can support partial bindings like
|
||||
// 'bindings' => [
|
||||
// 'foo/{type}' => 4,
|
||||
// 'bar/{type}' => 2
|
||||
//],
|
||||
if (Str::is("*$path*", $uri)) {
|
||||
preg_match('/({.+?})/', $path, $parameter);
|
||||
$uri = str_replace("{$parameter['1']}", $binding, $uri);
|
||||
}
|
||||
}
|
||||
// Replace any unbound parameters with '1'
|
||||
$uri = preg_replace('/{(.+?)}/', 1, $uri);
|
||||
|
||||
return $uri;
|
||||
}
|
||||
}
|
||||
@@ -140,12 +140,12 @@ fetch(url, {
|
||||
> Example request:
|
||||
|
||||
```bash
|
||||
curl -X GET -G "http://localhost/api/users/{user}" \
|
||||
curl -X GET -G "http://localhost/api/users/1" \
|
||||
-H "Accept: application/json"
|
||||
```
|
||||
|
||||
```javascript
|
||||
const url = new URL("http://localhost/api/users/{user}");
|
||||
const url = new URL("http://localhost/api/users/1");
|
||||
|
||||
let headers = {
|
||||
"Accept": "application/json",
|
||||
@@ -180,12 +180,12 @@ fetch(url, {
|
||||
> Example request:
|
||||
|
||||
```bash
|
||||
curl -X GET -G "http://localhost/api/users/{user}/edit" \
|
||||
curl -X GET -G "http://localhost/api/users/1/edit" \
|
||||
-H "Accept: application/json"
|
||||
```
|
||||
|
||||
```javascript
|
||||
const url = new URL("http://localhost/api/users/{user}/edit");
|
||||
const url = new URL("http://localhost/api/users/1/edit");
|
||||
|
||||
let headers = {
|
||||
"Accept": "application/json",
|
||||
@@ -220,12 +220,12 @@ fetch(url, {
|
||||
> Example request:
|
||||
|
||||
```bash
|
||||
curl -X PUT "http://localhost/api/users/{user}" \
|
||||
curl -X PUT "http://localhost/api/users/1" \
|
||||
-H "Accept: application/json"
|
||||
```
|
||||
|
||||
```javascript
|
||||
const url = new URL("http://localhost/api/users/{user}");
|
||||
const url = new URL("http://localhost/api/users/1");
|
||||
|
||||
let headers = {
|
||||
"Accept": "application/json",
|
||||
@@ -255,12 +255,12 @@ fetch(url, {
|
||||
> Example request:
|
||||
|
||||
```bash
|
||||
curl -X DELETE "http://localhost/api/users/{user}" \
|
||||
curl -X DELETE "http://localhost/api/users/1" \
|
||||
-H "Accept: application/json"
|
||||
```
|
||||
|
||||
```javascript
|
||||
const url = new URL("http://localhost/api/users/{user}");
|
||||
const url = new URL("http://localhost/api/users/1");
|
||||
|
||||
let headers = {
|
||||
"Accept": "application/json",
|
||||
|
||||
Reference in New Issue
Block a user