javascript.blade.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. ```javascript
  2. const url = new URL(
  3. "{{ rtrim($baseUrl, '/') }}/{{ ltrim($route['boundUri'], '/') }}"
  4. );
  5. @if(count($route['cleanQueryParameters']))
  6. let params = {!! \Mpociot\ApiDoc\Tools\Utils::printQueryParamsAsKeyValue($route['cleanQueryParameters'], "\"", ":", 4, "{}") !!};
  7. Object.keys(params)
  8. .forEach(key => url.searchParams.append(key, params[key]));
  9. @endif
  10. @if(!empty($route['headers']))
  11. let headers = {
  12. @foreach($route['headers'] as $header => $value)
  13. "{{$header}}": "{{$value}}",
  14. @endforeach
  15. @if(!array_key_exists('Accept', $route['headers']))
  16. "Accept": "application/json",
  17. @endif
  18. @if(!array_key_exists('Content-Type', $route['headers']))
  19. "Content-Type": "application/json",
  20. @endif
  21. };
  22. @endif
  23. @if(count($route['cleanBodyParameters']))
  24. let body = {!! json_encode($route['cleanBodyParameters'], JSON_PRETTY_PRINT) !!}
  25. @endif
  26. fetch(url, {
  27. method: "{{$route['methods'][0]}}",
  28. @if(count($route['headers']))
  29. headers: headers,
  30. @endif
  31. @if(count($route['bodyParameters']))
  32. body: body
  33. @endif
  34. })
  35. .then(response => response.json())
  36. .then(json => console.log(json));
  37. ```