ntegral / nestjs-s3

Provides an injectable s3 client to provide s3 storage access from nestjs modules
ISC License
8 stars 3 forks source link

Type 'typeof S3' is not a constructor function type. #2

Closed incompletude closed 3 years ago

incompletude commented 4 years ago

Hello, I'm getting an error:

node_modules/@ntegral/nestjs-s3/dist/services/s3.service.d.ts:3:40 - error TS2507: Type 'typeof S3' is not a constructor function type.

3 export declare class S3Service extends S3 {

My package.json:

{
  "name": "poc",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "prettier": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "sort": "npx sort-package-json",
    "start": "nest start",
    "start:dev": "cross-env NODE_ENV=dev nest start --watch"
  },
  "dependencies": {
    "@nestjs/common": "^7.0.0",
    "@nestjs/config": "^0.5.0",
    "@nestjs/core": "^7.0.0",
    "@nestjs/graphql": "^7.5.5",
    "@nestjs/jwt": "^7.1.0",
    "@nestjs/passport": "^7.1.0",
    "@nestjs/platform-express": "^7.0.0",
    "@nestjs/typeorm": "^7.1.0",
    "@ntegral/nestjs-s3": "^1.0.3",
    "@ntegral/nestjs-sendgrid": "^1.0.0",
    "apollo-server-express": "^2.16.0",
    "aws-sdk": "^2.715.0",
    "bcryptjs": "^2.4.3",
    "dataloader": "^2.0.0",
    "dotenv": "^8.2.0",
    "graphql": "^15.3.0",
    "graphql-tools": "^6.0.13",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "pg": "^8.3.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.5.4",
    "typeorm": "^0.2.25",
    "typeorm-naming-strategies": "^1.1.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.0.0",
    "@nestjs/schematics": "^7.0.0",
    "@nestjs/testing": "^7.0.0",
    "@types/bcryptjs": "^2.4.2",
    "@types/express": "^4.17.3",
    "@types/jest": "^26.0.4",
    "@types/node": "^14.0.23",
    "@types/passport-jwt": "^3.0.3",
    "@types/supertest": "^2.0.8",
    "@typescript-eslint/eslint-plugin": "^3.6.1",
    "@typescript-eslint/parser": "^3.6.1",
    "cross-env": "^7.0.2",
    "eslint": "^7.4.0",
    "eslint-config-prettier": "^6.10.0",
    "eslint-plugin-import": "^2.20.1",
    "jest": "^26.1.0",
    "prettier": "^2.0.5",
    "prettier-plugin-organize-imports": "^1.1.1",
    "supertest": "^4.0.2",
    "ts-jest": "^26.1.2",
    "ts-loader": "^8.0.1",
    "ts-node": "^8.6.2",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^3.7.4"
  }
}

I this it doesn't support nest 7.

ntegral commented 4 years ago

are you still getting this error

incompletude commented 3 years ago

Yes,

node_modules/@ntegral/nestjs-s3/dist/services/s3.service.d.ts:3:40 - error TS2507: Type 'typeof S3' is not a constructor function type.

