Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Last active November 23, 2021 18:27
Show Gist options
  • Save james2doyle/884ff73991459d047fa8e019e5ebb9d6 to your computer and use it in GitHub Desktop.
Save james2doyle/884ff73991459d047fa8e019e5ebb9d6 to your computer and use it in GitHub Desktop.
A Scribe partial for creating example requests using Axios. Place in resources/views/vendor/scribe/partials/example-requests/axios.md.blade.php
{{--
File: resources/views/vendor/scribe/partials/example-requests/axios.md.blade.php
--}}
@php
use Knuckles\Scribe\Tools\WritingUtils as u;
/** @var Knuckles\Camel\Output\OutputEndpointData $endpoint */
@endphp
```javascript
const instance = axios.create({
baseURL: '{{ rtrim($baseUrl, '/') }}',
headers: {
@if(!$endpoint->hasFiles())
@foreach($endpoint->headers as $header => $value)
'{{$header}}': '{{$value}}',
@endforeach
@endif
@empty($endpoint->headers['Accept'])
'Accept': 'application/json',
@endempty
'X-Requested-With': 'XMLHttpRequest',
},
withCredentials: true,
});
instance({
method: "{{$endpoint->httpMethods[0]}}",
url: "/{{ ltrim($endpoint->boundUri, '/') }}",
@if(count($endpoint->cleanQueryParameters))
params: {!! u::printQueryParamsAsKeyValue($endpoint->cleanQueryParameters, "\"", ":", 4, "{}") !!},
@endif
@if($endpoint->hasFiles())
...(() => {
const form = new FormData();
@foreach($endpoint->cleanBodyParameters as $parameter => $value)
@foreach( u::getParameterNamesAndValuesForFormData($parameter, $value) as $key => $actualValue)
form.append('{!! $key !!}', '{!! $actualValue !!}');
@endforeach
@endforeach
@foreach($endpoint->fileParameters as $parameter => $value)
@foreach( u::getParameterNamesAndValuesForFormData($parameter, $value) as $key => $file)
form.append('{!! $key !!}', document.querySelector('input[name="{!! $key !!}"]').files[0]);
@endforeach
@endforeach
return {
headers: form.getHeaders(),
data: form,
};
})(),
@elseif(count($endpoint->cleanBodyParameters))
data: {!! json_encode($endpoint->cleanBodyParameters, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) !!}
@endif
})
.then(({ data, status, statusText, headers, config }) => {
// ...
});
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment