# umijs/umi-request architecture

> Architecture diagram of the GitHub repository umijs/umi-request, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/umijs/umi-request
- Repository: https://github.com/umijs/umi-request

## Overview

##### Project Type and Purpose
- The project is a pure JavaScript library for making HTTP requests. It wraps around the browser’s native fetch (or node’s fetch polyfill) and provides a unified and enhanced API call method.
- Its purpose is to simplify network requests by providing features like request/response interception, middleware for request handling (onion architecture), caching, timeout handling, and cancellation.

##### Analyzing the File Structure
- The top-level directories and key files include:
- /src – the implementation of the library.
◦ cancel/ – files implementing cancel functionality (cancel.js, cancelToken.js, isCancel.js).
◦ interceptor/ – files for intercepting and modifying requests/responses.
◦ middleware/ – various middleware implementations (e.g. simpleGet.js, simplePost.js, fetch.js, parseResponse.js) to extend or alter request behavior.
◦ onion/ – the implementation of the middleware composition, likely based on the onion model (see compose.js and index.js).
◦ core.js, request.js, index.js – core functionalities and the entry point.
◦ utils.js – utility functions which support various operations.
- /test – test cases covering nearly every module and use case.
- /types – TypeScript definitions for consumers of the library.
- Notice how these components are modular, which suggests that the architecture needs to show separate building blocks for each critical function (e.g., cancel tokens, interceptors, and middleware).

##### Main Components
a. Client API Layer:
- The entry point (src/index.js) which exposes functions like get, post, delete, etc.
- The extend() function for creating customized request instances.
b. Request Processing Core:
- The core module (src/core.js) that handles the underlying request logic.
- Request.js that wraps and orchestrates the API call using the native fetch.
- The utilities that support parameter serialization and error handling.
c. Middleware System (Onion Architecture):
- The middleware directory illustrating different middleware (simpleGet, simplePost, fetch, parseResponse) which are composed using the onion pattern (see src/onion/compose.js and index.js in the onion folder).
- How the middleware chain processes a request sequentially—indicating the “before” and “after” phases.
d. Cancellation Mechanisms:
- The cancel directory modules (cancel.js, cancelToken.js, isCancel.js) that allow requests to be canceled.
- Integration with both AbortController and the custom cancel token.
e. Interceptors:
- The interceptor module (src/interceptor/addfix.js) which can modify requests and responses.
- That this layer sits either before or after the middleware chain.
f. Error Handling and Response Processing:
- How errors and response transformations are managed (as described in the README and in errorHandler functionality in the extend API and usage examples).

##### Relationships and Interactions
- From the core, highlight a branch that handles cancellation and another that deals with interceptors.
- The interceptors can be diagrammatically represented as a layer that pre-processes (modifies URL or headers) and post-processes (adjusts responses, error handling) the data.

##### Architectural Patterns to Emphasize
- The outer layer is the Client API.
- The next inner layer is the middleware chain (onion architecture).
- Followed by the core request handling.
- Separate modules for interceptors and cancellation.
- Use of the Onion pattern in middleware composition.

##### Relevant Technologies, Frameworks, and Libraries
- Mention the integration of TypeScript (notice the /types folder) for type definitions and better developer ergonomics.
- That ESLint, Prettier, and Jest are used for code quality and testing (from the configuration files and test directory).

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Client API (src/index.js)"]:::client
    B["Request Orchestrator (src/request.js)"]:::core
    C["Core Logic (src/core.js)"]:::core
    D["Utilities (src/utils.js)"]:::core

    subgraph "Middleware Pipeline"
        E1["Middleware Modules (src/middleware: fetch.js, parseResponse.js, simpleGet.js, simplePost.js)"]:::middleware
        E2["Onion Composition (src/onion: compose.js, index.js)"]:::middleware
    end

    F["Cancellation Module (src/cancel: cancel.js, cancelToken.js, isCancel.js)"]:::cancel
    G["Interceptor Module (src/interceptor/addfix.js)"]:::interceptor

    A -->|"calls"| B
    B -->|"invokes"| C
    C -->|"utilizes"| D
    B -->|"processesThrough"| E1
    E1 -->|"composes"| E2
    E2 -->|"returnsResponse"| B
    C -->|"handlesCancel"| F
    C -->|"appliesInterceptor"| G

    click A "https://github.com/umijs/umi-request/blob/master/src/index.js"
    click B "https://github.com/umijs/umi-request/blob/master/src/request.js"
    click C "https://github.com/umijs/umi-request/blob/master/src/core.js"
    click D "https://github.com/umijs/umi-request/blob/master/src/utils.js"
    click E1 "https://github.com/umijs/umi-request/tree/master/src/middleware"
    click E2 "https://github.com/umijs/umi-request/tree/master/src/onion"
    click F "https://github.com/umijs/umi-request/tree/master/src/cancel"
    click G "https://github.com/umijs/umi-request/blob/master/src/interceptor/addfix.js"

    classDef client fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef core fill:#90ee90,stroke:#333,stroke-width:2px;
    classDef middleware fill:#FFFF99,stroke:#333,stroke-width:2px;
    classDef cancel fill:#FFB6C1,stroke:#333,stroke-width:2px;
    classDef interceptor fill:#E6E6FA,stroke:#333,stroke-width:2px;
```
