enkot / nuxt-open-fetch

Generate zero-overhead, typed OpenAPI clients for Nuxt.
https://nuxt-open-fetch.vercel.app
MIT License
114 stars 10 forks source link

Nitro server support #22

Closed skf-funzt closed 2 months ago

skf-funzt commented 4 months ago

Features

Usage - Nitro Server support

nuxt.config.ts

export default defineNuxtConfig({
  ...
  openFetch: {
    clients: {
      pets: {
        baseURL: 'https://petstore3.swagger.io/api/v3'
      },
    },
    // Adding server support
    // ! The baseURL  needs to be set properly via config or the schema
    servers: {
      pets: {
        baseURL: 'https://petstore3.swagger.io/api/v3'
      },
    },
  },
  ...
})

server/api/pets.ts

export default defineEventHandler(async (event) => {
  const { $fetchPets } = useNuxtOpenFetchServer()
  return await $fetchPets ("/pet/{petId}", {
    path: {
      petId: 1,
    },
  });
})

Using automatic server API routes

const {
  data: usePetsFetchServerData,
  execute: usePetsFetchServerExecute,
  error: usePetsFetchServerError,
} = await usePetsFetchServer("/pet/{petId}", {
  immediate: false,
  path: {
    petId: 1,
  },
});

Improvements

Missing

vercel[bot] commented 4 months ago

Someone is attempting to deploy a commit to a Personal Account owned by @enkot on Vercel.

@enkot first needs to authorize it.

skf-funzt commented 4 months ago

@enkot I added the feature for automatic server API route creation. Could you please have a look?

enkot commented 4 months ago

Hi @skf-funzt Sorry for not replying for a long time. Thanks for your PR! I really appreciate it. 💚 While the general idea is correct, my vision for the implementation is a bit different. The idea is to have the same preconfigured client implementation in the Nuxt and Nitro context, same way we have plain $fetch:

// Nuxt
const { $petsFetch } = useNuxtApp()

$petsFetch('/pet/{petId}', {
  path: {
    petId: 2
  }
})

// Nitro
const { $petsFetch } = useNitroApp()

$petsFetch('/pet/{petId}', {
  path: {
    petId: 2
  }
})

Because it requires quite a lot of changes changes in this branch, I created another PR - https://github.com/enkot/nuxt-open-fetch/pull/23. I will add you as a contributor there :)

skf-funzt commented 4 months ago

@enkot What do you think about the automatic nitro route creation to avoid having CORS issues on the API's backend side?

enkot commented 4 months ago

I think handling CORS is beyond the scope of this module. NuxtOpenFetch simply extends $fetch/useFetch functionality with OpenAPI types.

skf-funzt commented 4 months ago

OK understood. Thanks for pointing that out. I thought it would be nice to have an immediate solution for the CORS issues with certain backends as I ran into the same issue and this was the solution for me without using a different package.

enkot commented 3 months ago

@skf-funzt I noticed you made some changes regarding /utils. Just want to note that utils was removed, and functions for module and runtime are used in place now. So it should work correctly now :)