Nanometers Per Millisecond

    aws-sigv4-fetch
    TypeScript icon, indicating that this package has built-in type declarations

    1.2.2 • Public • Published

    npm version

    aws-sigv4-fetch

    AWS SignatureV4 fetch API function to automatically sign HTTP request with given AWS credentials. Built entirely on the newest version of the official AWS SDK for JS.

    Signature Version 4

    Signature Version 4 (SigV4) is the process to add authentication information to AWS API requests sent by HTTP. For security, most requests to AWS must be signed with an access key. The access key consists of an access key ID and secret access key, which are commonly referred to as your security credentials

    AWS documentation on Signature Version 4 signing process

    Install

    npm install --save aws-sigv4-fetch

    Usage

    This package exports a function createSignedFetcher that returns a fetch function to automatically sign HTTP requests with AWS Signature V4 for the given AWS service and region. The credentials can be passed to the function directly, or they will be retrieved from the environment by defaultProvider() from package @aws-sdk/credential-provider-node.

    import { createSignedFetcher } from 'aws-sigv4-fetch';
    
    const fetch = createSignedFetcher({ service: 'appsync', region: 'eu-west-1' });
    const url = 'https://mygraphqlapi.appsync-api.eu-west-1.amazonaws.com/graphql';
    
    const body = { a: 1 };
    
    const response = await fetch(url, {
      method: 'post',
      body: JSON.stringify(body),
      headers: {'Content-Type': 'application/json'}
    });
    
    const data = await response.json();

    Sign GraphQL Requests with graphql-request

    If you are using graphql-request as GraphQL library, you can easily sign all HTTP requests. The library has fetchoption to pass a custom fetch method:

    import { createSignedFetcher } from 'aws-sigv4-fetch';
    import { GraphQLClient } from 'graphql-request';
    
    const fetch = createSignedFetcher({ service: 'appsync', region: 'eu-west-1' });
    const url = 'https://mygraphqlapi.appsync-api.eu-west-1.amazonaws.com/graphql';
    
    const query = `
      mutation CreateItem($input: CreateItemInput!) {
        createItem(input: $input) {
          id
          createdAt
          updatedAt
          name
        }
      }
    `;
    
    const variables = {
      input: {
        name,
      },
    };
    
    const client = new GraphQLClient(url, {
      fetch: createSignedFetcher({ service: 'appsync', region }),
    });
    
    const result = await client.request(query, variables);

    Resources

    Keywords

    none

    Install

    npm i aws-sigv4-fetch

    DownloadsWeekly Downloads

    103

    Version

    1.2.2

    License

    ISC

    Unpacked Size

    10.1 kB

    Total Files

    6

    Last publish

    Collaborators

    • chriszirkel