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

bapi.js

v0.0.2

Published

✏️ A simple and versatile API manager for Node.

Downloads

3

Readme

Bapi v0.0.2 Documentation

What is Bapi?

✏️ Bapi is an open source versitile NPM package made to create APIs easily and effeciently. Bapi provides an easy to use syntax and easy compatability for use in any kind of API project.

How should Bapi be used?

While it is possible to create an API that does not run through a server, that probably shouldn't be done... in case you didn't know APIs are to be used in the web to access information from various projects, as well as to allow developers to integrate your program into their code! You can create middleware/plugins to easily integrate any server system. However, Bapi comes with a built in Express handler. See bapi.Server for information regarding it!

Installation

Install via NPM:

npm install bapi.js --save

Bapi

Default export for Bapi.

| Argument | Type | Description | |-----------|------------|----------------------------| | Options | object | Options for API client |

@Returns new Client(...)

Example

const bapi = require('bapi.js');
const client = bapi({version: "v1.0"});
console.log(client.options) // { version: "v1.0" }

Class: Client

General constructor for all things Bapi! | Argument | Type | Description | Stored | |-----------|--------|------------------------|------------| | Options | object | Options for API client | true |

Example

const {Client} = require('bapi.js');
const client = new Client({version: "v1.0"});
console.log(client.options); // { version: "v1.0" }

Methods

  • public function use(middleware: Function)
  • public function call(method: string, route: string, context: any)
  • public function get(route: string, callback: Function)
  • public function post(route: string, callback: Function)
  • public function on(listener: string, callback: Function)

Client.use(middleware: Function)

Creates a middleware that is executed every time the Client.call method is used. Function is passed a context parameter that should also be returned after adjustments have been made.

Example
// imports...
client.use(function(ctx: any) {
	ctx.version = "v1.0";
	return ctx;
});

Client.call(method: string, route: string, context: any)

Calls a created GET or POST request that has been made on the same Client constructor. | Argument | Type | Description | |-----------|--------|------------------------------------------| | Method | string | POST/GET to identify action | | Route | string | API method label/name | | Context | any | Context passed to middlewares and action |

Example

// imports...
client.get("version", (context: any) => {
	return { context.name, "version": "v1.0" };
});
client.call("GET", "version", {name: "John"});

client[get, post](route: string, callback: Function)

Creates an API method which is either a GET or POST request to be handled with a server or middleware. Note: in your callback function, you must return the data you want to be sent in the response. | Argument | Type | Description | |----------|----------|------------------------------------| | Route | string | Identifies the route to be created | | Callback | Function | Handles creating response |

Example

// imports...
client.get("version", (context: any) => {
	return { context.name, "version": "v1.0" };
});

client.on(listener: string, callback: Function)

Creates an event listener to be called every time a certain Client action happens. | Argument | Type | Description | |----------|----------|-------------------------------------| | Listener | string | Identifies the listener to be used | | Callback | Function | Action ran when the event is called |

Listener Types

| Listener | Called | Passes | |-------------|---------------------------------------|---------------| | createRoute | On client.get(...) & client.post(...) | route: object |

Example
// imports...
client.on("createRoute", (ctx: any) => {
	console.log(`Route ${ctx.label} was created as a ${ctx.method} request and returns ${ctx.action()}`);
});
client.get("test", (ctx: any) => {
	return { "status": "success" }
}); // console log: "Route test was created as a GET request and returns {'status': 'success'}"

Server

Built-in Express server builder. | Argument | Type | Description | |-----------|------------|----------------------------| | Client | Client | Client to build server for |

@Returns Express.Router (see documentation)

Example

const {Client, Serer} = require('bapi.js');
const client = new Client();
const server = Server(client);

client.get("version", (ctx: any) => { // go to URL loalhost:8080/api/version
	return { version: "v1.0" }
});

server.get("/", (req: any, res: any) => {
	res.send("Hello, world!");
});

server.listen(8080, (err?: any) => {
	if(err) throw err;
	console.log("API running!");
})

Creating Bapi middlewares

Bapi is built to be expendable and easy to use for all developers, thus, it is simple to create middlewares with Bapi

Method 1

Build a middleware and export to be used with Client.use(). This makes it so that every time Client.call(...) is executed your middleware will subsequently be executed with all previous context.

Example
function Middleware(context: any) {
	context.isAuthorized = false;
	return context;
}
export { Middleware }

Method 2

Build a more plugin-like function or class that uses the Bapi Client as an argument. This is useful for building server builders- like the built-in Express one.

Example
function Server(client: any) {
    const express = require('express');
    const app = express();
	
    client.on('createRoute', (ctx: any, next?: Function) => {
        let fn = (req: any, res: any) => res.json(client.call(ctx.method, ctx.label, {req, res}));
        switch (ctx.method) {
            case "GET": app.get(`/api/${ctx.label}`, fn);
            case "POST": app.post(`/api/${ctx.label}`, fn);
            default: app.get(`/api/${ctx.label}`, fn);
        }
    });
    return app;
}
export {Server}