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

ngimport

v1.0.0

Published

Easy to use ES6 imports for $http, $log, and other Angular 1 services

Downloads

42,440

Readme

ngimport Build Status npm mit

ES6 imports for $http, $log, and other Angular 1 services

Install

# Using Yarn:
yarn add ngimport angular

# Or, using NPM:
npm install ngimport angular --save

If you're using ngResource, also install ngimport-ngresource.

Example

Note: This example is in TypeScript, but it works equally well in plain JavaScript.

Before:

import {IHttpService, ILogService, IPromise} from 'angular'

angular.factory('Get', function($http: IHttpService, $log: ILogService) {
  return function(url: string): IPromise<string> {
    return $http.get(url).then(data => {
      $log.info('Got data!', data)
      return data
    })
  }
})

export interface Get {
  (url: string): IPromise<string>
}

After:

import {IPromise} from 'angular'
import {$http, $log} from 'ngimport'

export function Get(url: string): IPromise<string> {
  return $http.get(url).then(data => {
    $log.info('Got data!', data)
    return data
  })
}

Full Example

Before:

angular.module('myModule', [])

// Contents of Get.ts:

import {IHttpService, ILogService, IPromise} from 'angular'

angular.module('myModule').factory('Get', function(
  $http: IHttpService,
  $log: ILogService
) {
  return function(url: string): IPromise<string> {
    return $http.get(url).then(data => {
      $log.info('Got data!', data)
      return data
    })
  }
})

export interface Get {
  (url: string): IPromise<string>
}

// Contents of MyComponent.ts:

import {Get} from './Get'

angular.module('myModule').component('MyComponent', {
  controller: class MyComponentController {
    constructor(private Get: Get) {},
    get() {
      this.Get('/foo').then(data => ...)
    }
  }
})

After:

angular.module('myModule', ['bcherny/ngimport'])

// Contents of Get.ts:

import {IPromise} from 'angular'
import {$http, $log} from 'ngimport'

export function Get(url: string): IPromise<string> {
  return $http.get(url).then(data => {
    $log.info('Got data!', data)
    return data
  })
}

// Contents of MyComponent.ts:

import {Get} from './Get'

angular.module('myModule').component('MyComponent', {
  controller: class MyComponentController {
    get() {
      Get('/foo').then(data => ...)
    }
  }
})

Why?

Angular 1 DI made sense when there was no JavaScript module standard. But with the advent of CommonJS, and now ES Modules, Angular DI only makes your code less portable.

If you add TypeScript to the mix, you'll often find yourself repeating class interface definitions: you might create a typed service class, but because its dependencies are injected via a closure, you can't export the class directly, and instead need to create a second interface and export it instead! And if you use the class' constructor to inject dependencies, then you can't pass arguments to a new instance of your constructor!

With the ngimport approach, all of these issues are solved.

But the biggest benefit is your code becomes much more portable: you can mix and match Angular 1, Angular 2, or even React components with zero friction. And if you're using TypeScript, you can do all of this in a 100% typesafe way.

Upsides of this approach

  • No more ugly, proprietary DI! Use standard imports
  • No lock in: easy migration path to Angular2, React, etc.
  • Use constructors to pass in arguments, rather than for DI
  • Avoid duplicated TypeScript interface declarations
  • Mock Angular dependencies with $provide in your unit tests, as usual
  • Assert against HTTP requests with $httpBackend in your unit tests, as usual
  • Use it as an adapter to migrate your codebase to imports piece by piece

Using this technique to wrap your own legacy modules

You can easily use the same technique that ngimport uses to expose your own, legacy Angular 1 modules via ES6 imports. Let's say you have the following code:

// Contents of myModule.js:

angular
  .module('myModule', [])
  .service('fooService', function($http) {
    this.foo = function() {
      return $http.get('/url')
    }
  })

To consume fooService today, you need to DI it; instead, let's expose it and its typings so we can import it:

// Contents of fooService.ts:

import {IPromise, module} from 'angular'
export let fooService = undefined

interface FooService {
  foo: () => IPromise<{ data: string }>
}

module('myModule').run(function ($injector) {
  fooService = <FooService>$injector.get('fooService')
})

Voila! Now instead of DIing fooService, we can now simply write import {fooService} from './fooService'. We then have the freedom to migrate fooService to TypeScript/ES6 at our own pace.

Limitations

  • Angular builtins ($http, $rootScope) will be undefined until you bootstrap your app. This is due to the way Angular creates injectors. Be careful to either not use these builtins at the top level, or bootstrap the app before you do.
  • If transpiling to CommonJS, be careful to destructure the import rather than importing a default value. Otherwise when the exported reference updates, your consumer will still have a pointer to the old, undefined reference.

License

MIT

Running the tests

npm test

Todo

  • Add support for $animate, $animateCss, $aria, $cookies, $provide, $resource, $rootRouter, $route, $routeParams, $routerRootComponent, $sanitize, $swipe, $touch