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

@bowtie/api

v0.6.3

Published

A simple class to standardize API interactions

Downloads

36

Readme

Maintainability Test Coverage

build status npm version node version npm downloads GitHub contributors license

GitHub pull requests GitHub closed pull requests GitHub issues GitHub closed issues

@bowtie/api

JavaScript utilities and helpers

Installation

npm install --save @bowtie/api

Documentation

Documentation

Basic Usage

// Require @bowtie/api class definition
const Api = require('@bowtie/api')

// Create api instance
const api = new Api({
  root: 'api.example.com' // Will default to https:// if no protocol is provided here
})

GET Example

// REST GET /todos - Get all todos
api.get('todos')
  .then(resp => {
    // resp = response from GET https://api.example.com/todos
  })
  .catch(err => {
    // Something went wrong
  })

POST Example

// REST POST /todos - Create new todo
api.post('todos', { name: 'new todo' })
  .then(resp => {
    // resp = response from POST https://api.example.com/todos
  })
  .catch(err => {
    // Something went wrong
  })

PUT Example

// REST PUT /todos/1 - Update todo with id=1
api.put('todos/1', { name: 'changed name' })
  .then(resp => {
    // resp = response from PUT https://api.example.com/todos/1
  })
  .catch(err => {
    // Something went wrong
  })

DELETE Example

// REST DELETE /todos/1 - Delete todo with id=1
api.delete('todos/1')
  .then(resp => {
    // resp = response from DELETE https://api.example.com/todos/1
  })
  .catch(err => {
    // Something went wrong
  })

Base URL Config

// Create api instance
const api = new Api({
  root: 'api.example.com', // Set beginning of the baseUrl to https://api.example.com
  stage: 'test',           // Append "/test" to baseUrl (i.e. https://api.example.com/test)
  prefix: 'api',           // Append "/api" to baseUrl  (i.e. https://api.example.com/test/api)
  version: 'v1'            // Append "/v1" to baseUrl   (i.e. https://api.example.com/test/api/v1)
})

// baseUrl is constructed as follows: (only api.root is required, any combination of stage/prefix/version is allowed)
baseUrl = `${api.root}/${api.stage}/${api.prefix}/${api.version}`;

Authorization

Basic Auth

// Create api instance
const api = new Api({
  root: 'api.example.com', // Will default to https:// if no protocol is provided here
  authorization: 'Basic'   // Specify Basic authorization type for this api
})

// Authorize api for basic auth with username & password
api.authorize({
  username: 'user',
  password: 'pass'
})

Bearer Token

// Create api instance
const api = new Api({
  root: 'api.example.com', // Will default to https:// if no protocol is provided here
  authorization: 'Bearer'  // Specify Bearer authorization type for this api
})

// Authorize with static token value
api.authorize({
  token: 'abc123'
})

// Authorize with dynamic token function
api.authorize({
  token: () => localStorage.getItem('access_token')
})

Custom Auth

// Create api instance
const api = new Api({
  root: 'api.example.com', // Will default to https:// if no protocol is provided here
  authorization: 'Custom'  // Specify Custom authorization type for this api
})

// Authorize with static headers
api.authorize({
  headers: {
    'token': 'abc123'
  },
  validate: () => {
    // Function to validate the custom auth, returns true/false
    return true
  }
})

// Authorize with dynamic headers
api.authorize({
  token: () => ({
    'token': localStorage.getItem('access_token'),
    'uid': localStorage.getItem('uid')
  }),
  validate: () => {
    // Function to validate the custom auth, returns true/false
    return true
  }
})

Example using the j-toker package with headers from devise_token_auth

const Auth = require('j-toker')

// Configure j-toker apiUrl to match api.baseUrl()
Auth.configure({
  apiUrl: api.baseUrl()
})

// Authorize with custom headers that retrieve authHeaders from j-toker "Auth" instance
api.authorize({
  headers: () => Auth.retrieveData('authHeaders'),
  validate: () => Auth.user.signedIn
})

Ignore security

// Create api instance
const api = new Api({
  root: 'http://localhost:3000', // Use a local development API over HTTP (not secure HTTPS)
  secureOnly: false              // Disable secure HTTPS requirement
})

Enable Debug logs

// Create api instance
const api = new Api({
  root: 'api.example.com', // Will default to https:// if no protocol is provided here
  verbose: true            // Set verbose=true to enable debug log output
})