diff --git a/resources/views/partials/route.blade.php b/resources/views/partials/route.blade.php index 3f88360b..fbda256e 100644 --- a/resources/views/partials/route.blade.php +++ b/resources/views/partials/route.blade.php @@ -26,24 +26,46 @@ ``` ```javascript -var settings = { - "async": true, - "crossDomain": true, - "url": "{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($route['uri'], '/') }}", - "method": "{{$route['methods'][0]}}", - @if(count($route['bodyParameters'])) -"data": {!! str_replace("\n}","\n }", str_replace(' ',' ',json_encode(array_combine(array_keys($route['bodyParameters']), array_map(function($param){ return $param['value']; },$route['bodyParameters'])), JSON_PRETTY_PRINT))) !!}, - @endif -"headers": { +const url = new URL("{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($route['uri'], '/') }}"); +@if(count($route['queryParameters'])) + + let params = { + @foreach($route['queryParameters'] as $attribute => $parameter) + "{{ $attribute }}": "{{ $parameter['value'] }}", + @endforeach + }; + Object.keys(params).forEach(key => url.searchParams.append(key, params[key])); +@endif + +let headers = { @foreach($route['headers'] as $header => $value) - "{{$header}}": "{{$value}}", + "{{$header}}": "{{$value}}", @endforeach - } +@if(!array_key_exists('Accept', $route['headers'])) + "Accept": "application/json", +@endif +@if(!array_key_exists('Content-Type', $route['headers'])) + "Content-Type": "application/json", +@endif } +@if(count($route['bodyParameters'])) -$.ajax(settings).done(function (response) { - console.log(response); -}); +let body = JSON.stringify({ +@foreach($route['bodyParameters'] as $attribute => $parameter) + "{{ $attribute }}": "{{ $parameter['value'] }}", +@endforeach +}) +@endif + +fetch(url, { + method: "{{$route['methods'][0]}}", + headers: headers, +@if(count($route['bodyParameters'])) + body: body +@endif +}) + .then(response => response.json()) + .then(json => console.log(json)); ``` @if(in_array('GET',$route['methods']) || (isset($route['showresponse']) && $route['showresponse'])) diff --git a/tests/Fixtures/index.md b/tests/Fixtures/index.md index 430f598f..752a0388 100644 --- a/tests/Fixtures/index.md +++ b/tests/Fixtures/index.md @@ -37,21 +37,19 @@ curl -X GET -G "http://localhost/api/withDescription" \ ``` ```javascript -var settings = { - "async": true, - "crossDomain": true, - "url": "http://localhost/api/withDescription", - "method": "GET", - "headers": { - "accept": "application/json", - "Authorization": "customAuthToken", - "Custom-Header": "NotSoCustom", - } +const url = new URL("http://localhost/api/users"); + +let headers = { + "Accept": "application/json", + "Content-Type": "application/json", } -$.ajax(settings).done(function (response) { - console.log(response); -}); +fetch(url, { + method: "GET", + headers: headers, +}) + .then(response => response.json()) + .then(json => console.log(json)); ``` > Example response: diff --git a/tests/Fixtures/partial_resource_index.md b/tests/Fixtures/partial_resource_index.md index c76e0996..2ee81eca 100644 --- a/tests/Fixtures/partial_resource_index.md +++ b/tests/Fixtures/partial_resource_index.md @@ -32,19 +32,19 @@ curl -X GET -G "http://localhost/api/users" \ ``` ```javascript -var settings = { - "async": true, - "crossDomain": true, - "url": "http://localhost/api/users", - "method": "GET", - "headers": { - "Accept": "application/json", - } +const url = new URL("http://localhost/api/users"); + +let headers = { + "Accept": "application/json", + "Content-Type": "application/json", } -$.ajax(settings).done(function (response) { - console.log(response); -}); +fetch(url, { + method: "GET", + headers: headers, +}) + .then(response => response.json()) + .then(json => console.log(json)); ``` > Example response: @@ -72,19 +72,19 @@ curl -X GET -G "http://localhost/api/users/create" \ ``` ```javascript -var settings = { - "async": true, - "crossDomain": true, - "url": "http://localhost/api/users/create", - "method": "GET", - "headers": { - "Accept": "application/json", - } +const url = new URL("http://localhost/api/users/create"); + +let headers = { + "Accept": "application/json", + "Content-Type": "application/json", } -$.ajax(settings).done(function (response) { - console.log(response); -}); +fetch(url, { + method: "GET", + headers: headers, +}) + .then(response => response.json()) + .then(json => console.log(json)); ``` > Example response: diff --git a/tests/Fixtures/resource_index.md b/tests/Fixtures/resource_index.md index d1e760e2..80d7463e 100644 --- a/tests/Fixtures/resource_index.md +++ b/tests/Fixtures/resource_index.md @@ -32,19 +32,19 @@ curl -X GET -G "http://localhost/api/users" \ ``` ```javascript -var settings = { - "async": true, - "crossDomain": true, - "url": "http://localhost/api/users", - "method": "GET", - "headers": { - "Accept": "application/json", - } +const url = new URL("http://localhost/api/users"); + +let headers = { + "Accept": "application/json", + "Content-Type": "application/json", } -$.ajax(settings).done(function (response) { - console.log(response); -}); +fetch(url, { + method: "GET", + headers: headers, +}) + .then(response => response.json()) + .then(json => console.log(json)); ``` > Example response: @@ -72,19 +72,19 @@ curl -X GET -G "http://localhost/api/users/create" \ ``` ```javascript -var settings = { - "async": true, - "crossDomain": true, - "url": "http://localhost/api/users/create", - "method": "GET", - "headers": { - "Accept": "application/json", - } +const url = new URL("http://localhost/api/users/create"); + +let headers = { + "Accept": "application/json", + "Content-Type": "application/json", } -$.ajax(settings).done(function (response) { - console.log(response); -}); +fetch(url, { + method: "GET", + headers: headers, +}) + .then(response => response.json()) + .then(json => console.log(json)); ``` > Example response: @@ -112,19 +112,19 @@ curl -X POST "http://localhost/api/users" \ ``` ```javascript -var settings = { - "async": true, - "crossDomain": true, - "url": "http://localhost/api/users", - "method": "POST", - "headers": { - "Accept": "application/json", - } +const url = new URL("http://localhost/api/users"); + +let headers = { + "Accept": "application/json", + "Content-Type": "application/json", } -$.ajax(settings).done(function (response) { - console.log(response); -}); +fetch(url, { + method: "POST", + headers: headers, +}) + .then(response => response.json()) + .then(json => console.log(json)); ``` @@ -145,19 +145,19 @@ curl -X GET -G "http://localhost/api/users/{user}" \ ``` ```javascript -var settings = { - "async": true, - "crossDomain": true, - "url": "http://localhost/api/users/{user}", - "method": "GET", - "headers": { - "Accept": "application/json", - } +const url = new URL("http://localhost/api/users/{user}"); + +let headers = { + "Accept": "application/json", + "Content-Type": "application/json", } -$.ajax(settings).done(function (response) { - console.log(response); -}); +fetch(url, { + method: "GET", + headers: headers, +}) + .then(response => response.json()) + .then(json => console.log(json)); ``` > Example response: @@ -185,19 +185,19 @@ curl -X GET -G "http://localhost/api/users/{user}/edit" \ ``` ```javascript -var settings = { - "async": true, - "crossDomain": true, - "url": "http://localhost/api/users/{user}/edit", - "method": "GET", - "headers": { - "Accept": "application/json", - } +const url = new URL("http://localhost/api/users/{user}/edit"); + +let headers = { + "Accept": "application/json", + "Content-Type": "application/json", } -$.ajax(settings).done(function (response) { - console.log(response); -}); +fetch(url, { + method: "GET", + headers: headers, +}) + .then(response => response.json()) + .then(json => console.log(json)); ``` > Example response: @@ -225,19 +225,19 @@ curl -X PUT "http://localhost/api/users/{user}" \ ``` ```javascript -var settings = { - "async": true, - "crossDomain": true, - "url": "http://localhost/api/users/{user}", - "method": "PUT", - "headers": { - "Accept": "application/json", - } +const url = new URL("http://localhost/api/users/{user}"); + +let headers = { + "Accept": "application/json", + "Content-Type": "application/json", } -$.ajax(settings).done(function (response) { - console.log(response); -}); +fetch(url, { + method: "PUT", + headers: headers, +}) + .then(response => response.json()) + .then(json => console.log(json)); ``` @@ -260,19 +260,19 @@ curl -X DELETE "http://localhost/api/users/{user}" \ ``` ```javascript -var settings = { - "async": true, - "crossDomain": true, - "url": "http://localhost/api/users/{user}", - "method": "DELETE", - "headers": { - "Accept": "application/json", - } +const url = new URL("http://localhost/api/users/{user}"); + +let headers = { + "Accept": "application/json", + "Content-Type": "application/json", } -$.ajax(settings).done(function (response) { - console.log(response); -}); +fetch(url, { + method: "DELETE", + headers: headers, +}) + .then(response => response.json()) + .then(json => console.log(json)); ```