ImageKit Storage Provider

This provider adds the ImageKit Node SDK, validates IMAGEKIT_PRIVATE_KEY with Zod, and ships the same Multer memory-upload middleware pattern as the Cloudinary provider.

Features

  • Zod-validated ImageKit credentials
  • Shared ImageKit client instance from config
  • Multer memory storage with allowlist MIME types and 5 MB max file size
  • MVC and Feature architecture variants

Installation Guide

npx servercn-cli add pr imagekit-storage

File Structure

  • src/configs/env.ts
  • src/configs/imagekit.ts
  • src/middlewares/upload-file.ts
  • src/shared/configs/env.ts
  • src/shared/configs/imagekit.ts
  • src/shared/middlewares/upload-file.ts

Environment Configuration

src/configs/env.ts
import "dotenv-flow/config";
import { z } from "zod";
 
export const envSchema = z.object({
  IMAGEKIT_PRIVATE_KEY: z.string()
});
 
export type Env = z.infer<typeof envSchema>;
 
const result = envSchema.safeParse(process.env);
 
if (!result.success) {
  console.error("❌ Invalid environment configuration");
  console.error(z.prettifyError(result.error));
  process.exit(1);
}
 
export const env: Readonly<Env> = Object.freeze(result.data);
 
export default env;

ImageKit client

src/configs/imagekit.ts
import ImageKit from "@imagekit/nodejs";
import env from "./env";
 
const imagekitClient = new ImageKit({
  privateKey: env.IMAGEKIT_PRIVATE_KEY
});
 
export default imagekitClient;

The template file includes commented uploadToImageKit and deleteFileFromImageKit helpers using toFile from @imagekit/nodejs—use those as a starting point in a service layer.

Upload middleware (Multer)

src/middlewares/upload-file.ts
import multer from "multer";
 
export const ALLOWED_FILE_TYPES = [
  "image/jpeg",
  "image/png",
  "image/webp",
  "video/mp4",
  "video/mpeg",
  "video/quicktime",
  "application/pdf"
];
 
export const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
 
const storage = multer.memoryStorage();
 
const fileFilter: multer.Options["fileFilter"] = (_req, file, cb) => {
  if (!ALLOWED_FILE_TYPES.includes(file.mimetype)) {
    return cb(null, false);
  }
  cb(null, true);
};
 
const upload = multer({
  storage,
  limits: { fileSize: MAX_FILE_SIZE },
  fileFilter
});
 
export default upload;

Usage

Use upload.single("fieldName") (or array/fields) on your route, then upload req.file.buffer via the helpers described in the template’s imagekit.ts comments.

Example .env

IMAGEKIT_PRIVATE_KEY=your_private_key

File & Folder Structure

Loading files...

Installation

npx servercn-cli add provider imagekit-storage