Skip to content

A wrapper around fetch which automatically includes a authorization header, and handles 401 errors.

License

Notifications You must be signed in to change notification settings

TerryRPatterson/authorizedFetch

Repository files navigation

Authorized Fetch is a wrapper around the fetch API which will automatically bundle a Bearer token into the call and run a function upon receiving a status 401 unauthorized.

This module exports:

{

  configure: function accepts two parameters:
    getUserAuthorization: function that is called to retrieve user credentials
    that will be sent with the fetch calls.

    authFailure: function that is called when a 401 is received.



  fetch: function accept two parameters:

    URL: String of the URL to fetch from.

    Headers: Object contain all headers you want included. This object will be
    merged with an authorization header containing the results of
    getUserAuthorization. This will not overwrite any authorization
    header you pass to it.
}

If you are using a bundler like webpack you can install this module by:

npm install authorizedfetch

Then import the module in any file using the authorized fetch command. The file extension may be required depending on the bundler.

import any name here from "authorizedfetch";

If you are not using a bundler your main script tag should be label type module.

Example:

<script src="index.js" type="module"></script>

Then the module can be imported just like before, but you must include a file extension

import any name here from "authfetch.js";

Make sure to run configure before using it.

About

A wrapper around fetch which automatically includes a authorization header, and handles 401 errors.

Resources

License

Stars

Watchers

Forks

Packages

No packages published