microsoft / kiota-typescript

TypeScript libraries for Kiota-generated API clients.
https://aka.ms/kiota/docs
MIT License
37 stars 31 forks source link

[React Native iOS] Kiota is not working with React Native iOS #1199

Open AlexPalaz opened 6 months ago

AlexPalaz commented 6 months ago

Kiota TypeScript integration is not working with React Native. I'm not getting any kind of error on logs. After debugging for a while, seems like that createClient from kiota has not being initialized. I tried to create a custom apiClientProxifier removing all the throw new Error and it works until send method inside the FetchRequestAdapter. So I'm supposing there's an issue inside the apiClientProxifier and specially on FetchRequestAdapter.

I tried on React Native Web and Kiota works fine, but on iOS is not working.

Tested on:

OS: iOS 17.5.1 React Native: 0.74.1 Expo SDK 50

koros commented 6 months ago

Hi @AlexPalaz, Thank you for using Kiota and raising this issue. Could you please let me know if you are testing on an actual device or an emulator? This information will help me replicate the issue on my end

AlexPalaz commented 6 months ago

Hi @koros, I'm testing on real device but I've also tried with emulator but didn't work unfortunately

koros commented 5 months ago

@AlexPalaz Alright thanks for the info I'll set up a similar environment.

BrianUribe6 commented 2 months ago

I'm not sure if this is related to the same issue, but when working on React Native (Android) I was unable to send any http requests. The error I was getting were not helpful at all. I got things like TypeError: Network Request Failed. After further investigation I figured that this might be related to React Native's fetch API.

When you don't specify a custom fetch callback. Kiota defaults to window.fetch (or this is what I assume it is getting transpiled to.)

https://github.com/microsoft/kiota-typescript/blob/8e980dacbd77b72598db65df1b8379fc70feb655/packages/http/fetch/src/middlewares/middlewareFactory.ts#L27

To fix this all you need to do is to create an instance of HttpClient and pass it your own fetch callback, and then use it to create your client.


function createRequestAdapter(provider: AuthenticationProvider) {
    // By Default it will use window.fetch if a fetch function is not specified.
    // We need to explicitly instantiate HttpClient and specify React Native's fetch.
    const httpClient = new HttpClient(fetch);
    const adapter = new FetchRequestAdapter(
        provider,
        undefined,
        undefined,
        httpClient
    );
    return adapter;
}

function createAPIClient() {
    const provider = new AnonymousAuthenticationProvider();
    const adapter = createRequestAdapter(provider);
    return createMyAPIClient(adapter);
}