tsc-query-string-serializer

1.1.1 • Public • Published

TypeScript Query String Serializer

Tests Compilation

tsc-query-string-serializer is an utility written in TypeScript to serialize objects into query strings for GET HTTP requests.

Signature (declaration):

export declare const queryStringSerializer: {
    serialize: (obj: Object, encodeUri?: boolean) => string;
};

By default the query string is encoded. If you prefer not encode it just set to false the optional argument encodeUri.

Usage example:

import { queryStringSerializer } from "tsc-query-string-serializer/queryStringSerializer";
const searchCriteria = { ids: [ 'product-1', 'product-2' ] };
const queryString = queryStringSerializer.serialize(searchCriteria);
const endPoint = "https://myendpoint.com/api/products?" + queryString;
myHttpClient.get(endPoint);

Output examples without encode:

Input Output
{ a: 'b' } a=b
{ a: 'b', c: 'd' } a=b&c=d
{ a: ['foo', 'bar', 'baz'] } a[]=foo&a[]=bar&a[]=baz
{ a: [ 'b', [ 'c', [ 'd' ] ] ] } a[]=b&a[1][]=c&a[1][1][]=d
{ a: { b: 'c', d: { e: 'f' } } } a[b]=c&a[d][e]=f

For more complex examples you can see the tests.

Package Sidebar

Install

npm i tsc-query-string-serializer

Weekly Downloads

0

Version

1.1.1

License

MIT

Unpacked Size

16.4 kB

Total Files

16

Last publish

Collaborators

  • nakerdev