Giter Site home page Giter Site logo

kugacz / nj-request-scope Goto Github PK

View Code? Open in Web Editor NEW
22.0 1.0 7.0 429 KB

Performant request scope dependency injection for NestJS framework using express server

License: MIT License

TypeScript 100.00%
dependency-injection nestjs typescript ioc nestjs-library request scope

nj-request-scope's People

Contributors

jkalberer avatar kugacz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nj-request-scope's Issues

Any plans to implement other Spring like proxy based decorators?:)

As in the title, I love what you did here. I'd have to take some more time to dig in, but from what I saw your message on SO, this should be a proper way of doing this DI, I can't believe they haven't yet introduced it in the core framework, do you observe any caveats?
That being said, it could be a nice initiative to build around NestJS proper Injectables() so we could benefit from such a basic stuff like Cache, Validation etc...
Let me know what you think :)
Greetings

Request Scope Is Empty With Graph

@kugacz Request scope is empty for graphql only works for the RESTful is there any way to update the request scope context. I am currently using the interceptor approach which doesn't seem to be clean

---- Interceptor -------
@Injectable()
export class InterceptorUsingReqScopedProvider implements NestInterceptor {
  constructor(
    private readonly modRef: ModuleRef,
    private readonly contextProvider: ContextProvider
  ) {}

  async intercept(
    context: ExecutionContext,
    next: CallHandler
  ): Promise<Observable<unknown>> {
    const req = this.getRequest(context);
    if (req.baseUrl === '/graphql') {
      this.contextProvider.setRequest(req);
    }
    return next.handle();
  }
---- CONTEXT PROVIDER ------
import { Inject, Injectable, Scope } from '@nestjs/common';
import { Request } from 'express';
import { IncomingHttpHeaders } from 'http';
import { ClientSession } from 'mongodb';
import mongoose from 'mongoose';
import { NJRS_REQUEST, RequestScope } from 'nj-request-scope';

type RESTOrGraphQLRequest = Request;

@Injectable()
@RequestScope()
export class ContextProvider {
  private _request: any;
  private session: ClientSession;
  public headers: IncomingHttpHeaders;

  constructor(
    @Inject(NJRS_REQUEST) private readonly request: RESTOrGraphQLRequest
  ) {
    this._request = this.request; // NOTHING ON REQUEST
    this.headers = request.headers;
  }

  setRequest(request: Request) {
    this._request = request;
  }

  getUser() {
    return this._request.user;
  }

  getWorkspace() {
    return this._request.headers.workspace;
  }
}


--APP GRAPH SETTINGS ON APP MODULE ---
 GraphQLModule.forRootAsync({
      imports: [RequestScopeModule],
      inject: [NJRS_REQUEST],
      driver: ApolloFederationDriver,
      useFactory: (args) => {
        console.log(args);
        return {
          autoSchemaFile: {
            path: path.join(__dirname, 'schema.graphql'),
            federation: 2,
          },
          cors: true,
          context: async ({ req }) => {
            return { req };
          },
        } as GqlModuleOptions;
      },
    })

NotImplementedException when using @RequestScope()

I'm getting compilation error when using @requestScope() decorator.
Is there a missing part I should implement to use this ?

node_modules/nj-request-scope/lib/util/dynamic.object.handler.factory.js:27
            throw new NotImplementedException();
                  ^
Error: Not implemented. Feel free to implement.
    at Object.getPrototypeOf (//node_modules/nj-request-scope/lib/util/dynamic.object.handler.factory.js:27:19)
    at Function.[Symbol.hasInstance] (<anonymous>)
    at mapToProxy (node_modules/@nestjs/core/nest-factory.js:167:35)
    at Proxy.<anonymous> (//node_modules/@nestjs/core/nest-factory.js:182:32)
    at bootstrap (/src/main.ts:39:16)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Bump major when bumping dependencies to a new major

Hi Marek,

I noticed from 1.0.5 to 1.0.6 the dependencies to nesjts packages have been bumped from v8 to v9.
It's usually good pratice to issue a new major release when that happens, especially with nestjs. As npm deps resolution might install both v9 and v8 version without any modification in package.json.

No real work needed, but it might be useful for next major nestjs migration.
Apart maybe documenting how to install your extension for Nestjs v8 :

npm install --save --save-exact [email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.