feat: Ability to set custom fetch function as an option on HTTP transport#301
feat: Ability to set custom fetch function as an option on HTTP transport#301bradjones1 wants to merge 4 commits intoopen-rpc:masterfrom
Conversation
| interface HTTPTransportOptions { | ||
| credentials?: CredentialsOption | ||
| headers?: Record<string, string> | ||
| fetch?: any |
There was a problem hiding this comment.
I'm not really sure what to put here (not a typescript guy...) but the linter didn't like Function
Codecov Report
@@ Coverage Diff @@
## master #301 +/- ##
=======================================
Coverage 99.26% 99.27%
=======================================
Files 11 11
Lines 410 411 +1
Branches 62 63 +1
=======================================
+ Hits 407 408 +1
Misses 3 3
Continue to review full report at Codecov.
|
|
Hey sorry for the delayed response on this, but I think what you want to do here is just make a custom transport. If it's not problematic for you and reactnative you could even just extend HttpTransport and then overwrite the connect method. class CustomHttpTransport extends HTTPTransport {
connect(){
....
}
}Then usage within the client context is const transport = new CustomHTTPTransport("http://localhost:8545");
const client = new Client(new RequestManager([transport]));
const result = await client.request({method: "addition", params: [2, 2]});If you need access to it from another part of the stack such as the generator, for now because CustomHttpTransport extends HTTPTransport, you should be able to pass the custom transport in from the generated client data. It's possible that we've typed it too strictly from the generator side, if so could you put up an issue https://github.com/open-rpc/generator Thanks for the PR, but I think the proper thing here is a custom transport |
|
OK, thanks for the feedback. |
|
This got done in basically a similar way at #330 |
I am implementing a native app with React Native and use OAuth for authentication. To facilitate token refresh on demand, I am using https://github.com/badgateway/fetch-mw-oauth2 which provides a fetch middleware to retry failed requests after refreshing access tokens.
This pattern works well when clients allow setting a custom fetch function, e.g. in the case of Orbit.js (a json:api client.)
I am not really much of a TypeScript developer but I have worked this out locally and it "seems to work." Hoping this could be an easy/backwards and forwards compatible change that would make this library even better. Thanks!