#How to return a void function from const?

17 messages · Page 1 of 1 (latest)

lapis sleet
#

Hi guys,
I did something like that for a service,

export default class LimiterService {


  public async login(loginLimiter: LoginLimiterType) {

    const {increment} = await this.applyLimiter({
      blockDuration: "30 mins",
      duration: "15 mins",
      requests: 2,
      request: loginLimiter.request,
      response: loginLimiter.response,
      throttleKey: loginLimiter.throttleKey
    })


    return {increment: () => increment()}

  }


  public async applyLimiter(genericLimiter: GenericLimiterType) {
    const limiter = await Limiter.use({
      requests: genericLimiter.requests,
      duration: genericLimiter.duration,
      blockDuration: genericLimiter.blockDuration,
    })

    if (await limiter.isBlocked(genericLimiter.throttleKey)) {
      return genericLimiter.response.tooManyRequests('Too many requests, please be patient.')
    }

    return {
      limiter,
      increment: () => limiter.increment(genericLimiter.throttleKey)
    }

  }

}

And I don't really appreciate the return {increment: () => increment()} and typescript is yelling at me.

winged burrow
#

can't you simply return { increment }?

oblique cypress
#

I don't see why you would get an error from that

#

I don't get any complaints on TS playground, although it's missing various types

formal barnBOT
#
sandiford#0

Preview:```ts
type LoginLimiterType = {
request: any
response: any
throttleKey: any
}

type GenericLimiterType = {
request: any
response: any
throttleKey: any
}

export default class LimiterService {
public async login(loginLimiter: LoginLimiterType) {
const incremen
...```

oblique cypress
#

this cut down form doesn't complain about returns, don't know why yours would

#

So perhaps explain the issue or show the error messages?

oblique cypress
winged burrow
oblique cypress
#

Well yeah

formal barnBOT
#
ascor8522#0

Preview:ts interface Response { tooManyRequests(message: string): unknown } interface LoginLimiterType { request: unknown response: Response throttleKey: boolean } interface GenericLimiterType { blockDuration: string requests: number duratio ...

winged burrow
#

@lapis sleet

formal barnBOT
#
sandiford#0

Preview:```ts
type ApplyLimiterResponse = Promise<{
limiter: Limiter
increment: () => unknown
}>

interface Response {
tooManyRequests(
message: string
): ApplyLimiterResponse
}
interface LoginLimiterType {
request: unknown
response: Response
throttleKey: boolean
...```

oblique cypress
#

Added a type to complete it

lapis sleet
#

buy everything's fine now with some research and your responses

#

ty