1.0.0 • Published 5 years ago

@zakkudo/query-string v1.0.0

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
5 years ago

@zakkudo/query-string

Make working with url query-strings enjoyable.

Build Status Coverage Status Known Vulnerabilities Node License

Why use this?

  • Consistancy with simplicity
  • The instance acts like a plain-old-object
  • JSON.stringify() will serialize it to json like it was a normal object
  • Casting to a string will format it to be directly usable in a query Update the properties after initialization and the serialization will reflect the updates
  • Complex params are automatically serialized and deserialized from json

Install

# Install using npm
npm install @zakkudo/query-string
# Install using yarn
yarn add @zakkudo/query-string

Examples

Initializing with an object

import QueryString from '@zakkudo/query-string';

const query = new QueryString({
  page: 3,
  title: 'awesomeness',
  complex: {'test': 'value'}
});

String(query) // '?page=3&title=awesomeness&complex=%7B%22test%22%3A%22value%22%7D&'
query.toString() // '?page=3&title=awesomeness&complex=%7B%22test%22%3A%22value%22%7D&'

const url = `http://example${query}` //Automatically serializes correctly

const url = `http://example${query}` //Automatically serializes correctly with changes

Initializing with a URL and making changes dynamically

import QueryString from '@zakkudo/query-string';

const query = new QueryString('http://example?page=3&title=awesomeness');

delete query.page;

String(query) // '?title=awesomeness'
query.toString() // '?title=awesomeness'

Error handling

import QueryString from '@zakkudo/query-string';
import QueryStringError from '@zakkudo/query-string/QueryStringError';

try {
    const query = new QueryString('http://invalid.com/?first=1?second=2')
} catch(e) {
    if (e instanceof QueryStringError) {
        console.error(e.message); // Trying to add duplicate query param when already exists
    } else {
        throw e;
    }
}

API

@zakkudo/query-string~QueryString ⏏

Kind: Exported class

new QueryString(data, options)

Throws:

ParamTypeDefaultDescription
dataString | Object | QueryStringInitial data. A url String will be parsed, and Object/QueryString instances will be copied.
optionsObjectModifiers for how the query string object is contructed
options.unsafeBooleanfalseDisable url escaping of key/value pairs. Useful for servers that use unsafe characters as delimiters

@zakkudo/query-string/QueryStringError~QueryStringError ⇐ Error

Error class used by QueryString for raising errors generated during parsing or serialization.

Kind: Exported class

Extends: Error

new QueryStringError(message, url)

ParamTypeDescription
messageStringA message describing the reason for the error.
urlStringThe related url fragment when the error was generated

queryStringError.url

The related url fragment when the error was generated

Kind: instance property of QueryStringError

1.0.0

5 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago