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

@ando.gq/backend

v0.1.0

Published

Simplistic backend API structure. See `/example` for an example usage.

Downloads

4

Readme

Simplistic backend API structure. See /example for an example usage.

Overview of example

There are two main folders, api and modules. api contains all of the endpoints for the API. The structure of the API is determined by the filesystem, along with the path defined within each individual endpoint. The modules folder contains any modules that may be required, such as authentication. Modules are run on each request (where an endpoint has specified) and are able to pass data (such as a user object) back to the handler.

Endpoint Structure

  • path: The path of the endpoint. Can be nested multiple layers within a single path (eg /nested/endpoint).
  • method: The method for the endpoint of type Method
  • modules: (optional) An object containing a key-value pair, where the key is the name of a module (which must be within the modules folder) and the value is some data that can be passed to the module, or true if nothing needs to be passed.
  • handler: The handler function for the endpoint. It can return a promise or directly return a value. The value returned from the promise or directly will be sent back to the user. It can have up to two parameters passed to it:
    • The first object is the parameters for the endpoint which are generated from the body, query and URL paramters, as described below.
    • The second object is the data that has been passed from the modules. It may or may not exist depending on whether any modules have been added to the endpoint.
  • validation: The validations for the endpoint, which can come from the body, query, or the URL parameters. A validation must exist for a parameter in order for it to be passed to the endpoint. This is an object with 3 optional keys, body, query and parameter, each of which contains another object of key-value pairs, where the key is the parameter name and the value is either another such object, or a validation function. Any parameters passed to the handler are guaranteed to have passed the validator, so don't need to be tested further.

Endpoint Errors

By default, returning from an endpoint handler will result in a 200 status code and the value being sent to the user. To override this, throw an object with the status property set to the desired status code and the message property with a user-readable string describing what's happened. Such an object is a HandledError and will be sent to the user.

If an object is thrown that isn't a HandledError it will be treated as an unhandled error, and will be logged to the console with a generic 500 error sent to the user.

Modules

Modules are very similar to middleware in Express, and are executed before an endpoint. They can intercept and respond to a request, or pass the request on to the handler with some optional data passed on.

Modules must be a function that takes two parameters, an Express Request object and an Express Response object. The request and response objects are free to be used in any way.

Modules can return three different types of values:

  • falsey values indicate that the request has been responded to by the module, and will result in the endpoint not being called. Not ending the request from the module when returning a falsey value will result in the request hanging. Not returning anything from the module is classed as a falsey value, and will result in the handler not being called.
  • truthey values indicate that the request is ok and can be passed to the endpoint. Headers can be sent from such modules, as long as the response isn't sent.
  • The only exception to truthey values is objects that are returned by the module. The request will still be passed onto the endpoint, however the returned object will be merged with the results of all other modules, and passed to the endpoint handler in the second parameter.

Since modules use the same types as Express middleware, it shouldn't require too much effort to bootstrap a middleware package into a module. A wrapper for this might be provided at some point.