1.3.6 • Published 7 months ago

gt-query-string v1.3.6

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

gt-query-string

An public module to serialize and deserialize a query string.

serialize an object to query string

The function serializeObject gets an object or array nested in an object and returns a query string from it

the object

{ name:'xxx', start:123 }

will return

?name=xxx&start=123

the array

{ list:[4,5,6] }

will return

?list[0]=4&list[1]=5&list[2]=6

an embedded object

{ person:{ name:'xxx', id:123 } }

will return ?person.name=xxx&person.id:123

an array that contains an object

{ or:[{ item:10, point:3 }, { name:5} ]} 

will return

?or[0]item=10&or[0]point:3&or[1]name=5`

The function seializeObject returns the following object:

{ query, options}

the options contains the data that needs to be sent as headers in the request.

deserialize an object from the query string object

There are two opptions to deserialize the object 1. desrializeObject : gets an encoded querystring and returns the original object. 2. an express middleware that deserializes the querystring back to the original object. The request must contain the following headers: {'query-serialize' : 'gt'}. The deseialized object is stored in the desQuery property of the request

how to use the package

  1. install the package
npm i gt-query-string
  1. client side use the serializeObject function, and send the request with the approriate headers
const {serializeObject} = require('gt-query-string');

const obj = {type:'triangle', points:[{x:0, y:10}, {x:15, y:45}, {x:30, y:12}] };

const {query, options} = serializeObject(obj);

fetch(`<serverurl>${query}`, {method: 'GET', headers:{...options}}).then(...)
  1. server side - express can be used with js and ts

app.js

const express = require('express')
const {deserializeURI} = require('gt-query-string');

app = express()

app.get('/data', deserializeURI(), (req, res, next)=>{
    console.log(req.desQuery);
    res.status(200)
});

app.ts

import express from 'express'
import {deserializeURI} from 'gt-query-string';

app = express()

app.get('/data', deserializeURI(), (req:Request, res: Response, next: NextFunction)=>{
    console.log(req.desQuery);
    const query: object = req.desQuery;
    res.status(200);
});
1.3.6

7 months ago

1.3.5

7 months ago

1.3.4

7 months ago

1.3.3

7 months ago

1.3.2

8 months ago

1.3.1

8 months ago

1.3.0

8 months ago

1.1.0

10 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago