From 515f9d59eaffca8af4b645f9eda191aa3d42592d Mon Sep 17 00:00:00 2001 From: krutoo Date: Mon, 22 Apr 2024 11:01:02 +0500 Subject: [PATCH] #38 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `deps`: обновлен пакет `@krutoo/fetch-tools` (minor) - `examples/node`: добавлен пример проксирования (patch) --- examples/node/src/app/index.ts | 9 ++++++++- package-lock.json | 14 +++++++------- package.json | 2 +- src/http/index.ts | 2 +- src/preset/node/providers/main-express-app.ts | 3 ++- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/examples/node/src/app/index.ts b/examples/node/src/app/index.ts index d59984a..69246fe 100644 --- a/examples/node/src/app/index.ts +++ b/examples/node/src/app/index.ts @@ -12,8 +12,15 @@ export function MainApp() { // используем пресет "node" с базовыми компонентами, такими как logger и тд app.preset( PresetNode(({ override }) => { - // переопределяем провайдеры пресета + // переопределяем компонент маршрутов override(TOKEN.Lib.Express.pageRoutes, providePageRoutes); + + // добавляем проксирование + override(TOKEN.Lib.Http.Serve.Proxy.config, () => ({ + filter: '/api', + target: 'https://jsonplaceholder.typicode.com/', + pathRewrite: pathname => pathname.replace('/api', ''), + })); }), ); diff --git a/package-lock.json b/package-lock.json index f61e33b..267a577 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "Apache-2.0", "dependencies": { "@humanwhocodes/env": "^3.0.2", - "@krutoo/fetch-tools": "^0.0.15", + "@krutoo/fetch-tools": "^0.0.16", "@opentelemetry/api": "^1.4.1", "@opentelemetry/exporter-prometheus": "^0.38.0", "@opentelemetry/exporter-trace-otlp-http": "^0.39.1", @@ -3674,9 +3674,9 @@ } }, "node_modules/@krutoo/fetch-tools": { - "version": "0.0.15", - "resolved": "https://registry.npmjs.org/@krutoo/fetch-tools/-/fetch-tools-0.0.15.tgz", - "integrity": "sha512-MCPqnnWvF6jlbiwFmvVrkxATxWfQIUWMyBxvEj+2TueIBR8W0DQFpU3Is4X1Cie0oWzjBaaT8e3BfuYrVcGQsA==" + "version": "0.0.16", + "resolved": "https://registry.npmjs.org/@krutoo/fetch-tools/-/fetch-tools-0.0.16.tgz", + "integrity": "sha512-T4FubtKUtmTCmC9K1aPcw9jpO/K0IsFILQdu/6GNBZezV5ERhHwUsnVa9WG7Hjuhg00L1ggMwSxyNPsAbpDuZg==" }, "node_modules/@nicolo-ribaudo/chokidar-2": { "version": "2.1.8-no-fsevents.3", @@ -18532,9 +18532,9 @@ } }, "@krutoo/fetch-tools": { - "version": "0.0.15", - "resolved": "https://registry.npmjs.org/@krutoo/fetch-tools/-/fetch-tools-0.0.15.tgz", - "integrity": "sha512-MCPqnnWvF6jlbiwFmvVrkxATxWfQIUWMyBxvEj+2TueIBR8W0DQFpU3Is4X1Cie0oWzjBaaT8e3BfuYrVcGQsA==" + "version": "0.0.16", + "resolved": "https://registry.npmjs.org/@krutoo/fetch-tools/-/fetch-tools-0.0.16.tgz", + "integrity": "sha512-T4FubtKUtmTCmC9K1aPcw9jpO/K0IsFILQdu/6GNBZezV5ERhHwUsnVa9WG7Hjuhg00L1ggMwSxyNPsAbpDuZg==" }, "@nicolo-ribaudo/chokidar-2": { "version": "2.1.8-no-fsevents.3", diff --git a/package.json b/package.json index f3d27ff..fcb93c6 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ }, "dependencies": { "@humanwhocodes/env": "^3.0.2", - "@krutoo/fetch-tools": "^0.0.15", + "@krutoo/fetch-tools": "^0.0.16", "@opentelemetry/api": "^1.4.1", "@opentelemetry/exporter-prometheus": "^0.38.0", "@opentelemetry/exporter-trace-otlp-http": "^0.39.1", diff --git a/src/http/index.ts b/src/http/index.ts index 2837335..875c2e2 100644 --- a/src/http/index.ts +++ b/src/http/index.ts @@ -1,5 +1,4 @@ export type { - ProxyOptions, Handler, Enhancer, Middleware, @@ -8,6 +7,7 @@ export type { FailLogData, LogHandler, LogHandlerFactory, + ProxyOptions, EitherResponse, ResponseDone, ResponseFail, diff --git a/src/preset/node/providers/main-express-app.ts b/src/preset/node/providers/main-express-app.ts index f980ef9..886605d 100644 --- a/src/preset/node/providers/main-express-app.ts +++ b/src/preset/node/providers/main-express-app.ts @@ -21,7 +21,7 @@ export function provideMainExpressApp(resolve: Resolve): express.Application { if (config.env === 'development' && proxyConfig) { const proxyConfigs = Array.isArray(proxyConfig) ? proxyConfig : [proxyConfig]; - for (const { target, filter } of proxyConfigs) { + for (const { target, filter, pathRewrite } of proxyConfigs) { // ВАЖНО: так как не можем предоставить web-интерфейс Request бросаем ошибку if (typeof filter === 'function') { throw new Error('Currently function is not supported for proxy "filter"'); @@ -33,6 +33,7 @@ export function provideMainExpressApp(resolve: Resolve): express.Application { createProxyMiddleware({ target, changeOrigin: true, + pathRewrite, pathFilter: inputPath => proxyPaths.some(proxyPath => inputPath.startsWith(proxyPath)), }), );