@tsed/async-hook-context

Add Async Hook context support in Ts.ED

Usage no npm install needed!

<script type="module">
  import tsedAsyncHookContext from 'https://cdn.skypack.dev/@tsed/async-hook-context';
</script>

README

Ts.ED logo

@tsed/async-hook-context

Build & Release PR Welcome Coverage Status npm version semantic-release code style: prettier backers

Website   •   Getting started   •   Slack   •   Twitter

Add Async Hook context support in Ts.ED.

Usage

Ts.ED provide an injectable [RequestContext] to get all request and response information during a request. But, you have to inject the context from your controller and forward context instance manually to the service.

@Injectable()
export class CustomRepository {
  async findById(id: string, ctx: PlatformContext) {
    ctx.logger.info("Where are in the repository");
    return {
      id,
      headers: this.$ctx?.request.headers
    };
  }
}

@Controller("/async-hooks")
export class AsyncHookCtrl {
  @Inject()
  repository: CustomRepository;

  @Get("/:id")
  async get(@PathParams("id") id: string, @Context() ctx: PlatformContext) {
    return this.repository.findById(id, ctx);
  }
}

With this package, you can inject directly the PlatformContext in the service without injecting it in the controller:

@Injectable()
export class CustomRepository {
  @InjectContext()
  $ctx?: PlatformContext;

  async findById(id: string) {
    this.ctx?.logger.info("Where are in the repository");

    return {
      id,
      headers: this.$ctx?.request.headers
    };
  }
}

@Controller("/async-hooks")
export class AsyncHookCtrl {
  @Inject()
  repository: CustomRepository;

  @Get("/:id")
  async get(@PathParams("id") id: string) {
    return this.repository.findById(id);
  }
}

Installation

Async Hook context required Node.js at least v13.10.0. `

Install the @tsed/async-hook-context:

npm install --save @tsed/async-hook-context

Then import @tsed/async-hook-context in your Server:

import {Configuration} from "@tsed/common";
import "@tsed/async-hook-context";

@Configuration({})
export class Server {}

Contributors

Please read contributing guidelines here

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

The MIT License (MIT)

Copyright (c) 2016 - 2021 Romain Lenzotti

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.