Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { AppConfig } from '../config/app.config'
- import BaseApi from './protectedApi'
- // our "constructor"
- const create = () => {
- // ------
- // STEP 1
- // ------
- //
- // Create and configure an apisauce-based api object.
- //
- const baseApi = BaseApi(AppConfig.BASE_URL)
- const setHeaders = (headers) => {
- baseApi.setHeaders(headers)
- }
- // ------
- // 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.
- // Session
- const getProducts = args => baseApi.get('products/list', { ...args })
- // ------
- // 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
- setHeaders,
- getProducts,
- }
- }
- // let's return back our create method as the default.
- export default {
- create
- }
- import * as NetworkInstance from '../../api/networkInstance'
- const api = NetworkInstance.getApi()
- api.getProducts()
Add Comment
Please, Sign In to add comment