npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

url-buildr

v0.0.16

Published

Url Builder

Downloads

28

Readme

url-buildr

Author: Hasnain-Haider

Overview: This object assembles URLs and routes

An NPM package:

Quickly assemble routes and urls. Can be used for a variety of purposes like

  • Route Generation/Modularization
  • API testing
  • Webcrawling/scraping
  • etc.

API:

Class: URLBuildr

Creates an object to create urls

URLBuildr.create(options)

The preferred method of creating strings. Returns a URLBuildr object, calls the constructor

Parameters

initializer: object|string, Creates a URLBuildr

Returns: this

e.g. var myUrl = ub.create(myOptions).toString()

{options}

  • options.prefix {string} - at the very beginning of the url
  • options.pathPrefix {string} - at the very beginning of the path
  • options.additions {string[]} - additions to the path, added sequentially
  • options.port {integer|string} - port of the url
  • options.host {string} - url host
  • options.params {string[]|object} - parameters and their values // [key1,val1, key2,val2] OR {key1:val1, key2:val2}
  • options.queries: {string[]|object} - queries and the values // (Same format as above)

The url is made in the following format: (prefix)(host)(:port)(pathPrefix)(additions).

URLBuildr.add(paths)

Adds to url path, after the prefix, host, port and pathPrefix

Parameters

paths: ...string|string[], Creates an object to create urls

Returns: this

e.g. ub.add('accounts', 'users') or ub.add(['accounts', 'users'])

URLBuildr.param(params)

Fills in parameters in the url if they are present

Parameters

params: string[]|...string|object, pass a string array, object, or several strings

Returns: this

e.g.

ub.param([key1, val1, key2, val2]);
ub.param(key1, val1, key2, val2);
ub.param({
    key1: val1,
    key2: val2
});

URLBuildr.query(query)

Insert query strings at the end of the url

Parameters

query: string[]|..string|object, pass a string array, object, or several strings

Returns: this

URLBuildr.clear()

reset the Object options()

URLBuildr.toString()

Returns the constructed URL

Returns: string, urlString

URLBuildr.set(opt)

Assign a value to the given options field

Parameters

opts: object, set fields in the options object of "this"

Returns: this

e.g. ub.set({host: 3000})

URLBuildr.setOptions(options)

Overwrites the entire options object of "this"

Parameters

options: object, Object containing configurations for the url

Returns: this


Examples

There are two ways to use this module

  1. Instantiate the object with an 'options' object
    • if this method is used, you MUST pass an object with a defined host to the constructor
  2. Instantiate with a string
    • Some features are disabled in this method
    • the URLs are generated with object chaining

The leading and following forward slash(es) are ignored in most cases.

Here's a quick example showing the first way

var ub = require('url-buildr');
var urlObj = ub.create({
  prefix: 'https://',
  pathPrefix: 'accounts',
  additions: ['users', ':userId', 'cart'],
  port: 65132,
  params: {
    userId: 54298
  },
  host: 'thegreatsite.co',
  queries: {
    showAllPurchases: true
  }
});

When toString() is called the following string is returned: https://thegreatsite.co:65132/accounts/users/54298/cart?showAllPurchases=true

Example producing the same url as above using the chaining method:

var urlObj = ub.create('https://thegreatsite.co:65132')
         .add('accounts', 'users', ':userId', 'cart')
         .query({showAllPurchases: true})
         .param({userId: 54298});