Coder Social home page Coder Social logo

seanlin816 / wpcom.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from automattic/wpcom.js

0.0 1.0 0.0 1.29 MB

WordPress.com JavaScript API client designed for Node.js and browsers

Home Page: http://wpcomjs.com/

License: MIT License

Makefile 1.46% JavaScript 98.54%

wpcom.js's Introduction

wpcom.js

Official JavaScript library for the WordPress.com REST API. Compatible with Node.js and web browsers.

How to use

Node.js

Introduce the wpcom dependency into your package.json, and then initialize it with your API token (optional).

// Edit a post on a site
var wpcom = require('wpcom')('<your-token>');
var blog = wpcom.site('your-blog.wordpress.com');
blog.posts({ number: 8 })
  .then(list => { ... })
  .catch(error => { ... });

Browser

Include dist/wpcom.js and a suitable browser shim if needed to support the ES2015 features in <script> tags:

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.25/browser-polyfill.js"></script>
<script src="wpcom.js"></script>
<script>
  var wpcom = WPCOM('<your-token>');
  var blog = wpcom.site('your-blog.wordpress.com');
  blog.posts({ number: 8 })
    .then(list => { ... })
    .catch(error => { ... });
</script>

Authentication

Not all requests require a REST API token. For example, listing posts on a public site is something anyone can do.

If you do need a token, here are some links that will help you generate one:

API

Examples

// Edit a post on a site
var wpcom = require('wpcom')('<your-token>');
var blog = wpcom.site('your-blog.wordpress.com');
blog.post({ slug: 'a-post-slug' }).update(data)
  .then(res => { ... })
  .catch(err => { ... });

You can omit the API token for operations that don't require permissions:

// List the last 8 posts on a site
var wpcom = require('wpcom')();
var blog = wpcom.site('your-blog.wordpress.com');
blog.posts({ number: 8 })
  .then(list => { ... })
  .catch(error => { ... });

More pre-made examples are in the examples/ directory.

Test

The token and site vars must be given to testing scripts either using TOKEN and SITE environment vars respectively or through of a config.json file into test/ folder like bellow:

{
  "site": "<site-url>",
  "token": "<token>"
}

Run tests:

$ make test-all

Also tests can be filtered using make test <filter>:

$ make test wpcom.site.post

License

MIT โ€“ Copyright 2014 Automattic

wpcom.js's People

Contributors

retrofox avatar tootallnate avatar dmsnell avatar nylen avatar mattsherman avatar timmyc avatar rauchg avatar rralian avatar bluefuton avatar chengfred avatar aduth avatar jkudish avatar johngodley avatar lezama avatar mjangda avatar rads avatar tug avatar

Watchers

Sean Lin avatar

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.