System Architecture
High-level overview of MyLib's architecture, components, and design principles.
Overview
MyLib is designed around three core principles:
- Type safety — All public APIs expose rich TypeScript types
- Zero dependencies — No runtime dependencies beyond the standard library
- Tree-shakable — Import only what you use
Component Diagram
┌────────────────────────────────────────────────┐
│ MyLib Client │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Users API │ │ Auth API │ │ ... API │ │
│ └──────┬──────┘ └──────┬──────┘ └────┬────┘ │
│ └────────────────┴───────────────┘ │
│ │ │
│ ┌───────────▼──────────┐ │
│ │ HTTP Transport │ │
│ │ (fetch / axios) │ │
│ └───────────┬──────────┘ │
│ │ │
│ ┌───────────▼──────────┐ │
│ │ Retry / Rate Limit │ │
│ │ Middleware │ │
│ └───────────┬──────────┘ │
│ │ │
│ ┌───────────▼──────────┐ │
│ │ Response Parser & │ │
│ │ Error Normalizer │ │
│ └──────────────────────┘ │
└────────────────────────────────────────────────┘
│
HTTP ────▼──── REST API
Layers
Client Layer
The top-level MyLib class acts as a service locator, providing scoped sub-clients for each resource type (users, auth, etc.). Each sub-client is lazy-initialized.
HTTP Transport
The transport layer is pluggable. By default, MyLib uses the global fetch API. You can inject a custom transport for testing:
const client = new MyLib({
apiKey: 'test-key',
transport: mockTransport,
});
Middleware Pipeline
Requests pass through a composable middleware pipeline before being sent:
- Auth Middleware — Injects the
Authorizationheader - Retry Middleware — Retries
5xxand429with exponential backoff - Rate Limit Middleware — Queues requests to stay within the rate limit
Response Parsing
All responses are deserialized and validated against strict Zod schemas. If the server returns an unexpected shape, MyLib throws a ParseError with details, rather than silently returning malformed data.
Design Decisions
Why no dependencies?
External dependencies introduce supply chain risk. By building on the standard library and Web Platform APIs, MyLib can be audited entirely in-house and bundled without version conflicts.
Why Zod for validation?
Zod provides schema-first TypeScript type inference. The same schema used for runtime validation also generates the TypeScript types, eliminating drift between types and runtime behaviour.