function wrapOperation()

in modules/kms-keyring/src/kms_client_supplier.ts [121:160]


  function wrapOperation(
    client: Client,
    name: keyof AwsEsdkKMSInterface
  ): Client {
    const original = client[name]
    client[name] = async function wrappedOperation(
      this: Client,
      args: any
    ): Promise<any> {
      // @ts-ignore (there should be a TypeScript solution for this)
      const v2vsV3Response = original.call(client, args)
      const v2vsV3Promise =
        'promise' in v2vsV3Response ? v2vsV3Response.promise() : v2vsV3Response
      return v2vsV3Promise
        .then((response: any) => {
          clientsCache[region] = Object.assign(client, {
            encrypt,
            decrypt,
            generateDataKey,
          })
          return response
        })
        .catch(async (e: any) => {
          /* Errors from a KMS contact mean that the region is "live".
           * As such the client can be cached because the problem is not with the client per se,
           * but with the request made.
           */
          if (e.$metadata && e.$metadata.httpStatusCode) {
            clientsCache[region] = Object.assign(client, {
              encrypt,
              decrypt,
              generateDataKey,
            })
          }
          // The request was not successful
          return Promise.reject(e)
        })
    }
    return client
  }