@azure-net/kit 1.3.0 → 1.3.1
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Options } from 'ky';
|
|
1
|
+
import { type Options, type ResponsePromise } from 'ky';
|
|
2
2
|
export interface IHttpServiceResponse<T = unknown> {
|
|
3
3
|
headers: Record<string, string>;
|
|
4
4
|
status: number;
|
|
@@ -29,16 +29,31 @@ export declare class HttpServiceError<T> implements IHttpServiceError<T> {
|
|
|
29
29
|
export interface IHttpServiceOptions extends Options {
|
|
30
30
|
responseFormat?: 'json' | 'blob' | 'text' | 'arrayBuffer' | 'body';
|
|
31
31
|
}
|
|
32
|
+
export interface IHttpServiceInstance {
|
|
33
|
+
<T>(url: string | URL | Request, options?: IHttpServiceOptions): ResponsePromise<T>;
|
|
34
|
+
get<T>(url: string | URL | Request, options?: IHttpServiceOptions): ResponsePromise<T>;
|
|
35
|
+
post<T>(url: string | URL | Request, options?: IHttpServiceOptions): ResponsePromise<T>;
|
|
36
|
+
put<T>(url: string | URL | Request, options?: IHttpServiceOptions): ResponsePromise<T>;
|
|
37
|
+
delete<T>(url: string | URL | Request, options?: IHttpServiceOptions): ResponsePromise<T>;
|
|
38
|
+
patch<T>(url: string | URL | Request, options?: IHttpServiceOptions): ResponsePromise<T>;
|
|
39
|
+
head(url: string | URL | Request, options?: IHttpServiceOptions): ResponsePromise;
|
|
40
|
+
create(defaultOptions?: IHttpServiceOptions): IHttpServiceInstance;
|
|
41
|
+
extend: (defaultOptions: IHttpServiceOptions | ((parentOptions: IHttpServiceOptions) => IHttpServiceOptions)) => IHttpServiceInstance;
|
|
42
|
+
readonly stop: typeof stop;
|
|
43
|
+
}
|
|
32
44
|
export declare class HttpService {
|
|
33
45
|
private readonly baseUrl;
|
|
34
46
|
private readonly instance;
|
|
35
47
|
private readonly requestHandler?;
|
|
36
48
|
private readonly errorHandler?;
|
|
49
|
+
private readonly globalErrorHandler?;
|
|
37
50
|
constructor(opts: {
|
|
38
51
|
baseUrl: string;
|
|
52
|
+
instance?: IHttpServiceInstance;
|
|
39
53
|
baseOptions?: Options;
|
|
40
|
-
|
|
41
|
-
|
|
54
|
+
onRequest?: (options: Options) => void | Promise<void>;
|
|
55
|
+
onError?: (err: IHttpServiceError) => HttpServiceError<unknown>;
|
|
56
|
+
handleError?: (err: unknown) => Promise<HttpServiceError<unknown>>;
|
|
42
57
|
});
|
|
43
58
|
private prepareOptions;
|
|
44
59
|
private prepareResponse;
|
|
@@ -48,4 +63,5 @@ export declare class HttpService {
|
|
|
48
63
|
delete<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
|
|
49
64
|
put<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
|
|
50
65
|
private handleError;
|
|
66
|
+
private baseErrorHandler;
|
|
51
67
|
}
|
|
@@ -34,11 +34,13 @@ export class HttpService {
|
|
|
34
34
|
instance;
|
|
35
35
|
requestHandler;
|
|
36
36
|
errorHandler;
|
|
37
|
+
globalErrorHandler;
|
|
37
38
|
constructor(opts) {
|
|
38
39
|
this.baseUrl = opts.baseUrl;
|
|
39
|
-
this.instance = ky.create(opts?.baseOptions);
|
|
40
|
-
this.errorHandler = opts?.
|
|
41
|
-
this.requestHandler = opts?.
|
|
40
|
+
this.instance = opts.instance ? opts.instance.create(opts?.baseOptions) : ky.create(opts?.baseOptions);
|
|
41
|
+
this.errorHandler = opts?.onError ?? undefined;
|
|
42
|
+
this.requestHandler = opts?.onRequest ?? undefined;
|
|
43
|
+
this.globalErrorHandler = opts?.handleError ?? undefined;
|
|
42
44
|
}
|
|
43
45
|
async prepareOptions(requestOptions) {
|
|
44
46
|
const options = typeof requestOptions === 'object' ? { ...requestOptions } : {};
|
|
@@ -132,7 +134,10 @@ export class HttpService {
|
|
|
132
134
|
throw await this.handleError(error);
|
|
133
135
|
});
|
|
134
136
|
}
|
|
135
|
-
async handleError(
|
|
137
|
+
async handleError(error) {
|
|
138
|
+
return this.globalErrorHandler ? await this.globalErrorHandler(error) : await this.baseErrorHandler(error);
|
|
139
|
+
}
|
|
140
|
+
async baseErrorHandler(err) {
|
|
136
141
|
let headers;
|
|
137
142
|
let response;
|
|
138
143
|
try {
|