Skip to content

Type Alias: FetchInterceptorResult<Args>

FetchInterceptorResult<Args> = Interceptor<unknown, Args>

Defined in: packages/fetch/src/types/interceptors.ts:165

Fetch result interceptor to be invoked before returning parsed fetch result.

Result interceptors are executed ONLY when a result is successfully parsed (as ArrayBuffer, Blob, JSON, Text...). Result interceptors WILL NOT be executed if:

  • return type is set to Response by using FetchAs.response in the FetchOptions.as
  • exceptions is thrown even before attempting to parse
  • parse fails

This interceptor can also be used as a transformer by returns a different/modified result.

Type Parameters

Args

Args extends unknown[] = FetchArgsInterceptor

Example

Intercept and transform fetch result

javascript
import fetch from '@superutils/fetch'

const result = await fetch.get('[DUMMYJSON-DOT-COM]/users/1', {
  interceptors: {
    result: [
      // 1. check & convert user crypto balance to BigInt
	     userCryptoBalanceToBigInt,

      // 2. log balance (no transformation)
	     logUserBalance
	   ]
  }
})
console.log({result})

function userCryptoBalanceToBigInt(user = {}) {
  user.crypto ??= {}
  user.crypto.balance ??= '0x0'
  user.crypto.balance = BigInt(user.crypto.balance || '0x0')
  return user
}

function logUserBalance(user, url) {
  console.log(
    new Date().toISOString(),
    '[UserBalance] UserID:', user.id,
    user.crypto.balance
  )
}