Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
A saner alternative to Angular 1 dependency injection
docs and tests coming soon...
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
})
}
// Contents of Get.ts:
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>
}
// Contents of MyComponent.ts:
import {Get} from './Get'
angular.component('MyComponent', {
controller: class MyComponentController {
constructor (private Get: Get) {},
get() {
this.Get('/foo').then(data => ...)
}
}
})
// 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.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 serves to make 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 usualAll imported references must be executed by Angular after they are resolved in their respective exports. So for example, the following will not work - you need to call your injectable after Angular has finished initializing all of its providers:
// bad
import {$http} from 'ngimport'
$http.get('/url') // FAIL! $http is not yet resolved
// good
import {$http} from 'ngimport'
export function get() { return $http.get('/url') }
TODO
TODO
TODO
MIT
TODO
FAQs
Easy to use ES6 imports for $http, $log, and other Angular 1 services
The npm package ngimport receives a total of 8,208 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
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.