Skip to content

Use Inject outside class #2036

Answered by Romakita
lgmf asked this question in Q&A
Aug 11, 2022 · 2 comments · 17 replies
Discussion options

You must be logged in to vote

Use a middleware on class and/or endpoint and store data

This example show you, how you can create a custom decorator to collect metadata on class and/or on class method and register a middleware.

import {Context, Middleware, MiddlewareMethods, UseBeforeEach} from "@tsed/common";
import {StoreSet, useDecorators} from "@tsed/core";

interface MyMiddlewareParams {
  code: number;
}

@Middleware()
class MyMiddlewareImpl implements MiddlewareMethods {
  static params: MyMiddlewareParams;

  async use(@Context() ctx: Context) {
    const code = ctx.endpoint.store.get("code") || ctx.endpoint.parent.store.get("code");

    ctx.set("code", code);
  }
}

export function MyMiddleware(code: number) {

Replies: 2 comments 17 replies

Comment options

You must be logged in to vote
16 replies
@Romakita
Comment options

@lgmf
Comment options

@Romakita
Comment options

@lgmf
Comment options

@Romakita
Comment options

Comment options

You must be logged in to vote
1 reply
@lgmf
Comment options

Answer selected by lgmf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants