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

node-api-path

v0.0.2

Published

Create url's for your api endpoints while keeping it configurable by environment.

Downloads

3

Readme

node-api-path

Simple module to organise your api paths and add networking to your node apps.

Installation

npm install --save node-api-path

Usage

In a node class where you want to configure your paths, simply import ApiPathFetch and initialise it as shown. ApiPathFetch uses isomorphic fetch to make api calls. If you want to use your own networking library, simply import ApiPath instead of ApiPathFetch. ApiPath will build full URL's from your paths without providing networking. Passing the host path (in the example below: http://127.0.0.1:8080/my/api) is optional, if you don't specify it the library will look for the host path in the SVC_URL environment variable (process.env.SVC_URL).

//ES6 module syntax
 import ApiPathFetch from 'node-api-path/lib/ApiPathFetch';

 let path = new ApiPathFetch({
         test_relative_path: 'verify-email/{0}/{1}',
         test_absolute_path: '/verify-email?username={0}&email={1}',
         test_full_url: 'http://example.com/verify-email/{0}/{1}',
         test_api_call: 'https://jsonplaceholder.typicode.com/posts/{0}'
       }, "http://127.0.0.1:8080/my/api")

   //usage
   //get full url
   let url = svcPath.test_relative_path
   //returns http://127.0.0.1:8080/my/api/verify-email/{0}/{1}
   let url = svcPath.test_absolute_path
   //returns http://127.0.0.1:8080/verify-email?username={0}&email={1}
   
   //get full url with params filled in
   let url = svcPath.test_relative_path.params("hello", "world")
   //returns http://127.0.0.1:8080/my/api/verify-email/hello/world
   let url = svcPath.test_absolute_path.params("hello", "world")
   //returns http://127.0.0.1:8080/verify-email?username=hello&email=world
   
   //or get full url with params filled in and fetch it (default is get) 
   svcPath.test_absolute_path.params("hello", "world").fetch().then(response => {
     //Success do something
   }).catch(error => {
       //error
     });
   
   //or if you don't want to chain and take advantage of being able to change
   //the default headers
   svcPath.setDefaultOptions({method:'POST'})
   let url = svcPath.test_absolute_path.params("hello", "world")
   svcPath.fetch(url, {body:"Hello World"}).then(response => {
         //success
       }).catch(error => {
         //error
       });
   
   //If you don't want to change the global options, you can pass them
   //to the fetch method
   svcPath.fetch(url, {method: 'POST', body:"Hello World"}).then(response => {
         //success
       }).catch(error => {
         //error
       });

params(...parameters) will encode your parameters. One less thing to worry about.

fetch() can also take in options that can be used to change from 'GET' to 'POST' or pass in a body or headers. For more, take a look at the fetch github page

Alternatively, if you want to use your own networking library, you can still use ApiPath to maintain your paths.

addPath(key, path) takes in a key-value and maps the new key against the path after converting the supplied path to a full URL

getApiHost() for the above example, this would return http://127.0.0.1:8080

getApiBase() for the above example, this would return http://127.0.0.1:8080/my/api

\\ES6 module syntax
  import ApiPath from 'node-api-path/lib/ApiPath';

  let path = new ApiPath({
          test_relative_path: 'verify-email/{0}/{1}',
          test_absolute_path: '/verify-email?username={0}&email={1}',
          test_full_url: 'http://example.com/verify-email/{0}/{1}',
          test_api_call: 'https://jsonplaceholder.typicode.com/posts/{0}'
        }, "http://127.0.0.1:8080/my/api")
    });

//usage
    //get full url
    let url = svcPath.test_api_call
    //get full url with params filled in
    let url = svcPath.test_api_call.params("hello", "world")

Questions or suggestions?

Feel free to contact me on Twitter or open an issue.