Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fast-pots-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sap-ux/ui5-proxy-middleware': patch
'@sap-ux/ui5-config': patch
---

fix: pathReplace not taken into account or truncated in case of nested router instances
1 change: 1 addition & 0 deletions packages/ui5-config/src/types/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { AuthenticationType } from '@sap-ux/store';
export interface UI5ProxyConfigTarget {
path: string | string[];
url: string;
pathReplace?: string;
}

export interface UI5ProxyConfig {
Expand Down
9 changes: 8 additions & 1 deletion packages/ui5-proxy-middleware/src/base/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ export function directLoadProxy(

/**
* Create a rewrite based on the provided configuration.
* This will either replace the configured path with the provided pathReplace value or
* will prepend the UI5 version to the path.
* It also ensures that the path from the http-proxy-middleware starts with the
* config path in case of nested router instances.
*
* @param config proxy configuration
* @param ui5Ver UI5 version string
Expand All @@ -359,7 +363,10 @@ export function getPathRewrite(config: ProxyConfig, ui5Ver: string): Options['pa
if (config.pathReplace) {
// Remove trailing slash from pathReplace if present
const sanitizedPathReplace = config.pathReplace?.replace(/\/$/, '');
return (path: string) => path.replace(config.path, sanitizedPathReplace);
return (path: string) =>
path.startsWith(config.path)
? path.replace(config.path, sanitizedPathReplace)
: sanitizedPathReplace + path;
}
return (path: string) => (path.startsWith(config.path) ? ui5Ver + path : ui5Ver + config.path + path);
}
1 change: 1 addition & 0 deletions packages/ui5-proxy-middleware/src/ui5/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ module.exports = async ({ resources, options }: MiddlewareParameters<UI5ProxyCon
path: ui5Path,
url: envUI5Url || ui5.url,
version: ui5Version,
pathReplace: ui5.pathReplace,
proxy: config.proxy
};

Expand Down
11 changes: 11 additions & 0 deletions packages/ui5-proxy-middleware/test/base/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,17 @@ describe('utils', () => {
expect((rewrite as Function)('/mypath')).toEqual('this/path/should/rewrite/mypath');
});

test('custom pathRewrite (simulate missing path because of nested router instances)', () => {
const config = {
pathReplace: 'this/path/should/rewrite/mypath',
path: '/mypath',
url: 'https://example.example'
};
const ui5Ver = '';
const rewrite = getPathRewrite(config, ui5Ver);
expect((rewrite as Function)('/test.ts')).toEqual('this/path/should/rewrite/mypath/test.ts');
});

test('handle pathRewrite with trailing slash', () => {
const config = {
path: '/resources',
Expand Down
17 changes: 17 additions & 0 deletions packages/ui5-proxy-middleware/test/ui5/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ describe('middleware', () => {
);
});

test('pathReplace', async () => {
await getTestServer({
...config,
ui5: {
path: '/resources',
url: 'http://ui5.example',
pathReplace: '/new-resources'
}
});
expect(ui5ProxySpy).toHaveBeenCalledWith(
expect.objectContaining({ pathReplace: '/new-resources' }),
expect.objectContaining({}),
undefined,
expect.any(ToolsLogger)
);
});

test('secure', async () => {
await getTestServer({
...config,
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

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

Loading