diff --git a/.changeset/fast-pots-sit.md b/.changeset/fast-pots-sit.md new file mode 100644 index 00000000000..63afe98354f --- /dev/null +++ b/.changeset/fast-pots-sit.md @@ -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 diff --git a/packages/ui5-config/src/types/middlewares.ts b/packages/ui5-config/src/types/middlewares.ts index 1a3ab849c66..483c702720e 100644 --- a/packages/ui5-config/src/types/middlewares.ts +++ b/packages/ui5-config/src/types/middlewares.ts @@ -2,6 +2,7 @@ import type { AuthenticationType } from '@sap-ux/store'; export interface UI5ProxyConfigTarget { path: string | string[]; url: string; + pathReplace?: string; } export interface UI5ProxyConfig { diff --git a/packages/ui5-proxy-middleware/src/base/utils.ts b/packages/ui5-proxy-middleware/src/base/utils.ts index 8eb5013ddf8..6270e2064c8 100644 --- a/packages/ui5-proxy-middleware/src/base/utils.ts +++ b/packages/ui5-proxy-middleware/src/base/utils.ts @@ -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 @@ -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); } diff --git a/packages/ui5-proxy-middleware/src/ui5/middleware.ts b/packages/ui5-proxy-middleware/src/ui5/middleware.ts index da184812b68..c6aaff0c306 100644 --- a/packages/ui5-proxy-middleware/src/ui5/middleware.ts +++ b/packages/ui5-proxy-middleware/src/ui5/middleware.ts @@ -100,6 +100,7 @@ module.exports = async ({ resources, options }: MiddlewareParameters { 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', diff --git a/packages/ui5-proxy-middleware/test/ui5/middleware.test.ts b/packages/ui5-proxy-middleware/test/ui5/middleware.test.ts index d92fcaf0bb3..3ee1e796e70 100644 --- a/packages/ui5-proxy-middleware/test/ui5/middleware.test.ts +++ b/packages/ui5-proxy-middleware/test/ui5/middleware.test.ts @@ -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, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 29602276fb1..1a49774cea6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11136,7 +11136,7 @@ packages: dependencies: '@typescript-eslint/typescript-estree': 5.62.0(typescript@5.9.2) '@typescript-eslint/utils': 5.62.0(eslint@8.57.0)(typescript@5.9.2) - debug: 4.3.7 + debug: 4.4.1 eslint: 8.57.0 tsutils: 3.21.0(typescript@5.9.2) typescript: 5.9.2