Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sumor/api-middleware

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sumor/api-middleware - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

src/checkData.js

16

package.json
{
"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
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc