How umijs/umi-request works
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).