@azure-net/kit 5.0.0 → 5.0.2
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/infra/http-service/HttpServiceInstance.d.ts +3 -2
- package/dist/infra/http-service/HttpServiceInstance.js +8 -2
- package/dist/shared/app/App.d.ts +2 -0
- package/dist/shared/app/App.js +3 -1
- package/dist/shared/index.d.ts +1 -0
- package/dist/shared/index.js +1 -0
- package/dist/shared/logger/index.d.ts +23 -0
- package/dist/shared/logger/index.js +73 -0
- package/package.json +1 -1
|
@@ -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
|
};
|
|
@@ -213,7 +214,7 @@ const parseResponse = async (response, request) => {
|
|
|
213
214
|
if (!response.ok || parseError) {
|
|
214
215
|
const message = !response.ok
|
|
215
216
|
? `Request failed with status code ${response.status}. ${response.statusText}`
|
|
216
|
-
: (parseError?.message ??
|
|
217
|
+
: (parseError?.message ?? `Internal error. Request failed. ${response?.status ?? ''} ${response?.statusText ?? ''}`);
|
|
217
218
|
throw new HttpServiceError({
|
|
218
219
|
status: response.status,
|
|
219
220
|
data,
|
|
@@ -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
|
package/dist/shared/app/App.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { type ContextData } from '../../external/edges/ServerContext.js';
|
|
|
2
2
|
import { type IServerMiddleware } from './middleware/ServerMiddleware.js';
|
|
3
3
|
import type { Handle, HandleClientError, HandleServerError, RequestEvent, ServerInit } from '@sveltejs/kit';
|
|
4
4
|
import { type IClientMiddleware } from './middleware/ClientMiddleware.js';
|
|
5
|
+
import { useLogger } from '../logger/index.js';
|
|
5
6
|
type MaybePromise<T> = T | Promise<T>;
|
|
6
7
|
type HandleInput = Parameters<Handle>[0];
|
|
7
8
|
type Resolve = HandleInput['resolve'];
|
|
@@ -54,6 +55,7 @@ export interface AppServerErrorContext<TDependencies extends Record<string, unkn
|
|
|
54
55
|
event: RequestEvent;
|
|
55
56
|
status: number;
|
|
56
57
|
message: string;
|
|
58
|
+
useLogger: typeof useLogger;
|
|
57
59
|
}
|
|
58
60
|
export type AppUniversalLifecycleCallback<TDependencies extends Record<string, unknown>> = (context: AppUniversalLifecycleContext<TDependencies>) => MaybePromise<void>;
|
|
59
61
|
export type AppClientLifecycleCallback<TDependencies extends Record<string, unknown>> = (context: AppClientLifecycleContext<TDependencies>) => MaybePromise<void>;
|
package/dist/shared/app/App.js
CHANGED
|
@@ -5,6 +5,7 @@ import { AzureNetKitInternalError } from '../app-error/AppError.js';
|
|
|
5
5
|
import { executeServerMiddlewares } from './middleware/ServerMiddleware.js';
|
|
6
6
|
import { beforeNavigate } from '$app/navigation';
|
|
7
7
|
import { executeClientMiddlewares } from './middleware/ClientMiddleware.js';
|
|
8
|
+
import { useLogger } from '../logger/index.js';
|
|
8
9
|
const DEFAULT_APP_NAME = '__AzureNetKitGlobalAppContainer__';
|
|
9
10
|
const clientLifecycleFlags = new WeakMap();
|
|
10
11
|
const serverInitLifecycleFlags = new WeakMap();
|
|
@@ -189,7 +190,8 @@ export const createApp = (callback, name = DEFAULT_APP_NAME) => {
|
|
|
189
190
|
error,
|
|
190
191
|
event,
|
|
191
192
|
status,
|
|
192
|
-
message
|
|
193
|
+
message,
|
|
194
|
+
useLogger
|
|
193
195
|
});
|
|
194
196
|
};
|
|
195
197
|
const runClientError = ({ error, event, status, message }) => {
|
package/dist/shared/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { AppEvents } from './event-bus/index.js';
|
|
|
3
3
|
export { ClassMirror } from './class-mirror/index.js';
|
|
4
4
|
export { createBoundaryProvider, cleanupProvider, type ProviderSettings } from './boundary-provider/index.js';
|
|
5
5
|
export { createApp, type AppContainer, type CreateAppInstance, type IServerMiddleware, type IClientMiddleware } from './app/index.js';
|
|
6
|
+
export { useLogger, LoggerErrors, type ILoggerError, type ILoggerSettings } from './logger/index.js';
|
package/dist/shared/index.js
CHANGED
|
@@ -3,3 +3,4 @@ export { AppEvents } from './event-bus/index.js';
|
|
|
3
3
|
export { ClassMirror } from './class-mirror/index.js';
|
|
4
4
|
export { createBoundaryProvider, cleanupProvider } from './boundary-provider/index.js';
|
|
5
5
|
export { createApp } from './app/index.js';
|
|
6
|
+
export { useLogger, LoggerErrors } from './logger/index.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AppError, type IAppError } from '../app-error/index.js';
|
|
2
|
+
export declare enum LoggerErrors {
|
|
3
|
+
AzureNetKitInternal = 0,
|
|
4
|
+
AzureNetAppError = 1,
|
|
5
|
+
ProjectError = 2,
|
|
6
|
+
UnknownError = 3
|
|
7
|
+
}
|
|
8
|
+
export interface ILoggerError {
|
|
9
|
+
message: string;
|
|
10
|
+
type: LoggerErrors;
|
|
11
|
+
original: Error | IAppError | AppError;
|
|
12
|
+
}
|
|
13
|
+
export interface ILoggerSettings {
|
|
14
|
+
collector?: {
|
|
15
|
+
request: (error: ILoggerError) => Request;
|
|
16
|
+
onError?: (error: unknown) => void;
|
|
17
|
+
};
|
|
18
|
+
prefix?: string;
|
|
19
|
+
joinToLog?: (error: ILoggerError) => string;
|
|
20
|
+
filterLog?: (error: ILoggerError) => boolean;
|
|
21
|
+
includeOnly?: LoggerErrors[];
|
|
22
|
+
}
|
|
23
|
+
export declare const useLogger: (error: unknown, settings: ILoggerSettings) => void;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { AppError, AzureNetKitInternalError } from '../app-error/index.js';
|
|
2
|
+
import { BROWSER } from '../../external/tools/index.js';
|
|
3
|
+
import { RequestContext } from '../../external/edges/ServerContext.js';
|
|
4
|
+
export var LoggerErrors;
|
|
5
|
+
(function (LoggerErrors) {
|
|
6
|
+
LoggerErrors[LoggerErrors["AzureNetKitInternal"] = 0] = "AzureNetKitInternal";
|
|
7
|
+
LoggerErrors[LoggerErrors["AzureNetAppError"] = 1] = "AzureNetAppError";
|
|
8
|
+
LoggerErrors[LoggerErrors["ProjectError"] = 2] = "ProjectError";
|
|
9
|
+
LoggerErrors[LoggerErrors["UnknownError"] = 3] = "UnknownError";
|
|
10
|
+
})(LoggerErrors || (LoggerErrors = {}));
|
|
11
|
+
const prepareError = (error) => {
|
|
12
|
+
if (error instanceof AppError || (error && typeof error === 'object' && 'appErrorConvert' in error)) {
|
|
13
|
+
const typedError = error;
|
|
14
|
+
return {
|
|
15
|
+
original: typedError,
|
|
16
|
+
message: `[AppError ${typedError.type}] ${typedError.message}`,
|
|
17
|
+
type: LoggerErrors.AzureNetAppError
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
if (error instanceof AzureNetKitInternalError) {
|
|
21
|
+
return {
|
|
22
|
+
original: error,
|
|
23
|
+
message: `[AzureNetInternalError] ${error.message}`,
|
|
24
|
+
type: LoggerErrors.AzureNetKitInternal
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (error instanceof Error) {
|
|
28
|
+
return {
|
|
29
|
+
original: error,
|
|
30
|
+
message: error.message,
|
|
31
|
+
type: LoggerErrors.ProjectError
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
original: new Error('Unknown error occurred'),
|
|
36
|
+
message: 'Unknown error occurred',
|
|
37
|
+
type: LoggerErrors.UnknownError
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
const getFetcher = () => {
|
|
41
|
+
if (BROWSER)
|
|
42
|
+
return fetch;
|
|
43
|
+
try {
|
|
44
|
+
return RequestContext.current()?.event?.fetch ?? fetch;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return fetch;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
export const useLogger = (error, settings) => {
|
|
51
|
+
const preparedError = prepareError(error);
|
|
52
|
+
if (settings.filterLog && settings.filterLog(preparedError)) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (settings.includeOnly && !settings.includeOnly.includes(preparedError.type)) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const prefix = settings?.prefix ?? '[Logger]';
|
|
59
|
+
const errorMessage = prefix + preparedError.message + (settings?.joinToLog ? settings.joinToLog(preparedError) : '');
|
|
60
|
+
console.log(errorMessage);
|
|
61
|
+
if (settings.collector) {
|
|
62
|
+
const fetcher = getFetcher();
|
|
63
|
+
try {
|
|
64
|
+
void fetcher(settings.collector.request(preparedError)).catch((err) => {
|
|
65
|
+
settings.collector?.onError?.(err);
|
|
66
|
+
return;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
settings.collector.onError?.(err);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|