#Inject outside Angular2 application - dependency-injection

Is it possible to retrieve any #Injectable like:
var config = #Inject(Config);
Thanks

If you want to get new instances from dependency injection, you need a reference to the Injector then you can acquire new instances with
injector.get(Config);
How you can get a reference to Injector depends on where your code is.
In an Angular component or service, just inject it
constructor(private injector:Injector) {}
You can also just create your own injector like
var injector = Injector.resolveAndCreate([Car, Engine]);
where Car and Engine are the providers the injector can create instances for.
To get the injector of your Angular application to be used outside your Angular application you can use
Example from https://github.com/angular/angular/issues/4112#issuecomment-153811572
let appInjectorRef: Injector;
export const appInjector = (injector?: Injector):Injector => {
if (injector) {
appInjectorRef = injector;
}
return appInjectorRef;
};
bootstrap(App, [
Auth,
HTTP_PROVIDERS,
ROUTER_PROVIDERS,
Car,
Engine
]).then((appRef: ComponentRef) => {
// store a reference to the application injector
appInjector(appRef.injector);
});
let injector: Injector = appInjector();
injector.get(Car);

I found it easier to declare injector as a global variable so I could use it a bit easier.
In the file where you bootstrap angular:
declare global {
var injector: Injector;
}
bootstrap(/* your bootstrap stuff */).then((appRef) => {
injector = appRef.injector;
});
The above will give you access to an injector variable anywhere else in your code.
In the file where you need an instance of Config:
import { Config } from './path/to/config.service';
class TestClass {
private config: Config;
constructor() {
this.config = injector.get(Config);
}
}

Related

AWS CDK- Access resource from other stack

Suppose i have a resource, say stepfunction activity, in one stack. How can i access it's arn in other stack?
I found CfnConstruct suitables for exporting (https://docs.aws.amazon.com/cdk/api/latest/docs/#aws-cdk_core.CfnOutput.html). As of now i have used CfnConstruct to export it:
this.initiateValidationActivityArn = new cdk.CfnOutput(this, 'InitiateValidationActivityArn', {
value: igvsStateMachine.initiateValidationActivity.activityArn
});
Now how can i access it in other file. I have tried this:
ecsService.initiateValidationActivityArn.value
But value is private to construct so can't use it.
If you have the stacks in one deployable cdk-app, you can access the property value from the stack by making it accessible from outside / not-private.
What I recommend doing is to keep it readonly so that it can't be re-initialized from outside the stack.
// file: ./lib/first-stack.ts
// import statements
export class FirstStack extends Stack {
readonly vpc: IVpc;
constructor(...) {
// setup
this.vpc = new Vpc(this, "VPC", {...});
}
}
In the dependent stack, you can pass it via (custom) props.
// file: ./lib/second-stack.ts
export interface SecondStackProps extends StackProps {
importedVpc: IVpc;
}
export class SecondStack extends Stack {
constructor(scope: cdk.Construct, id: string, props: SecondStackProps) {
super(scope, id, props);
const importedVpc = props.importedVpc;
// do sth. with your imported resource
}
}
Why is that working you may ask...?
It works because CDK generates the Resource IDs and during the synth phase it can put the tokens for the resources inside the generated templates.
This doesn't work, when you have separated cdk-apps / deployments of stacks, because CDK can't resolve the (existing) Resource ID Tokens during cdk-synth.
With that, you need to have another approach:
// file: ./lib/first-stack-separated-deployment.ts
// import statements
export class FirstStackSeparatedDeployment extends Stack {
cfnVpcId: CfnOutput;
constructor(...) {
// setup
const vpc = new Vpc(this, "VPC", {...});
this.cfnVpcId= new cdk.CfnOutput(this, "FirstStackCfnVpcId", {
value: vpc.vpcId,
exportName: "UniqueNameForYourVpcId"
});
}
}
In the other stack, which requires the already deployed resource, you do the following:
// file: ./lib/second-stack-separated-deployment.ts
export class SecondStackSeparatedDeployment extends Stack {
constructor(...) {
// setup
const vpcId = Fn.importValue("UniqueNameForYourVpcId")
const importedVpc = ec2.Vpc.fromVpcAttributes(this, "ImportedVpc", {
vpcId: vpcId,
availabilityZones: [this.region],
})
// proceed further...
}
}
Basically, you take the exportName from the CfnOutput Construct as an identifier and import it via the core Fn.importValue.
With that approach, the first stack already needs to be deployed.

How to pass reflector to Nest.js global guard?

I am new to nest.js and I have a question.
I have a Roles Guard like this
import { CanActivate, ExecutionContext, Injectable } from '#nestjs/common';
import { Observable } from 'rxjs';
import { Reflector } from '#nestjs/core';
#Injectable()
export class RolesGuard implements CanActivate {
constructor(private readonly reflector: Reflector) {
}
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
const roles = this.reflector.get<string[]>('roles', context.getHandler());
if (!roles) {
return true;
}
const request = context.switchToHttp().getRequest();
const user = request.user;
return user.role.some(role => !!roles.find(item => item === role));
}
}
Now I want to use this guard as a global guard like this
app.useGlobalGuards(new RolesGuard())
But it says that I need to pass argument(the reflector) to the guard as I mentioned in the constructor, now will it be okay to initialize the reflector like this?
const reflector:Reflector = new Reflector();
app.useGlobalGuards(new RolesGuard(reflector))
Or is there a better way to do this?
On the official Nest JS fundamentals course, in lecture "54 Protect Routes with Guards", the instructor specifies it is not best practice to create instance of reflector yourself.
A better way to resolve dependencies is to create a common module, and register your guard there. That way, reflector instance is resolved by nest runtime and you can also specify imports array for any other dependencies.
import { Module } from '#nestjs/common';
import { APP_GUARD } from '#nestjs/core';
import { AuthTokenGuard } from './guards/auth-token.guard';
import { ConfigModule } from '#nestjs/config';
#Module({
imports: [ConfigModule],
providers: [
{
provide: APP_GUARD,
useClass: AuthTokenGuard,
},
],
})
export class CommonModule {}
app.useGlobalGuards(new RolesGuard(new Reflector()));
It is working also. Could not find any better solution.
Although my answer might not add much value, I just want to re-iterate that is the intended way to get the reflector, this is a quote from NestJS's creator
kamilmysliwiec
When you create instance manually, you can create Reflector by
yourself:
new RoleGuard(new Reflector());
Source: https://github.com/nestjs/nest/issues/396#issuecomment-363111707
2023, NestJs 9, related problem:
In case you inject request-scoped dependency into the globally registered guard, the reflector will be undefined.
You can solve this issue by resolving such dependencies using ContextIdFactory and moduleRef.resolve() instead of injecting them normally:
const req = context.switchToHttp().getRequest();
const contextId = ContextIdFactory.getByRequest(req);
this.moduleRef.registerRequestByContextId(req, contextId);
this.authorizationService = await this.moduleRef.resolve(
RequestScopedService,
contextId
);
References:
https://docs.nestjs.com/fundamentals/module-ref
code example
https://discord.com/channels/520622812742811698/1060904277607985172

