close
Skip to content
This repository was archived by the owner on Sep 4, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"jsdom": "^6.5.1",
"jshint": "^2.8.0",
"mocha": "^2.1.0",
"node-fetch": "^1.3.3",
"request": "^2.56.0",
"socket.io-client": "^1.3.5",
"superagent": "^1.2.0",
Expand Down
23 changes: 22 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [jQuery](https://jquery.com/)
- [Superagent](http://visionmedia.github.io/superagent/)
- [request](https://github.com/request/request)
- Fetch: works in supported browsers, React Native or modules like [node-fetch](https://github.com/bitinn/node-fetch).
- Websockets (with real-time updates)
- [Socket.io](http://socket.io/)
- [Primus](https://github.com/primus/primus)
Expand Down Expand Up @@ -57,7 +58,7 @@ todoService.find().then(function(todos) {

## REST

Connecting to a Feathers service via the REST API is possible using [jQuery](https://jquery.com/), [request](https://github.com/request/request) or [Superagent](http://visionmedia.github.io/superagent/):
Connecting to a Feathers service via the REST API is possible using [jQuery](https://jquery.com/), [request](https://github.com/request/request), [Superagent](http://visionmedia.github.io/superagent/) or Fetch:

__Important__: REST client services do emit `created`, `updated`, `patched` and `removed` events but only _locally for their own instance_. Real-time events from other clients can only be received by using a websocket connection.

Expand Down Expand Up @@ -98,6 +99,15 @@ var app = feathers('http://todos.feathersjs.com')
.configure(feathers.superagent(superagent));
```

### Fetch

Fetch currently works with a default configuration:

```js
var app = feathers('http://todos.feathersjs.com')
.configure(feathers.fetch(fetch));
```

## Websockets

Websocket real-time connections can be established via [Socket.io](http://socket.io/) or [Primus](https://github.com/primus/primus). Websocket services emit all events that they receive allowing you to implement real-time functionality.
Expand Down Expand Up @@ -131,6 +141,17 @@ var app = feathers()
.configure(feathers.socketio(socket));
```

#### React Native

Fetch is included in React Native so you don't have to install any additional dependencies

Then pass in fetch:

```js
var app = feathers('http://todos.feathersjs.com')
.configure(feathers.fetch(fetch));
```

### Primus

[Primus](https://github.com/primus/primus) works similar to Socket.io:
Expand Down
39 changes: 39 additions & 0 deletions src/rest/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Base } from './base';

export class Service extends Base {
request(options) {
var fetchOptions = {
method: options.method,
headers: {
'Accept': 'application/json'
}
};

return new Promise((resolve, reject) => {
if (options.body) {
fetchOptions.body = JSON.stringify(options.body);
fetchOptions.headers['Content-Type'] = 'application/json';
}
this.connection(options.url, fetchOptions)
.then((response) => {
return response.json();
})
.then((result) => {
resolve(result);
}).catch((e) => {
reject(e);
});
});
}
}

export default function (fetch) {
if (!fetch) {
throw new Error('fetch needs to be provided');
}

return function () {
this.Service = Service;
this.connection = fetch;
};
}
3 changes: 2 additions & 1 deletion src/rest/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import jquery from './jquery';
import request from './request';
import superagent from './superagent';
import fetch from './fetch';

export default { jquery, request, superagent };
export default { jquery, request, superagent, fetch };
2 changes: 2 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ module.exports = function(service) {
assert.equal(typeof require('../lib/client'), 'function');
assert.equal(typeof require('../lib/client').jquery, 'function');
assert.equal(typeof require('../lib/client').request, 'function');
assert.equal(typeof require('../lib/client').superagent, 'function');
assert.equal(typeof require('../lib/client').fetch, 'function');
assert.equal(typeof require('../lib/client').socketio, 'function');
assert.equal(typeof require('../lib/client').primus, 'function');
});
Expand Down
22 changes: 22 additions & 0 deletions test/rest/fetch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fetch from 'node-fetch';

import app from '../fixture';
import baseTests from '../base';
import { Service } from '../../src/rest/fetch';

describe('fetch REST connector', function() {
let service = new Service('todos', {
base: 'http://localhost:8889',
connection: fetch
});

before(function(done) {
this.server = app().listen(8889, done);
});

after(function(done) {
this.server.close(done);
});

baseTests(service);
});