normalize-args.ts

Overview

The normalize-args.ts file provides a utility function named normalize that standardizes the input arguments for a data fetching hook or function. This normalization process ensures that the arguments — which can vary in number and type — are transformed into a consistent tuple format with three elements: a key, a fetcher function (or null), and a partial configuration object.

This utility is typically used in libraries or modules that implement data fetching patterns, such as SWR (stale-while-revalidate), where the user can supply different combinations of parameters to customize fetching behavior. By normalizing the arguments, downstream code can operate on a predictable input structure, improving maintainability and simplifying internal logic.


Detailed Explanation

Imports


Function: normalize

export const normalize = <KeyType = Key, Data = any>(
  args:
    | [KeyType]
    | [KeyType, Fetcher<Data> | null]
    | [KeyType, SWRConfiguration | undefined]
    | [KeyType, Fetcher<Data> | null, SWRConfiguration | undefined]
): [KeyType, Fetcher<Data> | null, Partial<SWRConfiguration<Data>>]

Purpose

The normalize function processes a variable number and type of arguments related to data fetching, and returns a normalized tuple containing:

  1. Key: The identifier of the resource.

  2. Fetcher: The function that fetches the data or null if none is provided.

  3. Configuration: A partial configuration object that customizes fetching behavior.

Parameters

Returns

Implementation Details

The function uses a type guard to check if the second argument (args[1]) is a function:

This logic covers all possible input shapes and ensures the output is consistently structured.


Usage Example

import { normalize } from './normalize-args'
import type { Fetcher, SWRConfiguration } from '../types'

const key = '/api/user'

// Case 1: Only key
const [key1, fetcher1, config1] = normalize([key])
// fetcher1 === null, config1 === {}

// Case 2: Key and fetcher
const fetcher: Fetcher<User> = url => fetch(url).then(res => res.json())
const [key2, fetcher2, config2] = normalize([key, fetcher])
// fetcher2 === fetcher, config2 === {}

// Case 3: Key and config
const config: SWRConfiguration = { refreshInterval: 5000 }
const [key3, fetcher3, config3] = normalize([key, config])
// fetcher3 === null, config3 === config

// Case 4: Key, fetcher, and config
const [key4, fetcher4, config4] = normalize([key, fetcher, config])
// fetcher4 === fetcher, config4 === config

Interaction with Other Parts of the System


Summary

Aspect

Description

File Purpose

Normalize variable input arguments into a standard tuple for data fetching operations.

Core Function

normalize — transforms input tuples into `[key, fetcher

Inputs Supported

Single key, key + fetcher, key + config, key + fetcher + config.

Outputs

Tuple with consistent structure for downstream processing.

Key Benefits

Simplifies argument parsing, improves type safety, and standardizes configuration handling.


Mermaid Flowchart Diagram

flowchart TD
    A[normalize(args)] --> B{Is args[1] a function?}
    B -- Yes --> C[Return [args[0], args[1], args[2] || {}]]
    B -- No --> D{Is args[1] === null?}
    D -- Yes --> E[Return [args[0], null, args[2] || {}]]
    D -- No --> F[Return [args[0], null, args[1] || {}]]

This documentation provides a complete understanding of the normalize-args.ts file, its purpose, usage, and how it fits within the larger data fetching architecture.