Coder Social home page Coder Social logo

couchr's Introduction

couchr

Simple request library for CouchDB. Provides both a Node.js module and a browser module (based on jQuery.ajax), with better CouchDB error reporting and a simpler API than making XHR requests directly in the browser.

Examples

// browser
require(['couchr'], function (couchr) {
    couchr.get('/dbname/docid', function (err, doc) {
        ...
    });
});

// Node
var couchr = require('couchr');
couchr.get('http://hostname:port/dbname/docid', function (err, doc) {
    ...
});

Methods

couchr.get (url, /*optional*/params, function (err, res, req) { ... })
couchr.post(url, /*optional*/data,   function (err, res, req) { ... })
couchr.put (url, /*optional*/data,   function (err, res, req) { ... })
couchr.del (url, /*optional*/data,   function (err, res, req) { ... })
couchr.head(url, function (err, res, req) { ... })

couchr.copy(from, to, function (err, res, req) { ... }) 

var feed = couchr.changes(db_url);
feed.on('change', function (change_object) { ...  });
feed.on('error', function (err) { ...  });
feed.pause();
feed.resume();

Installation

Browser (using jam):

jam install couchr

Node (using npm):

npm install couchr

couchr's People

Contributors

ryanramage avatar skiqh avatar wombleton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

couchr's Issues

follow redirects?

Right now if you get a 301 or 302 from a request, couchr handles it like an error. Would it be useful to follow the redirect before returning an error (as well as handling auth headers and such)?

Possible mis-handling UTF-8 strings in HEAD requests

I use HEAD requests to get eTag values for caching purposes for given queries.

If my cache under that eTag is a miss, then I make a GET request.

Since the couchr.head() call does not accept arguments as GET does, I pass the arguments along as a query string to the HEAD request.

In doing so, I discovered that when "key=" strings containing UTF-8 values (emoji), the HEAD would return a different eTag value than the GET would using the same strings. Non-emoji-containing strings would work as expected.

I was able to work around this seeming limitation by performing - for HEAD requests - the encoding dance that couchr uses for GET strings.

Unless I am abusing the spec, it would seem that couchr should natively handle this string encoding.

Here is a snippet I used to take a json object containing values and returning a string properly encoded for use as the arguments to the HEAD call.

function getHeadArgs(lookup) {
  var dup = JSON.parse(JSON.stringify(lookup));
  ['key', 'keys', 'startkey', 'endkey'].forEach(function(key) {
    if (dup[key] != null) {
      dup[key] = JSON.stringify(dup[key]);
    }
  });
  return querystring.stringify(dup);
}

var encoded = getHeadArgs({ key: '๐Ÿ’Ÿ๐Ÿ’œ๐Ÿ’›๐Ÿ’š๐Ÿ’™๐Ÿ’™!!' });

couchr.head(URL + '?' + encoded);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.