Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: React Native support with fetchOption and callOptions #653

Merged
merged 14 commits into from Nov 22, 2022

Conversation

wackyleo459
Copy link
Contributor

Description

For React Native project, to create new fetch settings that work with React-Native-Fetch-API polyfill.

new HttpAgent ({
    fetchOptions: {
      reactNative: {
        __nativeResponseType: "base64",
      },
    },
    callOptions: {
      reactNative: {
        textStreaming: true,
      },
    },
    host: "http://localhost:8000",
})

When source isn't provided in constructing the HttpAgent, this._fetchOptions and this._callOptions will automatically pull from provided options. Currently this is the only place where fetchOptions and callOptions will be set.

      this._fetchOptions = options.fetchOptions; 
      this._callOptions = options.callOptions;

krpeacock and others added 8 commits October 24, 2022 15:41
…l included the logic:

- `this._fetchOptions = options.source._fetchOptions || options.fetchOptions;`
- `this._callOptions = options.source._callOptions || options.callOptions;`
…Options and this._callOptions will automatically get fromm provided options.
…l included the logic:

- `this._fetchOptions = options.source._fetchOptions || options.fetchOptions;`
- `this._callOptions = options.source._callOptions || options.callOptions;`
…Options and this._callOptions will automatically get fromm provided options.
@wackyleo459 wackyleo459 requested a review from a team as a code owner November 8, 2022 18:36
@wackyleo459
Copy link
Contributor Author

@krpeacock
I'm wondering if this has anything to do with the new change for DFX with changed port proxy from 8000 to 4943?

Screen Shot 2022-11-10 at 12 49 38 AM

Copy link
Contributor

@rvanasa rvanasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Added a few quick suggestions.

@@ -313,26 +313,26 @@ test('use anonymous principal if unspecified', async () => {

describe('getDefaultFetch', () => {
it("should use fetch from window if it's available", async () => {
const generateAgent = () => new HttpAgent({ host: 'localhost:8000' });
const generateAgent = () => new HttpAgent({ host: 'localhost:4943' });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this string value is being used so many times here, perhaps we could use this PR as an opportunity to introduce something like const HTTP_AGENT_HOST = 'localhost:4943'; near the top of the file, and then pointing each of these usages to this constant value. This would simplify the process of making similar changes in the future (such as if we wanted to determine the port from a config file, etc).

fetchOptions?: Record<string, unknown>;

// Additional options to pass along to fetch for the call API.
callOptions?: Record<string, unknown>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might eventually make sense to include one or two unit tests to ensure that these options can be used and overridden as expected. The implementation looks good, so this would just be to minimize the chance that someone doesn't accidentally break the expected behavior a few years from now (since a regression might cause some very subtle bugs that could be difficult to identify otherwise).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've attempted to create a minor unit test here.
There is a repeated error that shows (node:2473) V8: /home/runner/work/agent-js/agent-js/node_modules/borc/src/decoder.asm.js:3 Linking failure in asm.js: Unexpected stdlib member.
Not sure but this might be due to issue here of jest globals and node globals differing. jestjs/jest#2549.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, could be something like that. What Node.js version are you using?

The unit test works as expected on my laptop (equivalent to the CI output).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed some fixes for the unit test to hopefully save you a bit of time. Feel free to make any changes or improvements as you see fit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much! I was using node version 16.17.0. (I'm still not sure how to get the unit test to work locally.) But I'm glad this fix works!

@wackyleo459 wackyleo459 removed the request for review from ericswanson-dfinity November 14, 2022 17:13
@krpeacock krpeacock merged commit 6e5b2c6 into main Nov 22, 2022
@krpeacock krpeacock deleted the kyle/fetchOptions branch November 22, 2022 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants