Skip to content

Repository files navigation

dAPIr

Dan's API Router (dAPIr) is a simple API router based on Express.js that leverages the file system for creating routes. dAPIr also helps you manage and create OpenAPI documentation.

Quick Start

Let's get started with using dAPIr!

Create API

Create your API route file

process.chdir(__dirname);import{Server,HTTPContext}from'dapir';// Context that will be passed throughout app (databases, wrappers, etc)constcontext={test: 'test',};// Create apiconstapi=newServer({port: 3000,host: '0.0.0.0',cors: {enabled: true,origin: '*',},routes: {enabled: true,folder: './routes',context: context,}})// Export typesexporttypeRouteHandler=typeofapi.routeHandler<HTTPContext>;exporttypeRouteConfig=typeofapi.routeConfig;exporttypeContext=typeofcontext;exportconstauthenticationMethod=api.authenticationMethod;// Start serverapi.listen();

Routing

Creating a route file

dAPIr uses the file system to create routes. To create a route create a ts file with a HTTP method as the name. To create a dynamic url create a folder or prefix the file name with [var] (replace var with whatever you want it to be named). Here are some examples of what they might look like.

File PathRoute Path
/get.tsGET /
/foo/get.tsGET /foo
/foo.get.tsGET /foo
/bar/post.tsPOST /bar
/bar/get.tsGET /bar
/[id]/get.tsGET /:id
/[id].get.tsGET /:id

dAPIr will detect duplicate routes and error them to the console.

Setup route

To setup a route, simply import the types from your api index.

import{RouteConfig,RouteHandler}from'@/';exportconsthandler: RouteHandler=(ctx)=>ctx.res.json({message: 'Hello World'});exportconstconfiguration: RouteConfig={/* ... */};

Middlewares

dAPIr supports two types of middlewares, global and local.

Global Middleware

Global Middlewares run on every request

Unfinished...

Local Middleware

Unfinished...

About

An api-making library

Resources

Stars

0 stars

Watchers

0 watching

Forks

Packages

Used by

Contributors

Languages