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

aws4-axios

v3.3.13

Published

Axios request interceptor for signing requests with AWSv4

Downloads

567,995

Readme

aws4-axios

All Contributors

npm version npm downloads

This is a request interceptor for the Axios HTTP request library to allow requests to be signed with an AWSv4 signature.

This may be useful for accessing AWS services protected with IAM auth such as an API Gateway.

Installation

| yarn | npm | | --------------------- | ------------------------------- | | yarn add aws4-axios | npm install --save aws4-axios |

Compatibility

This interceptor is heavily dependent on Axios internals, so minor changes to them can cause the interceptor to fail.

Please make sure you are using one of the following versions of Axios before submitting issues etc.

| Axios Version | Supported? | | ------------------- | ---------- | | < 1.4.0 | ❌ No | | >= 1.4.0 <= 1.6.7 | ✅ Yes | | > 1.6.7 | Unknown |

Usage

To add an interceptor to the default Axios client:

import axios from "axios";
import { aws4Interceptor } from "aws4-axios";

const interceptor = aws4Interceptor({
  options: {
    region: "eu-west-2",
    service: "execute-api",
  },
});

axios.interceptors.request.use(interceptor);

// Requests made using Axios will now be signed
axios.get("https://example.com/foo").then((res) => {
  // ...
});

Or you can add the interceptor to a specific instance of an Axios client:

import axios from "axios";
import { aws4Interceptor } from "aws4-axios";

const client = axios.create();

const interceptor = aws4Interceptor({
  options: {
    region: "eu-west-2",
    service: "execute-api",
  },
});

client.interceptors.request.use(interceptor);

// Requests made using Axios will now be signed
client.get("https://example.com/foo").then((res) => {
  // ...
});

You can also pass AWS credentials in explicitly (otherwise taken from process.env)

const interceptor = aws4Interceptor({
  options: {
    region: "eu-west-2",
    service: "execute-api",
  },
  credentials: {
    accessKeyId: "",
    secretAccessKey: "",
  },
});

You can also pass a custom CredentialsProvider factory instead

const customCredentialsProvider = {
  getCredentials: async () => {
    return Promise.resolve({
      accessKeyId: "custom-provider-access-key-id",
      secretAccessKey: "custom-provider-secret-access-key",
    });
  },
};

const interceptor = aws4Interceptor({
  options: {
    region: "eu-west-2",
    service: "execute-api",
  },
  credentials: customCredentialsProvider,
});

Migration to v3

The interface for options changed in v3. You should now pass a single object with configuration.

The previous options object is now nested under the property options.

E.g (v2).

const interceptor = aws4Interceptor({
  region: "eu-west-2",
  service: "execute-api",
  assumeRoleArn: "arn:aws:iam::111111111111:role/MyRole",
});

would become (v3):

const interceptor = aws4Interceptor({
  options: {
    region: "eu-west-2",
    service: "execute-api",
    assumeRoleArn: "arn:aws:iam::111111111111:role/MyRole",
  },
});

If you passed a custom credential provider, this is now done via the credentials property.

E.g (v2).

const interceptor = aws4Interceptor(
  {
    region: "eu-west-2",
    service: "execute-api",
  },
  {
    accessKeyId: "AKIAIOSFODNN7EXAMPLE",
    secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
  }
);

would become (v3):

const interceptor = aws4Interceptor({
  options: {
    region: "eu-west-2",
    service: "execute-api",
    assumeRoleArn: "arn:aws:iam::111111111111:role/MyRole",
  },
  credentials: {
    accessKeyId: "AKIAIOSFODNN7EXAMPLE",
    secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
  },
});

Assuming the IAM Role

You can pass a parameter to assume the IAM Role with AWS STS and use the assumed role credentials to sign the request. This is useful when doing cross-account requests.

const interceptor = aws4Interceptor({
  options: {
    region: "eu-west-2",
    service: "execute-api",
    assumeRoleArn: "arn:aws:iam::111111111111:role/MyRole",
    assumeRoleSessionName: "MyApiClient", // optional, default value is "axios"
  },
});

Obtained credentials are cached and refreshed as needed after they expire.

You can use expirationMarginSec parameter to set the number of seconds before the received credentials expiration time to invalidate the cache. This allows setting a safety margin. Default to 5 seconds.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!