Skip to content

Commit

Permalink
Merge pull request #15 from Automattic/add/pluggable-req
Browse files Browse the repository at this point in the history
[WIP] Add pluggable "request" interface for interacting with the REST API
  • Loading branch information
retrofox authored and jsnajdr committed Jan 27, 2020
1 parent 6307aa8 commit 9a9237e
Show file tree
Hide file tree
Showing 23 changed files with 370 additions and 243 deletions.
11 changes: 6 additions & 5 deletions packages/wpcom.js/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dist
node_modules
test/data.json
npm-debug.log
tmp
/node_modules
/test/data.json
/npm-debug.log
/tmp

/example/express/node_modules
41 changes: 39 additions & 2 deletions packages/wpcom.js/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@

# get Makefile directory name: http://stackoverflow.com/a/5982798/376773
THIS_MAKEFILE_PATH:=$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
THIS_DIR:=$(shell cd $(dir $(THIS_MAKEFILE_PATH));pwd)

# BIN directory
BIN := $(THIS_DIR)/node_modules/.bin

# applications
NODE ?= node
NPM ?= $(NODE) $(shell which npm)
MOCHA ?= $(NODE) $(BIN)/mocha
BROWSERIFY ?= $(NODE) $(BIN)/browserify

all: dist/wpcom.js dist/wpcom-proxy.js

standalone-xhr: dist/wpcom.js

standalone-proxy: dist/wpcom-proxy.js

install: node_modules

clean:
@rm -rf node_modules dist

dist:
@mkdir -p $@

dist/wpcom.js: node_modules index.js wpcom+xhr.js dist lib/*
@$(BROWSERIFY) -s WPCOM wpcom+xhr.js > $@

dist/wpcom-proxy.js: node_modules index.js wpcom+proxy.js dist lib/*
@$(BROWSERIFY) -s WPCOM wpcom+proxy.js > $@

node_modules: package.json
@NODE_ENV= $(NPM) install
@touch node_modules

test:
@./node_modules/.bin/mocha \
@$(MOCHA) \
--require should \
--timeout 10s \
--slow 3s \
--bail \
--reporter spec


.PHONY: test
.PHONY: standalone install clean test
22 changes: 16 additions & 6 deletions packages/wpcom.js/readme.md → packages/wpcom.js/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
$ npm install wpcom
```


## How to use it

```js
Expand Down Expand Up @@ -64,9 +63,13 @@ site.posts(function(err, list){
});
```

## Example
## Examples

### Express

Into `example/express` folder:

Into `example/` folder download the npm dependencies:
... download the npm dependencies:

```bash
$ npm install
Expand All @@ -79,14 +82,22 @@ $ node index.js
```

Finally open a browser and load the page pointing to http://localhost:3000
Keep in mind that this app gets the config data from test/data.json file

### Node.js

Into `example/node` run:

```nash
$ node freshlyPressed.js
```

## Test

Create `data.json` file into `test/` folder to can run the tests. You can copy
or rename the `test/data_example.json` file.

```json

{
"token": {
"global": "<global token>"
Expand All @@ -108,7 +119,6 @@ or rename the `test/data_example.json` file.
"content": "<div style=\"color: red;\">The content of the new testing post</div>"
}
}

```

... and then
Expand All @@ -117,7 +127,7 @@ or rename the `test/data_example.json` file.
$ make
```

**Note**: don't add `http://` in`public_site` and `private_site` values.
**Note**: Don't add `http://` in `public_site` and `private_site` values.


## License
Expand Down
21 changes: 21 additions & 0 deletions packages/wpcom.js/example/browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>wpcom.js browser example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<script src="../../dist/wpcom-proxy.js"></script>
<script>
var wpcom = WPCOM();
wpcom.me().get(function(err, me){
if (err) throw err;
console.log(me);

var div = document.createElement('div');
div.innerHTML = 'Your WordPress.com "username" is: <b>@' + me.username + '<\/b>';
document.body.appendChild(div);
});
</script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,45 @@
*/

var express = require('express');
var WPCONN = require('../');
var WPCOM = require('../../');

/**
* wpcon app data
*/

var wpapp = require('../test/data');
var wpapp = require('../../test/data');

/**
* Create a WPCONN instance
* Create a WPCOM instance
*/

var wpconn = WPCONN(wpapp.token);
var wpcom = WPCOM();

// setup middleware

var app = express();
var pub = __dirname + '/public';
app.use(express.static(pub));

app.set('views', __dirname + '/');
app.set('view engine', 'jade');

app.get('/', function(req, res){
// set site id
wpconn.site.id(wpapp.public_site);
var site = wpcom.sites(wpapp.site.public.url);

// get site info
wpconn.site.info(function(err, site){
site.get(function(err, info){
if (err) return console.log(err);

// get lastest posts
wpconn.site.posts({ number: 10 }, function(err, posts) {
site.posts({ number: 10 }, function(err, posts) {
if (err) return console.log(err);

res.render('layout', { site: site, posts: posts });
res.render('layout', { site: info, posts: posts });
});
});
});

app.listen(3000);
console.log('WPConn app started on port 3000');
console.log('wpcom app started on port 3000');
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
doctype html
html
head
title wp-connect
link(rel="stylesheet", href="/stylesheets/style.css")
title wpcom.js
link(rel="stylesheet", href="/main.css")

body
.info
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{
"name": "wp-connect-testing-app",
"description": "A application to test the wp-connect",
"name": "wpcom-testing-app",
"description": "An application to test wpcom.js",
"version": "0.0.1",
"author": {
"name": "Damian Suarez",
Expand Down
36 changes: 36 additions & 0 deletions packages/wpcom.js/example/express/public/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
body {
font: 14px "Helvetica Nueue", "Lucida Grande", Arial, sans-serif;
padding: 50px 80px;
margin: 0;
}

h1 {
color: #a04;
}

h2 {
margin-top: 50px;
}

h2.err {
background-color: #fee;
color: red;
padding: 5px 20px;
}

h2.ok {
background-color: #efe;
color: #080;
padding: 5px 20px;
}

ul,
ul li {
list-style: none;
margin: 0;
padding: 0;
}

.author em {
color: #a04;
}
8 changes: 8 additions & 0 deletions packages/wpcom.js/example/node/freshlyPressed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

// anonymous auth since `freshlyPressed()` doesn't require auth
var WPCOM = require('../../')();

WPCOM.freshlyPressed(function (err, res) {
if (err) throw err;
console.log(res);
});
76 changes: 63 additions & 13 deletions packages/wpcom.js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@

var Me = require('./lib/me');
var Sites = require('./lib/sites');

var req = require('./lib/req');
var ends = require('./lib/endpoint');
var debug = require('debug')('wpcom');

/**
* WordPress REST-API class
* WordPress.com REST API class.
*
* @param {String} [token]
* @api public
*/

function WPCOM(token){
if (!(this instanceof WPCOM)) return new WPCOM(token);

this.tkn = token;

// request instance
this.req = new req(this);
function WPCOM(request){
if (!(this instanceof WPCOM)) return new WPCOM(request);
if ('function' !== typeof request) {
throw new TypeError('a `request` WP.com function must be passed in');
}
this.request = request;
}

/**
Expand All @@ -32,7 +29,7 @@ function WPCOM(token){
*/

WPCOM.prototype.me = function(){
return Me(this);
return new Me(this);
};

/**
Expand All @@ -43,7 +40,60 @@ WPCOM.prototype.me = function(){
*/

WPCOM.prototype.sites = function(id){
return Sites(id, this);
return new Sites(id, this);
};

/**
* List Freshly Pressed Posts
*
* @param {Object} params (optional)
* @param {Function} fn callback function
* @api public
*/

WPCOM.prototype.freshlyPressed = function(params, fn){
this.sendRequest('freshly-pressed.get', null, params, fn);
};

/**
* Request to WordPress REST API
*
* @param {String} type endpoint type
* @param {Object} vars to build endpoint
* @param {Object} params
* @param {Function} fn
* @api private
*/

WPCOM.prototype.sendRequest = function (type, vars, params, fn){
debug('sendRequest("%s")', type);

// params.query || callback function
if ('function' == typeof params.query) {
fn = params.query;
params.query = {};
}

if (!fn) fn = function(err){ if (err) throw err; };

// endpoint config object
var end = ends(type);

// request method
params.method = (params.method || end.method || 'GET').toUpperCase();

// build endpoint url
var endpoint = end.path;
if (vars) {
for (var k in vars) {
var rg = new RegExp("%" + k + "%");
endpoint = endpoint.replace(rg, vars[k]);
}
}
params.path = endpoint;
debug('endpoint: `%s`', endpoint);

this.request(params, fn);
};

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/wpcom.js/lib/endpoint/freshly-pressed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"get": {
"method": "GET",
"path": "/freshly-pressed"
}
}
Loading

0 comments on commit 9a9237e

Please sign in to comment.