nuxt-open-fetch icon indicating copy to clipboard operation
nuxt-open-fetch copied to clipboard

Helper: URL path composable

Open 0xjogge opened this issue 4 months ago • 6 comments

It's not always that we are fetching things directly via OpenFetch.

One example is PrimeVue's Upload component which takes an url an handles the post for you.

My idea was to have a composable like this:

const { path, responseType } = useApiPath('v1/files');

The path in this case would be http://localhost:1234/v1/files.

In the PrimeVue upload example that would look like this:

const { path, responseType } = useApiPath('v1/files');

<FileUpload
  name="files"
  :multiple="true"
  accept="image/*"
  :url="path"
>
  <template #empty>
    <span>Drag and drop files to here to upload.</span>
  </template>
</FileUpload>

I can see how it could be a bit out of scope for this project. But it would allow to use the type safety even for other things than fetching directly.

0xjogge avatar Aug 09 '25 18:08 0xjogge

Do you need the responseType value? Currently you can easily do something like:

import type { paths as ApiPaths } from '#open-fetch-schemas/api'

function useApiPath(path: keyof ApiPaths) {
    return useRuntimeConfig().public.openFetch.api.baseURL + path
}

Norbiros avatar Aug 10 '25 12:08 Norbiros

Ah, for what we need right no that is actually perfect! It works as I would have expected.

It also makes sense that paths is available since it's using openapi-typescript under the hood. Thank you! I will close this issue.🙏🏼

0xjogge avatar Aug 11 '25 09:08 0xjogge

But it was a bit hard for you to discover this feature, so I'll update the docs with that info

Norbiros avatar Aug 11 '25 10:08 Norbiros

Actually, I just realized that it wouldn't work. My example doesn't show it, but it will not work if the urls have ids in them. Because the string that is returned would have the {fileId} placeholders in them.

0xjogge avatar Aug 11 '25 12:08 0xjogge

hmm, you could look at this https://openapi-ts.dev/cli#pathparamsastypes

Norbiros avatar Aug 11 '25 12:08 Norbiros

Ah cool, will take a look at that. Thank you!🙏🏼

0xjogge avatar Aug 11 '25 12:08 0xjogge