Pass the publicBaseUrl to the template parser#2011
Conversation
653e06b to
b6f2509
Compare
There was a problem hiding this comment.
Pull request overview
This PR adds support for a publicBaseUrl parameter to the template parser, allowing the baseUrl helper to return a public base URL instead of constructing one from the environment's protocol, hostname, and port. This is useful when a mock server is deployed on a public domain with a URL that differs from its internal configuration.
Key changes:
- Added
publicBaseUrloptional parameter to the template parser and related functions - Updated the
baseUrlhelper to prioritizepublicBaseUrlwhen provided - Threaded the
publicBaseUrlparameter through all template parsing call sites
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
packages/commons-server/test/specs/templating-helpers/request-helpers.test.ts |
Added test case to verify baseUrl helper returns the publicBaseUrl when provided with an endpoint prefix |
packages/commons-server/src/libs/templating-helpers/request-helpers.ts |
Modified RequestHelpers function to accept publicBaseUrl parameter and updated baseUrl helper to use it when available |
packages/commons-server/src/libs/template-parser.ts |
Added publicBaseUrl as an optional parameter to the TemplateParser function and passed it to RequestHelpers |
packages/commons-server/src/libs/server/ws.ts |
Added publicBaseUrl to ServerContext type and updated WebSocket instance to pass it to template parsing |
packages/commons-server/src/libs/server/server.ts |
Updated all template parsing call sites to pass this.options.publicBaseUrl |
packages/commons-server/src/libs/response-rules-interpreter.ts |
Added publicBaseUrl parameter to constructor and passed it to template parsing |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| : ''; | ||
|
|
||
| if (publicBaseUrl) { | ||
| return `${publicBaseUrl}${prefix}`; |
There was a problem hiding this comment.
The dedupSlashes function uses a regex that replaces all consecutive slashes (2 or more) with a single slash. This will incorrectly modify the protocol part of the URL (e.g., https:// becomes https:/).
When publicBaseUrl is 'https://subdomain.mockoon.app:12345/' and prefix is /myapi, the concatenation results in 'https://subdomain.mockoon.app:12345//myapi'. The dedupSlashes function will then replace both // in the protocol and the duplicate slash before myapi, breaking the URL.
Consider removing the trailing slash from publicBaseUrl before concatenating, or use a more sophisticated deduplication that preserves the protocol part of URLs.
| return `${publicBaseUrl}${prefix}`; | |
| // Remove trailing slash from publicBaseUrl and leading slash from prefix, then join with single slash | |
| const sanitizedBaseUrl = publicBaseUrl.replace(/\/+$/, ''); | |
| const sanitizedPrefix = prefix.replace(/^\/+/, ''); | |
| return sanitizedPrefix | |
| ? `${sanitizedBaseUrl}/${sanitizedPrefix}` | |
| : sanitizedBaseUrl; |
packages/commons-server/test/specs/templating-helpers/request-helpers.test.ts
Show resolved
Hide resolved
b6f2509 to
7352438
Compare
Allow the baseUrl helper to return a public base URL instead of a constructed one (when the mock is deployed on a public domain for example). Closes #1488
Allow the baseUrl helper to return a public base URL instead of a constructed one (when the mock is deployed on a public domain for example).
Closes #1488
Technical implementation details
Checklist
Closes #{issue_number}