|
1 | 1 | # http_build_query |
2 | | -Javascript port of PHP http_build_query |
3 | 2 |
|
4 | | -WIP |
| 3 | +[](https://codecov.io/gh/Modularity-Bulgaria/http_build_query) |
| 4 | + |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +Have you ever found yourself needing to connect to a legacy PHP API that requires the usage of `http_build_query` with **RFC1738**? |
| 9 | + |
| 10 | +This library will enable you to do so with ease :beer: |
| 11 | + |
| 12 | +## Requirements |
| 13 | +- NodeJS 12.x |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +Install using either `npm` or `yarn` |
| 18 | +```sh |
| 19 | +npm i @modularitybg/http_build_query |
| 20 | +//or |
| 21 | +yarn add @modularitybg/http_build_query |
| 22 | +``` |
| 23 | + |
| 24 | +## How to use |
| 25 | + |
| 26 | +```js |
| 27 | +const httpBuildQuery = require('@modularitybg/http_build_query'); |
| 28 | + |
| 29 | +const rfcFormattedQuery = httpBuildQuery({ |
| 30 | + amount: { amount: 2900 }, |
| 31 | + stringValueWithWhiteSpace: "String Value", |
| 32 | + city: "Unlandstraße", |
| 33 | + arrayValues: [1, "string"], |
| 34 | +}) |
| 35 | +`` |
| 36 | +//Result |
| 37 | +"amount=%7B%22amount%22%3A2900%7D&stringValueWithWhiteSpace=String+Value&city=Unlandstra\\u00dfe&arrayValues=%5B1%2C%22string%22%5D" |
| 38 | +``` |
| 39 | +_Notice that special characters are transformed using UTF-8 mapping, similar to how is handled in PHP_ |
| 40 | + |
| 41 | + |
| 42 | +## Why not simply use _URLSearchParams_? |
| 43 | + |
| 44 | +Let's compare the difference: |
| 45 | +```js |
| 46 | +nativeQueryResult = new URLSearchParams({ |
| 47 | + amount:{ |
| 48 | + amount: 2900 |
| 49 | + }, |
| 50 | + stringValueWithWhiteSpace: "String Value", |
| 51 | + city: "Unlandstraße", |
| 52 | + arrayValues: [1, "string"] |
| 53 | +}).toString() |
| 54 | + |
| 55 | +//Result |
| 56 | +'amount=%5Bobject+Object%5D&stringValueWithWhiteSpace=String+Value&city=Unlandstra%C3%9Fe&arrayValues=1%2Cstring' |
| 57 | +``` |
| 58 | + |
| 59 | +This type of encoding may not be compatible with the API you are trying to connect. |
| 60 | +Notice the **Object**, **arrayValues** definitions and the **special character** in the city name. |
| 61 | + |
| 62 | +## License |
| 63 | + |
| 64 | +MIT |
0 commit comments