Repository navigation
Expand file tree
/
Copy pathtypes.ts
More file actions
155 lines (142 loc) · 5.4 KB
/
Copy pathtypes.ts
File metadata and controls
155 lines (142 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
/**
* Public type definitions for @eserstack/httpclient.
*
* Provides compatibility with @eserstack/functions patterns:
* - Result<T,E> re-exported for consumer convenience
* - RetryConfig shape mirrors @eserstack/functions/resources.ts retryWithBackoff options
*
* @module
*/
export type { Fail, Ok, Result } from "@eserstack/primitives/results";
import type * as goClient from "./go-client.ts";
export type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD";
/** Rate-limit metadata extracted from response headers. */
export interface RateLimitInfo {
remaining?: number;
limit?: number;
resetAt?: number;
retryAfterSeconds?: number;
}
/** Structured response returned by all HttpClient methods. */
export interface HttpResponse<T = unknown> {
data: T;
status: number;
statusText: string;
headers: Headers;
request: Request;
raw: Response;
retries: number;
rateLimit?: RateLimitInfo;
}
/**
* Response from a streaming request.
* Body is NOT consumed — the caller owns the ReadableStream lifecycle.
*
* Response interceptors do NOT run on stream responses because they expect a
* typed `HttpResponse<T>` with parsed `data`, which streaming doesn't have.
* Request interceptors run normally.
*/
export interface HttpStreamResponse {
/** Raw body stream — caller reads and closes this. */
body: ReadableStream<Uint8Array>;
/** HTTP status code. */
status: number;
/** HTTP status text. */
statusText: string;
/** Response headers. */
headers: Headers;
/** The Request object that was sent (after request interceptors). */
request: Request;
/** Original Response object (escape hatch). */
raw: Response;
/** Number of retries attempted before the stream connected. */
retries: number;
/** Rate-limit metadata extracted from response headers. */
rateLimit?: RateLimitInfo;
}
/** Named request interceptor — transforms a Request before sending. */
export type RequestInterceptor = {
name?: string;
intercept: (request: Request) => Request | Promise<Request>;
};
/** Named response interceptor — transforms an HttpResponse after receiving. */
export type ResponseInterceptor = {
name?: string;
intercept: <T>(
response: HttpResponse<T>,
) => HttpResponse<T> | Promise<HttpResponse<T>>;
};
/** Interceptor collections for client configuration. */
export interface Interceptors {
request?: ReadonlyArray<RequestInterceptor>;
response?: ReadonlyArray<ResponseInterceptor>;
}
/**
* Retry configuration.
* Shape mirrors @eserstack/functions/resources.ts retryWithBackoff options
* for consistency across the monorepo.
*/
export interface RetryConfig {
/** Maximum number of attempts (default: 3). Matches @eserstack/functions. */
maxAttempts?: number;
/** Initial backoff delay in ms (default: 1000). Matches @eserstack/functions. */
initialDelay?: number;
/** Maximum backoff delay in ms (default: 30_000). Matches @eserstack/functions. */
maxDelay?: number;
/** Backoff multiplication factor (default: 2). Matches @eserstack/functions. */
factor?: number;
/** Add randomness to delays to avoid thundering herd (default: true). */
jitter?: boolean;
/** HTTP status codes that trigger a retry (default: [408, 429, 500, 502, 503, 504]). */
retryableStatuses?: readonly number[];
/** Honour the Retry-After response header for 429 responses (default: true). */
respectRetryAfter?: boolean;
}
/**
* How requests leave the process. `"fetch"` (the default) uses `fetchFn` or
* the global `fetch`. `"native"` uses the Go HTTP client of `@eserstack/ajan`,
* whose calls block the JavaScript thread; the README's Native Client section
* lists what else differs.
*/
export type HttpTransport = "fetch" | "native";
/** Client-level configuration. */
export interface HttpClientConfig {
/** Prefix for request paths that are not absolute URLs. */
baseUrl?: string;
/** Headers sent with every request; a request's own headers win. */
headers?: Record<string, string>;
/** Request timeout in ms (default: 30_000). */
timeout?: number;
/** Retry policy — pass false to disable retry entirely. */
retry?: RetryConfig | false;
/** Request and response interceptors, run in order. */
interceptors?: Interceptors;
/** Where requests are sent; see {@link HttpTransport}. Default: "fetch". */
transport?: HttpTransport;
/** Inject a custom fetch for testing. Not allowed with transport "native". */
fetchFn?: typeof fetch;
/** Inject a custom delay function to control timing in tests. */
_delayFn?: (ms: number) => Promise<void>;
/** Inject the Go client factory in tests of transport "native". */
_createGoClient?: (
options: goClient.GoHttpClientOptions,
) => Promise<goClient.GoHttpClient>;
}
/** Per-request overrides. */
export interface RequestOptions {
headers?: Record<string, string>;
/**
* Request body. Auto-serialization rules:
* - Plain object / array → `JSON.stringify(body)`, sets `Content-Type: application/json`
* (header is NOT overwritten if already set by the caller)
* - `string`, `ArrayBuffer`, `ArrayBufferView`, `Blob`, `FormData`,
* `URLSearchParams`, `ReadableStream` → forwarded as-is to `fetch()`
* - `undefined` → no body sent
*/
body?: BodyInit | Record<string, unknown>;
params?: Record<string, string>;
timeout?: number;
retry?: RetryConfig | false;
signal?: AbortSignal;
}