Guest User

Untitled

a guest
Jul 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import { AppConfig } from '../config/app.config'
  2. import BaseApi from './protectedApi'
  3.  
  4. // our "constructor"
  5. const create = () => {
  6. // ------
  7. // STEP 1
  8. // ------
  9. //
  10. // Create and configure an apisauce-based api object.
  11. //
  12. const baseApi = BaseApi(AppConfig.BASE_URL)
  13.  
  14. const setHeaders = (headers) => {
  15. baseApi.setHeaders(headers)
  16. }
  17.  
  18. // ------
  19. // STEP 2
  20. // ------
  21. //
  22. // Define some functions that call the api. The goal is to provide
  23. // a thin wrapper of the api layer providing nicer feeling functions
  24. // rather than "get", "post" and friends.
  25. //
  26. // I generally don't like wrapping the output at this level because
  27. // sometimes specific actions need to be take on `403` or `401`, etc.
  28. //
  29. // Since we can't hide from that, we embrace it by getting out of the
  30. // way at this level.
  31.  
  32. // Session
  33. const getProducts = args => baseApi.get('products/list', { ...args })
  34.  
  35. // ------
  36. // STEP 3
  37. // ------
  38. //
  39. // Return back a collection of functions that we would consider our
  40. // interface. Most of the time it'll be just the list of all the
  41. // methods in step 2.
  42. //
  43. // Notice we're not returning back the `api` created in step 1? That's
  44. // because it is scoped privately. This is one way to create truly
  45. // private scoped goodies in JavaScript.
  46. //
  47. return {
  48. // a list of the API functions from step 2
  49. setHeaders,
  50. getProducts,
  51. }
  52. }
  53.  
  54. // let's return back our create method as the default.
  55. export default {
  56. create
  57. }
  58.  
  59. import * as NetworkInstance from '../../api/networkInstance'
  60. const api = NetworkInstance.getApi()
  61. api.getProducts()
Add Comment
Please, Sign In to add comment