Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support using direct injectable instance #928

Closed
shlomiassaf opened this issue Jul 31, 2018 · 4 comments
Closed

Support using direct injectable instance #928

shlomiassaf opened this issue Jul 31, 2018 · 4 comments

Comments

@shlomiassaf
Copy link

I'm submitting a...


[ ] Regression 
[x] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

@Injtectable Classes can't return their own instance, the instance will always be
a single instance of the prototype which is later get's values assigned to it from the new
instance created.

For example, if I have class XYZ {}, when Nest init's it will do this:

component.instance = Object.create(XYZ.prototype);

Laster when XYZ is needed and instantiated it will do this:

component.instance = Object.assign(component.instance, new XYZ());

See here:
https://github.com/nestjs/nest/blob/master/packages/core/injector/injector.ts#L63-L66

Expected behavior

Allow an opt-in option to use the returned value from the constructor (i.e. new XYZ()) directly.

What is the motivation / use case for changing the behavior?

Mainly, using Proxy classes and other sort of thing requiring the return of a different value.

Here is an example of a winston wrapper:

import { Injectable, Inject } from '@nestjs/common';
import { LeveledLogMethod, Logger, LoggerOptions, createLogger } from 'winston';

const WINSTON_PROXY_HANDLER: ProxyHandler<WLogger> = {
  has: (target: WLogger, p: PropertyKey) => Reflect.has(target.winston, p),
  get: (target: WLogger, p: PropertyKey, receiver: any) => Reflect.get(target.winston, p, target.winston),
  set: (target: WLogger, p: PropertyKey, value: any, receiver: any) => Reflect.set(target.winston, p, value, target.winston),
};

@Injectable()
export class WLogger {
  readonly winston: Logger;

  // for cli and npm levels
  error: LeveledLogMethod;
  warn: LeveledLogMethod;
  help: LeveledLogMethod;
  data: LeveledLogMethod;
  info: LeveledLogMethod;
  debug: LeveledLogMethod;
  prompt: LeveledLogMethod;
  http: LeveledLogMethod;
  verbose: LeveledLogMethod;
  input: LeveledLogMethod;
  silly: LeveledLogMethod;

  // for syslog levels only
  emerg: LeveledLogMethod;
  alert: LeveledLogMethod;
  crit: LeveledLogMethod;
  warning: LeveledLogMethod;
  notice: LeveledLogMethod;

  constructor(@Inject(WINSTON_LOGGER_OPTIONS) options?: LoggerOptions) {
    this.winston = createLogger(options);
    const proxy = new Proxy(this, WINSTON_PROXY_HANDLER);
    return proxy;
  }
}

Notice the last part, the constructor...

Now, I know I can use a factory provider to solve this, but it makes to solution cumbersome and requires extra steps. Proxies are powerful but not natively usable with nest.

I know it's a big deal and a core behaviour, this is why I suggest to add it as an opt-in feature.

@kamilmysliwiec
Copy link
Member

Fixed in 5.4.1

@willsoto
Copy link
Contributor

@kamilmysliwiec Nice! Thanks for adding this. Will there be documentation added? How can we opt-in to this behavior?

@kamilmysliwiec
Copy link
Member

It just fixed old issues (no API changes)

@lock
Copy link

lock bot commented Sep 24, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants