Dear developer! Saddle one of our beautiful tapirs and have a good ride!
We made too much express routing and at one point we made a wrapper that handles:
- routing
- authorization
- async methods
- request options
- errors
- internal calls
- API docs generation
- API testing
Yes, our tapirs know that there is a swagger snorkeling out there. The Swagger is a great tool if you like to write (or generate) that enormous configuration files.
Tapir loves juicy requests with a body filled with JSON. We would change this behaviour in future releases.
Tapir can be spawned just as any node.js package.
yarn add api-tapir
Feel free to use any package manager (npm, pnpm, yarn, etc.)
Tapir likes to tie URI with the request method, but you can specify method
as a route property if you wish.
Common CRUD for all standard tapir manipulations can look like this:
POST:/api/tapir
GET:/api/tapir
GET:/api/tapir/:id
PUT:/api/tapir/:id
REMOVE:/api/tapir/:id
const Tapir = require('api-tapir'),
api = new Tapir({
timeout: 66666,
router: express.Router(),
docs: 'GET:/myApi'
});
Tapir would try to require express
and create an instance of Router itself if this argument is not specified.
Default request timeout duration in milliseconds
. You can overwrite it in the route options.
Each of your API leaves can be configured
Keep in mind that all options are optional.
Specify all parameters that your api method needs. This specifications would be used for type checking
, type casting
, default values
, documentation
.
Tapir get parameters from request body < URI matched parts < URI query parameters
Tapir knows basic types: Number, String, Boolean, Array, Date, "Any".
This would be extended in a future version. Tapir would be able to cast nested properties.
You can extend it in Tapir's module mappers
.
The extend method is a function that takes something as an argument and returns it in the proper data type. If input data is wrong — you MUST throw an Exception.
Tapir throws an Error if required
property is not specified.
This is the function that is doing the real work. It can be async. If this function throws an Error — it would be sent in the response.
Authorization checking function that can be async. If specified — should return true if the user looks authorized enough.
Feel free to modify args object
Middleware pattern for queries processing. It is widely used in express.js, passport.js
Just glued before anything in middleware
Request timeout duration in milliseconds
.
Specifies maximum request size