mardi 29 décembre 2020

Passing injected dependencies to external functions

I have a NestJS application where I have some logic in regards to using AWS S3 buckets.

I have one module, which contains a service with a promiseWrapper function, to convert callback to async.

  private s3PromiseWrapper(action: string, params: S3ParamGetDTO): Promise<string> {
    console.log(params)
    return new Promise((resolve, reject) => {
      this.s3.getSignedUrl(
        action,
        {
          Bucket: params.bucketName,
          Key: params.key,
          Expires: params.expirationTimeSeconds,
        },
        (err: any, url: string) => {
          if (err) reject(err)
          resolve(url)
        }
      )
    })

No have another module, that needs to use the same function, so to not repeat the code I wanted to create a global function as a util function, but this requires me to pass the instance as an argument to the function.

  private s3PromiseWrapper(action: string, params: S3ParamGetDTO, s3Instance: S3): Promise<string> {
    console.log(params)
    return new Promise((resolve, reject) => {
      s3Instance.getSignedUrl(
        action,
        {
          Bucket: params.bucketName,
          Key: params.key,
          Expires: params.expirationTimeSeconds,
        },
        (err: any, url: string) => {
          if (err) reject(err)
          resolve(url)
        }
      )
    })

Is this anti-pattern in terms of dependency injection?

Aucun commentaire:

Enregistrer un commentaire