Zemi is data-driven and reverse-routing library for Express. It provides out-of-the-box OpenAPI support, allowing you to specify and autogenerate an OpenAPI spec.

Overview

zemi

github build codecov vulnerabilities npm version

license types dependencies install size

code style: prettier

zemi is a data-driven routing library for Express, built with Typescript.

Features:

Table of Contents

  1. Data-driven
  2. Reverse-routing
  3. Middleware
  4. Parameter Inheritance
  5. OpenApi
    1. Defining Route Parameters
    2. Generating an OpenApi JSON spec
    3. Leveraging All OpenApi Features
    4. Why is this better than directly defining an OpenApi JSON spec?
  6. Interfaces
  7. Examples
  8. Limitations

Data-driven

Assume you have the following functions defined: petsHandler, dogBreedHandler, dogBreedsIdHandler, catsByIdHandler ; e.g.:

const petsHandler = (request: ZemiRequest, response: ZemiResponse) => {
  // do something with this request and respond
  response.status(200).json({ pets: ["dogs", "cats", "rabbits"] });
};

Then the following code:

import express from "express";
import zemi, { ZemiRoute, ZemiMethod } from "zemi";

const { GET } = ZemiMethod

const routes: Array<ZemiRoute> = [
  {
    name: "pets",
    path: "/pets",
    [GET]: { handler: petsHandler },
    routes: [
      {
        name: "dogBreeds",
        path: "/dogs/:breed",
        [GET]: { handler: dogBreedHandler },
        routes: [
          {
            name: "dogsByBreedById",
            path: "/:id",
            [GET]: { handler: dogBreedsIdHandler },
          },
        ],
      },
      {
        name: "catsById",
        path: "/cats/:id",
        [GET]: { handler: catsByIdHandler },
      },
    ],
  },
];

const app = express();
app.use(express.json());
app.use("/", zemi(routes));
app.listen(3000);

Generates an API like:

routes response
/pets {pets: ['dogs', 'cats', 'rabbits']}
/pets/dogs Cannot GET /pets/dogs/ (since it was not defined)
/pets/dogs/labrador {"result":["Fred","Barney","Wilma"]}
/pets/dogs/labrador/1 {"result":"Barney"}
/pets/cats Cannot GET /pets/cats/ (since it was not defined)
/pets/cats/2 {"result":"Daphne"}

Reverse-routing

zemi builds route-definitions for all routes and adds them to the ZemiRequest passed to the handler function.

All route-definitions are named (index-accessible) and follow the same naming convention: [ancestor route names]-[parent route name]-[route name], e.g. basePath-greatGrandparent-grandparent-parent-myRoute, pets-dogsBreeds-dogsByBreedById.

Each route-definition contains the name, path, and path-parameters (if present) of the route. It also contains a reverse function which — when invoked with an object mapping path-parameters to values — will return the interpolated path with values.

E.g. the handler:

import { ZemiRequest, ZemiResponse, ZemiRouteDefinition } from "zemi";

const petsHandler = (request: ZemiRequest, response: ZemiResponse) => {
  const routeDefinitions: Record<string, ZemiRouteDefinition> = request.routeDefinitions;
  const { path, name, parameters, reverse } = routeDefinitions["pets-dogBreeds-dogsByBreedById"];
  response.status(200).json({ path, name, parameters, reverse: reverse({ breed: 'Corgi', id: '99' }) });
};

Returns:

  {
  "path": "/pets/dogs/:breed/:id",
  "name": "pets-dogBreeds-dogsByBreedById",
  "parameters": [
    "breed",
    "id"
  ],
  "reverse": "/pets/dogs/corgi/99"
}

This allows you to generate links, redirect, and change path values without having to hardcode strings and change them later.

Middleware

zemi lets you define middleware functions at the route level:

Retaking and tweaking our example from the beginning:

import { ZemiRequest, ZemiResponse } from "zemi";
import { NextFunction } from "express";

const routes: Array<ZemiRoute> = [
  {
    name: "pets",
    path: "/pets",
    [GET]: { handler: petsHandler },
    routes: [
      {
        name: "dogBreeds",
        path: "/dogs/:breed",
        [GET]: { handler: dogBreedHandler },
        middleware: [
          function logRouteDefs(request: ZemiRequest, response: ZemiResponse, next: NextFunction) {
            console.log(JSON.stringify(request.routeDefinitions));
            next();
          }
        ],
        routes: [
          {
            name: "dogsByBreedById",
            path: "/:id",
            [GET]: { handler: dogBreedsIdHandler },
          },
        ],
      },
      {
        name: "catsById",
        path: "/cats/:id",
        [GET]: { handler: catsByIdHandler },
      },
    ],
  },
];

The middleware function logRouteDefs defined at the dogBreeds level will be applied to all the methods at that level and all nested routes — which means our dogsByBreedById route will gain that functionality also.

Parameter Inheritance

As show in previous examples, parameters defined at parent routes are passed and available to nested routes.

E.g. in this purposefully convoluted example:

const routes: Array<ZemiRoute> = [
  {
    name: "pets",
    path: "/pets",
    [GET]: { handler: petsHandler },
    routes: [
      {
        name: "dogBreeds",
        path: "/dogs/:breed",
        [GET]: { handler: dogBreedHandler },
        routes: [
          {
            name: "dogsByBreedById",
            path: "/:id",
            [GET]: { handler: dogBreedsIdHandler },
            routes: [
              {
                name: "dogsByBreedByIdDetailsSection",
                path: "/details/:section",
                [GET]: { handler: dogBreedsIdDetailsSectionHandler },
                routes: [
                  {
                    name: "newDogsByBreedByIdDetailsSection",
                    path: "/new",
                    [POST]: { handler: newDogsByBreedByIdDetailsSectionHandler },
                  }
                ]
              },
            ]
          },
        ],
      }
    ],
  },
];

The newDogsByBreedByIdDetailsSection route (path: /pets/dogs/:breed/:id/details/:section/new) will have breed, id, and section available as request parameters in the ZemiRequest object.

OpenApi

zemi supports OpenAPI out-of-the-box — but it's completely optional.

It has extensive Typescript support for OpenApi.

You can forgo it entirely, use every supported feature, or just the bare-minimum as needed.

It comes with a OpenApi spec generator — ZemiOpenApiSpecGenerator — which will create and save an openapi.json specification of your API.

Defining Route Parameters

You can pass a parameters array to each ZemiMethod definition, where each parameter is a OpenApiParameterObject.

Each OpenApiParameterObject requires the following properties:

{
  name: string
  in: "query"|"header"|"path"|"cookie"
  required: boolean
  schema: {
    type: string
  }
}

So a route with a GET that has the following parameters array:

const routes: Array<ZemiRoute> = [
  {
    name: "pets",
    path: "/pets",
    [GET]: { handler: petsHandler },
    routes: [
      {
        name: "dogBreeds",
        path: "/dogs/:breed",
        [GET]: { handler: dogBreedHandler },
        parameters: [
          {
            name: 'breed',
            in: 'path',
            required: true,
            schema: {
              type: 'string'
            }
          }
        ],
      },
    ]
  }
];

will correctly have the breed parameter detailed in the OpenApi spec.

Alternatively, and at the expense of full features, you can specify a shorthand for path parameters without specifying the parameters array:

const routes: Array<ZemiRoute> = [
  {
    name: "pets",
    path: "/pets",
    [GET]: { handler: petsHandler },
    routes: [
      {
        name: "dogBreeds",
        path: "/dogs/{breed|string}",
        [GET]: { handler: dogBreedHandler },
      }
    ]
  }
];

This notation — where each parameter is captured between curly braces ({}) — has the name and schema type of the parameter, delimited by a pipe (|).

This will:

  • still generate a valid Express path (where path parameters are prefixed with :) for actual routes
  • generate a valid OpenApi path for that route
  • extract the name (anything before |) and the schema type (anything after |) for use in the OpenApi parameter definition

Generating an OpenApi JSON spec

Assume you have ZemiRoutes defined at src/routes/index.ts.

Create a file at the root (project) level named zemi-openapi-spec-gen.ts:

import { OpenApiDoc, ZemiOpenApiSpecGenerator} from "zemi";
import routes from "./src/routes";

const doc: OpenApiDoc = {
  openapi: "3.0.0",
  info: {
    description: "API for pet store management",
    version: "1.0",
    title: "Pet Store API"
  },
  servers: [{ url: "https://api.bestpetstore.com/v1" }],
};

ZemiOpenApiSpecGenerator({ doc, routes });

// -- With options:
// const options = { path: '/path/to/save/openapijson/to' }
// ZemiOpenApiSpecGenerator({ doc, routes, options});

That's the minimum config you need to generate an OpenApi spec.

You can then use ts-node to run it:

npx ts-node zemi-openapi-spec-gen.ts

This will generate an openapi.json at that same dir level.

Note that you can pass an optional options object specifying the path you want to save the spec to.

Leveraging All OpenApi Features

zemi breaks down OpenApi into two parts:

  1. The general doc passed into the ZemiOpenApiSpecGenerator

  2. Documentation generated from ZemiRoutes

The general doc can be used to specify every valid object and definition supported by OpenApi, except paths.

Paths are specified via ZemiRoutes (although some general doc specifications might impact them): each route supports properties specified in OpenApiPathItemDefinitionObject , and each method supports properties defined in OpenApiOperationObject.

Combining both of these approaches, you can build a complete OpenApi spec.

