├── .gitignore ├── src ├── cloudinary │ ├── constants.ts │ ├── cloudinary.module.ts │ ├── cloudinary.provider.ts │ └── cloudinary.service.ts ├── main.ts ├── app.service.ts ├── app.module.ts └── app.controller.ts ├── .prettierrc ├── nest-cli.json ├── README.md ├── tsconfig.build.json ├── example.env ├── test ├── jest-e2e.json └── app.e2e-spec.ts ├── tsconfig.json ├── .eslintrc.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .env 3 | dist -------------------------------------------------------------------------------- /src/cloudinary/constants.ts: -------------------------------------------------------------------------------- 1 | export const CLOUDINARY = 'Cloudinary'; 2 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NestJS-Cloudinary 2 | An example of how to use the Cloudinary package to upload images to this service. 3 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /example.env: -------------------------------------------------------------------------------- 1 | # Default config 2 | PORT=Port that the API will be exposed 3 | 4 | # Cloudinary credentials 5 | CLD_CLOUD_NAME=Your cloudinary cloud name. 6 | CLD_API_KEY=Your cloudinary api key. 7 | CLD_API_SECRET=Your cloudinary api secret. -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(process.env.PORT); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /src/cloudinary/cloudinary.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { CloudinaryProvider } from './cloudinary.provider'; 3 | import { CloudinaryService } from './cloudinary.service'; 4 | 5 | @Module({ 6 | providers: [CloudinaryProvider, CloudinaryService], 7 | exports: [CloudinaryProvider, CloudinaryService], 8 | }) 9 | export class CloudinaryModule {} 10 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/cloudinary/cloudinary.provider.ts: -------------------------------------------------------------------------------- 1 | import { v2 } from 'cloudinary'; 2 | import { CLOUDINARY } from './constants'; 3 | export const CloudinaryProvider = { 4 | provide: CLOUDINARY, 5 | useFactory: (): void => { 6 | return v2.config({ 7 | cloud_name: process.env.CLD_CLOUD_NAME, 8 | api_key: process.env.CLD_API_KEY, 9 | api_secret: process.env.CLD_API_SECRET, 10 | }); 11 | }, 12 | }; 13 | -------------------------------------------------------------------------------- /src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, BadRequestException } from '@nestjs/common'; 2 | import { CloudinaryService } from './cloudinary/cloudinary.service'; 3 | 4 | @Injectable() 5 | export class AppService { 6 | constructor(private cloudinary: CloudinaryService) {} 7 | async uploadImageToCloudinary(file: Express.Multer.File) { 8 | return await this.cloudinary.uploadImage(file).catch(() => { 9 | throw new BadRequestException('Invalid file type.'); 10 | }); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | import { CloudinaryModule } from './cloudinary/cloudinary.module'; 5 | import { ConfigModule } from '@nestjs/config'; 6 | 7 | @Module({ 8 | imports: [ 9 | CloudinaryModule, 10 | ConfigModule.forRoot({ 11 | envFilePath: '.env', 12 | }), 13 | ], 14 | controllers: [AppController], 15 | providers: [AppService], 16 | }) 17 | export class AppModule {} 18 | -------------------------------------------------------------------------------- /src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { 2 | Controller, 3 | Post, 4 | UploadedFile, 5 | UseInterceptors, 6 | } from '@nestjs/common'; 7 | import { FileInterceptor } from '@nestjs/platform-express'; 8 | import { AppService } from './app.service'; 9 | 10 | @Controller() 11 | export class AppController { 12 | constructor(private readonly appService: AppService) {} 13 | 14 | @Post('upload') 15 | @UseInterceptors(FileInterceptor('file')) 16 | uploadImage(@UploadedFile() file: Express.Multer.File) { 17 | return this.appService.uploadImageToCloudinary(file); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/cloudinary/cloudinary.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { UploadApiErrorResponse, UploadApiResponse, v2 } from 'cloudinary'; 3 | import toStream = require('buffer-to-stream'); 4 | @Injectable() 5 | export class CloudinaryService { 6 | async uploadImage( 7 | file: Express.Multer.File, 8 | ): Promise { 9 | return new Promise((resolve, reject) => { 10 | const upload = v2.uploader.upload_stream((error, result) => { 11 | if (error) return reject(error); 12 | resolve(result); 13 | }); 14 | toStream(file.buffer).pipe(upload); 15 | }); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/recommended', 10 | 'plugin:prettier/recommended', 11 | ], 12 | root: true, 13 | env: { 14 | node: true, 15 | jest: true, 16 | }, 17 | ignorePatterns: ['.eslintrc.js'], 18 | rules: { 19 | '@typescript-eslint/interface-name-prefix': 'off', 20 | '@typescript-eslint/explicit-function-return-type': 'off', 21 | '@typescript-eslint/explicit-module-boundary-types': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nest-js-cloudinary", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^7.6.13", 25 | "@nestjs/config": "^0.6.3", 26 | "@nestjs/core": "^7.6.13", 27 | "@nestjs/platform-express": "^7.6.13", 28 | "buffer-to-stream": "^1.0.0", 29 | "cloudinary": "^1.25.1", 30 | "reflect-metadata": "^0.1.13", 31 | "rimraf": "^3.0.2", 32 | "rxjs": "^6.6.6" 33 | }, 34 | "devDependencies": { 35 | "@nestjs/cli": "^7.5.6", 36 | "@nestjs/schematics": "^7.2.7", 37 | "@nestjs/testing": "^7.6.13", 38 | "@types/express": "^4.17.11", 39 | "@types/jest": "^26.0.20", 40 | "@types/multer": "^1.4.5", 41 | "@types/node": "^14.14.31", 42 | "@types/supertest": "^2.0.10", 43 | "@typescript-eslint/eslint-plugin": "^4.15.2", 44 | "@typescript-eslint/parser": "^4.15.2", 45 | "eslint": "^7.20.0", 46 | "eslint-config-prettier": "^8.1.0", 47 | "eslint-plugin-prettier": "^3.3.1", 48 | "jest": "^26.6.3", 49 | "prettier": "^2.2.1", 50 | "supertest": "^6.1.3", 51 | "ts-jest": "^26.5.2", 52 | "ts-loader": "^8.0.17", 53 | "ts-node": "^9.1.1", 54 | "tsconfig-paths": "^3.9.0", 55 | "typescript": "^4.1.5" 56 | }, 57 | "jest": { 58 | "moduleFileExtensions": [ 59 | "js", 60 | "json", 61 | "ts" 62 | ], 63 | "rootDir": "src", 64 | "testRegex": ".*\\.spec\\.ts$", 65 | "transform": { 66 | "^.+\\.(t|j)s$": "ts-jest" 67 | }, 68 | "collectCoverageFrom": [ 69 | "**/*.(t|j)s" 70 | ], 71 | "coverageDirectory": "../coverage", 72 | "testEnvironment": "node" 73 | } 74 | } 75 | --------------------------------------------------------------------------------