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

schlepp

v1.0.9

Published

API client for authenticated requests using a JWT bearer token

Downloads

4

Readme

Schlepp

CircleCI

Coverage Status

API client used for making authenticated JSON requests using a JWT bearer token to a specified endpoint. Unauthenticated requests are also supported.

This API client follows the convention of storing the bearer token in local storage. When creating an instance of the API client, pass the function the name of the key in local storage to use when retrieving the bearer token for authenticated requests.

Setup

Create an instance of Schlepp passing in your API host and local storage bearer token key

import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token', // assumes localStorage.getItem('auth_token') will return the bearer token
  headers: { Accept: 'application/json' }, // default is { 'Content-Type': 'application/json' }
  host: 'https://example.com',
);

Usage

Use the authenticated and unauthenticated objects to send an HTTP request to the server. Both objects expose the same HTTP method functions (get, delete, post, patch).

The difference between the authenticated and unauthenticated request is that the authenticated requests include an Authorization header: ({ Authorization: 'Bearer my-bearer-token' }).

Request methods

The following request methods can be performed as either authenticated or unauthenticated requests

#delete

delete = (path: string, headers?: object = {}) => promise

// Example:
api.authenticated.delete('users/1', { Accept: 'application/json' });
api.unauthenticated.delete('users/1');

#get

get = (path: string, headers?: object = {}) => promise

// Example:
api.authenticated.get('users');
api.unauthenticated.get('users', { Accept: 'application/json' });

#patch

patch = (path: string, params: object, headers?: object = {}) => promise

// Example:
api.authenticated.patch('users/1', JSON.stringify({ first_name: 'Mike' }));
api.unauthenticated.patch(
  '/users/1',
  JSON.stringify({ first_name: 'Mike' }),
  { Accept: 'application/json' },
);

#post

post = (path: string, params: object, headers?: object = {}) => promise

// Example:
api.authenticated.post('comments', JSON.stringify({ comment: 'This is neat!' }));
api.unauthenticated.post(
  'comments',
  JSON.stringify({ comment: 'This is neat!' }),
  { Accept: 'application/json' },
);

Examples

Unauthenticated requests

import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  host: 'https://example.com',
);

api.unauthenticated.get('posts?title=Unicorns');
api.unauthenticated.delete('users/1');
api.unauthenticated.post('users', JSON.stringify({ first_name: 'Charlie', last_name: 'Brown' }));
api.unauthenticated.patch('users/1', JSON.stringify{ last_name: 'Chaplin' }));

Authenticated requests

import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  host: 'https://example.com',
);

api.authenticated.get('posts?title=Unicorns');
api.authenticated.delete('users/1');
api.authenticated.post('users', JSON.stringify({ first_name: 'Charlie', last_name: 'Brown' }));
api.authenticated.patch('users/1', JSON.stringify({ last_name: 'Chaplin' }));

Specifying headers

By default, request headers will be set to { 'Content-Type': 'application/json' }. Request headers can be amended/overridden in 2 ways.

1) Instantiating the class with headers

If you'd like to set headers to be used across all requests made by the API client, you may want to set headers when creating an instance of the Schlepp class.

import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  headers: { Accept: 'application/json' },
  host: 'https://example.com',
);

// in the above example all requests send from the `api` constant will include the following headers:
// { Accept: 'application/json', 'Content-Type': 'application/json' }

import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  headers: { 'Content-Type': 'text/plain' },
  host: 'https://example.com',
);

// in the above example all requests send from the `api` constant will include the following headers:
// { 'Content-Type': 'text/plain' }

2) Setting headers on each request

To override or amend the default or instance headers, you can specify the headers of an individual request.

import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  headers: { Accept: 'text/plain' },
  host: 'https://example.com',
);

api.unauthenticated.get('users', { Accept: 'application/json' });

// in the above example the request will include the following headers:
// { Accept: 'application/json', 'Content-Type': 'application/json' }

import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  host: 'https://example.com',
);

api.unauthenticated.get('users', { 'Content-Type': 'text/plain' });

// in the above example the request will include the following headers:
// { 'Content-Type': 'text/plain' }