Why is this better than directly defining an OpenApi JSON spec?

  1. It's code: types, auto-completion, and all the benefits you get with code on a modern IDE.

  2. Self documentation: as you're writing your API, you document it, which makes it easier to keep the spec up-to-date.

  3. Path generation: path and method definitions are, at the least, partly generated; more so if you want a straightforward, simple spec.

Interfaces

ZemiMethod

Enum

The HTTP methods supported by ZemiRoute.

Member Value
GET get
POST post
PUT put
DELETE delete
OPTIONS options

ZemiHandlerDefinition

extends OpenApiOperationObject

The object that maps to a ZemiMethod; where the core of functionality resides. This object is a wrapper for OpenApiOperationObject (for OpenApi spec generation purposes), but adds the key function handler: ZemiRequestHandler, which is where the logic for the route's method lives.

{
  handler: ZemiRequestHandler
  
  // inherited from OpenApiOperationObject
  
  tags?: Array<string>;
  summary?: string;
  description?: string;
  externalDocs?: OpenApiExternalDocumentationObject;
  operationId?: string;
  parameters?: Array<OpenApiReferenceObject | OpenApiParameterObject>;
  requestBody?: OpenApiReferenceObject | OpenApiRequestBodyObject;
  responses?: Record<string, OpenApiResponseObject>;
  callbacks?: Record<string, OpenApiReferenceObject | OpenApiCallbackObject>;
  deprecated?: boolean | false;
  security?: Array<OpenApiSecurityRequirementObject>;
  servers?: Array<OpenApiServerObject>;
}

ZemiRequestHandler

How to handle incoming requests for this route method; basically express.RequestHandler, but gets passed its own request and response versions, plus adds that routes ZemiRouteDefinition as an optional fourth param.

(
  request: ZemiRequest,
  response: ZemiResponse,
  next: express.NextFunction,
  routeDef: ZemiRouteDefinition
) => void

ZemiRequest

extends express.Request

A wrapper for express.Request; adds routeDefinitions and allowedResponseHttpCodes to it.

allowedResponseHttpCodes is generated from OpenApiOperationObject.responses, if provided to the ZemiRoute.

{
  routeDefinitions: Record<string, ZemiRouteDefinition>;
  allowedResponseHttpCodes: Record<string, Record<string, Array<string>>>;

  // all other members from express.Request
}

ZemiResponse

extends express.Response

Just a wrapper for future-proofing; same as express.Response.

ZemiRouteDefinition

Route definition for a given ZemiRoute. Contains the name, path, and path-parameters (if present) of the route it's defining. Also provides a reverse function that, when invoked with an object that has parameter-values, will return the resolved path.

{
  name: string;
  path: string;
  parameters: Array<string>;
  reverse: (parameterValues: object) => string;
}

ZemiRoute

extends OpenApiPathItemDefinitionObject

Overrides OpenApiPathItemDefinitionObject's parameters property, so that it only accepts an array of OpenApiParameterObject and not OpenApiReferenceObject. This inheritance exists to support OpenApi spec generation, but most of the functional aspects are provided by the native properties of this object.

It must be provided a name: string and path: string; a ZemiMethod:ZemiHandlerDefinition needs to be provided if that path should have functionality, but doesn't need to be if the path is just present as a path-prefix for nested routes.

{
   [ZemiMethod]: ZemiHandlerDefinition;
   name: string;
   path: string;
   middleware?: Array<RequestHandler>;
   routes?: Array<ZemiRoute>;
   parameters?: Array<OpenApiParameterObject>;
}

ZemiOpenApiSpecGenerator

Takes an OpenApiDoc object and an array of ZemiRoutes to generate an opeanapi.json spec.

Accepts an optional ZemiOpenApiDocGenerationOptions.

(
  doc: OpenApiDoc,
  routes: Array<ZemiRoute>,
  options: ZemiOpenApiDocGenerationOptions
) => void

ZemiOpenApiSpecGenerationOptions

Lets you provide a path: string value that specifies the location of where to save the openapi.json spec.

{
  path: string
}

Examples

Examples are available in the examples dir:

  1. Simple

  2. With Middleware

  3. Using Reverse Routing

  4. With Param Inheritance from Parent Routes

  5. Using {paramName|paramType}-style Parameters in Path

  6. Using More OpenApi features

Limitations

zemi is a recursive library: it uses recursion across a number of operations in order to facilitate a low footprint and straightforward, declarative definitions.

Recursive operations can break the call-stack by going over its limit, generating Maximum call stack size exceeded errors. This means that the recursive function was called too many times, and exceeded the limit placed on it by Node.

While recursive functions can be optimized via tail call optimization (TCO), that feature has to be present in the environment being run for optimization to work.

Unfortunately — as of Node 8.x — TCO is no longer supported.

