Giter Site home page Giter Site logo

Comments (8)

amery avatar amery commented on May 24, 2024 2

@amery this is fixed in v0.32.0, thanks for catching! 🤝

thank you for fixing it so quickly!

I'm still curious how you use these models with useFetch()?

I am fairly new to the typescript and reactive world, but I have a types package that exports everything from models and some extra details, then I have a composable wrapping useFetch() injecting content-type and the authentication token like this:

export function useAPI<T>(url: string, opts: UseFetchOptions<T> = {}) {
  const { public: config } = useRuntimeConfig();
  const Authorization = useAuth().tokenStrategy?.token?.get();

  // headers
  const headers = opts.headers || {};
  if (typeof Authorization === 'string') {
    opts.headers = {
      Authorization,
      ...headers,
    };
  }

  const defaults: typeof opts = {
    baseURL: config.apiPrefix,
    key: url,
    server: false,
    headers: {
      Accept: 'application/json',
    },

    onRequest(ctx) {
      useLoadingIndicator().finish();
      return ctx;
    },

    onResponseError({ response }) {
      useLoadingIndicator().finish();

      if (response.status !== 401) {
        throw createError({
          statusCode: response.status,
          statusMessage: response.statusText,
          stack: response._data?.stack,
        });
      }

      redirectToLogin();
    },
  };

  const params = defu(opts, defaults);
  useLoadingIndicator().start();
  return useFetch(url, params);

then per-service composables using it

export function useFunctionTypes(opts: UseFetchOptions<FunctionTypeInfo> = {}) {
  return useAPI<FunctionTypeInfo>('/config/function/types', opts);
}

and finally on .vue files things like

const { data, error, pending, refresh } = await useFunctionsConfig({
  transform: (data):TransformedFunctionCollection => asTransformedFunctionCollection(data),
});

or the same within a pinia store.

I would use generated composables if I could tell you the name of the useFetch wrapper (useAPI in my case) . Explicit import isn't needed with Nuxt but it would be useful for other vue-based frameworks. generating such useFetch wrapper might not be worth pursuing initially. transformations are out of scope, but I need to be able to pass that along.

@mrlubos

from openapi-ts.

mrlubos avatar mrlubos commented on May 24, 2024 1

@jordanshatford i think this is related to #155

Separate issue are those partially exported files

from openapi-ts.

mrlubos avatar mrlubos commented on May 24, 2024 1

@amery i think you'll get a different result if you use the config file, this seems to be related to the CLI options parsing

from openapi-ts.

mrlubos avatar mrlubos commented on May 24, 2024 1

I think we will want to split the type generator into a separate package at some point btw

from openapi-ts.

mrlubos avatar mrlubos commented on May 24, 2024

@amery can you share an example call using useFetch()? Curious how you use models with it

from openapi-ts.

mrlubos avatar mrlubos commented on May 24, 2024

@amery this is fixed in v0.32.0, thanks for catching! 🤝 I'm still curious how you use these models with useFetch()?

from openapi-ts.

mrlubos avatar mrlubos commented on May 24, 2024

Thanks for the additional context @amery. Do you use this wrapper to make POST/PUT requests too?

We'd eventually want to offer this kind of wrappers for clients such as Nuxt too, so this is helpful. I see for example that your URLs aren't type-safe, we could improve it a bit. In the meantime, let me know if there's anything we can do to make your life easier!

from openapi-ts.

amery avatar amery commented on May 24, 2024

Thanks for the additional context @amery. Do you use this wrapper to make POST/PUT requests too?

Yes

We'd eventually want to offer this kind of wrappers for clients such as Nuxt too, so this is helpful. I see for example that your URLs aren't type-safe, we could improve it a bit. In the meantime, let me know if there's anything we can do to make your life easier!

Generating the 3-line wrappers would be enough to save me some pain and reduce simple mistakes.

I'll be happy to test such experiments, but my typescript is still too weak to be able to get involved in the code itself yet.

from openapi-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.