@azure-net/kit 1.2.7 → 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.
- package/dist/core/infra/httpService/HttpService.d.ts +19 -3
- package/dist/core/infra/httpService/HttpService.js +9 -4
- package/dist/core/infra/query/QueryBuilder.d.ts +1 -0
- package/dist/core/shared/boundaryProvider/Provider.d.ts +4 -2
- package/dist/core/shared/boundaryProvider/Provider.js +42 -6
- package/package.json +1 -1
|
@@ -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 {
|
|
@@ -11,9 +11,11 @@ export interface ProviderWithType<T extends ServiceMap> {
|
|
|
11
11
|
type ProviderFactory<T extends ServiceMap, D extends Record<string, ProviderWithType<ServiceMap>>> = (context: {
|
|
12
12
|
[K in keyof D]: InferProviderType<D[K]>;
|
|
13
13
|
}) => T;
|
|
14
|
-
interface
|
|
14
|
+
export interface ProviderSettings<T extends ServiceMap, D extends Record<string, ProviderWithType<ServiceMap>>> {
|
|
15
15
|
dependsOn?: D;
|
|
16
|
+
boot?: (services: ResolvedServices<T>) => void | Promise<void>;
|
|
17
|
+
register: ProviderFactory<T, D>;
|
|
16
18
|
}
|
|
17
|
-
export declare const createBoundaryProvider: <T extends ServiceMap, D extends Record<string, ProviderWithType<ServiceMap>>>(name: string,
|
|
19
|
+
export declare const createBoundaryProvider: <T extends ServiceMap, D extends Record<string, ProviderWithType<ServiceMap>>>(name: string, settings: ProviderSettings<T, D>) => ProviderWithType<T>;
|
|
18
20
|
export declare function cleanupProvider(name: string): void;
|
|
19
21
|
export {};
|
|
@@ -3,6 +3,7 @@ import { EnvironmentUtil } from 'azure-net-tools';
|
|
|
3
3
|
const clientCache = new Map();
|
|
4
4
|
const factoriesCache = new WeakMap();
|
|
5
5
|
const providerProxyCache = new Map();
|
|
6
|
+
const clientBootFlags = new Map();
|
|
6
7
|
const getProviderCache = (providerName) => {
|
|
7
8
|
if (EnvironmentUtil.isBrowser) {
|
|
8
9
|
if (!clientCache.has(providerName)) {
|
|
@@ -26,8 +27,34 @@ const getProviderCache = (providerName) => {
|
|
|
26
27
|
return providers.get(providerName);
|
|
27
28
|
}
|
|
28
29
|
};
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
const getBootFlag = (providerName) => {
|
|
31
|
+
if (EnvironmentUtil.isBrowser) {
|
|
32
|
+
return clientBootFlags.get(providerName) ?? false;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
const context = RequestContext.current();
|
|
36
|
+
if (!context.data.bootFlags) {
|
|
37
|
+
context.data.bootFlags = new Map();
|
|
38
|
+
}
|
|
39
|
+
const bootFlags = context.data.bootFlags;
|
|
40
|
+
return bootFlags.get(providerName) ?? false;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const setBootFlag = (providerName, value) => {
|
|
44
|
+
if (EnvironmentUtil.isBrowser) {
|
|
45
|
+
clientBootFlags.set(providerName, value);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
const context = RequestContext.current();
|
|
49
|
+
if (!context.data.bootFlags) {
|
|
50
|
+
context.data.bootFlags = new Map();
|
|
51
|
+
}
|
|
52
|
+
const bootFlags = context.data.bootFlags;
|
|
53
|
+
bootFlags.set(providerName, value);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
export const createBoundaryProvider = (name, settings) => {
|
|
57
|
+
const { dependsOn = {}, boot, register } = settings;
|
|
31
58
|
const providerFn = () => {
|
|
32
59
|
if (EnvironmentUtil.isBrowser && providerProxyCache.has(name)) {
|
|
33
60
|
return providerProxyCache.get(name);
|
|
@@ -37,8 +64,8 @@ export const createBoundaryProvider = (name, services, options) => {
|
|
|
37
64
|
const getFactories = () => {
|
|
38
65
|
if (factories)
|
|
39
66
|
return factories;
|
|
40
|
-
if (factoriesCache.has(
|
|
41
|
-
factories = factoriesCache.get(
|
|
67
|
+
if (factoriesCache.has(register)) {
|
|
68
|
+
factories = factoriesCache.get(register);
|
|
42
69
|
return factories;
|
|
43
70
|
}
|
|
44
71
|
const deps = {};
|
|
@@ -54,9 +81,9 @@ export const createBoundaryProvider = (name, services, options) => {
|
|
|
54
81
|
}
|
|
55
82
|
});
|
|
56
83
|
}
|
|
57
|
-
factories =
|
|
84
|
+
factories = register(deps);
|
|
58
85
|
if (EnvironmentUtil.isBrowser) {
|
|
59
|
-
factoriesCache.set(
|
|
86
|
+
factoriesCache.set(register, factories);
|
|
60
87
|
}
|
|
61
88
|
return factories;
|
|
62
89
|
};
|
|
@@ -90,6 +117,14 @@ export const createBoundaryProvider = (name, services, options) => {
|
|
|
90
117
|
if (EnvironmentUtil.isBrowser) {
|
|
91
118
|
providerProxyCache.set(name, providerProxy);
|
|
92
119
|
}
|
|
120
|
+
if (boot && !getBootFlag(name)) {
|
|
121
|
+
setBootFlag(name, true);
|
|
122
|
+
getFactories();
|
|
123
|
+
const bootResult = boot(providerProxy);
|
|
124
|
+
if (bootResult instanceof Promise) {
|
|
125
|
+
bootResult.catch((err) => console.error(`Error in boot for provider '${name}':`, err));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
93
128
|
return providerProxy;
|
|
94
129
|
};
|
|
95
130
|
return providerFn;
|
|
@@ -114,6 +149,7 @@ export function cleanupProvider(name) {
|
|
|
114
149
|
void cleanupCache(cache);
|
|
115
150
|
}
|
|
116
151
|
providerProxyCache.delete(name);
|
|
152
|
+
clientBootFlags.delete(name);
|
|
117
153
|
}
|
|
118
154
|
else {
|
|
119
155
|
const context = RequestContext.current();
|