Skip to content

Pass the publicBaseUrl to the template parser#2011

Merged
255kb merged 1 commit intomainfrom
feature/1488-baseurl
Dec 12, 2025
Merged

Pass the publicBaseUrl to the template parser#2011
255kb merged 1 commit intomainfrom
feature/1488-baseurl

Conversation

@255kb
Copy link
Member

@255kb 255kb commented Dec 12, 2025

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

  • data migration added (@mockoon/commons)
  • commons lib tests added (@mockoon/commons)
  • commons-server lib tests added (@mockoon/commons-server)
  • CLI tests added (@mockoon/cli)
  • desktop UI automated tests added (@mockoon/app)

Closes #{issue_number}

Copilot AI review requested due to automatic review settings December 12, 2025 13:27
@255kb 255kb force-pushed the feature/1488-baseurl branch from 653e06b to b6f2509 Compare December 12, 2025 13:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 publicBaseUrl optional parameter to the template parser and related functions
  • Updated the baseUrl helper to prioritize publicBaseUrl when provided
  • Threaded the publicBaseUrl parameter 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}`;
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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;

Copilot uses AI. Check for mistakes.
@255kb 255kb force-pushed the feature/1488-baseurl branch from b6f2509 to 7352438 Compare December 12, 2025 13:44
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
@255kb 255kb merged commit e5d8722 into main Dec 12, 2025
15 checks passed
@255kb 255kb deleted the feature/1488-baseurl branch December 15, 2025 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Helper {{baseUrl}} is not returning the container port

2 participants