Skip to content

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

License

Notifications You must be signed in to change notification settings

Nakerdev/tsc-query-string-serializer

Repository files navigation

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.

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •