∺ Hingejs

Fetch Interceptor

Fetch Interceptor allows you to accept the response before it continue down the chain or after. A use case can be to add authentication before making the call

↩ Go Back

Working sample

view the console for details

Sample code

      import { FetchInterceptor } from 'https://cdn.jsdelivr.net/npm/@hingejs/services/index.min.js'

      (async () => {
        FetchInterceptor.register({
          request: (url, options) => {
            // Modify the url or options here
            console.log('fetch request interceptor')
            return [url, options]
          },
          requestError: error => {
            // Called when an error occurred during another 'request' interceptor call
            return Promise.reject(error)
          },
          response: response => {
            // Modify the response object
            console.log('fetch response interceptor')
            return response
          },
          responseError: error => {
            // Handle an fetch error
            return Promise.reject(error)
          }
        })

        fetch('http://example.com/', {
          mode: 'no-cors'
        }).then(() => {
          console.log('fetch completed')
        })

      })()