3 export declare class S3Service extends S3 {

This is my class. If you can help me to solve it, i will save the commit.

import { Injectable, NotFoundException } from "@nestjs/common"
import { InjectS3 } from "@ntegral/nestjs-s3"
import { UserInputError } from "apollo-server-express"
import * as S3 from "aws-sdk/clients/s3"
import { EnvConfig } from "~/common/env.config"
import { FileUploadInput } from "./dto/file-upload.input"
import { FileUploadObject } from "./dto/file-upload.object"
import { FileInput } from "./dto/file.input"

@Injectable()
export class FileService {
  public constructor(@InjectS3() private readonly s3: S3) {}

  public async upload(fileInput: FileInput, fileUploadInput: FileUploadInput): Promise<FileUploadObject> {
    if (!fileInput.mimetype.match(/(jpg|jpeg|png|gif|pdf)$/)) {
      throw new UserInputError("Invalid mimetype")
    }

    const key = `${fileUploadInput.slug}/${Math.floor(Date.now() / 1000)}.${fileInput.mimetype.split("/").slice(-1)[0]}`

    const params = { Bucket: EnvConfig.S3_BUCKET, Key: key, Body: fileInput.buffer }

    await this.s3.putObject(params).promise()

    return {
      key,
    }
  }

  public async download(key: string): Promise<string> {
    try {
      const params = { Bucket: EnvConfig.S3_BUCKET, Key: key, Expires: 5 * 60 }

      const url = await this.s3.getSignedUrlPromise("getObject", params)

      return url
    } catch (error) {
      throw new NotFoundException()
    }
  }
}
ntegral commented 3 years ago

I'll look into this.

ntegral commented 3 years ago

{ "name": "nestjs-s3", "version": "0.0.1", "description": "", "author": "", "private": true, "license": "UNLICENSED", "scripts": { "prebuild": "rimraf dist", "build": "nest build", "format": "prettier --write \"src//*.ts\" \"test/*/.ts\"", "start": "nest start", "start:dev": "nest start --watch", "start:debug": "nest start --debug --watch", "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}//*.ts\" --fix", "test": "jest", "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { "@nestjs/common": "^7.0.0", "@nestjs/config": "^0.5.0", "@nestjs/core": "^7.0.0", "@nestjs/platform-express": "^7.0.0", "@ntegral/nestjs-s3": "^1.0.3", "aws-sdk": "^2.766.0", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^6.5.4" }, "devDependencies": { "@nestjs/cli": "^7.0.0", "@nestjs/schematics": "^7.0.0", "@nestjs/testing": "^7.0.0", "@types/express": "^4.17.3", "@types/jest": "25.1.4", "@types/node": "^13.9.1", "@types/supertest": "^2.0.8", "@typescript-eslint/eslint-plugin": "^2.23.0", "@typescript-eslint/parser": "^2.23.0", "eslint": "^6.8.0", "eslint-config-prettier": "^6.10.0", "eslint-plugin-import": "^2.20.1", "jest": "^25.1.0", "prettier": "^1.19.1", "supertest": "^4.0.2", "ts-jest": "25.2.1", "ts-loader": "^6.2.1", "ts-node": "^8.6.2", "tsconfig-paths": "^3.9.0", "typescript": "^3.7.4" }, "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "rootDir": "src", "testRegex": ".spec.ts$", "transform": { "^.+\.(t|j)s$": "ts-jest" }, "coverageDirectory": "../coverage", "testEnvironment": "node" } }

ntegral commented 3 years ago

This is the package.json for the sample project I just tested using the library. I did not get the error that you noted above?

incompletude commented 3 years ago

Have you tried the one I sent you?

ntegral commented 3 years ago

Did you add the configuration for the module to your app.module file??

import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { S3Module } from '@ntegral/nestjs-s3'; import { AppController } from './app.controller'; import { AppService } from './app.service';

@Module({ imports: [ ConfigModule, S3Module.forRootAsync({ imports: [ConfigModule], useFactory: async (cfg: ConfigService) => ({ accessKeyId: cfg.get('S3_KEY'), secretAccessKey: cfg.get('S3_SECRET'), region: cfg.get('S3_REGION'), maxSockets: cfg.get('S3_MAXSOCKETS') }), inject: [ConfigService] }), ConfigModule.forRoot() ], controllers: [AppController], providers: [AppService], }) export class AppModule {}

See the sample app.module file that configures the module

incompletude commented 3 years ago

Yes, my module does contain this:

import { Module } from '@nestjs-common';
import { S3Module } from '@ntegral/nestjs-s3';

@Module({
  imports: [
    S3Module.forRoot({
      accessKeyId: 'aws_access_key_id',
      secretAccessKey: 'aws_secret_access_key',
      region?: 'aws_region_id',
      sessionToken?: 'aws_session_token', | null, 
      apiVersion?: 's3_api_version' //based on s3 api version //
    }),
  ],
})
export class AppModule {}
ntegral commented 3 years ago

I've not been able to recreate the error that you are seeing?

ntegral commented 3 years ago

Unable to reproduce this issue

incompletude commented 3 years ago

Sorry, i just got the time to build a test-app.

test-app-s3.zip

npm i
npm run start:dev

Most likely something related to tsconfig.json.

Can you reopen pls?

incompletude commented 3 years ago

@ntegral