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

wpcom-xhr-request

v1.2.0

Published

REST API requests to WordPress.com via XMLHttpRequest (and CORS)

Downloads

3,283

Readme

wpcom-xhr-request

REST-API and WP-API requests via XMLHttpRequest (and CORS)

CircleCI

You likely want to use the high-level APIs in wpcom.js instead of using this module directly.

Works in both the browser and Node.js via superagent.

Installation

Install wpcom-xhr-request using npm:

$ npm install wpcom-xhr-request

Example

<html>
  <body>
    <script src="wpcom-xhr-request.js"></script>
    <script>
      WPCOM.xhr('/me', function(err, data) {
        if (err) throw err;

        var div = document.createElement('div');
        div.innerHTML = 'Your WordPress.com "username" is: <b>@' + data.username + '<\/b>';
        document.body.appendChild(div);
      });
    </script>
  </body>
</html>

API

wpcomXhrRequest( [params], fn )

Params: optional parameters

  • method: GET as default.
  • apiNamespace: WP-API namepsace.
  • apiVersion: REST-API app version - 1 as default.
  • proxyOrigin: https://public-api.wordpress.com as default.
  • authToken: token authentication.
  • query: object used to pass the query to the request.
  • body: object used to pass the body to the request.
  • form-data: POST FormData (for multipart/form-data, usually a file upload).
  • processResponseInEnvelopeMode: default TRUE.

fn: request callback function

This function has three parameters:

  • error: defined if the request fails
  • body: the object body of the response
  • headers: the headers of the response
import handler from `wpcom-xhr-request`;

// get .com blog data usign `REST-API`
handler( '/sites/en.blog.wordpress.com', ( error, body, headers ) => {
  if ( error ) {
    return console.error( 'Request failed: ', error );
  }
  
  console.log( 'WordPress blog: ', body );
} );

// get .com blog data using `WP-API`
handler( {
  path: '/sites/en.blog.wordpress.com',
  apiNamespace: 'wp/v2'
}, ( error, body, headers ) => {
  if ( error ) {
    return console.error( 'Request failed: ', error );
  }
  
  console.log( 'WordPress blog: ', body );
} );

// get .org blog data (`WP-API`)
handler( {
  proxyOrigin: 'http://myblog.org/wp-json',
  path: '/',
  apiNamespace: 'wp/v2'
}, ( error, body, headers ) => {
  if ( error ) {
    return console.error( 'Request failed: ', error );
  }
  
  console.log( 'WordPress blog: ', body );
} );

Authentication

For API requests that require authentication to WordPress.com, you must pass in an OAuth token as the authToken parameter in the params object for the API call.

You can get an OAuth token server-side through node-wpcom-oauth, or any other OAuth2 interaction mechanism.

Tests

make test
make test-watch

License

MIT – Copyright Automattic 2014