GithubHelp home page GithubHelp logo

Comments (7)

skellock avatar skellock commented on August 20, 2024

Where do the headers come from? Are they known at api creation time? It sounds like you're saying they may not be.

Each api will need to be told it's headers. You can do this at creation time api.create({ headers: {} }). Or you can do it later via api.setHeader('X-SOMETHING', 'ABCDEF').

from apisauce.

Blackening999 avatar Blackening999 commented on August 20, 2024

Some of them know at api creation time, some of them not.
I'm receiving headers from saga loginFlow. Other places I'm using API.create (which return api in turn) do not know about that update. So no, I cannot do api.setHeader :\
The only possible solution is to use a global namespace for api and use api.setHeader('X-SOMETHING', 'ABCDEF'), for create I have to bake api.create each time with headers which is even worse

from apisauce.

skellock avatar skellock commented on August 20, 2024

I don't think recreating your api every request is a bad thing. That'll only add like 1 or 2ms onto a network request that takes .. what... 50ms? maybe?

Another option to look at is installing a response transform to all APIs which will sniff out your api responses for tokens & add them there.

from apisauce.

Blackening999 avatar Blackening999 commented on August 20, 2024

@skellock this might be interesting about request transforms. how should I declare it?
I have an api in the form of

// a library to wrap and simplify api calls
import apisauce from 'apisauce'

// our "constructor"
const create = (baseURL = 'http://localhost:3000') => {
  // ------
  // STEP 1
  // ------
  //
  // Create and configure an apisauce-based api object.
  //
  const api = apisauce.create({
    // base URL is read from the "constructor"
    baseURL,
    // here are some default headers
    headers: {
      Accept: 'application/json'
    },
    // 10 second timeout...
    timeout: 10000
  })

  // Force OpenWeather API Key on all requests
  // api.addRequestTransform((request) => {
  //   request.params['APPID'] = '0e44183e8d1018fc92eb3307d885379c'
  // })

  // Wrap api's addMonitor to allow the calling code to attach
  // additional monitors in the future.  But only in __DEV__ and only
  // if we've attached Reactotron to console (it isn't during unit tests).
  if (__DEV__ && console.tron) {
    api.addMonitor(console.tron.apisauce)
  }

  // ------
  // STEP 2
  // ------
  //
  // Define some functions that call the api.  The goal is to provide
  // a thin wrapper of the api layer providing nicer feeling functions
  // rather than "get", "post" and friends.
  //
  // I generally don't like wrapping the output at this level because
  // sometimes specific actions need to be take on `403` or `401`, etc.
  //
  // Since we can't hide from that, we embrace it by getting out of the
  // way at this level.
  //
  const getCity = (city) => api.get('weather', {q: city})

  const login = (email, password) => api.post('/oauth/token', { email, password, grant_type: 'password' })

  // ------
  // STEP 3
  // ------
  //
  // Return back a collection of functions that we would consider our
  // interface.  Most of the time it'll be just the list of all the
  // methods in step 2.
  //
  // Notice we're not returning back the `api` created in step 1?  That's
  // because it is scoped privately.  This is one way to create truly
  // private scoped goodies in JavaScript.
  //
  return {
    // a list of the API functions from step 2
    getCity,
    login
  }
}

// let's return back our create method as the default.
export default {
  create
}

I'm interesting how can apply transform considering the fact I'm originally using API as a function which returns the result of creating a new api )

from apisauce.

skellock avatar skellock commented on August 20, 2024

You'd have to pass it in, define it inside, or expose it as a key in the return of that function. A few options, but really, it's up to you.

from apisauce.

Blackening999 avatar Blackening999 commented on August 20, 2024

Wait, you mean if I'll pass transform into a function once it'll be pulled up globally on any consecutive call? If you can provide a short example I'll be much appreciated ;)

from apisauce.

skellock avatar skellock commented on August 20, 2024

Not globally, but for the lifespan of that instance yep. There's some examples of these transforms on the readme. Hope that works for ya.

from apisauce.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.