1.1.1 • Published 4 years ago

tsc-query-string-serializer v1.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

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:

InputOutput
{ 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&a1=c&a1[]=d
{ a: { b: 'c', d: { e: 'f' } } }ab=c&ad=f

For more complex examples you can see the tests.