Browse Source

Use javascript fetch instead of jquery. resolve #330

Gabriel Peixoto 6 years ago
parent
commit
12da3ca253

+ 36 - 14
resources/views/partials/route.blade.php

@@ -26,24 +26,46 @@ curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"
 ```
 
 ```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']))

+ 11 - 13
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:

+ 22 - 22
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:

+ 77 - 77
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));
 ```