angular-aws-apig
This library provides an Interceptor for angular $http
service that signs all request to AWS APIGateway with IAM credentials. It is handy when you use temprorary IAM Credentials from AWS Cognito
or Auth0
. Although AWS APIGateway provides autogenerated Javascript SDK, you can't use it with angular $http
service and you need to regenerate it every time you change something in the API.
Installing it
You have several options:
bower install angular-aws-apig --save
npm install angular-aws-apig --save
Or just include dist/angular-aws-apig.js
or dist/angular-aws-apig.min.js
in to your index.html
Basic usage
angular;
APIGInterceptorProvider.config
headers
- global headers that would be added to all api requests (default:{}
)region
- AWS region (default:us-east-1
)service
- AWS service (default:execute-api
)urlRegex
- RegEx string, Interceptor would ignore requests to url that doesn't match this RegEx. (default:''
)
All options could be passed in APIGInterceptorProvider.config
function as a single object or assigned directly
APIGInterceptorProviderurlRegex = 'myapi.com';
APIGInterceptorProvider.headersGetter
A function that provides dynamic headers. It accepts $http request
object as a parameter and must return headers
object. You can pass angular dependencies in this function.
APIGInterceptorProvider { var headers = requestheaders headersfoo = $rootScopefoo; return headers; };
APIGInterceptorProvider.credentialsGetter
A function that provides dynamic AWS IAM Credentials. It accepts $http request
object as a parameter and must return credentials
object. You can pass angular dependencies in this function. Function can return $q
promise.
If this function is not specified APIGInterceptor
will try to get credentials from AWS.config.credentials
APIGInterceptorProvider { return awsCredentials; };
In this example awsCredentials.get
returns a promise that resolves with credentials
object
accessKeyId: 'accessKeyId' secretAccessKey: 'secretAccessKey' sessionToken: 'sessionToken'
$APIGError event
This event would be triggered on request error.
$rootScope;
Credits
This library is a wrapper around aws4 npm package.