akanjs 2.2.2 → 2.2.3-rc.0
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.
|
@@ -142,8 +142,12 @@ export class FetchClient {
|
|
|
142
142
|
const argMap = new Map(serializerMap.entries().map(([key, serializer], idx) => [key, serializer(args[idx])]));
|
|
143
143
|
const url = FetchClient.makeHttpUrl(key, endpoint, prefix, argMap);
|
|
144
144
|
const headers = this.#makeAuthHeaders(option);
|
|
145
|
-
const
|
|
146
|
-
|
|
145
|
+
const baseUrl = option?.url;
|
|
146
|
+
|
|
147
|
+
const requestQuery = () => this.http.get(url, { headers, baseUrl });
|
|
148
|
+
const response = baseUrl
|
|
149
|
+
? await requestQuery()
|
|
150
|
+
: await memoizeRequestQuery(FetchClient.#makeRequestQueryCacheKey(this.origin, url, headers), requestQuery);
|
|
147
151
|
const parsedReturn = parseReturn(FetchClient.#deepCopy(response), { crystalize: option?.crystalize ?? true });
|
|
148
152
|
return parsedReturn;
|
|
149
153
|
};
|
|
@@ -158,6 +162,7 @@ export class FetchClient {
|
|
|
158
162
|
const body = HttpClient.makeBody(bodyArgs, uploadArgs, argMap);
|
|
159
163
|
const response = await this.http.post(url, body, {
|
|
160
164
|
headers: this.#makeAuthHeaders(option),
|
|
165
|
+
baseUrl: option?.url,
|
|
161
166
|
});
|
|
162
167
|
const parsedReturn = parseReturn(response, { crystalize: option?.crystalize ?? true });
|
|
163
168
|
return parsedReturn;
|
|
@@ -24,6 +24,7 @@ export interface ErrorConstructor {
|
|
|
24
24
|
|
|
25
25
|
interface FetchOptions {
|
|
26
26
|
headers?: Record<string, string>;
|
|
27
|
+
baseUrl?: string;
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
export class HttpClient {
|
|
@@ -38,8 +39,11 @@ export class HttpClient {
|
|
|
38
39
|
setErrorConstructor(ErrorCls?: ErrorConstructor) {
|
|
39
40
|
this.ErrorCls = ErrorCls;
|
|
40
41
|
}
|
|
42
|
+
#resolveBaseUrl(baseUrl?: string) {
|
|
43
|
+
return (baseUrl ?? this.baseUrl).replace(/\/$/, "");
|
|
44
|
+
}
|
|
41
45
|
async get<Returns = unknown>(url: string, options: FetchOptions = {}): Promise<Returns> {
|
|
42
|
-
const res = await fetch(`${this.baseUrl}${url}`, {
|
|
46
|
+
const res = await fetch(`${this.#resolveBaseUrl(options.baseUrl)}${url}`, {
|
|
43
47
|
headers: { "Content-Type": "application/json", ...options.headers },
|
|
44
48
|
});
|
|
45
49
|
return await this.#readJsonResponse<Returns>(res);
|
|
@@ -55,7 +59,7 @@ export class HttpClient {
|
|
|
55
59
|
options: FetchOptions = {},
|
|
56
60
|
): Promise<Returns> {
|
|
57
61
|
const { body, headers } = this.#makeReqContent(data);
|
|
58
|
-
const res = await fetch(`${this.baseUrl}${url}`, {
|
|
62
|
+
const res = await fetch(`${this.#resolveBaseUrl(options.baseUrl)}${url}`, {
|
|
59
63
|
method: "PUT",
|
|
60
64
|
body,
|
|
61
65
|
headers: { ...headers, ...options.headers },
|
|
@@ -68,7 +72,7 @@ export class HttpClient {
|
|
|
68
72
|
options: FetchOptions = {},
|
|
69
73
|
): Promise<Returns> {
|
|
70
74
|
const { body, headers } = this.#makeReqContent(data);
|
|
71
|
-
const res = await fetch(`${this.baseUrl}${url}`, {
|
|
75
|
+
const res = await fetch(`${this.#resolveBaseUrl(options.baseUrl)}${url}`, {
|
|
72
76
|
method: "POST",
|
|
73
77
|
body,
|
|
74
78
|
headers: { ...headers, ...options.headers },
|
|
@@ -76,7 +80,7 @@ export class HttpClient {
|
|
|
76
80
|
return await this.#readJsonResponse<Returns>(res);
|
|
77
81
|
}
|
|
78
82
|
async delete<Returns = unknown>(url: string, options: FetchOptions = {}): Promise<Returns> {
|
|
79
|
-
const res = await fetch(`${this.baseUrl}${url}`, {
|
|
83
|
+
const res = await fetch(`${this.#resolveBaseUrl(options.baseUrl)}${url}`, {
|
|
80
84
|
method: "DELETE",
|
|
81
85
|
headers: { "Content-Type": "application/json", ...options.headers },
|
|
82
86
|
});
|
package/package.json
CHANGED