Code Monkey home page Code Monkey logo

Comments (7)

BJS-kr avatar BJS-kr commented on September 2, 2024

can you provide your implementation details?
like, how do you create controllers using 'super'?

if you can tell me more about your case, I will add your case in the test

thanks

from nestjs-omacache.

moioo91120 avatar moioo91120 commented on September 2, 2024

So, I have a generic exchange controller

export class GenericExchangeController implements IGenericExchangeController {
  protected logger = new Logger(this.constructor.name);
  constructor(
    protected readonly exchangeService: ExchangeService,
    protected readonly bankSyncService: BankSyncService,
    protected readonly provider?: SourceProvider
  ) {
    if (!provider) this.provider = null;
  }

  @Get('getTicker24h')
  @CacheTTL(5 * MINUTES)
  @UseInterceptors(CacheInterceptor)
  async getTicker24h(@Query() params: ZTickersParamsDto): Promise<ITickerResponse[]> {
    return this.exchangeService.execPublic(this.provider, 'fetchTickers', params);
  }
...
}

and then multiple controllers for each provider using the generic controller

@Controller(`provider/binance`)
export class BinanceController extends GenericExchangeController implements IBinanceController {
  constructor(
    protected readonly exchangeService: ExchangeService,
    protected readonly bankSyncService: BankSyncService
  ) {
    super(exchangeService, bankSyncService, 'binance');
  }
....
}

@Controller(`provider/coinbase`)
export class CoinbaseController extends GenericExchangeController implements ICoinbaseController {
  constructor(
    protected readonly exchangeService: ExchangeService,
    protected readonly bankSyncService: BankSyncService
  ) {
    super(exchangeService, bankSyncService, 'coinbase');
  }
}

Then, endpoints calls are like
GET /provider/binance/getTicker24h
GET /provider/coinbase/getTicker24h

and so on.

from nestjs-omacache.

BJS-kr avatar BJS-kr commented on September 2, 2024

ah, now I get it clearly.

I have few ideas, but I'm trying not to be too hacky, which is dangerous for future nest's changes.
I'll share when I find out some good ways.

and of course, I'm always open, so tell me your ideas anytime

thank you for using this package

from nestjs-omacache.

moioo91120 avatar moioo91120 commented on September 2, 2024

I think a good way to handle it would be to use something like that

And construct the cache key name from a variable. That way, we can control it in controller as well as service and the user can chose how he wants to construct his cache key name (or prefix maybe ?) directly in the class constructor.

from nestjs-omacache.

BJS-kr avatar BJS-kr commented on September 2, 2024

thanks! I'll check it out

from nestjs-omacache.

BJS-kr avatar BJS-kr commented on September 2, 2024

how about using custom param decorator and use index of it?
for example,

export const FullUrl = createParamDecorator(
  (data: unknown, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    const fullUrl = ...

    return fullUrl
  },
);

and use it in parent class like

export class GenericExchangeController implements IGenericExchangeController {
  protected logger = new Logger(this.constructor.name);
  constructor(
    protected readonly exchangeService: ExchangeService,
    protected readonly bankSyncService: BankSyncService,
    protected readonly provider?: SourceProvider
  ) {
    if (!provider) this.provider = null;
  }

  @Get('getTicker24h')
  @CacheStorage({
    key: `getTicker24h`,
    kind: 'temporal',
    ttl: 60,
    paramIndex: [0,1],
  })
  async getTicker24h(@Query() params: ZTickersParamsDto, @FullUrl() fullUrl: string): Promise<ITickerResponse[]> {
    return this.exchangeService.execPublic(this.provider, 'fetchTickers', params);
  }
...
}

I think this would do the job

from nestjs-omacache.

BJS-kr avatar BJS-kr commented on September 2, 2024

Should I close this issue?
Let me know if you need anything

from nestjs-omacache.

Related Issues (9)

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.