Quantcast
Channel: Krzysztof Żuraw blog's RSS Feed
Viewing all articles
Browse latest Browse all 205

Today I Learned - query string library have stringifyUrl

$
0
0

I’ve been using query-string library to create my query string for a long time. Normally I’ve used it as follows:

import*as qs from'query-string';constAPI_URL='/users?'+ qs.stringfy({ user:'1'});// API_URL will be /users?user=1

Everything was fine until my parameter was null or undefined. When such case occurs my API_URL looked something like /users?. It wasn’t a problem for the endpoint - the request was hitting backend yet I’ve some feeling that it can be done better. Today when I was integrating query-string into a new project I found out about new function: stringifyUrl. Let’s see it in action:

import*as qs from'query-string';constAPI_URL= qs.stringifyUrl({
    url:'/users',
    query:{
      user:1,},},{ skipNull:true});// API_URL will be /users?user=1// and in case when user id is null// /users

Perfect 🎉. Now I’m happy and I’ve learned new stuff - so if you happen to use query-string consider using stringifyUrl.


Viewing all articles
Browse latest Browse all 205

Trending Articles