Comments (8)
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.
from openapi-ts.
@jordanshatford i think this is related to #155
Separate issue are those partially exported files
from openapi-ts.
@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.
I think we will want to split the type generator into a separate package at some point btw
from openapi-ts.
@amery can you share an example call using useFetch()? Curious how you use models with it
from openapi-ts.
@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.
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.
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)
- schema compliance with jsr HOT 3
- Allow inlining types in services signature instead of reference to $OpenApiTs HOT 1
- Allow easy differentiation by status codes HOT 4
- invalid identifier created (starting with digits) HOT 1
- formData: mismatch Type HOT 5
- Remove service classes
- Typescript enums not generated HOT 3
- Default response types (error) override response types HOT 3
- Setting a custom httpsAgent on the generated AxiosInstance HOT 9
- Accessing Both Response Body and Headers HOT 2
- Export `OpenApiSchema` interface in the generated client HOT 2
- Pass APIRequestOptions to the response interceptor
- Client instance duplicate identifier
- Specify Line Endings HOT 1
- Dryrun flag checking causes the app not to run. HOT 8
- Inconsistent URL Encoding in Generated Services HOT 3
- Question regarding license HOT 9
- Switch to AbortSignal instead of CancellablePromise HOT 2
- Missed type for `type: ['array']`, produces unknown[] HOT 11
- Should "default" spec responses throw an error? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from openapi-ts.