Custom ViewResolver and dependency injection

I'm attempting to create a custom ViewResolver class (extending angular's built-in class) to augment my systems' style metadata with a custom service (I'm loading my styles from external systems). However I'm running into a problem with the dependency injection system and ViewResolver.
I have my system setup something like the following:
Boot.ts:
bootstrap(App, [
MyStyleService, // my custom service
SomeOtherService, // another custom service used by MyStyleService
{
provide: ViewResolver,
useClass: MyViewResolver // my custom ViewResolver
}
])
MyViewResolver.ts:
#Injectable()
export class MyViewResolover extends ViewResolver {
constructor(
private _reflector: ReflectorReader,
// I want to reference 'StyleService' from the providers array in boot.ts
private _styleService: StyleService
) {
super(_reflector);
}
public resolve(comopnent: Type) {
let meta: ViewMetadata = super.resolve(component);
let styles = this._styleService.someMethod(meta);
}
}
However inside MyViewResolver, this._styleService has NOT been injected and is currently undefined. It should be noted that MyStyleService also depends on another injected service SomeOtherService, so I need to make sure that that provider is also defined and available for the injector.
I want all of these services to be "provided" by the bootstrap, so that in the future I can provide alternate versions of any of my services on a per-system basis.
For reference this is angular's core ViewResolver:
view_resolver.ts (Angular2 core):
import {Injectable, ViewMetadata, ComponentMetadata,} from '#angular/core';
import {ReflectorReader, reflector} from '../core_private';
import {Type, stringify, isBlank, isPresent} from '../src/facade/lang';
import {BaseException} from '../src/facade/exceptions';
import {Map} from '../src/facade/collection';
#Injectable()
export class ViewResolver {
constructor(private _reflector: ReflectorReader = reflector) {}
resolve(component: Type): ViewMetadata {
... stuff here ...
}
}
You could try to configure your class with useFactory:
bootstrap(App, [
MyStyleService,
SomeOtherService,
{
provide: ViewResolver,
useFactory: (_reflector: ReflectorReader, styleService: StyleService) => {
return MyViewResolver(_reflector, _styleService);
},
deps: [ ReflectorReader, StyleService ]
}
]);

