aws4-react
TypeScript icon, indicating that this package has built-in type declarations

0.2.3 • Public • Published

AWS4 React

AWS4 React Library is used to get AWS Signature.

This library is to create a signed AWS API request in react or any js project.

Installation

Use the package manager npm to install aws4-react.

npm i aws4-react

or
yarn add aws4-react

Usage

import { AWSSigner } from 'aws4-react';

 let params = {
    data: JSON.stringify(body),
    method: 'GET',
    url:
      'https://z7hgc1k4qb.execute-api.us-east-1.amazonaws.com/master/batches',
  };
  let cred = {
    secret_key: 'kdjaskdjaksd',
    access_key: 'dkasndkjasjdkas',
    session_token: '',
  };

  console.log(cred);

  const serviceInfo = {
    region: 'us-east-1',
    service: 'execute-api',
  };

  const headers = AWSSigner.sign(params, cred, serviceInfo);

/* It returns
  {
  data?: string;
  metho?: string;
  url?: string;
  headers?: {
    host?: string;
    'x-amz-date': string;
    Authorization: string;
  };
}
 */

Now you can send the Authorization in the header as follows

Example with RTK Query

const signedHeaders = AWSSigner.sign(params, cred, serviceInfo);
fetchBaseQuery({
    baseUrl: "example.com",
    prepareHeaders: (headers) => {
       const authHeader = signedHeaders.headers
        for (let key in authHeader) {
         headers.set(key, authHeader[key]);
      }
      return headers;
    },
  });

Example with Axios

const signedHeaders = AWSSigner.sign(params, cred, serviceInfo);
  const authHeader = signedHeaders.headers
  axios.post('url', {"body":data}, {
    headers: {
    'Authorization': authHeader.Authorization
    }
  }
)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Package Sidebar

Install

npm i aws4-react

Weekly Downloads

0

Version

0.2.3

License

MIT

Unpacked Size

119 kB

Total Files

21

Last publish

Collaborators

  • tariqule