Package Exports
- aws-sigv4-fetch
- aws-sigv4-fetch/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (aws-sigv4-fetch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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-fetchUsage
This package exports a function createSignedFetcher that returns a fetch function to automtaically 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 via 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
The package graphql-request allows to pass a custom fetch method as option:
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);