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

inserturlparams

v2.0.4

Published

Insert dynamic data into url path params

Downloads

5,107

Readme

About inserturlparams

Insert dynamic data in url path/search params. Works client side or in NodeJS. Really useful for big web projects where you store large numbers of long urls.

  • TypeScript supported :3

How it works

  • It replaces any part of a url that starts with /:. i.e. /api/v1/:param/hello with a value from an array or object.
  • insertUrlParams(urlString, pathParams) => full-url-string.

Installation

  • npm i -s inserturlparams

Important

  • For path-params, non-primitive values will throw errors, empty strings will be skipped, but null and undefined will still be included.
  • For search-params, arrays and primitives are allowed: arrays can only contain primitives and will be join by ,. Empty strings/arrays and undefined will be skipped but null will still be included.

Sample code:

import insertUrlParams, {
  TParamObj,
  TParam,
  TSearchParams,
} from '../';


// Example 1: Object keys must match the param names in the url.
const data1: TParamObj = {
  id: 5,
  msg: 'hello',
  really: true,
  something: undefined,
  random: null,
};
const url1 = '/api/v1/:id/cheese/:msg/is-good/:really/dog/cow/:something/:random';
const resp1 = insertUrlParams(url1, data1);
console.log(resp1); // "/api/v1/5/cheese/hello/is-good/true/dog/cow/undefined/null"


// Example 2: NOTE The array must be in the order that you intend to replace them 
// with in the url and the length of the array must match the number of params.
const data2: TParam[] = [5, 'hello', true, undefined, null];
const url2 = '/api/v1/:id/cheese/:msg/is-good/:really/dog/cow/:something/:random';
const resp2 = insertUrlParams(url2, data2);
console.log(resp2); // "/api/v1/5/cheese/hello/is-good/true/dog/cow/undefined/null"


// Example 3: You can use a single primtive.
const resp3 = insertUrlParams('/api/v1/:id/cheese', 5);
console.log(resp3); // "/api/v1/5/cheese";

Note from the author

  • This is a simple library but a long function that I needed to use in the front-end (React) and the back-end (ExpressJS). Plus I hadn't written a client side library yet so this gave me some practice with rollup.js. In my front-end I had a large Routes.ts file and for readability I wanted to store the routes there the same as how they appeared in express, that is with /: inside the url. This required me to format the url before each api call though so I wrote this function to do just that.

Happy web-deving :)