This means that, depending on what you're building and the size of your API, zemi might not be the right fit for you. zemi uses recursion when dealing with nested routes, so if your application has a very high number of nested-routes within nested-routes, chances are you might exceed the call stack.

You might also like...

Cross provider map drawing library, supporting Mapbox, Google Maps and Leaflet out the box

Terra Draw Frictionless map drawing across mapping providers. TerraDraw centralises map drawing logic and provides a host of out the box drawing modes

Dec 31, 2022

'event-driven' library aims to simplify building backends in an event driven style

'event-driven' library aims to simplify building backends in an event driven style

'event-driven' library aims to simplify building backends in an event driven style(event driven architecture). For message broker, light weight Redis Stream is used and for event store, the well known NoSQL database, MongoDB, is used.

Jan 4, 2023

Query for CSS brower support data, combined from caniuse and MDN, including version support started and global support percentages.

css-browser-support Query for CSS browser support data, combined from caniuse and MDN, including version support started and global support percentage

Nov 2, 2022

we try to make a tiny p2p client spec, maybe for sigchain gossip thing, maybe for simple blockchain thing

mininode Mininode is a tiny p2p client for prototyping p2p protocols. It is a specification for a set of interfaces that I made to make it easier to t

Nov 23, 2022

An implementation of the ECMA-419 spec on the Raspberry Pi

raspi-419 An implementation of the ECMA-419 spec on the Raspberry Pi Licsense MIT License Copyright (c) Bryan Hughes Permission is hereby granted, fre

Jun 9, 2022

GitHub Action to validate that PR titles in n8n-io/n8n match n8n's version of the Conventional Commits spec

validate-n8n-pull-request-title GitHub Action to validate that PR titles in n8n-io/n8n match n8n's version of the Conventional Commits spec. Setup Cre

Oct 7, 2022

NoExGen is a node.js express application generator with modern folder structure, namespace/project mapping and much more! It contains preconfigured Settings and Routing files, ready to be used in any project.

Installation $ npm install -g noexgen Quick Start You can use Node Package Execution to create your node-express application as shown below: Create th

Oct 8, 2022

CLI utility that parses argv, loads your specified file, and passes the parsed argv into your file's exported function. Supports ESM/TypeScript/etc out of the box.

cleffa CLI tool that: Parses argv into an object (of command-line flags) and an array of positional arguments Loads a function from the specified file

Mar 6, 2022

Scaffold a full-stack SvelteKit application with tRPC and WindiCSS out of the box

create-sweet-app Interactive CLI to quickly set up an opinionated, full-stack, typesafe SvelteKit project. Inspired by the T3 Stack and create-t3-app

Dec 16, 2022
Releases(v1.3.0)
Owner
Yoaquim Cintrón
Dogs. Chocolate.
Yoaquim Cintrón
FortuneSheet is an online spreedsheet component library that provides out-of-the-box features just like Excel

FortuneSheet FortuneSheet is an online spreedsheet component library that provides out-of-the-box features just like Excel English | 简体中文 Purpose The

Suzhou Ruilisi Technology Co., Ltd 1.6k Jan 3, 2023
Use Cloudflare Pages Functions as a reverse proxy with custom domain support.

cf-page-func-proxy Use Cloudflare Pages Functions as a reverse proxy with custom domain support. Getting Start 1.下载或是Fork本仓库 2.修改_worker.js中的url.hostn

null 121 Dec 23, 2022
Out-of-the-box MPA plugin for Vite, with html template engine and virtual files support.

vite-plugin-virtual-mpa Out-of-the-box MPA plugin for Vite, with html template engine and virtual files support, generate multiple files using only on

QinXuYang 21 Dec 16, 2022
Specify various templates for different directories and create them with one click. 🤩

English | 简体中文 Gold Right Specify various templates for different directories and create them with one click. Reason Usually there is something in the

赵東澔 16 Aug 8, 2022
An application where a user can search a location by name and specify a genre of music. Based on the parameters entered, a list of radio stations generate based on genre selected in that area.

Signs of the Times Description An application that allows for the user to enter a date and see the horoscope for that day, and famous people born on t

null 3 Nov 3, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Custom alert box using javaScript and css. This plugin will provide the functionality to customize the default JavaScript alert box.

customAlertBoxPlugin Custom Alert Box Plugin Using JavaScript and CSS Author: Suraj Aswal Must Include CSS Code/Default Custom Alert Box Class: /* mus

Suraj Aswal 17 Sep 10, 2022
Fullstack Turborepo starter. Typescript, Nestjs, Nextjs, Tailwind, Prisma, Github Actions, Docker, And Reverse proxy configured

Turborepo (NestJS + Prisma + NextJS + Tailwind + Typescript + Jest) Starter This is fullstack turborepo starter. It comes with the following features.

Ejaz Ahmed 132 Jan 9, 2023