Skip to content
This repository has been archived by the owner on May 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #50 from marmelab/0.9
Browse files Browse the repository at this point in the history
[RFR] Rewrite all restful for better future
  • Loading branch information
jpetitcolas committed Sep 28, 2015
2 parents c55aa1d + ea02097 commit b14af84
Show file tree
Hide file tree
Showing 45 changed files with 2,507 additions and 2,111 deletions.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "airbnb/base",
"rules": {
"indent": [2, 4]
}
}
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
node_modules
bower_components
dist/restful.js
7 changes: 0 additions & 7 deletions .jshintrc

This file was deleted.

2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ sudo: false
branches:
only:
- master
before_script:
- npm install request
27 changes: 16 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
.PHONY: build test
PATH := ${CURDIR}/node_modules/.bin:${PATH}

.PHONY: build es5 test

install:
npm install
bower install
npm install whatwg-fetch
npm install request

build: jshint
${CURDIR}/node_modules/.bin/webpack --optimize-minimize --output-file=restful.min.js
build:
NODE_ENV=production webpack

watch:
${CURDIR}/node_modules/.bin/webpack --watch
build-dev:
webpack

jshint:
./node_modules/jshint/bin/jshint src/**/*.js
./node_modules/jshint/bin/jshint test/**/*.js
es5:
${CURDIR}/node_modules/.bin/babel --out-dir=dist/es5 --stage=0 src

watch:
webpack -d --watch

test: build
CHROME_BIN=`which chromium-browser` ${CURDIR}/node_modules/karma/bin/karma start test/karma.conf.js --single-run
test:
NODE_ENV=test mocha --compilers js:babel/register --colors --reporter=spec --timeout=10000 test/{**,**/**,**/**/**}/*.js
336 changes: 199 additions & 137 deletions README.md

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions UPGRADE-0.9.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Upgrade to 0.9

For any change, refer to the README for more details.

## Initialization

Initialization of a restful object has changed.

## Native promise

The polyfill for native promise is no longer included in restful.js, you must include it on your own if needed.

## Targeting a custom url

All methods `customUrl`, `allUrl` and `oneUrl` are replaced by a `custom` method.

## Entities

The `remove` method of an entity is now named `delete`.

``` diff
- entity.remove()
+ entity.delete()
```

## HTTP methods

All HTTP methods have now normalized parameters in this order `data, params, headers`.
`data` depends on the method.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "restful.js",
"version": "0.6.1",
"version": "0.9.0",
"homepage": "https://github.com/marmelab/restful.js",
"authors": [
"Robin Bressan <[email protected]>"
Expand Down
7 changes: 7 additions & 0 deletions build/restful.fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import restful from '../src';
import fetchBackend from '../src/http/fetch';
import 'whatwg-fetch';

export default function(baseUrl, httpBackend = fetchBackend(fetch)) {
return restful(baseUrl, httpBackend);
}
3 changes: 3 additions & 0 deletions build/restful.standalone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import restful from '../src';

export default restful;
8 changes: 0 additions & 8 deletions dist/restful.min.js

This file was deleted.

36 changes: 23 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
{
"name": "restful.js",
"version": "0.6.2",
"version": "0.9.0",
"repository": "https://github.com/marmelab/restful.js",
"bugs": {
"url": "https://github.com/marmelab/restful.js/issues"
},
"description": "A pure JS client for interacting with server-side RESTful resources. Think Restangular without Angular.",
"scripts": {
"test": "make test"
},
"main": "dist/restful.min.js",
"main": "dist/es5",
"author": "Robin Bressan <[email protected]>",
"license": "MIT",
"dependencies": {
"babel": "~5.8.23",
"babel-loader": "~5.3.2",
"immutable": "~3.7.5",
"object-assign": "~2.0.0"
},
"devDependencies": {
"axios": "^0.5.2",
"babel-core": "^5.5.8",
"babel-loader": "^5.1.4",
"jshint": "^2.8.0",
"karma": "0.12.14",
"karma-chrome-launcher": "0.1.3",
"karma-jasmine": "0.1.5",
"karma-phantomjs-launcher": "0.1.4",
"karma-spec-reporter": "0.0.16",
"object-assign": "^3.0.0",
"webpack": "^1.9.11"
"babel-eslint": "~4.0.10",
"chai": "~3.2.0",
"eslint": "~1.2.1",
"eslint-config-airbnb": "~0.0.7",
"mocha": "~2.3.0",
"nock": "~2.12.0",
"sinon": "~1.16.1",
"webpack": "~1.9.11"
},
"optionnalDependencies": {
"request": "~2.62.0",
"whatwg-fetch": "~0.9.0"
}
}
36 changes: 36 additions & 0 deletions src/http/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export default function(fetch) {
return (config) => {
const url = config.url;
delete config.url;

if (config.data) {
config.body = /application\/json/.test(config.headers['Content-Type']) ? JSON.stringify(config.data) : config.data;
delete config.data;
}

return fetch(url, config)
.then((response) => {
return response.json().then((json) => {
const headers = {};

response.headers.forEach((value, name) => {
headers[name] = value;
});

const responsePayload = {
data: json,
headers: headers,
statusCode: response.status,
};

if (response.status >= 200 && response.status < 300) {
return responsePayload;
}

const error = new Error(response.statusText);
error.response = responsePayload;
throw error;
});
});
};
}
44 changes: 44 additions & 0 deletions src/http/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export default function(request) {
return (config) => {
if (config.data) {
config.form = /application\/json/.test(config.headers['Content-Type']) ? JSON.stringify(config.data) : config.data;
delete config.data;
}

if (config.params) {
config.qs = config.params;
delete config.params;
}

return new Promise((resolve, reject) => {
request(config, (err, response, body) => {
if (err) {
throw err;
}

let data;

try {
data = JSON.parse(body);
} catch (e) {
data = body;
}

const responsePayload = {
data,
headers: response.headers,
statusCode: response.statusCode,
};

if (response.statusCode >= 200 && response.statusCode < 300) {
return resolve(responsePayload);
}

const error = new Error(response.statusMessage);
error.response = responsePayload;

reject(error);
});
});
};
}
21 changes: 21 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import endpoint from './model/endpoint';
import fetchBackend from './http/fetch';
import http from './service/http';
import { member } from './model/decorator';
import requestBackend from './http/request';
import scope from './model/scope';

export default function(baseUrl, httpBackend) {
const rootScope = scope();
rootScope.assign('config', 'entityIdentifier', 'id');
rootScope.set('debug', false);
if (!baseUrl && typeof(window) !== undefined && window.location) {
rootScope.set('url', `${window.location.protocol}//${window.location.host}`);
} else {
rootScope.set('url', baseUrl);
}

return member(endpoint(http(httpBackend))(rootScope));
}

export { fetchBackend, requestBackend };
101 changes: 0 additions & 101 deletions src/model/collection.js

This file was deleted.

Loading

0 comments on commit b14af84

Please sign in to comment.