1.0.2 • Published 3 years ago

@modularitybg/http_build_query v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

http_build_query

codecov GitHub Workflow Status

Introduction

Have you ever found yourself needing to connect to a legacy PHP API that requires the usage of http_build_query with RFC1738?

This library will enable you to do so with ease :beer:

Requirements

  • NodeJS 12.x

Installation

Install using either npm or yarn

npm i @modularitybg/http_build_query
//or
yarn add @modularitybg/http_build_query

How to use

const httpBuildQuery = require('@modularitybg/http_build_query');

const rfcFormattedQuery = httpBuildQuery({
    amount: { amount: 2900 },
    stringValueWithWhiteSpace: "String Value",
    city: "Unlandstraße",
    arrayValues: [1, "string"],
})
``
//Result
"amount=%7B%22amount%22%3A2900%7D&stringValueWithWhiteSpace=String+Value&city=Unlandstra\\u00dfe&arrayValues=%5B1%2C%22string%22%5D"

Notice that special characters are transformed using UTF-8 mapping, similar to how is handled in PHP

Why not simply use URLSearchParams?

Let's compare the difference:

nativeQueryResult = new URLSearchParams({
    amount:{ 
        amount: 2900 
    },
    stringValueWithWhiteSpace: "String Value",
    city: "Unlandstraße",
    arrayValues: [1, "string"]
}).toString()

//Result
'amount=%5Bobject+Object%5D&stringValueWithWhiteSpace=String+Value&city=Unlandstra%C3%9Fe&arrayValues=1%2Cstring'

This type of encoding may not be compatible with the API you are trying to connect. Notice the Object, arrayValues definitions and the special character in the city name.

License

MIT

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago