knuckleswtf / scribe

Generate API documentation for humans from your Laravel codebase.✍
https://scribe.knuckles.wtf/laravel/
MIT License
1.7k stars 303 forks source link

Try it out unable to get base Url #798

Closed markheramis closed 7 months ago

markheramis commented 7 months ago

Scribe version

4.9

PHP version

8.1

Framework

Laravel

Framework version

v9.52.16

Scribe config

title => "Laravel-Restful-API"
base_url => "http://localhost:8000"
type => "laravel"
static.output_path => "storage/app/public/docs"
try_it_out.base_url => "http://localhost:8000"
auth.enabled => true
auth.name => "Authorization"
logo => "/logo.png"
strategies.metadata => removed Knuckles\Scribe\Extracting\Strategies\Metadata\GetFromMetadataAttributes
strategies.urlParameters => added Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromLumenAPI: removed Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromUrlParamAttribute
strategies.queryParameters => removed Knuckles\Scribe\Extracting\Strategies\QueryParameters\GetFromFormRequest, Knuckles\Scribe\Extracting\Strategies\QueryParameters\GetFromInlineValidator, Knuckles\Scribe\Extracting\Strategies\QueryParameters\GetFromQueryParamAttribute
strategies.headers => added Knuckles\Scribe\Extracting\Strategies\Headers\GetFromRouteRules: removed Knuckles\Scribe\Extracting\Strategies\Headers\GetFromHeaderAttribute, [
    'override',
    [
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
    ],
]
strategies.bodyParameters => removed Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromInlineValidator, Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromBodyParamAttribute
strategies.responses => added Knuckles\Scribe\Extracting\Strategies\Responses\ResponseCalls: removed Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseAttributes, [
    'Knuckles\\Scribe\\Extracting\\Strategies\\Responses\\ResponseCalls',
    [
        'only' => [
            'GET *',
        ],
    ],
]
strategies.responseFields => removed Knuckles\Scribe\Extracting\Strategies\ResponseFields\GetFromResponseFieldAttribute
fractal.serializer => "League\\Fractal\\Serializer\\JsonApiSerializer"

What happened?

I try to submit any of the requests from try it out feature and will not be able to submit it throwing t he following error on the javascript console:

Error while making request:  TypeError: Failed to construct 'URL': Invalid URL
    at makeAPICall (tryitout-4.29.0.js:92:17)
    at tryitout-4.29.0.js:256:40

and on the execution-error element I get this

<span id="execution-error-POSTapi-auth-login">
    <blockquote>Request failed with error:</blockquote>
    <pre><code id="execution-error-message-POSTapi-auth-login" class="hljs language-undefined">Failed to construct 'URL': Invalid URL</code></pre>
</span>

image

The reason is that the try it out feature is unable to get the try it out base url since it is defined as

var tryItOutBaseUrl = "{{ $tryItOut['base_url'] ?? config('app.url') }}";

From here but when the button is clicked it will assembly the URL for the request based on the window.tryItOutBaseUrl

const url = new URL(window.tryItOutBaseUrl + '/' + path.replace(/^\//, ''));

found here and also here instead of doing

const url = new URL(tryItOutBaseUrl + '/' + path.replace(/^\//, ''));

and

return fetch(url, {
        method,
        headers,
        body: method === 'GET' ? undefined : body,
        signal: window.abortControllers[endpointId].signal,
        referrer: window.tryItOutBaseUrl,
        mode: 'cors',
        credentials: 'same-origin',
    })

Docs

shalvah commented 7 months ago

I'm confused... window.tryItOutBaseUrl and var tryItOutBaseUrl are the same variable. It seems, for some reason, your tryItOutBaseUrl is empty. Try logging it in your browser console.

markheramis commented 7 months ago

Sorry for the confusion, it looks like it is resolved now.

I tried

<script>
   console.log(tryItOutBaseUrl);
   console.log(window.tryItOutBaseUrl);
</script>

And they displayed the same value now. Odd that I had the same code below my page and didn't output same few days ago.

Can't reproduce the problem anymore.

Thank you @shalvah