This repository has been archived by the owner on May 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from marmelab/0.9
[RFR] Rewrite all restful for better future
- Loading branch information
Showing
45 changed files
with
2,507 additions
and
2,111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "airbnb/base", | ||
"rules": { | ||
"indent": [2, 4] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
node_modules | ||
bower_components | ||
dist/restful.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ sudo: false | |
branches: | ||
only: | ||
- master | ||
before_script: | ||
- npm install request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import restful from '../src'; | ||
|
||
export default restful; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.