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

neistion

v1.3.34

Published

Declare APIs instead of building them.

Downloads

136

Readme

Neistion

Declare your APIs instead of writing them.
Neistion comes with predefined parameter validation and sanitization, authorization and more you can think of. Supports multiple frameworks, and you can create your own framework wrapper easily.

Installation

$ npm install neistion --save

Remember to add these lines to tsconfig.json (if you are going to use decorators):

"experimentalDecorators": true,
"emitDecoratorMetadata": true

Example

import { IncomingHttpHeaders } from "http";
import { Neistion, sandhandsProp, ExpressApp } from "neistion";

class RandomParameters {
  @sandhandsProp
  public min!: number;
  @sandhandsProp
  public max!: number;
}

const api = new Neistion(new ExpressApp(), {
  routes: [
    {
      route: "/random",
      method: "GET",
      parametersSchema: "RandomParameters",
      call(parameters: RandomParameters) {
        const { max, min } = parameters;
        return Math.floor(Math.random() * (max - min)) + min;
      },
      verify(headers: IncomingHttpHeaders, parameters: RandomParameters) {
        return parameters.max > parameters.min;
      }
    }
  ],
  debug: true,
  strictPropertyCheck: true,
});

api.start(3000);

example

API

new Neistion(app, [options])

Returns the main Neistion object, which empowers the API.

app: IApp

This is the proxy between Neistion and your framework. Change this to change what framework is used in your API. You can easily implement your own proxy, check `src/proxy/` for that.

Currently, available options are `ExpressApp` and ~~`FastifyApp`~~

options: NeistionOptions

  • routes: IApiRoute[]

    This is where you define your api calls within options, so this should be defined even if it should be empty.
    • IApiRoute

      call: (parameters: PT) => Promise | any

      The function to be called for the API route. Runs last, after ~~normalizeParameters~~, ~~transformParameters~~ and verify.

      method: "GET" | "POST" | "PUT" | "DELETE"

      The method of the API call, as a string.

      parametersSchema: ISandhandsSchema | string

      This schema is used to validate incoming request parameters.
      Examples:
      {
          key: String,
          number: Number,
          isCool: Boolean
      }
      or, as a Typescript class using power of decorators:
      import { sandhandsProp } from "neistion";
      class ApiParameterType {
        @sandhandsProp
        public key: string;
      }
      // ...,
      parametersSchema: "ApiParameterType";
      // Because we defined sandhandsProp decorator on property, we can just type the name. Otherwise, we should duplicat e it.

      perRouteMiddlewares: RequestHandler[]

      An array of middlewares to be run only for this route.

      route: string

      The route string, used by express. You can use dynamic routes too, whatever express supports as a route.

      verify?: (headers: IncomingHttpHeaders, parameters: PT) => Promise<boolean> | boolean | Promise<IStatusMessagePair> | IStatusMessagePair

      Takes in headers and parameters of the request, and returns one of the types above. Use this for authentication.

      verifyCallback?: (headers: IncomingHttpHeaders, parameters: IncomingParameters, returnCallback: (result: IStatusMessagePair | boolean) => void) => void

      Same as verify, but waits for the callback instead. Designed to be used with old libraries, using callbacks.
  • debug?: boolean

    Whether the library should log the debug messages or not.
  • express?: (express: Express) => Promise<void>

    Takes express instance in. You can do anything with express you need here. Runs after defining routes.
  • json?: boolean

    Whether JSON serialization should be made or not. If not, literal objects are written to requests.
  • strictPropertyCheck?: boolean

    If set to true, parameter objects with extra properties will be an invalid parameter.

api.start(port)

Starts the API up at the given port.

api.setup()

Redefines the routes from scratch, depending on options.

api.addApiCall(call)

Adds an API call to the route handlers. You do not need to `setup()` after this function.

api.addRoutesFromDirectory(routesDirectoryPath)

Adds all modules inside given directory as routes to the API.

api.addRoutesFromDirectory() // uses the default path, which is /routes

Example tree structure:
- routes
    - random.js
    - index.js

All of the files inside `routes` directory should implement IApiRoute, otherwise an error will be thrown.

Missing something?

Feel free to open an issue for requests. They are welcome.

Contact

Implicit#8954