akanjs 3.0.0-alpha.74 → 3.0.0-alpha.75

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -98,7 +98,7 @@ export class FetchClient {
98
98
  private ErrorCls?: ErrorConstructor,
99
99
  ) {
100
100
  this.origin = origin;
101
- this.http = new HttpClient(origin, ErrorCls);
101
+ this.http = new HttpClient(origin, { ErrorCls });
102
102
  this.ws = new WsClient(FetchClient.#makeWsUri(origin), ErrorCls);
103
103
  Object.assign(this.#handlerStore, handler);
104
104
  this.handler = this.#makeHandlerProxy();
@@ -22,11 +22,15 @@ export interface ErrorConstructor {
22
22
  fromJSON: (payload: ErrorResponsePayload) => RestoredError;
23
23
  }
24
24
 
25
- interface FetchOptions {
25
+ export interface HttpClientOptions {
26
26
  headers?: Record<string, string>;
27
- baseUrl?: string;
28
27
  /** Milliseconds before the request is abandoned; `false` waits as long as the browser will. */
29
28
  timeout?: number | false;
29
+ ErrorCls?: ErrorConstructor;
30
+ }
31
+
32
+ interface FetchOptions extends Pick<HttpClientOptions, "headers" | "timeout"> {
33
+ baseUrl?: string;
30
34
  }
31
35
 
32
36
  const jsonContentType = /^application\/(?:[\w.+-]+\+)?json\b/i;
@@ -53,11 +57,14 @@ const DEFAULT_TIMEOUT_MS = 30_000;
53
57
 
54
58
  export class HttpClient {
55
59
  readonly baseUrl: string;
56
- constructor(
57
- baseUrl: string,
58
- private ErrorCls?: ErrorConstructor,
59
- ) {
60
+ #headers: Record<string, string>;
61
+ #timeout?: number | false;
62
+ private ErrorCls?: ErrorConstructor;
63
+ constructor(baseUrl: string, options: HttpClientOptions = {}) {
60
64
  this.baseUrl = baseUrl;
65
+ this.#headers = options.headers ?? {};
66
+ this.#timeout = options.timeout;
67
+ this.ErrorCls = options.ErrorCls;
61
68
  }
62
69
 
63
70
  setErrorConstructor(ErrorCls?: ErrorConstructor) {
@@ -70,13 +77,13 @@ export class HttpClient {
70
77
  return `${this.#resolveBaseUrl(options.baseUrl)}${url}`;
71
78
  }
72
79
 
73
- static #makeHeaders(headers: Record<string, string>, options: FetchOptions) {
74
- return { Accept: "application/json", ...headers, ...options.headers };
80
+ #makeHeaders(headers: Record<string, string>, options: FetchOptions) {
81
+ return { Accept: "application/json", ...this.#headers, ...headers, ...options.headers };
75
82
  }
76
83
  async get<Returns = unknown>(url: string, options: FetchOptions = {}): Promise<Returns> {
77
84
  return await this.#request<Returns>(
78
85
  this.#resolveUrl(url, options),
79
- { headers: HttpClient.#makeHeaders({ "Content-Type": "application/json" }, options) },
86
+ { headers: this.#makeHeaders({ "Content-Type": "application/json" }, options) },
80
87
  options,
81
88
  );
82
89
  }
@@ -94,7 +101,7 @@ export class HttpClient {
94
101
  const { body, headers } = this.#makeReqContent(data);
95
102
  return await this.#request<Returns>(
96
103
  this.#resolveUrl(url, options),
97
- { method, body, headers: HttpClient.#makeHeaders(headers, options) },
104
+ { method, body, headers: this.#makeHeaders(headers, options) },
98
105
  options,
99
106
  );
100
107
  }
@@ -115,13 +122,13 @@ export class HttpClient {
115
122
  async delete<Returns = unknown>(url: string, options: FetchOptions = {}): Promise<Returns> {
116
123
  return await this.#request<Returns>(
117
124
  this.#resolveUrl(url, options),
118
- { method: "DELETE", headers: HttpClient.#makeHeaders({ "Content-Type": "application/json" }, options) },
125
+ { method: "DELETE", headers: this.#makeHeaders({ "Content-Type": "application/json" }, options) },
119
126
  options,
120
127
  );
121
128
  }
122
129
 
123
130
  async #request<Returns>(url: string, init: RequestInit, options: FetchOptions = {}): Promise<Returns> {
124
- const res = await this.#fetch(url, HttpClient.#withTimeout(init, options));
131
+ const res = await this.#fetch(url, this.#withTimeout(init, options));
125
132
  return await this.#readJsonResponse<Returns>(res);
126
133
  }
127
134
 
@@ -130,8 +137,8 @@ export class HttpClient {
130
137
  * the request body is the only thing this side can tell that from. Everything else takes the default unless
131
138
  * the caller named one.
132
139
  */
133
- static #withTimeout(init: RequestInit, options: FetchOptions): RequestInit {
134
- const timeout = options.timeout ?? (init.body instanceof FormData ? false : DEFAULT_TIMEOUT_MS);
140
+ #withTimeout(init: RequestInit, options: FetchOptions): RequestInit {
141
+ const timeout = options.timeout ?? (init.body instanceof FormData ? false : (this.#timeout ?? DEFAULT_TIMEOUT_MS));
135
142
  if (timeout === false || !Number.isFinite(timeout) || timeout <= 0) return init;
136
143
  return { ...init, signal: AbortSignal.timeout(timeout) };
137
144
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.0.0-alpha.74",
3
+ "version": "3.0.0-alpha.75",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -18,17 +18,20 @@ export interface RestoredError extends Error {
18
18
  export interface ErrorConstructor {
19
19
  fromJSON: (payload: ErrorResponsePayload) => RestoredError;
20
20
  }
21
- interface FetchOptions {
21
+ export interface HttpClientOptions {
22
22
  headers?: Record<string, string>;
23
- baseUrl?: string;
24
23
  /** Milliseconds before the request is abandoned; `false` waits as long as the browser will. */
25
24
  timeout?: number | false;
25
+ ErrorCls?: ErrorConstructor;
26
+ }
27
+ interface FetchOptions extends Pick<HttpClientOptions, "headers" | "timeout"> {
28
+ baseUrl?: string;
26
29
  }
27
30
  export declare class HttpClient {
28
31
  #private;
29
- private ErrorCls?;
30
32
  readonly baseUrl: string;
31
- constructor(baseUrl: string, ErrorCls?: ErrorConstructor | undefined);
33
+ private ErrorCls?;
34
+ constructor(baseUrl: string, options?: HttpClientOptions);
32
35
  setErrorConstructor(ErrorCls?: ErrorConstructor): void;
33
36
  get<Returns = unknown>(url: string, options?: FetchOptions): Promise<Returns>;
34
37
  send<Returns = unknown>(method: HttpMutationMethod, url: string, data: FormData | Record<string, unknown>, options?: FetchOptions): Promise<Returns>;