...

/

Solution: Design a URL Builder with Conditional Parameters

Solution: Design a URL Builder with Conditional Parameters

Build a fluent URL builder that dynamically serializes query parameters while enforcing the presence of a base path.

We'll cover the following...

Solution explanation

  • Lines 2–6: We initialize an internal state with baseparams, and filters, keeping URL segments separate. The base must be explicitly provided before building.

  • Lines 8–31: We define fluent setter methods for different URL components.

    • withPagewithLimit, and sortBy add pagination and sorting options to params.

    • filterBy stores filter key-value pairs, supporting multiple dynamic filters.

    • Each method returns this, enabling flexible chaining. ...