serialize.ts


Overview

serialize.ts provides a utility function serialize that processes a given cache key into a normalized string form along with its original arguments. This transformation facilitates consistent key management, particularly useful in caching or data-fetching scenarios where keys can be strings, arrays, or functions returning keys. The primary goal is to produce a stable, serialized key string that uniquely identifies a resource or request, along with the original key arguments passed, enabling downstream fetchers or cache mechanisms to operate reliably.


Detailed Documentation

Function: serialize

serialize(key: Key): [string, Arguments]

Description

Converts a key into a tuple [serializedKey, originalArgs].

This tuple is useful in caching or data fetching libraries where:


Parameters

Name

Type

Description

key

Key

The input key representing the resource/request. It can be:

  • a string (e.g., URL)

  • an array of values (e.g., query parameters)

  • a function returning the above types

Key is imported from the project's type definitions and is generally a union type covering these possibilities.


Returns

Type

Description

[string, Arguments]

A tuple where:

  • The first element is a string representing the serialized and stable cache key

  • The second element is the original arguments (can be a string or array) passed to fetchers


Usage Example

import { serialize } from './serialize'

// Using a string key
const [key1, args1] = serialize('user:123')
console.log(key1)  // 'user:123'
console.log(args1) // 'user:123'

// Using an array key
const [key2, args2] = serialize(['user', 123, { active: true }])
console.log(key2)  // a stable hash string like 'a1b2c3d4...' representing the array content
console.log(args2) // ['user', 123, { active: true }]

// Using a function that returns a key
const [key3, args3] = serialize(() => ['post', 42])
console.log(key3)  // stable hash of ['post', 42]
console.log(args3) // ['post', 42]

// Using a function that throws
const [key4, args4] = serialize(() => { throw new Error('not ready') })
console.log(key4)  // ''
console.log(args4) // ''

Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram: Flowchart of serialize Function

flowchart TD
  A[Input: key (Key)] --> B{Is key a function?}
  B -- Yes --> C[Try to invoke key()]
  C --> D{Invocation successful?}
  D -- Yes --> E[Set key = result of function]
  D -- No --> F[Set key = '' (empty string)]
  B -- No --> E[Use key as is]
  E --> G[Set args = key]
  G --> H{Type of key}
  H -- string --> I[Use key as serialized key]
  H -- array and non-empty --> J[Generate stableHash(key)]
  H -- falsy or empty array --> K[Set serialized key = '']
  I --> L[Return [serialized key, args]]
  J --> L
  K --> L

Summary

serialize.ts provides a crucial utility function to normalize and serialize cache keys in a consistent, stable manner supporting strings, arrays, and function-generated keys. It ensures that keys are safely handled even when dependencies are missing or keys are dynamic. This functionality is vital for caching or data-fetching systems requiring robust and collision-resistant keys to identify resources or requests.


End of Documentation