Comments (7)
sry for choosed wrong label, help me pls!
from openapi-typescript.
You’ve described how the internals should change, but I’d love to see this from the perspective of the end-user. What code do they write? What does the API look like? What could they not do before but now they can? (is there any way to accomplish it with the current API, or was it impossible)? I’m more interested in how writing middleware could be easier with this change, rather than describing what a PR would do codewise.
from openapi-typescript.
It mainly solves the issue here #1122 (comment) , user can use it to resend some request in middleware like this.
client.use({
async onResponse(response, options) {
if(response.status === 401) {
// do some other request...
// then resend it.
return await client.send(new Request(options.requestUrl,options.requestOptions), options);
}
return response;
},
});
from openapi-typescript.
Currently, a simple way to solve this problem is to use a custom fetch function.
But, to be honest, using a custom fetch function, with more code, user can achieve everything that middleware can do. In that case, what is the meaning of middleware?
from openapi-typescript.
It mainly solves the issue here #1122 (comment) , user can use it to resend some request in middleware like this.
Ah I see. You’re right, I haven’t explicitly replied to that, but that is something I would definitely not want to add to the library—retries. I think retry mechanisms should always be handled at a layer above openapi-fetch, in its current design. Reason is, it inevitably bleeds into the caching and orchestration layer (e.g. React Query) which have retry mechanisms baked in. I do not want this library to take on caching, or orchestration, or retries, ever. I want this to remain a low-level typed fetch wrapper that can be used with anything on top of it, and therefore work in any stack and setup.
Perhaps in the future I’ll reverse the decision, but I feel this won’t happen in 0.x, and I haven’t started planning what 1.x would look like.
But, to be honest, using a custom fetch function, with more code, user can achieve everything that middleware can do. In that case, what is the meaning of middleware?
That’s true. Though it’s not always a design flaw to be able to do things in multiple ways! Custom fetch can own everything, but requires you to manage 100% of the code. Middleware is more modular and DRY, and also lets you use multiple third-party packages that all plays nicely with your code. There’s a reason why most libraries use this pattern.
from openapi-typescript.
I agree that this library shouldn't have first class support for retries or caching! However, providing "around" style middleware e.g. (request: Request, next: Middleware) => Request
gives end users total flexibility to build retries or whatever other feature they want on top of this library. Because data fetching is so foundational to many apps, I think that sort of extensibility is one of the differences between good and great libraries.
It's great that you have the custom fetch option, and maybe that's what people should reach for if they need that flexibility. However, it doesn't seem like much of a lift compared to the existing middleware implemenation.
from openapi-typescript.
On a related note, the current middleware doesn't provide the option to catch fetch errors it seems? onResponse is not called when the fetch throws, which makes some sense, but makes a common use of middleware difficult and/or puts you back to the "just use a custom fetch function" option.
from openapi-typescript.
Related Issues (20)
- advice on how to connect a frontend (swagger?) while using this lib
- Set baseUrl per request
- 0.9.4 breaks some Typescript configurations HOT 3
- Response types from openapi-fetch ignore null values
- Multiple security schemes not supported
- With React Query: Invariant: headers() expects to have requestAsyncStorage, none available. HOT 3
- `--properties-required-by-default` and `--default-non-nullable` flags do not work HOT 1
- Nested object's properties are mapped to unknown, but in OpenAPI they are typed correctly HOT 3
- "AdditionalProperties" reflects onto existing properties and causes TypeScript compile errors HOT 1
- Issues with references (`$ref`) resolution HOT 2
- Error with negative numbers in enums HOT 2
- Allow for custom types in path params serialization (and maybe other places serialization too)
- Types derived for oneOf construct result in TS2502 error HOT 7
- Uncaught error when body is `undefined` in request init HOT 6
- 'path' does not exist in type '{ query?: Record<string, unknown> | undefined; }' HOT 4
- issue on - generate client when yaml has mutiple same external ref
- Generate subset of schema
- Deduplicate enums
- `type: object` generating `Record<string, never>` HOT 1
- Throw error on error and data property be defined HOT 3
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-typescript.