Reputation: 254
I'm writing a module for nestjs and I want to publish it by npm. the package is a dynamic module for minio.
The problem is that the module work perfectly when it's inside of nest application project but when I copy it to another folder and try to make a npm module out of it, exception handling becomes a problem.
this module contains a minio.service.ts
file as provider and in this service file there are some conditions that some exceptions like UnsupportedMediaTypeException
or PayloadTooLargeException
. when the module is within the nest project exception handling work pretty fine and has no problem but when I try to publish it as npm package all exceptions are thrown as InternalServerException
.
Here are some of important files:
// src/minio.module.ts
import { Module, DynamicModule, Global } from '@nestjs/common';
import {ClientOptions } from 'minio';
import { MinioService } from './minio.service';
import { MinioOptions } from './types/minio.options';
import {MINIO_CONFIG, MINIO_OPTIONS} from "./types/constants";
@Global()
@Module({})
export class MinioModule {
static register(
minioConfig: ClientOptions,
minioOptions: MinioOptions,
): DynamicModule {
return {
global: true,
module: MinioModule,
providers: [
{ provide: MINIO_CONFIG, useValue: minioConfig },
{ provide: MINIO_OPTIONS, useValue: minioOptions },
MinioService,
],
exports: [MinioService],
};
}
}
// src/minio.service.ts
import {
Injectable,
Logger,
InternalServerErrorException,
Inject,
NotFoundException,
UnsupportedMediaTypeException,
PayloadTooLargeException,
} from '@nestjs/common';
import { Response } from 'express';
import * as crypto from 'crypto';
import { ClientOptions, Client } from 'minio';
import { BufferedFile } from './types/buffered-file.interface';
import { DeleteFileResponse, UploadFileResponse } from './dto/response.dto';
import { IMinioService } from './types/minio-service.interface';
import { MinioOptions } from './types/minio.options';
import { UploadValidator } from './types/upload-validator.interface';
import { MINIO_CONFIG, MINIO_OPTIONS } from "./types/constants";
@Injectable()
export class MinioService implements IMinioService {
private readonly service;
constructor(
@Inject(MINIO_CONFIG) private minioConfig: ClientOptions,
@Inject(MINIO_OPTIONS) private minioOptions: MinioOptions,
) {
this.service = new Client(this.minioConfig);
}
async upload(
file: BufferedFile,
bucket: string,
validator: UploadValidator = null,
): Promise<string> {
if (file.size > 50000) {
throw new BadRequestException();
}
return this.service
.putObject(bucket, fileName, file.buffer, metaData)
.then(() => {
const url = `/${bucket}/${fileName}`;
return url;
})
.catch((err) => {
throw new InternalServerException(err.message);
});
}
}
if you need to look at the project here is the link to its repository:
https://github.com/nimasfl/nest-minio
Upvotes: 0
Views: 1759
Reputation: 70131
Make the @nestjs/
dependencies peerDepedencies
and devDependencies
. This way typescript
resovles classes is kind of funny, and it becomes an issue when the instnaceof
check is ran to see if the error is an instance of HttpException
Upvotes: 2