Convenience wrapper for Got to interact with AWS v4 signed APIs
$ npm install got4aws
Instead of:
import got from 'got';
import * as aws4 from 'aws4';
import * as url from 'url';
(async () => {
const {protocol, host, path} = url.parse('https://12abc34.execute-api.us-east-1.amazonaws.com/v0');
const request = {
protocol,
host,
path,
responseType: 'json'
};
aws4.sign(request);
const {body} = await got.get(request);
console.log(body);
//=> {status: 'ok'}
})();
You can do:
import got4aws from 'got4aws';
const got = got4aws();
(async () => {
const {body} = await got.get('https://12abc34.execute-api.us-east-1.amazonaws.com/v0');
console.log(body);
//=> {status: 'ok'}
})();
If you want to load credentials from somewhere else, you can provide extra options to the factory function.
import got4aws from 'got4aws';
// Load credentials from `~/.aws/credentials`
const got = got4aws({
providers: new AWS.SharedIniFileCredentials({profile: 'myProfile'})
});
(async () => {
const {body} = await got.get('https://12abc34.execute-api.us-east-1.amazonaws.com/v0');
console.log(body);
//=> {status: 'ok'}
})();
If you want to invoke an API Gateway endpoint with a custom domain, you will have to set the service
and region
as well because they can't be inferred.
import got4aws from 'got4aws';
const got = got4aws({
providers: new AWS.SharedIniFileCredentials({profile: 'myProfile'}),
service: 'execute-api',
region: 'eu-west-1'
});
(async () => {
const {body} = await got.get('https://api.unicorn.com/v0');
console.log(body);
//=> {status: 'ok'}
})();
Returns a Got
instance with a default responseType
set to json
.
Type: Credentials | Credentials[]
Default: EnvironmentCredentials
A provider or a list of providers used to search for AWS credentials. If no providers are provided, it will use the EnvironmentCredentials provider.
See the CredentialProviderChain documentation for more information.
Type: string
Default: inferred through URL
The AWS service the request is being signed for. Will try to be inferred by the URL if not provided.
For example, when signing a request for API Gateway with a custom domain, this should be execute-api
.
Type: string
Default: inferred through URL
The region of the service being invoked. If it could not be inferred through the URL, it will default to us-east-1
.