Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
ES6 imports for $http, $log, and other Angular 1 services
# Using Yarn:
yarn add ngimport angular
# Or, using NPM:
npm install ngimport angular --save
If you're using ngResource, also install ngimport-ngresource.
Note: This example is in TypeScript, but it works equally well in plain JavaScript.
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>
}
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
})
}
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 => ...)
}
}
})
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 => ...)
}
}
})
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.
$provide
in your unit tests, as usual$httpBackend
in your unit tests, as usualYou can easily use the same technique that ngimport uses to expose your own, legacy Angular 1 modules via ES6 import
s. 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.
$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.MIT
npm test
FAQs
Easy to use ES6 imports for $http, $log, and other Angular 1 services
The npm package ngimport receives a total of 5,911 weekly downloads. As such, ngimport popularity was classified as popular.
We found that ngimport demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.