Skip to content

feat(fetch): Add configurable request ID generator for request tracing #111

@damusix

Description

@damusix

Summary

Add an optional generateRequestId function to the fetch engine configuration that defaults to @logosdx/utils's generateId. This enables correlating requests across event listeners for logging and debugging purposes.

Motivation

When listening to fetch events like fetch-before, fetch-error, and fetch-after, there's currently no built-in way to correlate these events for the same request. A unique request ID passed through all events would make this correlation straightforward.

Proposed API

import { FetchEngine } from '@logosdx/fetch';

const engine = new FetchEngine({
    baseUrl: 'https://api.example.com',
    // Optional: custom request ID generator (defaults to generateId from @logosdx/utils)
    generateRequestId: () => crypto.randomUUID(),
});

// Request ID available in all event payloads
engine.on('fetch-before', ({ requestId, url, options }) => {
    console.log(`[${requestId}] Starting request to ${url}`);
});

engine.on('fetch-error', ({ requestId, error }) => {
    console.error(`[${requestId}] Request failed:`, error);
});

engine.on('fetch-after', ({ requestId, response }) => {
    console.log(`[${requestId}] Request completed with status ${response.status}`);
});

Implementation Notes

  • Add generateRequestId?: () => string to engine config
  • Default to generateId from @logosdx/utils
  • Include requestId in all fetch event payloads (fetch-before, fetch-after, fetch-error)
  • Generate the ID once per request, before the first event fires

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpkg: fetchIssues related to @logosdx/fetch package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions