Type Definitions
This page documents the key TypeScript interfaces and types used across the Fetcher ecosystem. Types are organized by package and category.
Core Types (@ahoo-wang/fetcher)
Utility Types
Source: packages/fetcher/src/types.ts
| Type | Signature | Description |
|---|---|---|
PartialBy<T, K> | Omit<T, K> & Partial<Pick<T, K>> | Make specified keys optional |
RequiredBy<T, K> | Omit<T, K> & Required<Pick<T, K>> | Make specified keys required |
RemoveReadonlyFields<T> | Mapped type | Remove all readonly properties |
NamedCapable
interface NamedCapable {
name: string;
}Source: packages/fetcher/src/types.ts:141
FetcherConfigurer
Interface for objects that configure a Fetcher instance:
interface FetcherConfigurer {
applyTo(fetcher: Fetcher): void;
}Source: packages/fetcher/src/types.ts:248
Request Types
HttpMethod
enum HttpMethod {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE',
PATCH = 'PATCH',
HEAD = 'HEAD',
OPTIONS = 'OPTIONS',
TRACE = 'TRACE',
}Source: packages/fetcher/src/fetchRequest.ts:37
RequestHeaders
interface RequestHeaders {
'Content-Type'?: string;
Accept?: string;
Authorization?: string;
[key: string]: string | undefined;
}Source: packages/fetcher/src/fetchRequest.ts:68
RequestBodyType
type RequestBodyType = BodyInit | Record<string, any> | string | null;Source: packages/fetcher/src/fetchRequest.ts:88
FetchRequestInit
interface FetchRequestInit<BODY extends RequestBodyType = RequestBodyType>
extends TimeoutCapable, RequestHeadersCapable, UrlParamsCapable,
Omit<RequestInit, 'body' | 'headers'> {
body?: BODY;
abortController?: AbortController;
}Source: packages/fetcher/src/fetchRequest.ts:112
FetchRequest
interface FetchRequest<BODY extends RequestBodyType = RequestBodyType>
extends FetchRequestInit<BODY> {
url: string;
}Source: packages/fetcher/src/fetchRequest.ts:176
UrlParams
interface UrlParams {
path?: Record<string, any>;
query?: Record<string, any>;
}Source: packages/fetcher/src/urlBuilder.ts:27
Configuration Types
FetcherOptions
interface FetcherOptions extends BaseURLCapable, RequestHeadersCapable, TimeoutCapable {
urlTemplateStyle?: UrlTemplateStyle;
interceptors?: InterceptorManager;
validateStatus?: ValidateStatus;
}Source: packages/fetcher/src/fetcher.ts:51
RequestOptions
interface RequestOptions extends AttributesCapable, ResultExtractorCapable {}Source: packages/fetcher/src/fetcher.ts:94
ValidateStatus
type ValidateStatus = (status: number) => boolean;Source: packages/fetcher/src/validateStatusInterceptor.ts:62
Capability Interfaces
These interfaces define "capabilities" that types can implement:
| Interface | Property | Type | Source |
|---|---|---|---|
BaseURLCapable | baseURL | string | fetchRequest.ts:23 |
RequestHeadersCapable | headers? | RequestHeaders | fetchRequest.ts:81 |
TimeoutCapable | timeout? | number | timeout.ts |
UrlParamsCapable | urlParams? | UrlParams | fetchRequest.ts:48 |
UrlBuilderCapable | urlBuilder | UrlBuilder | urlBuilder.ts:154 |
ResultExtractorCapable | resultExtractor? | ResultExtractor<any> | resultExtractor.ts:31 |
AttributesCapable | attributes? | Record<string, any> | Map<string, any> | fetchExchange.ts:23 |
Interceptor Types
Interceptor
interface Interceptor extends NamedCapable, OrderedCapable {
readonly name: string;
readonly order: number;
intercept(exchange: FetchExchange): void | Promise<void>;
}Source: packages/fetcher/src/interceptor.ts:44
Specialized Interceptor Interfaces
interface RequestInterceptor extends Interceptor {}
interface ResponseInterceptor extends Interceptor {}
interface ErrorInterceptor extends Interceptor {}All three extend Interceptor without adding new members. They exist for semantic clarity and type discrimination.
Source: packages/fetcher/src/interceptor.ts:111-164
Result Extractor Types
interface ResultExtractor<R> {
(exchange: FetchExchange): R | Promise<R>;
}Source: packages/fetcher/src/resultExtractor.ts:23
Error Types
Type Hierarchy
classDiagram
class Error {
+message: string
+stack?: string
}
class FetcherError {
+cause?: Error | unknown
+name: "FetcherError"
}
class ExchangeError {
+exchange: FetchExchange
+name: "ExchangeError"
}
class HttpStatusValidationError {
+name: "HttpStatusValidationError"
}
class AutoGenerated {
+name: "AutoGenerated"
}
Error <|-- FetcherError
FetcherError <|-- ExchangeError
ExchangeError <|-- HttpStatusValidationError
Error <|-- AutoGenerated
style Error fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style FetcherError fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style ExchangeError fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style HttpStatusValidationError fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style AutoGenerated fill:#2d333b,stroke:#6d5dfc,color:#e6edf3| Class | Package | Source |
|---|---|---|
FetcherError | fetcher | fetcherError.ts:37 |
ExchangeError | fetcher | fetcherError.ts:86 |
HttpStatusValidationError | fetcher | validateStatusInterceptor.ts:27 |
AutoGenerated | decorator | generated.ts:25 |
EventBus Types (@ahoo-wang/fetcher-eventbus)
Source: packages/eventbus/src/types.ts
EventHandler
interface EventHandler<EVENT> extends NamedCapable, OrderedCapable {
once?: boolean;
handle(event: EVENT): void | Promise<void>;
}EventBus
class EventBus<Events extends Record<EventType, unknown>> {
on<Key extends EventType>(type: Key, handler: EventHandler<Events[Key]>): boolean;
off<Key extends EventType>(type: Key, name: string): boolean;
emit<Key extends EventType>(type: Key, event: Events[Key]): void | Promise<void>;
destroy(): void;
}Source: packages/eventbus/src/eventBus.ts:35
React Hook Types (@ahoo-wang/fetcher-react)
PromiseStatus
enum PromiseStatus {
IDLE = 'idle',
LOADING = 'loading',
SUCCESS = 'success',
ERROR = 'error',
}Source: packages/react/src/core/usePromiseState.ts:22
PromiseState
interface PromiseState<R, E = unknown> {
status: PromiseStatus;
loading: boolean;
result: R | undefined;
error: E | undefined;
}Source: packages/react/src/core/usePromiseState.ts:29
PromiseSupplier
type PromiseSupplier<R> = (abortController: AbortController) => Promise<R>;Source: packages/react/src/core/useExecutePromise.ts:51
QueryOptions
interface QueryOptions<Q> {
initialQuery?: Q;
query?: Q;
}Source: packages/react/src/core/useQueryState.ts:17
AutoExecuteCapable
interface AutoExecuteCapable {
autoExecute?: boolean; // Defaults to true
}Source: packages/react/src/types.ts:20
Decorator Types (@ahoo-wang/fetcher-decorator)
ParameterType
enum ParameterType {
PATH = 'path',
QUERY = 'query',
HEADER = 'header',
BODY = 'body',
REQUEST = 'request',
ATTRIBUTE = 'attribute',
}Source: packages/decorator/src/parameterDecorator.ts:19
ParameterMetadata
interface ParameterMetadata {
type: ParameterType;
name?: string;
index: number;
}Source: packages/decorator/src/parameterDecorator.ts:136
EndpointReturnType
enum EndpointReturnType {
EXCHANGE = 'Exchange',
RESULT = 'Result',
}Source: packages/decorator/src/endpointReturnTypeCapable.ts:14
ParameterRequest
interface ParameterRequest<BODY extends RequestBodyType = RequestBodyType>
extends FetchRequestInit<BODY>, PathCapable {}Source: packages/decorator/src/parameterDecorator.ts:352
Type Relationship Diagram
graph TB
subgraph sg_1 ["Fetcher Options"]
FO["FetcherOptions"]
RO["RequestOptions"]
VS["ValidateStatus"]
end
subgraph sg_2 ["Request Types"]
FRI["FetchRequestInit"]
FR["FetchRequest"]
UP["UrlParams"]
RH["RequestHeaders"]
end
subgraph sg_3 ["Exchange"]
FE["FetchExchange"]
FEI["FetchExchangeInit"]
end
subgraph sg_4 ["Interceptors"]
INT["Interceptor"]
RI["InterceptorRegistry"]
IM["InterceptorManager"]
end
subgraph sg_5 ["Extractors"]
RE["ResultExtractor<R>"]
RES["ResultExtractors"]
end
subgraph sg_6 ["Errors"]
FErr["FetcherError"]
EErr["ExchangeError"]
HErr["HttpStatusValidationError"]
end
FO -->|"creates"| IM
FO -->|"configures"| VS
FRI -->|"extends"| FR
FR -->|"flows through"| FE
FE -->|"processed by"| IM
IM -->|"manages"| RI
RI -->|"executes"| INT
FE -->|"extracted by"| RE
FErr -->|"extended by"| EErr
EErr -->|"extended by"| HErr
style FO fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style FR fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style FE fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style IM fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style INT fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style RE fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style FErr fill:#2d333b,stroke:#6d5dfc,color:#e6edf3Global Type Augmentation
Fetcher augments the global Response interface with a generic json<T>() method:
declare global {
interface Response {
json<T = any>(): Promise<T>;
}
}Source: packages/fetcher/src/types.ts:162
React Hook Type Composition
graph TD
subgraph sg_1 ["usePromiseState"]
PS["PromiseState<R, E>"]
PSE["UsePromiseStateReturn"]
end
subgraph sg_2 ["useExecutePromise"]
EPS["UseExecutePromiseOptions"]
EPR["UseExecutePromiseReturn"]
end
subgraph sg_3 ["useQuery"]
QO["UseQueryOptions"]
QR["UseQueryReturn"]
end
subgraph sg_4 ["useFetcher"]
FO["UseFetcherOptions"]
FR["UseFetcherReturn"]
end
PS --> PSE
PSE --> EPR
EPS --> EPR
EPS --> QO
EPR --> QR
QO --> QR
EPS --> FO
EPR --> FR
style PS fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style PSE fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style EPR fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style QR fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
style FR fill:#2d333b,stroke:#6d5dfc,color:#e6edf3Related Pages
- Fetcher Client API -- How to use these types in practice
- Decorators API -- Decorator-specific types
- React Hooks API -- React hook types
- API Overview -- Package summary