0.0.5 • Published 6 months ago

@truefusion/somnia v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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');

/* /blog/posts */
var posts = blog('posts');
posts.post({
    title: 'somnia',
});

/* /blog/posts?category=javascript */
posts.get({
    category: 'javascript',
});

/* /blog/posts/1 */
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');

/* /blog/posts/1/comments */
firstPostComments.get();

/* /blog/posts/1/comments/1 */
firstPostComments(1).get();

Easily converts to string:

String(somnia('/blog/posts')) // "/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'); // Will inherit fetchOptions

Provide your own drop-in replacement for fetch:

import fetch from 'node-fetch';

var blog = somnia('/blog', { fetch });
0.0.5

6 months ago

0.0.3

11 months ago

0.0.4

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago