Skip to content

Commit

Permalink
Merge pull request #133 from sima-land/38-proxy-enhance
Browse files Browse the repository at this point in the history
Шаг 82 #38 Доработки проксирования
  • Loading branch information
krutoo authored Apr 22, 2024
2 parents 86bfba2 + 515f9d5 commit b921668
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
9 changes: 8 additions & 1 deletion examples/node/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', ''),
}));
}),
);

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export type {
ProxyOptions,
Handler,
Enhancer,
Middleware,
Expand All @@ -8,6 +7,7 @@ export type {
FailLogData,
LogHandler,
LogHandlerFactory,
ProxyOptions,
EitherResponse,
ResponseDone,
ResponseFail,
Expand Down
3 changes: 2 additions & 1 deletion src/preset/node/providers/main-express-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"');
Expand All @@ -33,6 +33,7 @@ export function provideMainExpressApp(resolve: Resolve): express.Application {
createProxyMiddleware({
target,
changeOrigin: true,
pathRewrite,
pathFilter: inputPath => proxyPaths.some(proxyPath => inputPath.startsWith(proxyPath)),
}),
);
Expand Down

0 comments on commit b921668

Please sign in to comment.