Skip to content

Commit 3ffd1cc

Browse files
authored
Merge pull request #411 from Potelo/javascript-fetch
Use javascript fetch instead of jquery
2 parents 4d748ec + 12da3ca commit 3ffd1cc

File tree

4 files changed

+146
-126
lines changed

4 files changed

+146
-126
lines changed

resources/views/partials/route.blade.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,46 @@
2626
```
2727

2828
```javascript
29-
var settings = {
30-
"async": true,
31-
"crossDomain": true,
32-
"url": "{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($route['uri'], '/') }}",
33-
"method": "{{$route['methods'][0]}}",
34-
@if(count($route['bodyParameters']))
35-
"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))) !!},
36-
@endif
37-
"headers": {
29+
const url = new URL("{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($route['uri'], '/') }}");
30+
@if(count($route['queryParameters']))
31+
32+
let params = {
33+
@foreach($route['queryParameters'] as $attribute => $parameter)
34+
"{{ $attribute }}": "{{ $parameter['value'] }}",
35+
@endforeach
36+
};
37+
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
38+
@endif
39+
40+
let headers = {
3841
@foreach($route['headers'] as $header => $value)
39-
"{{$header}}": "{{$value}}",
42+
"{{$header}}": "{{$value}}",
4043
@endforeach
41-
}
44+
@if(!array_key_exists('Accept', $route['headers']))
45+
"Accept": "application/json",
46+
@endif
47+
@if(!array_key_exists('Content-Type', $route['headers']))
48+
"Content-Type": "application/json",
49+
@endif
4250
}
51+
@if(count($route['bodyParameters']))
4352

44-
$.ajax(settings).done(function (response) {
45-
console.log(response);
46-
});
53+
let body = JSON.stringify({
54+
@foreach($route['bodyParameters'] as $attribute => $parameter)
55+
"{{ $attribute }}": "{{ $parameter['value'] }}",
56+
@endforeach
57+
})
58+
@endif
59+
60+
fetch(url, {
61+
method: "{{$route['methods'][0]}}",
62+
headers: headers,
63+
@if(count($route['bodyParameters']))
64+
body: body
65+
@endif
66+
})
67+
.then(response => response.json())
68+
.then(json => console.log(json));
4769
```
4870

4971
@if(in_array('GET',$route['methods']) || (isset($route['showresponse']) && $route['showresponse']))

tests/Fixtures/index.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,19 @@ curl -X GET -G "http://localhost/api/withDescription" \
3737
```
3838

3939
```javascript
40-
var settings = {
41-
"async": true,
42-
"crossDomain": true,
43-
"url": "http://localhost/api/withDescription",
44-
"method": "GET",
45-
"headers": {
46-
"accept": "application/json",
47-
"Authorization": "customAuthToken",
48-
"Custom-Header": "NotSoCustom",
49-
}
40+
const url = new URL("http://localhost/api/users");
41+
42+
let headers = {
43+
"Accept": "application/json",
44+
"Content-Type": "application/json",
5045
}
5146

52-
$.ajax(settings).done(function (response) {
53-
console.log(response);
54-
});
47+
fetch(url, {
48+
method: "GET",
49+
headers: headers,
50+
})
51+
.then(response => response.json())
52+
.then(json => console.log(json));
5553
```
5654

5755
> Example response:

tests/Fixtures/partial_resource_index.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ curl -X GET -G "http://localhost/api/users" \
3232
```
3333

3434
```javascript
35-
var settings = {
36-
"async": true,
37-
"crossDomain": true,
38-
"url": "http://localhost/api/users",
39-
"method": "GET",
40-
"headers": {
41-
"Accept": "application/json",
42-
}
35+
const url = new URL("http://localhost/api/users");
36+
37+
let headers = {
38+
"Accept": "application/json",
39+
"Content-Type": "application/json",
4340
}
4441

45-
$.ajax(settings).done(function (response) {
46-
console.log(response);
47-
});
42+
fetch(url, {
43+
method: "GET",
44+
headers: headers,
45+
})
46+
.then(response => response.json())
47+
.then(json => console.log(json));
4848
```
4949

5050
> Example response:
@@ -72,19 +72,19 @@ curl -X GET -G "http://localhost/api/users/create" \
7272
```
7373

7474
```javascript
75-
var settings = {
76-
"async": true,
77-
"crossDomain": true,
78-
"url": "http://localhost/api/users/create",
79-
"method": "GET",
80-
"headers": {
81-
"Accept": "application/json",
82-
}
75+
const url = new URL("http://localhost/api/users/create");
76+
77+
let headers = {
78+
"Accept": "application/json",
79+
"Content-Type": "application/json",
8380
}
8481

85-
$.ajax(settings).done(function (response) {
86-
console.log(response);
87-
});
82+
fetch(url, {
83+
method: "GET",
84+
headers: headers,
85+
})
86+
.then(response => response.json())
87+
.then(json => console.log(json));
8888
```
8989

9090
> Example response:

tests/Fixtures/resource_index.md

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ curl -X GET -G "http://localhost/api/users" \
3232
```
3333

3434
```javascript
35-
var settings = {
36-
"async": true,
37-
"crossDomain": true,
38-
"url": "http://localhost/api/users",
39-
"method": "GET",
40-
"headers": {
41-
"Accept": "application/json",
42-
}
35+
const url = new URL("http://localhost/api/users");
36+
37+
let headers = {
38+
"Accept": "application/json",
39+
"Content-Type": "application/json",
4340
}
4441

45-
$.ajax(settings).done(function (response) {
46-
console.log(response);
47-
});
42+
fetch(url, {
43+
method: "GET",
44+
headers: headers,
45+
})
46+
.then(response => response.json())
47+
.then(json => console.log(json));
4848
```
4949

5050
> Example response:
@@ -72,19 +72,19 @@ curl -X GET -G "http://localhost/api/users/create" \
7272
```
7373

7474
```javascript
75-
var settings = {
76-
"async": true,
77-
"crossDomain": true,
78-
"url": "http://localhost/api/users/create",
79-
"method": "GET",
80-
"headers": {
81-
"Accept": "application/json",
82-
}
75+
const url = new URL("http://localhost/api/users/create");
76+
77+
let headers = {
78+
"Accept": "application/json",
79+
"Content-Type": "application/json",
8380
}
8481

85-
$.ajax(settings).done(function (response) {
86-
console.log(response);
87-
});
82+
fetch(url, {
83+
method: "GET",
84+
headers: headers,
85+
})
86+
.then(response => response.json())
87+
.then(json => console.log(json));
8888
```
8989

9090
> Example response:
@@ -112,19 +112,19 @@ curl -X POST "http://localhost/api/users" \
112112
```
113113

114114
```javascript
115-
var settings = {
116-
"async": true,
117-
"crossDomain": true,
118-
"url": "http://localhost/api/users",
119-
"method": "POST",
120-
"headers": {
121-
"Accept": "application/json",
122-
}
115+
const url = new URL("http://localhost/api/users");
116+
117+
let headers = {
118+
"Accept": "application/json",
119+
"Content-Type": "application/json",
123120
}
124121

125-
$.ajax(settings).done(function (response) {
126-
console.log(response);
127-
});
122+
fetch(url, {
123+
method: "POST",
124+
headers: headers,
125+
})
126+
.then(response => response.json())
127+
.then(json => console.log(json));
128128
```
129129

130130

@@ -145,19 +145,19 @@ curl -X GET -G "http://localhost/api/users/{user}" \
145145
```
146146

147147
```javascript
148-
var settings = {
149-
"async": true,
150-
"crossDomain": true,
151-
"url": "http://localhost/api/users/{user}",
152-
"method": "GET",
153-
"headers": {
154-
"Accept": "application/json",
155-
}
148+
const url = new URL("http://localhost/api/users/{user}");
149+
150+
let headers = {
151+
"Accept": "application/json",
152+
"Content-Type": "application/json",
156153
}
157154

158-
$.ajax(settings).done(function (response) {
159-
console.log(response);
160-
});
155+
fetch(url, {
156+
method: "GET",
157+
headers: headers,
158+
})
159+
.then(response => response.json())
160+
.then(json => console.log(json));
161161
```
162162

163163
> Example response:
@@ -185,19 +185,19 @@ curl -X GET -G "http://localhost/api/users/{user}/edit" \
185185
```
186186

187187
```javascript
188-
var settings = {
189-
"async": true,
190-
"crossDomain": true,
191-
"url": "http://localhost/api/users/{user}/edit",
192-
"method": "GET",
193-
"headers": {
194-
"Accept": "application/json",
195-
}
188+
const url = new URL("http://localhost/api/users/{user}/edit");
189+
190+
let headers = {
191+
"Accept": "application/json",
192+
"Content-Type": "application/json",
196193
}
197194

198-
$.ajax(settings).done(function (response) {
199-
console.log(response);
200-
});
195+
fetch(url, {
196+
method: "GET",
197+
headers: headers,
198+
})
199+
.then(response => response.json())
200+
.then(json => console.log(json));
201201
```
202202

203203
> Example response:
@@ -225,19 +225,19 @@ curl -X PUT "http://localhost/api/users/{user}" \
225225
```
226226

227227
```javascript
228-
var settings = {
229-
"async": true,
230-
"crossDomain": true,
231-
"url": "http://localhost/api/users/{user}",
232-
"method": "PUT",
233-
"headers": {
234-
"Accept": "application/json",
235-
}
228+
const url = new URL("http://localhost/api/users/{user}");
229+
230+
let headers = {
231+
"Accept": "application/json",
232+
"Content-Type": "application/json",
236233
}
237234

238-
$.ajax(settings).done(function (response) {
239-
console.log(response);
240-
});
235+
fetch(url, {
236+
method: "PUT",
237+
headers: headers,
238+
})
239+
.then(response => response.json())
240+
.then(json => console.log(json));
241241
```
242242

243243

@@ -260,19 +260,19 @@ curl -X DELETE "http://localhost/api/users/{user}" \
260260
```
261261

262262
```javascript
263-
var settings = {
264-
"async": true,
265-
"crossDomain": true,
266-
"url": "http://localhost/api/users/{user}",
267-
"method": "DELETE",
268-
"headers": {
269-
"Accept": "application/json",
270-
}
263+
const url = new URL("http://localhost/api/users/{user}");
264+
265+
let headers = {
266+
"Accept": "application/json",
267+
"Content-Type": "application/json",
271268
}
272269

273-
$.ajax(settings).done(function (response) {
274-
console.log(response);
275-
});
270+
fetch(url, {
271+
method: "DELETE",
272+
headers: headers,
273+
})
274+
.then(response => response.json())
275+
.then(json => console.log(json));
276276
```
277277

278278

0 commit comments

Comments
 (0)