Code Monkey home page Code Monkey logo

angular-aws-apig's Introduction

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.module('app', ['angular-aws-apig'])
.config(function Config($httpProvider, APIGInterceptorProvider) {
	APIGInterceptorProvider.config({
		headers: {},
		region: 'us-east-1',
		service: 'execute-api',
		urlRegex: ''
	})

	/* @ngInject */
	APIGInterceptorProvider.headersGetter = function(myService, request) {
		myService.doSomething();
		return request.headers;
	};

	/* @ngInject */
	APIGInterceptorProvider.credentialsGetter = function(store, request) {
		return store.get('credentials');
	};

	$httpProvider.interceptors.push('APIGInterceptor');
});

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

APIGInterceptorProvider.urlRegex = '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.headersGetter = function($rootScope, request) {
		var headers = request.headers
		headers.foo = $rootScope.foo;
		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.credentialsGetter = function(awsCredentials, auth) {
		return awsCredentials.get(auth.idToken);
	};

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.$on('$APIGError', (event, error) => {
  $log.debug(event, error);
});

Credits

This library is a wrapper around aws4 npm package.

angular-aws-apig's People

Contributors

jimmyn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

angular-aws-apig's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.