-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 501b97d
Showing
10 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.yml] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- '14' | ||
- '12' | ||
- '10' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
MIT License | ||
|
||
Copyright (c) Sam Verschueren <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"name": "got4aws", | ||
"version": "0.0.0", | ||
"description": "Convenience wrapper for Got to interact with AWS v4 signed APIs", | ||
"license": "MIT", | ||
"repository": "SamVerschueren/got4aws", | ||
"author": { | ||
"name": "Sam Verschueren", | ||
"email": "[email protected]", | ||
"url": "https://github.com/SamVerschueren" | ||
}, | ||
"main": "dist/index.js", | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"scripts": { | ||
"test": "xo", | ||
"compile": "tsc -p ." | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
"got-plugin", | ||
"got", | ||
"aws", | ||
"aws4", | ||
"api", | ||
"sigv4", | ||
"request", | ||
"http", | ||
"https", | ||
"get", | ||
"url", | ||
"utility" | ||
], | ||
"peerDependencies": { | ||
"aws-sdk": "^2.700.0" | ||
}, | ||
"dependencies": { | ||
"aws4": "^1.10.0", | ||
"got": "^11.3.0" | ||
}, | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^0.7.0", | ||
"@types/aws4": "^1.5.1", | ||
"aws-sdk": "^2.700.0", | ||
"typescript": "^3.9.5", | ||
"xo": "^0.32.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# aws4got [![Build Status](https://travis-ci.org/samverschueren/got4aws.svg?branch=master)](https://travis-ci.org/samverschueren/got4aws) | ||
|
||
> Convenience wrapper for [Got](https://github.com/sindresorhus/got) to interact with [AWS v4 signed](https://docs.aws.amazon.com/general/latest/gr/signing_aws_api_requests.html) APIs | ||
## Install | ||
|
||
``` | ||
$ npm install got4aws | ||
``` | ||
|
||
## Usage | ||
|
||
Instead of: | ||
|
||
```ts | ||
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: | ||
|
||
```ts | ||
import got4aws from 'got4aws'; | ||
|
||
const got = got4aws(); | ||
|
||
(async () => { | ||
const {body} = await got.get('https://12abc34.execute-api.us-east-1.amazonaws.com/v0', { | ||
responseType: 'json' | ||
}); | ||
|
||
console.log(body); | ||
//=> {status: 'ok'} | ||
})(); | ||
``` | ||
|
||
If you want to load credentials from somewhere else, you can provide extra options to the factory function. | ||
|
||
```ts | ||
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', { | ||
responseType: 'json' | ||
}); | ||
|
||
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. | ||
|
||
```ts | ||
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', { | ||
responseType: 'json' | ||
}); | ||
|
||
console.log(body); | ||
//=> {status: 'ok'} | ||
})(); | ||
``` | ||
|
||
|
||
## API | ||
|
||
### got4aws(options?) | ||
|
||
Returns a [`Got`](https://github.com/sindresorhus/got) instance. | ||
|
||
#### options | ||
|
||
##### providers | ||
|
||
Type: [`Credentials | Credentials[]`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Credentials.html)<br> | ||
Default: [`EnvironmentCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EnvironmentCredentials.html) | ||
|
||
One ore more credential providers that are passed through to the [CredentialsProviderChain](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CredentialProviderChain.html). These providers are used to infer the AWS credentials used for signing the requests. | ||
|
||
##### service | ||
|
||
Type: `string`<br> | ||
Default: *inferred through URL* | ||
|
||
The name of the service to sign the request for. For example, when signing a request for API Gateway with a custom domain, this should be `execute-api`. | ||
|
||
##### region | ||
|
||
Type: `string`<br> | ||
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import got from 'got'; | ||
import * as AWS from 'aws-sdk'; | ||
import * as aws4 from 'aws4'; | ||
|
||
export interface GotAWSOptions { | ||
/** | ||
* A provider or a list of providers used to search for AWS credentials. If no providers are provided, | ||
* it will use the [EnvironmentCredentials](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EnvironmentCredentials.html) provider. | ||
* | ||
* See the [CredentialProviderChain](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CredentialProviderChain.html) documentation for more information. | ||
*/ | ||
providers?: AWS.Credentials | AWS.Credentials[]; | ||
|
||
/** | ||
* The AWS service the request is being signed for. Will try to be inferred by the URL if not provided. | ||
*/ | ||
service?: string; | ||
|
||
/** | ||
* The AWS region the request is executed in. Will try to be inferred by the URL if not provided. | ||
*/ | ||
region?: string; | ||
} | ||
|
||
/** | ||
* Create a Got instance which will automatically sign the requests with a [AWS Version 4 signature](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html). | ||
* | ||
* @param awsOptions - A provider or a list of providers used to search for AWS credentials. | ||
*/ | ||
const got4aws = (awsOptions: GotAWSOptions = {}) => { | ||
let credentialProviders: AWS.Credentials[] | undefined; | ||
|
||
if (awsOptions.providers) { | ||
credentialProviders = Array.isArray(awsOptions.providers) ? awsOptions.providers : [awsOptions.providers]; | ||
} | ||
|
||
// Setup the credential provider chain to retrieve the signature credentials | ||
const chain = new AWS.CredentialProviderChain(credentialProviders as any); | ||
|
||
return got.extend({ | ||
hooks: { | ||
beforeRequest: [ | ||
async options => { | ||
if ((options as any).isStream) { | ||
// Don't touch streams | ||
return; | ||
} | ||
|
||
// Make sure the credentials are resolved | ||
const credentials = await chain.resolvePromise(); | ||
|
||
const {url, headers} = options; | ||
|
||
// Map the request to something that is signable by aws4 | ||
const request = { | ||
protocol: url.protocol, | ||
host: url.host, | ||
path: url.pathname, | ||
headers, | ||
body: options.json ? JSON.stringify(options.json) : options.body, | ||
service: awsOptions.service, | ||
region: awsOptions.region | ||
}; | ||
|
||
aws4.sign(request, credentials); | ||
|
||
options.headers = request.headers; | ||
} | ||
] | ||
} | ||
}); | ||
}; | ||
|
||
export default got4aws; | ||
|
||
// For CommonJS default export support | ||
module.exports = got4aws; | ||
module.exports.default = got4aws; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
{ | ||
"extends": "@sindresorhus/tsconfig", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"target": "es2018", // Node.js 10 | ||
"lib": [ | ||
"es2018", | ||
"es2019.string" | ||
] | ||
}, | ||
"include": [ | ||
"source" | ||
] | ||
} |