npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

monocle-client-js

v0.5.0

Published

JS client for Monocle APIs, supporting Angular and jQuery

Downloads

24

Readme

Monocle API Client for JavaScript

Monocle is a schema-powered API router that focuses on consistency, flexibility and performance.

QuickStart

Getting started with Monocle is simple:

  1. Add Angular and Monocle scripts to your page
  2. Add monocle as a dependency to your Angular application.
  3. Inject monocle and start making API calls!
// Add `monocle` as a dependency:
var app = angular.module('app', ['monocle']);

// Inject `monocle` to gain easy access to a Monocle-powered API
app.service('Users', function(monocle) {
    this.get = function(userId) {
        return monocle.get('/users/' + userId, {
            props: ['userId', 'displayName', 'age']
        });
    };
});

Configuration

The monocle client can be configured in your app's config phase.

// Configure the Monocle client:
app.config(function(monocleProvider) {
    // Set the base path for API calls.
    // All API calls will automatically be mounted onto this base path.
    monocleProvider.setBase('/api');

    // Set a custom timeout to be used by all requests (default: 30,000ms).
    monocleProvider.setTimeout(10000);

    // Set a custom header by value.
    monocleProvider.setHeader('x-custom-value', 'test-custom-value');

    // Set a custom header by providing a callback function.
    // The function will be called for each HTTP request to the Monocle server,
    // and its return value will be the value for the header.
    // If the function returns a promise, it will be resolved first.
    monocleProvider.setHeader('x-custom-callback', function() {
        return 'test-custom-callback-' + Math.random();
    });

    // Set a custom header by providing a promise.
    // The resolved value will be used as the header value.
    monocleProvider.setHeader('x-custom-promise', Promise.resolve('test-custom-promise'));

    // Set a custom header by providing a function that returns a promise.
    // The resolved value will be used as the header value.
    monocleProvider.setHeader('x-custom-promise-callback', function() {
        return Promise.resolve('test-custom-promise-callback');
    });
});

The monocle node adapter can be configured in your app's config phase.

var monocle = require('monocle-client-js');

// Configure the Monocle Node Adapter:
var nodeAdapter = new monocle.nodeAdapter();

// Set a custom timeout to be used by all requests.
nodeAdapter.setTimeout(30000);

// Set a custom header by value.
nodeAdapter.setHeader('x-custom-value', 'custom_client_value');

var monocleProvider = new monocle(nodeAdapter);

// Set the host path for API calls.
// All API calls will automatically be mounted onto this host path.
monocleProvider.setHost('http://www.yourhost.com');

// Set the base path for API calls.
// All API calls will automatically be mounted onto this base path.
monocleProvider.setBase('/api/v2');

To make a request with the node adapter or with the monocle client

// FOR GET
//The second parameter takes an optional object where props or query can be passed in
monocleProvider.get('/endpoint', {props: PROPS, query: QUERY);

//FOR POST, PUT, PATCH OR DELETE
//The second parameter takes an optional object where the body of the request can be passed in
//Note: Currently the node adapter does not support the body parameter to be passed in
monocleProvider.post('/endpoint', {body: {param: 'param1'});
monocleProvider.patch('/endpoint', {body: {param: 'param1'});
monocleProvider.put('/endpoint', {body: {param: 'param1'});
monocleProvider.delete('/endpoint');

//Examples:
monocleProvider.get('/users/123', {props: ['name'], query: {new: false}});
monocleProvider.post('/users/345', {body: {name : 'Mariane'}});
monocleProvider.patch('/users/123', {body: {name : 'Mariane'}});
monocleProvider.put('/users/123', {body: {name : 'Mariane'}});
monocleProvider.delete('/users/123'); //Deletes user 123