Skip to content

Commit

Permalink
Merge pull request #132 from sima-land/38-micro-refactor
Browse files Browse the repository at this point in the history
 Шаг 81 #38 Различные мелкие доработки
  • Loading branch information
krutoo authored Apr 10, 2024
2 parents e5b9d62 + 9802d17 commit 86bfba2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 28 deletions.
9 changes: 2 additions & 7 deletions src/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type {
ProxyOptions,
Handler,
Enhancer,
Middleware,
Expand All @@ -13,12 +14,6 @@ export type {
ResponseErrorInit,
} from './types';
export { configureFetch, applyMiddleware } from '@krutoo/fetch-tools';
export {
type ProxyOptions,
log,
proxy,
defaultHeaders,
validateStatus,
} from '@krutoo/fetch-tools/middleware';
export { log, proxy, defaultHeaders, validateStatus } from '@krutoo/fetch-tools/middleware';
export { StatusError, ResponseError } from './errors';
export { FetchUtil } from './utils';
1 change: 1 addition & 0 deletions src/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type {
FailLogData,
LogHandler,
LogHandlerFactory,
ProxyOptions,
} from '@krutoo/fetch-tools/middleware';

export type URLSearchParamsInit = Record<string, string | number | boolean | undefined | null>;
Expand Down
2 changes: 1 addition & 1 deletion src/log/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorDetails, BreadcrumbDetails } from './types';
import type { ErrorDetails, BreadcrumbDetails } from './types';

/**
* Ошибка с данными.
Expand Down
10 changes: 5 additions & 5 deletions src/preset/bun-handler/providers/handler-main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ export function provideHandlerMain(resolve: Resolve) {
const enhancer = applyMiddleware(
// ВАЖНО: прерываем исходящие в рамках обработчика http-запросы
async (request, next) => {
const response = await next(request);

abortController.abort();

return response;
try {
return await next(request);
} finally {
abortController.abort();
}
},
);

Expand Down
20 changes: 9 additions & 11 deletions src/preset/server/utils/get-serve-error-logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ export function getServeErrorLogging(logger: Logger): Middleware {
logger.error(
new DetailedError(String(error), {
level: 'error',
context: [
{
key: 'Incoming request details',
data: {
url: FetchUtil.withoutParams(request.url),
method: request.method,
headers: Object.fromEntries(request.headers.entries()),
params: Object.fromEntries(new URL(request.url).searchParams.entries()),
// @todo data
},
context: {
key: 'Incoming request details',
data: {
url: FetchUtil.withoutParams(request.url),
method: request.method,
headers: Object.fromEntries(request.headers.entries()),
params: Object.fromEntries(new URL(request.url).searchParams.entries()),
// @todo data
},
],
},
}),
);
},
Expand Down
7 changes: 3 additions & 4 deletions src/preset/server/utils/get-serve-logging.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Middleware } from '../../../http';
import type { Logger } from '../../../log';
import { toMilliseconds } from '../../../utils';
import { getClientIp } from './get-client-ip';

/**
Expand All @@ -10,7 +9,7 @@ import { getClientIp } from './get-client-ip';
*/
export function getServeLogging(logger: Logger): Middleware {
return async (request, next) => {
const start = process.hrtime.bigint();
const start = performance.now();
const clientIp = getClientIp(request);

logger.info({
Expand All @@ -22,15 +21,15 @@ export function getServeLogging(logger: Logger): Middleware {

const response = await next(request);

const finish = process.hrtime.bigint();
const finish = performance.now();

logger.info({
type: 'http.response[outgoing]',
route: request.url,
method: request.method,
status: response.status,
remote_ip: clientIp,
latency: toMilliseconds(finish - start),
latency: finish - start,
});

return response;
Expand Down

0 comments on commit 86bfba2

Please sign in to comment.