enzonotario / vitepress-openapi

Generate VitePress API Docs from OpenAPI specifications
https://vitepress-openapi.vercel.app
MIT License
54 stars 9 forks source link

Modify Sample Code Based on Variables #92

Closed wspeirs closed 1 week ago

wspeirs commented 1 month ago

What would you like?

The current version generates "static" code for each language, based only on the url and method. Unfortunately, the url is simply: const url = props.baseUrl + props.path. This has the issue that any path parameters are left as-is in the code.

It would be great if the sample code could be updated to include the variables in the "try it" box. This way the code sample would be "complete". I think this would be done in much the same was as the OACodeBlock currently computes the URL for curl

Why is this needed?

If you copy-and-paste the code sample as-is, you will end up with an invalid URL if there are path parameters.

Also, it would be nice to mix-and-match such that you can remove the try it portion of the playground, but keep the sample code. This way people can "build" a request in their language of choice based on the variables form.

How could it be implemented?

I think you would need to extract (or just duplicate?) the code used to compute the curl information:

const curl = computed(() => {
  const headers = request.value.headers
  if (!headers?.['Content-Type']) {
    headers['Content-Type'] = 'application/json'
  }

  return fetchToCurl({
    method: props.method.toUpperCase(),
    url: request.value.url,
    headers,
    body: request.value.body ? formatJson(request.value.body) : null,
  })
})

Then pass this as a parameter to generateCodeSamples so that it can generate each code sample appropriately.

Other information

It would also be awesome to select which languages you want to support, and possibly even some way to provide the template for each language. I realize this is above-and-beyond, but something to think about if you're changing this.

enzonotario commented 3 weeks ago

Hi @wspeirs I'm working on it in #116 , can you please validate it via pkg-pr-new release?

It nows generates correct code for JavaScript/PHP/Python, and allows to customize lang list and the generator function, as mentioned in: https://vitepress-openapi-git-feature-cod-7dde87-enzo-notarios-projects.vercel.app/customizations/code-samples.html

Any feedback/suggestions are welcome!

td-bill commented 3 weeks ago

For a few (Bruno & cURL at least), it seems to be duplicating the param image

enzonotario commented 2 weeks ago

Ups, I forgot to remove a duplicated process from other component. It's now centralized and working fine so far:

image

Can you please re-validate from pkg-pr-new release?

Thanks for this!

enzonotario commented 1 week ago

It's now released in v0.0.3-alpha.48.

Thanks for reporting, any feedback is welcome!