omnic (PROOF-OF-CONCEPT, NOT PRODUCTION-READY)
Epic declarative promise-based API client for the browser and node.js
npm i -S omnic
Features
- Supports
fetch
as a default http client - Make adapters for your own clients (XmlHttpRequest, other frameworks and etc.)
- Declarative and simple API description
- Promise support
- Transform request and response data and configs in hooks
- Intercept responses
- Cancel requests
- Automatically transforms request data to JSON
- Automatically encodes object params to URL params
- Customizable API factory
Declarative API definitions
The main goal of omnic
is to provide a simple and declarative way to write API clients. This enables you to strip repetative boilerplate code from your client's API definition and work with pure declarative structure instead.
Examples
A very simple example would be a client for an API that returns collections of users and their blogposts:
GET '/api/user/list' // Returns users data collectionGET '/api/user/{id}' // Returns a specific user dataGET '/api/user/{id}/post' // Returns a specific user blogpost collectionGET '/api/user/{id}/post/{id}' // Returns a specific user's blogpostPOST '/api/user/{id}/post' // Adds a specific user's blogpost GET '/api' // Checks if API is up
Using omnic
, the client for this API can be written very simply, like this:
// client.js// Importing the route constructor and specific request method constructors; // Creating a client factory with pre-set configurationconst generateClient = route; // Generating a final APIconst API = ;
; // Continue only if the API is upAPI;
Route configuration
Below is a full typed config that can be accepted by an alias function (GET
, POST
and etc.)
The route
function can also be used instead with an addition of another field: method
.