Angular 2 dependency injection for decorators

Is there any way to inject a service dependency into a #Component decoration, something like this?
#Component({
selector: injectedService.getPrefix() + 'my-component'
})
export class MyComponent { }
Or, if not, might it be possible to subsclass #Component and inject a dependency into the subclass to achieve a similar result?
update >= RC.5
#NgModule({
...
})
export class AppModule {
ngDoBootstrap(moduleRef) {
appInjector(moduleRef.injector);
}
}
appInjector implementation see below
original <= RC.5
This is not directly supported by Angular2. You can store the injector outside of your Angular app and then reference it from there like demonstrated as a workaround for the #CanActivate() decorator in https://github.com/angular/angular/issues/4112#issuecomment-153811572.
(Plunker example)
In main.ts the injector is assigned to appInjector
bootstrap(App, [
Auth,
HTTP_PROVIDERS,
ROUTER_PROVIDERS,
provide(LocationStrategy, {useClass: HashLocationStrategy})
]).then((appRef: ComponentRef) => {
// store a reference to the application injector
appInjector(appRef.injector);
});
app-injector.ts
let appInjectorRef: Injector;
export const appInjector = (injector?: Injector):Injector => {
if (injector) {
appInjectorRef = injector;
}
return appInjectorRef;
};
then you can get a reference to the injector like
appInjector()...
This won't work if the component is created before bootstrap() is completed.

Angular2 - Use Dependency Injection outside Components

In my app I use DI to pass information about the UserLogged around the different Components that need such info.
This means that I have a main.ts class like this
import {AppComponent} from './app.component';
import {UserLogged} from './userLogged'
bootstrap(AppComponent, [UserLogged]);
and the components that need to use the instance of UserLogged have a constructor like this
constructor(private _user: UserLogged)
Now I would like to use the same instance of UserLogged also in simple TypeScript classes (which are not #Component). Is this possible? In other words, can I get hold of the same instance of UserLogged injected by DI also if I am outside a #Component?
This constructor also works for services (other classes created by DI)
bootstrap(AppComponent, [OtherClass, UserLoggged]);
#Injectable()
export class UserLogged {
log(text) {
console.log(text);
}
}
#Injectable()
export class OtherClass {
constructor(private _user: UserLogged) {}
}
class SomeComponent {
constructor(private otherClass:OtherClass) {
this.otherClass._user.log('xxx');
}
}
If you create these classes using new SomeClass() then you can inject it like
class SomeComponent {
constructor(private _injector:Injector) {
let userLog = this._injector.get(UserLogged);
new SomeClass(userLog);
}
}
In the file where you bootstrap angular:
import { AppComponent } from './app.component';
import { UserLogged } from './userLogged';
declare global {
var injector: Injector;
}
bootstrap(AppComponent, [UserLogged]).then((appRef) => {
injector = appRef.injector;
});
In your other file:
import { UserLogged } from '../path/to/userLogged';
class TestClass {
private userLogged: UserLogged;
constructor() {
this.userLogged = injector.get(UserLogged);
}
}
To be able to use dependency injection in classes you need to have a decorator, the #Injectable one.
#Injectable()
export class SomeClass {
constructor(private dep:SomeDependency) {
}
}
The name Injectable isn't really self-explanatory. It's to make possible the injection within the class (and not into another class).
The provider for the SomeDependency class need to be visible from the component that initiates the call.
See this question for more details about dependency injection and hierarchical injectors:
What's the best way to inject one service into another in angular 2 (Beta)?
Im taking a wild guess here, but do you mean to say you want to Inject the class with having to use providers : [UserLogged] ?
If that is the case, this will work
providers: [ provide(UserLogged, {useClass: UserLogged} ]
add the above to your bootstrap and you are good to go when you 'do not want to use #Component'
sample.ts
export class Sample{
constructor(private ulog : UserLogged){}
}
In your case the bootstrap would be :
import {provide} from 'angular2/core';
import {HTTP_PROVIDERS} from 'angular2/http';
bootstrap(AppComponent,[HTTP_PROVIDERS,provide(UserLogged, { useClass : UserLogged})]);
Ive added the HTTP_PROVIDERS to demonstrate how to add multiple providers.
Cheers!

Resources