Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "ngimport", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Finally, imports for Angular 1 builtins!", | ||
@@ -12,3 +12,4 @@ "main": "./ngimport.js", | ||
"test": "ava", | ||
"tdd": "npm test -- --watch" | ||
"tdd": "npm test -- --watch", | ||
"postinstall": "typings install" | ||
}, | ||
@@ -32,3 +33,6 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/bcherny/ngimport#readme" | ||
"homepage": "https://github.com/bcherny/ngimport#readme", | ||
"dependencies": { | ||
"typings": "^0.7.12" | ||
} | ||
} |
111
README.md
# ngimport [![Circle CI](https://circleci.com/gh/bcherny/ngimport/tree/master.svg?style=svg)](https://circleci.com/gh/bcherny/ngimport/tree/master) | ||
> Finally, imports for Angular 1 builtins! | ||
> A saner alternative to Angular 1 dependency injection | ||
@@ -9,7 +9,24 @@ **docs and tests coming soon...** | ||
With ngimport: | ||
### Before: | ||
```ts | ||
// 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> | ||
} | ||
``` | ||
### After: | ||
```ts | ||
import {IPromise} from 'angular' | ||
@@ -24,18 +41,8 @@ import {$http, $log} from 'ngimport' | ||
} | ||
``` | ||
// Contents of MyComponent.ts: | ||
## Full Example | ||
import {Get} from './Get' | ||
### Before: | ||
angular.component('MyComponent', { | ||
controller: class MyComponentController { | ||
get() { | ||
Get('/foo').then(data => ...) | ||
} | ||
} | ||
}) | ||
``` | ||
Without ngimport: | ||
```ts | ||
@@ -76,10 +83,78 @@ // Contents of Get.ts: | ||
### After: | ||
```ts | ||
// 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 => ...) | ||
} | ||
} | ||
}) | ||
``` | ||
## 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 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. | ||
### 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 | ||
### Limitations of this approach | ||
- All 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: | ||
```ts | ||
// 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') } | ||
``` | ||
#### Specifically when using just the builtin imports included in this library | ||
TODO | ||
## Limitations of this approach | ||
#### Specifically when using this technique to wrap your own legacy modules | ||
- 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! | ||
## Backporting existing code | ||
TODO | ||
TODO | ||
## License | ||
@@ -95,2 +170,2 @@ | ||
- Add support for $animate, $animateCss, $aria, $cookies, $provide, $resource, $rootRouter, $route, $routeParams, $routerRootComponent, $sanitize, $swipe, $touch | ||
- Add support for $animate, $animateCss, $aria, $cookies, $provide, $resource, $rootRouter, $route, $routeParams, $routerRootComponent, $sanitize, $swipe, $touch |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16401
168
1
1
+ Addedtypings@^0.7.12
+ Addedabbrev@1.1.1(transitive)
+ Addedagent-base@2.1.1(transitive)
+ Addedansi-regex@2.1.15.0.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedany-promise@1.3.0(transitive)
+ Addedarchy@1.0.0(transitive)
+ Addedarray-uniq@1.0.3(transitive)
+ Addedarrify@1.0.1(transitive)
+ Addedasync@0.9.2(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbluebird@3.7.2(transitive)
+ Addedboxen@0.3.1(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbuffer-from@1.1.2(transitive)
+ Addedcapture-stack-trace@1.0.2(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedclone@1.0.4(transitive)
+ Addedcode-point-at@1.1.0(transitive)
+ Addedcolumnify@1.6.0(transitive)
+ Addedcombined-stream@0.0.7(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedconcat-stream@1.6.2(transitive)
+ Addedconfigstore@2.1.0(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedcreate-error-class@3.0.2(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddeep-extend@0.6.0(transitive)
+ Addeddefaults@1.0.4(transitive)
+ Addeddelayed-stream@0.0.5(transitive)
+ Addeddetect-indent@4.0.0(transitive)
+ Addeddot-prop@3.0.0(transitive)
+ Addedduplexer2@0.1.4(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedfilled-array@1.1.0(transitive)
+ Addedform-data@0.2.0(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedgot@5.7.1(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhas@1.0.4(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedhttp-proxy-agent@1.0.0(transitive)
+ Addedhttps-proxy-agent@1.0.0(transitive)
+ Addedimurmurhash@0.1.4(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedini@1.3.8(transitive)
+ Addedinvariant@2.2.4(transitive)
+ Addedis-absolute@0.2.6(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-finite@1.1.0(transitive)
+ Addedis-fullwidth-code-point@1.0.0(transitive)
+ Addedis-npm@1.0.0(transitive)
+ Addedis-obj@1.0.1(transitive)
+ Addedis-plain-obj@1.1.0(transitive)
+ Addedis-redirect@1.0.0(transitive)
+ Addedis-relative@0.2.1(transitive)
+ Addedis-retry-allowed@1.2.0(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedis-unc-path@0.1.2(transitive)
+ Addedis-utf8@0.2.1(transitive)
+ Addedis-windows@0.2.0(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisobject@3.0.1(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedlatest-version@2.0.0(transitive)
+ Addedlistify@1.0.3(transitive)
+ Addedlockfile@1.0.4(transitive)
+ Addedloose-envify@1.4.0(transitive)
+ Addedlowercase-keys@1.0.1(transitive)
+ Addedmake-error@1.3.6(transitive)
+ Addedmake-error-cause@1.2.2(transitive)
+ Addedmethods@1.1.2(transitive)
+ Addedmime-db@1.12.0(transitive)
+ Addedmime-types@2.0.14(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedms@2.0.0(transitive)
+ Addednode-status-codes@1.0.0(transitive)
+ Addednopt@1.0.10(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedobject.pick@1.3.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedos-homedir@1.0.2(transitive)
+ Addedos-tmpdir@1.0.2(transitive)
+ Addedosenv@0.1.5(transitive)
+ Addedpackage-json@2.4.0(transitive)
+ Addedparse-json@2.2.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedpopsicle@5.0.1(transitive)
+ Addedpopsicle-proxy-agent@1.0.0(transitive)
+ Addedpopsicle-retry@2.0.0(transitive)
+ Addedpopsicle-status@1.0.2(transitive)
+ Addedprepend-http@1.0.4(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedpromise-finally@2.2.1(transitive)
+ Addedpsl@1.13.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedrc@1.2.8(transitive)
+ Addedread-all-stream@3.1.0(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedregistry-auth-token@3.4.0(transitive)
+ Addedregistry-url@3.1.0(transitive)
+ Addedrepeating@2.0.1(transitive)
+ Addedrimraf@2.7.1(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedsemver@5.0.35.7.2(transitive)
+ Addedsemver-diff@2.1.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedslide@1.1.6(transitive)
+ Addedsort-keys@1.1.2(transitive)
+ Addedstring-template@1.0.0(transitive)
+ Addedstring-width@1.0.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedstrip-ansi@3.0.16.0.1(transitive)
+ Addedstrip-bom@2.0.0(transitive)
+ Addedstrip-json-comments@2.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedthenify@3.3.1(transitive)
+ Addedthroat@2.0.2(transitive)
+ Addedtimed-out@3.1.3(transitive)
+ Addedtouch@1.0.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtypedarray@0.0.6(transitive)
+ Addedtypescript@1.8.9(transitive)
+ Addedtypings@0.7.12(transitive)
+ Addedtypings-core@0.2.16(transitive)
+ Addedunc-path-regex@0.1.2(transitive)
+ Addedunzip-response@1.0.2(transitive)
+ Addedupdate-notifier@0.6.3(transitive)
+ Addedurl-parse-lax@1.0.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addeduuid@2.0.3(transitive)
+ Addedwcwidth@1.0.1(transitive)
+ Addedwidest-line@1.0.0(transitive)
+ Addedwordwrap@1.0.0(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedwrite-file-atomic@1.3.4(transitive)
+ Addedxdg-basedir@2.0.0(transitive)
+ Addedxtend@4.0.2(transitive)
+ Addedzip-object@0.1.0(transitive)