UNPKG

next-easy

Version:
34 lines (31 loc) 1.66 kB
import nextConnect from 'next-connect'; import multer from 'multer'; var nextFileUploader = function (data) { var _a, _b, _c, _d; var upload = multer({ storage: multer["".concat((_a = data === null || data === void 0 ? void 0 : data.multerConfig) === null || _a === void 0 ? void 0 : _a.storage)]({ destination: ((_b = data === null || data === void 0 ? void 0 : data.multerConfig) === null || _b === void 0 ? void 0 : _b.des) || "./public/uploads", // @ts-ignore filename: function (_req, file, cb) { var _a; return cb(null, ((_a = data === null || data === void 0 ? void 0 : data.multerConfig) === null || _a === void 0 ? void 0 : _a.fileName) || file.originalname); }, }), }); var apiRoute = nextConnect({ onError: function (error, _req, res) { res .status(501) .json({ error: "Sorry something Happened! ".concat(error.message) }); }, onNoMatch: function (req, res) { res.status(405).json({ error: "Method '".concat(req.method, "' Not Allowed") }); }, }); apiRoute.use(upload.array(((_c = data === null || data === void 0 ? void 0 : data.multerConfig) === null || _c === void 0 ? void 0 : _c.uploadKey) || "theFiles")); (_d = data === null || data === void 0 ? void 0 : data.middlewares) === null || _d === void 0 ? void 0 : _d.map(function (item) { return apiRoute.use(item); }); var config = { api: { bodyParser: false, // Disallow body parsing, consume as stream }, }; return { apiRoute: apiRoute, config: config }; }; export { nextFileUploader };