use-http icon indicating copy to clipboard operation
use-http copied to clipboard

Ability to pass options to request methods

Open mercyaj opened this issue 5 years ago • 1 comments

Is it possible to pass options to the request methods such as request.get directly? I know we can set options using Provider and useFetch, but sometimes I need to conditionally modify the request based on some computed property.

For example (using react native):

  const [isReady, setIsReady] = useState(false)
  const [auth, setAuth] = useContext(AuthContext)
  const [req, res] = useFetch()

  const handleSetup = async () => {
    const jwt = await SecureStore.getItemAsync('jwt')

    if (!jwt) {
      return setIsReady(true)
    }    

    // =============================
    // I need to set an Authorization header here, but can't because 
    // req.get doesn't accept an options object
    // =============================
   const data = await req.get('/auth/refresh')

    if (res.ok) {
      setAuth({ jwt: jwt, user: data.user })
    }
  }

  if (!isReady) {
    return (
      <AppLoading
        startAsync={handleSetup}
        onFinish={() => setIsReady(true)}
        onError={console.warn}
      />
    )
  }

  if (!auth.jwt) {
    return (
      <NavigationContainer>
        <AuthNavigator />
      </NavigationContainer>
    )
  }

  return (
    <NavigationContainer>
      <TabNavigator />
    </NavigationContainer>
  )

If we can't already, would you consider allowing the request methods to accept an options object?

mercyaj avatar Jul 07 '20 13:07 mercyaj

I can only agree. That feature is really missing here.

Blizard avatar Nov 18 '21 18:11 Blizard