npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular-apikeys

v1.1.1

Published

A Angular module with a service for a more elegant sharing and storing of your API keys

Downloads

2

Readme

angular-apikeys

A Angular module with a service for a more elegant sharing and storing of your API keys

Copyright (c) Ronny Haase, 2016.

Licensed under The MIT License.

https://github.com/ronnyhaase/angular-apikeys

Introduction

If you ever run into the problem of talking to a lot of APIs in your AngularJS application and needed a place to manage the API keys, this tiny, unfancy module with just one lonesome service (backed by unit tests) is for you!

It's also used as optional dependency by other modules and services implementing APIs, as a kind of registry. (More details here)

Why

Because it

  1. Decouples the problem of holding API keys
  2. Can be used as a generic way of handling API keys across projects
  3. Is more elegant than solving this with angular.constant or angular.value especially across multiple modules

Installation

Use npm:

$ npm install angular-apikeys

Or Bower:

$ bower install angular-apikeys

Or grab the latest release and add the JS file manually.

<script src="angular-apikeys.js"></script>

Or minified:

<script src="angular-apikeys.min.js"></script>

HowTo

The module is "apikeys" and it only consists of the service "apiKeys", which has the methods get(id), set(id, apikey) and has(id).

angular.module('myApp', ['apikeys']);

angular.module('myApp').run(['apiKeys', function (apiKeys) {
	fetchMyApiKeysSomehow().then(function(keys) {
		apiKeys.set('google.maps.geocode', keys.google.geocode);
	});
}]);

angular.module('myApp').factory('googleGeocode', ['apiKeys', function (apiKeys) {
	// ...
	$http({
		url: 'https://maps.googleapis.com/maps/api/geocode/json',
		params: {
			key: apiKey.has('google.maps.geocode')
				? apiKeys.get('google.maps.geocode')
				: '12345'
		}
		// ...
	});
	// ...
}]);

As I said: Its unfancy.

Both, key and value must be non-empty strings.

Angular-apikeys is unoptionated about how you name your keys, but the lower case dot notation, seems to make sense to me.

Also notice, that it's currently possible to overwrite existing keys with set(). But for now it didn't seem to make sense to add more logic around such problems.

That's it, the rest is up to you.


Also, since ApiKeys is a provider you can use it in the configuration phase of AngularJS, e.g. to do crazy things with $httpProvider:

angular.module('myOtherApp').config('apiKeysProvider', [function (apiKeys) {
	// Same API
	apiKeys.set('googlePlaces', '0987654321');
	apiKeys.get('googlePlaces');
}]).run(function (apiKeys) {
	// Already available for a long time...
	apiKeys.get('googlePlaces');
});

Things like read-only, more strict key/value validation or other crazy ideas may be added later, if necessary.

API Reference

Module: apikeys

Dependencies: none

apiKeys

Service (provider) in module apikeys

Description

Stores and delivers API keys.

Methods
get(id)

Returns a API key by it's ID.

Arguments

| Param | Type | Details | |-------|----------|-------------------------------| | id | String | The ID of the key to fetch |

Returns

String | undefined | false The key of the requested ID, or undefined if it's unknown, or false in case of an error.


has(id)

Returns if a API key ID is defined.

Arguments

| Param | Type | Details | |-------|----------|-------------------------------| | id | String | The ID of the key to look up |

Returns

Boolean If the API key is defined


set(id, apikey)

Sets or resets a API key.

Arguments

| Param | Type | Details | |--------|----------|--------------------------| | id | String | The ID of the key to set | | apikey | String | The API key to store |

Returns

Boolean If the API key was successfully stored

Using angular-apikeys as registry

Let's see this with an example.

Imagine you and your team build an application around Google Maps APIs which provide quiet a lot APIs, and you split modules and services - what totally makes sense.

Likely you want to reuse your services for Google Maps APIs across projects.

By injecting angular-apikeys without declaring it explicitly as dependency, your services can provide a generic way to provide them with API keys, but neither your API modules & services nor other applications/modules usign them need to use angular-apikeys.

This is how it could look like:

See on Plunker

// (No "hard" dependency to apiKeys)
angular.module('google.maps', []);

angular.module('google.maps')
  .provider('geocoding', function () {
    var ID_APIKEY = 'google.maps.geocoding'; // A Unique ID for our API key

    var apiKey = null;

    // For folks not using angular-apikeys
    this.setApiKey = function (key) {
      if (angular.isString(key)) {
        apiKey = key;
      }

      return !!apiKey;
    };

    this.$get = ['$injector', function ($injector) {
      // Check via $injector.has, $injector.get throws if service is unknown!
      var apiKeys = $injector.has('apiKeys')
          ? $injector.get('apiKeys')
          : null;

      // Prefer apiKeys (you don't have to, of course)
      if (apiKeys && apiKeys.has(ID_APIKEY)) {
        apiKey = apiKeys.get(ID_APIKEY);
      }

      // Can't live without
      if (!apiKey) {
        throw 'You must provide a API key via apiKeys or manually with setApiKey() via the provider'
        return null;
      }

      console.log('google.maps.geocoding is all set up!');

      // ... Your super awesome service code ...

      return {
        // ...
        _apiKey: apiKey
      };
    }];
  })
  .provider('geolocation', function () {
    var ID_APIKEY = 'google.maps.geolocation';

    // All the same as geocoding-service
    // ...
    this.$get = {};
  });



angular.module('myApp', ['apikeys', 'google.maps']);

// Now we set the expected ID...
angular.module('myApp').config(['apiKeysProvider', function (apiKeys) {
  apiKeys.set('google.maps.geocoding', '1234567890');
  apiKeys.set('google.maps.geolocation', '1234567891');
  // ...
}]);


// ... and geocoding automagically has it's API key
angular.module('myApp').run(['geocoding', function (geocoding) {
  console.log(geocoding._apiKey === '1234567890'); // true
}]);

Contributing

Please create an issue. If you add a pull request, try to respect my code style, check for JSHint and assure the unit tests do pass, and extend them if necessary!