npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sumor/api-middleware

v1.0.10

Published

API Middleware is a middleware for Node.JS. It can easily expose function to api, and validate parameters

Downloads

279,978

Readme

api-middleware

A Sumor Cloud Tool.
More Documentation

API Middleware is a middleware for Node.JS. It can easily expose function to api, and validate parameters

NPM Version NPM Downloads GitHub CI GitHub Test GitHub Coverage GitHub Audit

Installation

npm i @sumor/api-middleware --save

Prerequisites

Node.JS version

Require Node.JS version 18.x or above

require Node.JS ES module

As this package is written in ES module, please change the following code in your package.json file:

{
  "type": "module"
}

Usage

Basic Usage

  1. Add a file named plus.js in your project folder api
export default async (context, req, res) => {
  const { data } = context
  const { a, b } = data
  return a + b
}
  1. [Optional] Add a config file named plus.json in your project folder api
{
  "name": "plus",
  "parameters": {
    "a": {
      "name": "parameter a",
      "type": "number",
      "length": 3
    },
    "b": {
      "name": "parameter b",
      "type": "number"
    }
  }
}
  1. Add the following code in your index.js file
import express from 'express'
import apiMiddleware from '@sumor/api-middleware'

const app = express()

await apiMiddleware(app, process.cwd() + '/api')

app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000')
})
  1. run index.js
node index.js
  1. Test the api
curl -X POST http://localhost:3000/plus -H "Content-Type: application/json" -d '{"a": 1, "b": 2}'

or use browser to open http://localhost:3000/plus?a=1&b=2

Options for apiMiddleware

import express from 'express'
import apiMiddleware from '@sumor/api-middleware'

const app = express()

await apiMiddleware(app, process.cwd() + '/api', {
  prefix: '/api',
  prepare: async context => {
    // do something before api
  },
  finalize: async (context, result) => {
    // do something after api
  },
  exception: async (context, error) => {
    // handle error
  }
})
app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000')
})

More Config File Types

yaml

You can use yaml file to define config file, replace plus.json with plus.yml

type only support number, string, boolean, array, object

name: plus
parameters:
  a:
    name: parameter a
    type: number
    length: 3
  b:
    name: parameter b
    type: number
config.js

For support js function in config file, you can use config.js file, replace plus.json with plus.config.js

export default {
  name: 'plus',
  parameters: {
    a: {
      name: 'parameter a',
      type: 'number',
      length: 3
    },
    b: {
      name: 'parameter b',
      type: 'number',
      rule: [
        {
          code: 'TOO_BIG',
          message: 'b should be less than 100',
          function: function (value) {
            return value < 100
          }
        }
      ]
    }
  }
}

Parameter Rule

You can reference below example to apply rules to parameters

{
  "name": "plus",
  "parameters": {
    "a": {
      "name": "parameter a",
      "type": "number",
      "length": 3,
      "rule": [
        {
          "code": "GREATER_THAN_0",
          "expression": "^[1-9][0-9]*$",
          "message": "must be greater than 0"
        }
      ],
      "i18n": {
        "zh": {
          "GREATER_THAN_0": "必须大于0"
        }
      }
    },
    "b": {
      "name": "parameter b",
      "type": "number"
    }
  }
}

For more usage, please reference Validator

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