somnia
Beautifully RESTful.
Installation
npm install @truefusion/somnia --save
API
Basic syntax: somnia(URL, OPTIONS).METHOD(DATA, OPTIONS): Promise
METHOD: HTTP verb.
OPTIONS: fetch options.
Examples
import somnia from 'somnia';
var blog = somnia('/blog');
var posts = blog('posts');
posts.post({
title: 'somnia',
});
posts.get({
category: 'javascript',
});
var firstPost = posts(1);
firstPost.get();
firstPost.put({ title: 'somnia is simple' });
firstPost.patch({ title: 'somnia is really simple' });
firstPost.delete();
var firstPostComments = firstPost('1/comments');
firstPostComments.get();
firstPostComments(1).get();
Easily converts to string:
String(somnia('/blog/posts'))
To catch errors:
somnia('/posts')
.get()
.catch(function({ statusText }) {
console.log(statusText);
});
Options are inherited:
var posts = somnia('/blog/posts', fetchOptions);
var comments = posts('1/comments');
Provide your own drop-in replacement for fetch:
import fetch from 'node-fetch';
var blog = somnia('/blog', { fetch });