Coder Social home page Coder Social logo

ladjs / preserve-qs Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 91 KB

Preserve querystrings during redirect and creating new URLs for Node.js and browser environments (supports Lad, Koa, Express, and Connect)

License: MIT License

JavaScript 100.00%
preserve querystring request koa express lad connect node javascript browser

preserve-qs's Introduction

preserve-qs

build status code coverage code style styled with prettier made with lass license

Preserve querystrings during redirect and creating new URLs for Node.js and browser environments (supports Lad, Koa, Express, and Connect)

Table of Contents

Install

npm:

npm install preserve-qs

yarn:

yarn add preserve-qs

Usage

Imagine that we have the URL /foo/bar?beep=boop&limit=25 and we wish to redirect the user (or simply output) a new URL with pathname of /foo/baz โ€“ but also preserve the original querystring.

The examples below show how this package can be used for this, regardless of the environment being run in.

We assume you have imported the package in all these examples:

const preserveQs = require('preserve-qs');

Custom

preserveQs('/foo/bar?beep=boop&limit=25', '/foo/baz');

Browser

This uses window.location.pathname and window.location.search.

preserveQs(window, '/foo/baz');

Koa

This uses ctx.request.originalUrl.

preserveQs(ctx, '/foo/baz');

// ctx.redirect(preserveQs(ctx, '/foo/baz'));

Express

This uses req.originalUrl.

preserveQs(req, '/foo/baz');

// res.redirect(preserveQs(req, '/foo/baz'));

Override Properties

If you wish to override a property in the querystring, either from the original or the new URL, then you can pass a third argument of an object.

preserveQs(req, '/foo/baz?hello=false', { hello: true });

In the example above, the output would have the ?hello=false be overriden and become ?hello=true.

Blacklist Properties

If you wish to completely strip a single property or multiple from being included in the querystring then pass it as the third or fourth argument.

preserveQs(req, '/foo/baz', [ 'page', 'limit' ]);

This would strip the page and limit from the URL (e.g. it would not have ?page=1&limit=10 in the URL).

You can also combine this with the override argument by invoking in order as preserveQs(req, url, override, blacklist).

Contributors

Name Website
Nick Baugh http://niftylettuce.com/

License

MIT ยฉ Nick Baugh

preserve-qs's People

Contributors

niftylettuce avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.