@azure-net/kit 5.0.1 → 5.0.3
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.
|
@@ -63,7 +63,7 @@ export interface HttpInstanceNormalizedRequest {
|
|
|
63
63
|
export type HttpInstanceOnRequest = (request: HttpInstanceNormalizedRequest) => void | Promise<void>;
|
|
64
64
|
export type HttpInstanceOnError = (error: HttpServiceError<unknown>, request: HttpInstanceNormalizedRequest) => void | Promise<void>;
|
|
65
65
|
export type HttpInstanceDoFetch = <T = unknown>(request: HttpInstanceNormalizedRequest) => Promise<IHttpServiceResponse<T>>;
|
|
66
|
-
export interface IHttpInstanceOptions extends Omit<RequestInit, 'headers' | 'method'> {
|
|
66
|
+
export interface IHttpInstanceOptions<T = unknown> extends Omit<RequestInit, 'headers' | 'method'> {
|
|
67
67
|
headers?: NonNullable<RequestInit['headers']> | Record<string, string | undefined>;
|
|
68
68
|
json?: unknown;
|
|
69
69
|
stringifyJson?: (data: unknown) => string | undefined;
|
|
@@ -75,9 +75,10 @@ export interface IHttpInstanceOptions extends Omit<RequestInit, 'headers' | 'met
|
|
|
75
75
|
responseFormat?: 'json' | 'blob' | 'text' | 'arrayBuffer' | 'body';
|
|
76
76
|
onRequest?: HttpInstanceOnRequest;
|
|
77
77
|
onError?: HttpInstanceOnError;
|
|
78
|
+
onResponse?: (response: IHttpServiceResponse<T>) => void | Promise<void>;
|
|
78
79
|
doFetch?: HttpInstanceDoFetch;
|
|
79
80
|
}
|
|
80
|
-
export type HttpInstanceRequestMethod = <T = unknown>(url: string | URL, options?: IHttpInstanceOptions) => Promise<IHttpServiceResponse<T>>;
|
|
81
|
+
export type HttpInstanceRequestMethod = <T = unknown>(url: string | URL, options?: IHttpInstanceOptions<T>) => Promise<IHttpServiceResponse<T>>;
|
|
81
82
|
export interface IHttpServiceInstance {
|
|
82
83
|
get: HttpInstanceRequestMethod;
|
|
83
84
|
post: HttpInstanceRequestMethod;
|
|
@@ -111,6 +111,7 @@ const mergeOptions = (base = {}, extra = {}) => {
|
|
|
111
111
|
searchParams: mergedSearchParams,
|
|
112
112
|
stringifyJson: extra.stringifyJson ?? base.stringifyJson,
|
|
113
113
|
onRequest: extra.onRequest ?? base.onRequest,
|
|
114
|
+
onResponse: extra.onResponse ?? base.onResponse,
|
|
114
115
|
onError: extra.onError ?? base.onError,
|
|
115
116
|
doFetch: extra.doFetch ?? base.doFetch
|
|
116
117
|
};
|
|
@@ -308,7 +309,12 @@ const makeMethod = (method, defaults) => {
|
|
|
308
309
|
}
|
|
309
310
|
const doFetch = merged.doFetch ?? defaults.doFetch ?? defaultDoFetch;
|
|
310
311
|
try {
|
|
311
|
-
|
|
312
|
+
const fetched = await doFetch(normalizedRequest);
|
|
313
|
+
const onResponse = options.onResponse ?? defaults.onResponse;
|
|
314
|
+
if (onResponse) {
|
|
315
|
+
await onResponse(fetched);
|
|
316
|
+
}
|
|
317
|
+
return fetched;
|
|
312
318
|
}
|
|
313
319
|
catch (error) {
|
|
314
320
|
const normalizedError = error instanceof HttpServiceError
|