DEV Community

bugudiramu
bugudiramu

Posted on

A Comprehensive Guide to URLSearchParams in TypeScript 🚀

In the ever-evolving landscape of APIs, the quest for specific, filtered data is a common journey. This guide unveils a crucial technique for achieving this feat: the strategic use of searchable parameters in a GET request.

The Basics

When pursuing filtered data, two primary methods wield influence:

  1. POST Request with Body
  2. GET Request with Searchable Parameters

Today, our focus is on the latter.

The Code Walkthrough

Let's dissect a TypeScript example, exploring how to effectively filter data through a GET request.

Initial Implementation

import axios from "axios";

// Interface for the API request format
interface GetUsersRequest {
  id?: string;
  name?: string;
  username?: string;
  email?: string;
}

class UserService {
  async getUsers(payload: GetUsersRequest) {
    const searchParams: Record<string, any> = new URLSearchParams();

    // Append parameters if they exist in the payload
    if (!!payload.id) {
      searchParams.append("id", payload.id);
    }
    if (!!payload.name) {
      searchParams.append("name", payload.name);
    }
    if (!!payload.username) {
      searchParams.append("username", payload.username);
    }
    if (!!payload.email) {
      searchParams.append("email", payload.email);
    }

    // Construct the base URL
    const baseUrl = new URL("https://jsonplaceholder.typicode.com/users");

    // Attach search parameters to the base URL
    baseUrl.search = searchParams.toString();

    return await axios({
      url: baseUrl.toString(),
      method: "GET",
    });
  }
}

// Instantiate the UserService
const userService = new UserService();

const getUsersRequest: GetUsersRequest = {
  id: "1",
  name: "John",
};

userService
  .getUsers(getUsersRequest)
  .then((users) => {
    console.log("Users:", users);
  })
  .catch((error) => {
    console.error("Error:", error);
  });
Enter fullscreen mode Exit fullscreen mode

Can we optimize & write better code? Absolutely! Below is the implementation.

Optimized Solution

import axios from "axios";

// Improved API request structure with mandatory parameters
interface GetUsersRequest {
  id: string;
  name: string;
  username: string;
  email: string;
}

class UserService {
  async getUsers(payload: Partial<GetUsersRequest>) {
    // Added proper type for searchParams
    const searchParams: URLSearchParams = new URLSearchParams();

    // Destructure parameters from the payload
    const { id, name, username, email } = payload;

    const dataToAppend = [
      {
        key: "id",
        value: id,
      },
      {
        key: "name",
        value: name,
      },
      {
        key: "username",
        value: username,
      },
      {
        key: "email",
        value: email,
      },
    ];

    // Append parameters if they exist
    dataToAppend.forEach((item) => {
      if (!!item.value) {
        searchParams.append(item.key, item.value);
      }
    });

    // Construct the base URL
    const baseUrl = new URL("https://jsonplaceholder.typicode.com/users");

    // Attach search parameters to the URL
    baseUrl.search = searchParams.toString();

    return await axios({
      url: baseUrl.toString(),
      method: "GET",
    });
  }
}

const userService = new UserService();

// Define the parameters for the API request using Partial type
const getUsersRequest: Partial<GetUsersRequest> = {
  id: "1",
  name: "John",
};

userService
  .getUsers(getUsersRequest)
  .then((users) => {
    console.log("Users:", users);
  })
  .catch((error) => {
    console.error("Error:", error);
  });
Enter fullscreen mode Exit fullscreen mode

Exploring the Changes 🕵️‍♂️

In the optimized solution, the TypeScript utility Partial is harnessed for the API request structure. This approach ensures cleaner code and improved type safety, allowing you to specify only the parameters necessary for data filtration.

Now, your code stands poised to be more succinct, maintainable, and in harmony with the best practices of TypeScript development.

Exploring the Approach: Leveraging URLSearchParams for Effective API Filtering

When it comes to filtering data in API requests, the choice of methodology can significantly impact both code clarity and performance. In this guide, we'll dissect the approach of using URLSearchParams in a GET request to achieve precise data retrieval.

The Power of URLSearchParams

URLSearchParams is a native JavaScript object that provides a convenient way to construct and manipulate URL query parameters. In our context, it simplifies the process of appending search parameters to a URL for enhanced data filtering.

By utilizing this object, our code gains elegance and readability. Each conditional block appends a parameter only if it exists in the payload, ensuring a clean and concise implementation. This approach not only makes our intent clear but also reduces unnecessary clutter in the URL, promoting a more maintainable codebase.

A Comparative Glance

While various methodologies exist for filtering API requests, the URLSearchParams approach stands out for its simplicity and adherence to the principles of RESTful design. Let's briefly contrast it with other common techniques.

  1. POST Request with Body:

    • Suitable for more complex scenarios requiring extensive data manipulation.
    • May introduce overhead for smaller, straightforward filtering tasks.
  2. GET Request with Hardcoded Parameters:

    • Lacks flexibility as parameters are predetermined in the URL.
    • Prone to becoming unwieldy when dealing with multiple optional filters.

Why Choose URLSearchParams?

  1. Dynamic Filtering:

    • Enables dynamic filtering by appending only the parameters present in the payload.
    • Well-suited for scenarios where the set of filters may vary.
  2. Readability and Maintainability:

    • Promotes clean, readable code by avoiding redundant conditional checks.
    • Facilitates easy maintenance as each parameter addition is localized.
  3. Adherence to RESTful Principles:

    • Aligns with RESTful design principles by utilizing the query string for filtering.
    • Supports a resource-centric approach to API design.

In conclusion, the choice of URLSearchParams for filtering API requests in a GET scenario represents a balance between simplicity, readability, and adherence to best practices. This approach ensures that your code remains expressive and adaptable, laying the foundation for scalable and maintainable API interactions.

Thank you for reading this far; your support means a lot! If you have any questions, please don't hesitate to ask in the comments. Don't forget to like and share the article – your appreciation is highly valued. Your feedback and suggestions are also more than welcome. 🙏👍😊

Top comments (2)

Collapse
 
iamhectorsosa profile image
Hector Sosa

Nice post! I've written a simple package to handle URLSearchParams with full type-safety called @search-params/react -> github.com/iamhectorsosa/search-pa....

Check it out and let me know what you think. If you find it useful, I'd appreciate a star on GitHub

Collapse
 
bugudiramu profile image
bugudiramu

Great work man