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

rest-api-url-builder

v1.2.2

Published

Build URLs for REST API without any unnecessary complexity. You can bind named parameters as well as query parameters with just few lines of code

Downloads

16

Readme

rest-api-url-builder

This plugin is used for building URLs for REST APIs (server-side compatible). Easy to use. Supports named and query parameters as well as the base url binding.

Size: 6.55 KiB minified, 3.05 KiB gzipped

Installation

Using npm or yarn:

npm install rest-api-url-builder
# or
yarn add rest-api-url-builder

Syntax

const urlBuilder = new UrlBuilder(routes: { [routeName: string]: string|RouteConfig }, options: UrlBuilderOptions);

Parameters

Routes

The routes object may contain a routeName: url (string: string) or routeName: RouteConfig (string: object) pairs. RouteConfig is an object with following structure:

interface RouteConfig {
    path: string;
    baseURL?: string;
}

Routes

The options object may contain one or more available options

Usage

Routes with absolute URLs

import UrlBuilder from 'rest-api-url-builder';

const routes = {
    'homepage': 'https://www.example.com/homepage',
    'about-us': {
        'path': 'https://www.example.com/about-us'
    }
};

const urlBuilder = new UrlBuilder(routes);
const route = urlBuilder.build('homepage').get(); // https://www.example.com/homepage

Routes with relative URLs & base URL

import UrlBuilder from 'rest-api-url-builder';

const options = {
    'baseURL': 'https://www.example.com'
};

const routes = {
    'homepage': '/homepage',
    'about-us': {
        'path': '/about-us'
    }
};

const urlBuilder = new UrlBuilder(routes, options);
const route = urlBuilder.build('homepage').get(); // https://www.example.com/homepage

Rewriting baseURL for specific URLs

import UrlBuilder from 'rest-api-url-builder';

const options = {
    'baseURL': 'https://www.example.com'
};

const routes = {
    'homepage': '/homepage',
    'contact:': '/contact',
    'about-us': {
        'path': '/about-us',
        'baseURL': 'https://www.other.com'
    }
};

const urlBuilder = new UrlBuilder(routes, options);
const homepageURL = urlBuilder.build('homepage').get(); // https://www.example.com/homepage
const contactURL  = urlBuilder.build('contact').get();  // https://www.example.com/contact
const aboutUsURL  = urlBuilder.build('about-us').get(); // https://www.other.com/about-us

Binding named parameters

import UrlBuilder from 'rest-api-url-builder';

const routes = {
    'product': 'https://www.example.com/product/:id'
};

const urlBuilder = new UrlBuilder(routes);
const productURL = urlBuilder.build('product')
    .setNamedParameter('id', 1010)
    .get();
console.log(productURL); // https://www.example.com/product/1010

Binding query parameters

import UrlBuilder from 'rest-api-url-builder';

const routes = {
    'search': 'https://www.example.com/search'
};

const urlBuilder = new UrlBuilder(routes);
const searchURL = urlBuilder.build('search')
    .setQueryParameter('sort', 'price')
    .get();
console.log(searchURL); // https://www.example.com/search?sort=price

Binding array query parameters

import UrlBuilder from 'rest-api-url-builder';

const routes = {
    'search': 'https://www.example.com/search'
};

const urlBuilder = new UrlBuilder(routes);
const searchURL = urlBuilder.build('search')
    .setQueryParameter('filters', ['value1', 'value2'])
    .get();
console.log(searchURL); // https://www.example.com/search?filters[]=value1&filters[]=value2

Chaining parameters binding

import UrlBuilder from 'rest-api-url-builder';

const routes = {
    'search': 'https://www.example.com/search/:category'
};

const urlBuilder = new UrlBuilder(routes);
const searchURL = urlBuilder.build('search')
    .setQueryParameter('filters', ['value1', 'value2'])
    .setQueryParameter('sort', 'price')
    .setNamedParameter('category', 'cars')
    .get();
console.log(searchURL); // https://www.example.com/search/cars?filters[]=value1&filters[]=value2&sort=price

Available UrlBuilder options

{
    /**
     * Base url that is going to be prepended to each url
     * parts: protocol (required) + host (required) + port (optional)
     * default: ''
     */
    baseURL: 'https://www.example.com'
}