@sumor/api-middleware
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "@sumor/api-middleware", | ||
"description": "API Middleware is a middleware for Node.JS. It can easily expose function to api, and validate parameters", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"license": "MIT", | ||
@@ -18,8 +18,11 @@ "repository": "sumor-cloud/api-middleware", | ||
"dependencies": { | ||
"@sumor/config": "^1.1.2", | ||
"@sumor/validator": "^1.0.2", | ||
"@sumor/logger": "^1.2.0", | ||
"body-parser": "^1.20.1" | ||
"@sumor/config": "^1.3.1", | ||
"@sumor/validator": "^1.2.0", | ||
"@sumor/logger": "^1.2.7", | ||
"@sumor/error": "^1.0.6", | ||
"body-parser": "^1.20.1", | ||
"multer": "^1.4.5-lts.1" | ||
}, | ||
"devDependencies": { | ||
"fs-extra": "^11.2.0", | ||
"@jest/globals": "^29.7.0", | ||
@@ -47,2 +50,3 @@ "axios": "^1.6.8", | ||
"lint": "eslint .", | ||
"autofix": "eslint --fix . && prettier --write . && npm audit fix --force", | ||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testMatch='**/test/**/*.test.js'", | ||
@@ -56,4 +60,4 @@ "coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --testMatch='**/test/**/*.test.js'", | ||
"prepare": "husky", | ||
"check": "eslint --fix . && prettier --write . && npm audit fix --force && npm run coverage" | ||
"check": "npm run autofix && npm run coverage" | ||
} | ||
} |
@@ -88,1 +88,19 @@ # api-middleware | ||
``` | ||
### context | ||
##### data | ||
It includes all parameters passed in the request | ||
file upload will be parsed as below object: | ||
- `name` uploaded file name | ||
- `size` uploaded file size(bytes) | ||
- `mime` uploaded file mime type(e.g. image/png) | ||
- `encoding` uploaded file encoding(e.g. 7bit) | ||
- `path` uploaded file path | ||
##### exposeApis | ||
It includes all exposed apis |
@@ -1,2 +0,63 @@ | ||
import host from './host.js' | ||
export default host | ||
import loadApi from './load.js' | ||
import bodyParser from './middleware/bodyParser.js' | ||
import checkData from './checkData.js' | ||
import fileClearUp from './fileClearUp.js' | ||
import clientEnv from './middleware/clientEnv.js' | ||
import errorCatcher from './error/errorCatcher.js' | ||
import errorMiddleware from './error/errorMiddleware.js' | ||
import sendResponse from './sendResponse.js' | ||
const exposeApis = {} | ||
export default async (app, path, options) => { | ||
options = options || {} | ||
options.prepare = options.prepare || function () {} | ||
options.finalize = options.finalize || function () {} | ||
options.exception = options.exception || function () {} | ||
const apisMeta = await loadApi(path, options.prefix) | ||
for (const path in apisMeta) { | ||
// add exposeApi to global object | ||
exposeApis[apisMeta[path].route] = { | ||
name: apisMeta[path].name, | ||
desc: apisMeta[path].desc, | ||
parameters: apisMeta[path].parameters | ||
} | ||
let middlewares = bodyParser(apisMeta[path].parameters) | ||
middlewares.push(clientEnv) | ||
middlewares.push(async function (req, res, next) { | ||
req.exposeApis = exposeApis | ||
next() | ||
}) | ||
middlewares.push(async function (req, res, next) { | ||
await options.prepare(req, res) | ||
req.data = checkData(req.data, apisMeta[path].parameters) | ||
const response = await apisMeta[path].program(req, res) | ||
await options.finalize(req, res) | ||
await fileClearUp(req) | ||
sendResponse(res, response) | ||
}) | ||
middlewares = middlewares.map(errorCatcher) | ||
middlewares.push(async (error, req, res, next) => { | ||
error.language = req.client.language | ||
req.logger.trace(error) | ||
await options.exception(req, res) | ||
next(error, req, res, next) | ||
}) | ||
middlewares.push(errorMiddleware) | ||
app.all(apisMeta[path].route, ...middlewares) | ||
} | ||
} |
@@ -1,7 +0,66 @@ | ||
import { findReference } from '@sumor/config' | ||
import { meta } from '@sumor/config' | ||
import { pathToFileURL } from 'url' | ||
import logger from './i18n/apiLogger.js' | ||
export default async root => { | ||
const meta = await findReference(`${root}`, ['js']) | ||
// console.log(`${root}`, meta) | ||
return meta | ||
const programCache = {} | ||
const loadProgram = async path => { | ||
if (!programCache[path]) { | ||
const result = {} | ||
try { | ||
result.program = await import(pathToFileURL(path)) | ||
} catch (e) { | ||
result.error = e | ||
} | ||
programCache[path] = result | ||
} | ||
return programCache[path] | ||
} | ||
export default async (path, prefix) => { | ||
prefix = prefix || '' | ||
const result = {} | ||
const apiMeta = await meta(path, ['js']) | ||
for (const path in apiMeta) { | ||
apiMeta[path].route = `${prefix}/${path.replace(/\./g, '/')}` | ||
apiMeta[path].name = apiMeta[path].name || '' | ||
apiMeta[path].desc = apiMeta[path].desc || '' | ||
apiMeta[path].parameters = apiMeta[path].parameters || {} | ||
let hasFile = false | ||
for (const parameter in apiMeta[path].parameters) { | ||
if (apiMeta[path].parameters[parameter].type === 'file') { | ||
hasFile = true | ||
break | ||
} | ||
} | ||
const filePath = apiMeta[path].js | ||
let program | ||
const programResult = await loadProgram(filePath) | ||
if (programResult.error) { | ||
apiMeta[path].error = 'syntaxError' | ||
logger.code('API_LOAD_FAILED_SYNTAX_ERROR', { path: apiMeta[path].route }) | ||
logger.error(programResult.error) | ||
} else { | ||
program = programResult.program | ||
} | ||
if (program) { | ||
if (program.default) { | ||
program = program.default | ||
if (hasFile) { | ||
logger.code('API_LOAD_SUCCESS_WITH_FILE', { path: apiMeta[path].route }) | ||
} else { | ||
logger.code('API_LOAD_SUCCESS', { path: apiMeta[path].route }) | ||
} | ||
} else { | ||
apiMeta[path].error = 'missingDefaultExport' | ||
logger.code('API_LOAD_FAILED_MISSING_DEFAULT', { path: apiMeta[path].route }) | ||
} | ||
apiMeta[path].program = program | ||
} | ||
result[path] = apiMeta[path] | ||
} | ||
return result | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
17813
16
422
106
6
12
6
1
+ Added@sumor/error@^1.0.6
+ Addedmulter@^1.4.5-lts.1
+ Addedappend-field@1.0.0(transitive)
+ Addedbuffer-from@1.1.2(transitive)
+ Addedbusboy@1.6.0(transitive)
+ Addedconcat-stream@1.6.2(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedmulter@1.4.5-lts.1(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstreamsearch@1.1.0(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedtypedarray@0.0.6(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedxtend@4.0.2(transitive)
Updated@sumor/config@^1.3.1
Updated@sumor/logger@^1.2.7
Updated@sumor/validator@^1.2.0