fv-cms-server
Advanced tools
Comparing version 0.0.8 to 0.0.12
import { Express } from "express"; | ||
import { Cms, Options } from "./types"; | ||
export default function cms(app: Express, options: Options): Cms; | ||
import { Server, ServerOptions } from "./types"; | ||
export default function create(app: Express, options: ServerOptions): Server; |
@@ -44,7 +44,21 @@ "use strict"; | ||
const mime_types_1 = __importDefault(require("mime-types")); | ||
function cms(app, options) { | ||
function create(app, options) { | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
const tmpPath = os_1.default.tmpdir(); | ||
let prefix = (_a = options.prefix) !== null && _a !== void 0 ? _a : ""; | ||
if (prefix === "/") { | ||
prefix = ""; | ||
} | ||
// Remove trailing slash | ||
if (prefix.endsWith("/")) { | ||
prefix = prefix.replace(/\/$/, ""); | ||
} | ||
// Undefined option fallbacks | ||
const dataFilePath = (_c = (_b = options.data) === null || _b === void 0 ? void 0 : _b.path) !== null && _c !== void 0 ? _c : tmpPath; | ||
const uploadsPath = (_e = (_d = options.uploads) === null || _d === void 0 ? void 0 : _d.path) !== null && _e !== void 0 ? _e : tmpPath; | ||
const uploadMaxFileSize = (_g = (_f = options.uploads) === null || _f === void 0 ? void 0 : _f.maxSize) !== null && _g !== void 0 ? _g : 50 * 1024 * 1024; | ||
app.use((0, express_fileupload_1.default)({ | ||
limits: { fileSize: 50 * 1024 * 1024 }, | ||
limits: { fileSize: uploadMaxFileSize }, | ||
useTempFiles: true, | ||
tempFileDir: os_1.default.tmpdir(), | ||
tempFileDir: tmpPath, | ||
})); | ||
@@ -54,3 +68,3 @@ function getData(req, res) { | ||
try { | ||
const data = yield promises_1.default.readFile(options.dataFilePath, "utf-8"); | ||
const data = yield promises_1.default.readFile(dataFilePath, "utf-8"); | ||
const components = JSON.parse(data); | ||
@@ -69,3 +83,3 @@ res.json({ components }); | ||
const components = JSON.stringify(req.body, null, 2); | ||
yield promises_1.default.writeFile(options.dataFilePath, components, "utf-8"); | ||
yield promises_1.default.writeFile(dataFilePath, components, "utf-8"); | ||
res.json({ messages: { server: "Data saved" } }); | ||
@@ -87,4 +101,10 @@ } | ||
const filename = `${file.md5}.${mime_types_1.default.extension(file.mimetype)}`; | ||
yield file.mv(path.join(options.uploadDirPath, filename)); | ||
files.push(`${options.serverUrl}/files/${filename}`); | ||
yield file.mv(path.join(uploadsPath, filename)); | ||
// Remove trailing slash | ||
let url = options.url; | ||
if (url.endsWith("/")) { | ||
url = url.replace(/\/$/, ""); | ||
} | ||
const uploadsUrl = `${url}${prefix}`; | ||
files.push(`${uploadsUrl}/files/${filename}`); | ||
} | ||
@@ -103,12 +123,10 @@ catch (err) { | ||
} | ||
app.use("/files", express.static(options.uploadDirPath)); | ||
return { | ||
actions: { | ||
getData, | ||
saveData, | ||
upload, | ||
}, | ||
}; | ||
app.use(`${prefix}/files`, express.static(uploadsPath)); | ||
app.get(`${prefix}/`, getData); | ||
app.patch(`${prefix}/`, saveData); | ||
app.post(`${prefix}/upload`, upload); | ||
// @todo return an API for users of this package to interact with | ||
return {}; | ||
} | ||
exports.default = cms; | ||
exports.default = create; | ||
//# sourceMappingURL=index.js.map |
@@ -1,13 +0,12 @@ | ||
import { Request, Response } from "express"; | ||
export declare type Options = { | ||
serverUrl: string; | ||
uploadDirPath: string; | ||
dataFilePath: string; | ||
}; | ||
export declare type Cms = { | ||
actions: { | ||
getData: (req: Request, res: Response) => Promise<any>; | ||
saveData: (req: Request, res: Response) => Promise<any>; | ||
upload: (req: Request, res: Response) => Promise<any>; | ||
export declare type ServerOptions = { | ||
url: string; | ||
prefix?: string; | ||
uploads?: { | ||
path?: string; | ||
maxSize?: number; | ||
}; | ||
data?: { | ||
path?: string; | ||
}; | ||
}; | ||
export declare type Server = {}; |
{ | ||
"name": "fv-cms-server", | ||
"version": "0.0.8", | ||
"version": "0.0.12", | ||
"main": "build/index.js", | ||
"types": "build/index.d.ts", | ||
"types": "build/types.d.ts", | ||
"author": "Finer Vision", | ||
"license": "MIT", | ||
"scripts": { | ||
"package": "tsc -p tsconfig.json && tsconfig-replace-paths -p tsconfig.json", | ||
"prepublishOnly": "npm run package" | ||
"build": "tsc -p tsconfig.json" | ||
}, | ||
@@ -17,14 +16,13 @@ "devDependencies": { | ||
"mime-types": "^2.1.35", | ||
"prettier": "^2.5.1", | ||
"ts-node": "^10.4.0", | ||
"tsconfig-replace-paths": "^0.0.11", | ||
"typescript": "^4.5.4" | ||
"prettier": "^2.6.2", | ||
"ts-node": "^10.7.0", | ||
"typescript": "^4.6.4" | ||
}, | ||
"dependencies": { | ||
"express-fileupload": "^1.3.1" | ||
}, | ||
"peerDependencies": { | ||
"@types/node": ">= 14", | ||
"express": ">= 4" | ||
}, | ||
"dependencies": { | ||
"express-fileupload": "^1.3.1" | ||
} | ||
} |
import fs from "fs/promises"; | ||
import os from "os"; | ||
import * as path from "path"; | ||
import * as express from "express"; | ||
import { Express, Request, Response } from "express"; | ||
import * as express from "express"; | ||
import fileUpload, { UploadedFile } from "express-fileupload"; | ||
import mime from "mime-types"; | ||
import { Cms, Options } from "@/types"; | ||
import { Server, ServerOptions } from "./types"; | ||
export default function cms(app: Express, options: Options): Cms { | ||
export default function create(app: Express, options: ServerOptions): Server { | ||
const tmpPath = os.tmpdir(); | ||
let prefix = options.prefix ?? ""; | ||
if (prefix === "/") { | ||
prefix = ""; | ||
} | ||
// Remove trailing slash | ||
if (prefix.endsWith("/")) { | ||
prefix = prefix.replace(/\/$/, ""); | ||
} | ||
// Undefined option fallbacks | ||
const dataFilePath = options.data?.path ?? tmpPath; | ||
const uploadsPath = options.uploads?.path ?? tmpPath; | ||
const uploadMaxFileSize = options.uploads?.maxSize ?? 50 * 1024 * 1024; | ||
app.use( | ||
fileUpload({ | ||
limits: { fileSize: 50 * 1024 * 1024 }, // 50MB | ||
limits: { fileSize: uploadMaxFileSize }, // 50MB | ||
useTempFiles: true, | ||
tempFileDir: os.tmpdir(), | ||
tempFileDir: tmpPath, | ||
}) | ||
@@ -21,3 +37,3 @@ ); | ||
try { | ||
const data = await fs.readFile(options.dataFilePath, "utf-8"); | ||
const data = await fs.readFile(dataFilePath, "utf-8"); | ||
const components = JSON.parse(data); | ||
@@ -34,3 +50,3 @@ res.json({ components }); | ||
const components = JSON.stringify(req.body, null, 2); | ||
await fs.writeFile(options.dataFilePath, components, "utf-8"); | ||
await fs.writeFile(dataFilePath, components, "utf-8"); | ||
res.json({ messages: { server: "Data saved" } }); | ||
@@ -50,4 +66,10 @@ } catch (err) { | ||
const filename = `${file.md5}.${mime.extension(file.mimetype)}`; | ||
await file.mv(path.join(options.uploadDirPath, filename)); | ||
files.push(`${options.serverUrl}/files/${filename}`); | ||
await file.mv(path.join(uploadsPath, filename)); | ||
// Remove trailing slash | ||
let url = options.url; | ||
if (url.endsWith("/")) { | ||
url = url.replace(/\/$/, ""); | ||
} | ||
const uploadsUrl = `${url}${prefix}`; | ||
files.push(`${uploadsUrl}/files/${filename}`); | ||
} catch (err) { | ||
@@ -64,11 +86,9 @@ console.error(err); | ||
app.use("/files", express.static(options.uploadDirPath)); | ||
app.use(`${prefix}/files`, express.static(uploadsPath)); | ||
app.get(`${prefix}/`, getData); | ||
app.patch(`${prefix}/`, saveData); | ||
app.post(`${prefix}/upload`, upload); | ||
return { | ||
actions: { | ||
getData, | ||
saveData, | ||
upload, | ||
}, | ||
}; | ||
// @todo return an API for users of this package to interact with | ||
return {}; | ||
} |
@@ -1,15 +0,15 @@ | ||
import { Request, Response } from "express"; | ||
export type Options = { | ||
serverUrl: string; | ||
uploadDirPath: string; | ||
dataFilePath: string; | ||
export type ServerOptions = { | ||
url: string; | ||
prefix?: string; | ||
uploads?: { | ||
path?: string; | ||
maxSize?: number; | ||
}; | ||
data?: { | ||
path?: string; | ||
}; | ||
}; | ||
export type Cms = { | ||
actions: { | ||
getData: (req: Request, res: Response) => Promise<any>; | ||
saveData: (req: Request, res: Response) => Promise<any>; | ||
upload: (req: Request, res: Response) => Promise<any>; | ||
}; | ||
export type Server = { | ||
// | ||
}; |
@@ -18,9 +18,13 @@ { | ||
"moduleResolution": "node", | ||
"baseUrl": ".", | ||
"paths": { | ||
"@/*": ["src/*"] | ||
} | ||
"baseUrl": "." | ||
}, | ||
"watchOptions": { | ||
"watchFile": "useFsEvents", | ||
"watchDirectory": "useFsEvents", | ||
"fallbackPolling": "dynamicPriority", | ||
"synchronousWatchDirectory": true, | ||
"excludeDirectories": ["node_modules", "build"] | ||
}, | ||
"exclude": ["node_modules", "build"], | ||
"include": ["src/**/*"] | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13933
7
267