diff --git a/.babelrc b/.babelrc
new file mode 100644
index 0000000..831f20a
--- /dev/null
+++ b/.babelrc
@@ -0,0 +1,4 @@
+{
+ "presets": ["es2015"],
+ "plugins": ["transform-object-rest-spread"]
+}
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..83cf254
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+lib
+node_modules
+.npm
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..a00bc98
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2018 Carlos Almeida
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..9935399
--- /dev/null
+++ b/README.md
@@ -0,0 +1,101 @@
+# Loopback Client for react-admin
+For using [Loopback 3](https://loopback.io/) with [react-admin](https://github.com/marmelab/react-admin).
+
+## Installation
+
+```bash
+npm install --save react-admin-loopback
+```
+
+## Usage
+
+```js
+// in src/App.js
+import React from 'react';
+import { Admin, Resource } from 'react-admin';
+import loopbackClient, { authProvider } from 'react-admin-loopback';
+import { List, Datagrid, TextField, NumberField } from 'react-admin';
+
+import { ShowButton, EditButton, Edit, SimpleForm, DisabledInput, TextInput, NumberInput } from 'react-admin';
+import { Create} from 'react-admin';
+import { Show, SimpleShowLayout } from 'react-admin';
+
+const BookList = (props) => (
+
+
+
+
+
+
+
+
+);
+export const BookShow = (props) => (
+
+
+
+
+
+
+);
+export const BookEdit = (props) => (
+
+
+
+
+
+
+
+);
+export const BookCreate = (props) => (
+
+
+
+
+
+
+);
+const App = () => (
+
+
+
+);
+
+export default App;
+```
+
+The dataProvider supports include:
+
+```js
+// dataProvider.js
+import loopbackProvider from 'react-admin-loopback';
+
+const dataProvider = loopbackProvider('http://localhost:3000');
+export default (type, resource, params) =>
+ new Promise(resolve =>
+ setTimeout(() => resolve(dataProvider(type, resource, params)), 500)
+ );
+```
+
+```js
+
+import dataProvider from './dataProvider';
+
+...
+
+dataProvider(GET_LIST, 'books', {
+ filter: { hide: false },
+ sort: { field: 'id', order: 'DESC' },
+ pagination: { page: 1, perPage: 0 },
+ include: 'users'
+}).then(response => {
+ ...
+});
+
+...
+
+```
+
+## License
+
+This library is licensed under the [MIT Licence](LICENSE).
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..25b3c34
--- /dev/null
+++ b/package.json
@@ -0,0 +1,37 @@
+{
+ "name": "react-admin-loopback",
+ "version": "1.0.0",
+ "description": "Packages related to using Loopback with react-admin",
+ "main": "lib/index.js",
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1",
+ "build": "babel ./src -d lib --ignore '*.spec.js' --presets babel-preset-es2015"
+ },
+ "files": [
+ "*.md",
+ "lib",
+ "src"
+ ],
+ "devDependencies": {
+ "babel-cli": "^6.23.0",
+ "babel-core": "^6.23.1",
+ "babel-plugin-transform-object-rest-spread": "^6.23.0",
+ "babel-preset-es2015": "^6.22.0"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/darthwesker/react-admin-loopback.git"
+ },
+ "keywords": [
+ "react",
+ "react-admin",
+ "loopback",
+ "rest-client"
+ ],
+ "author": "Carlos Almeida",
+ "license": "MIT",
+ "bugs": {
+ "url": "https://github.com/darthwesker/react-admin-loopback/issues"
+ },
+ "homepage": "https://github.com/darthwesker/react-admin-loopback#readme"
+}
diff --git a/src/authProvider.js b/src/authProvider.js
new file mode 100644
index 0000000..73d0ba6
--- /dev/null
+++ b/src/authProvider.js
@@ -0,0 +1,46 @@
+import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_CHECK, AUTH_ERROR } from 'react-admin';
+import storage from './storage';
+
+export const authProvider = (loginApiUrl, noAccessPage = '/login') => {
+ return (type, params) => {
+ if (type === AUTH_LOGIN) {
+ const request = new Request(loginApiUrl, {
+ method: 'POST',
+ body: JSON.stringify(params),
+ headers: new Headers({ 'Content-Type': 'application/json' }),
+ });
+ return fetch(request)
+ .then(response => {
+ if (response.status < 200 || response.status >= 300) {
+ throw new Error(response.statusText);
+ }
+ return response.json();
+ })
+ .then(({ ttl, ...data }) => {
+ storage.save('lbtoken', data, ttl);
+ });
+ }
+ if (type === AUTH_LOGOUT) {
+ storage.remove('lbtoken');
+ return Promise.resolve();
+ }
+ if (type === AUTH_ERROR) {
+ const { status } = params;
+ if (status === 401 || status === 403) {
+ storage.remove('lbtoken');
+ return Promise.reject();
+ }
+ return Promise.resolve();
+ }
+ if (type === AUTH_CHECK) {
+ const token = storage.load('lbtoken');
+ if (token && token.id) {
+ return Promise.resolve();
+ } else {
+ storage.remove('lbtoken');
+ return Promise.reject({ redirectTo: noAccessPage });
+ }
+ }
+ return Promise.reject('Unkown method');
+ };
+};
diff --git a/src/fetch.js b/src/fetch.js
new file mode 100644
index 0000000..736a92b
--- /dev/null
+++ b/src/fetch.js
@@ -0,0 +1,10 @@
+import { fetchUtils } from 'react-admin';
+import storage from './storage';
+
+export default (url, options = {}) => {
+ options.user = {
+ authenticated: true,
+ token: storage.load('lbtoken').id
+ }
+ return fetchUtils.fetchJson(url, options);
+}
\ No newline at end of file
diff --git a/src/index.js b/src/index.js
new file mode 100644
index 0000000..c335468
--- /dev/null
+++ b/src/index.js
@@ -0,0 +1,170 @@
+import { stringify } from 'query-string';
+import fetchJson from './fetch';
+
+import {
+ GET_LIST,
+ GET_ONE,
+ GET_MANY,
+ GET_MANY_REFERENCE,
+ CREATE,
+ UPDATE,
+ UPDATE_MANY,
+ DELETE,
+ DELETE_MANY,
+} from 'react-admin';
+
+export * from './authProvider';
+export { default as storage } from './storage';
+
+export default (apiUrl, httpClient = fetchJson) => {
+ /**
+ * @param {String} type One of the constants appearing at the top if this file, e.g. 'UPDATE'
+ * @param {String} resource Name of the resource to fetch, e.g. 'posts'
+ * @param {Object} params The data request params, depending on the type
+ * @returns {Object} { url, options } The HTTP request parameters
+ */
+ const convertDataRequestToHTTP = (type, resource, params) => {
+ let url = '';
+ const options = {};
+ switch (type) {
+ case GET_LIST: {
+ const { page, perPage } = params.pagination;
+ const { field, order } = params.sort;
+ const include = params.include;
+ const query = {};
+ query['where'] = {...params.filter};
+ if (field) query['order'] = [field + ' ' + order];
+ if (perPage > 0) {
+ query['limit'] = perPage;
+ if (page >= 0) query['offset'] = (page - 1) * perPage;
+ }
+ if (include) query['include'] = include;
+ url = `${apiUrl}/${resource}?${stringify({filter: JSON.stringify(query)})}`;
+ break;
+ }
+ case GET_ONE:
+ url = `${apiUrl}/${resource}/${params.id}`;
+ break;
+ case GET_MANY: {
+ const query = {
+ filter: JSON.stringify({ id: params.ids }),
+ };
+ url = `${apiUrl}/${resource}?${stringify(query)}`;
+ break;
+ }
+ case GET_MANY_REFERENCE: {
+ const { page, perPage } = params.pagination;
+ const { field, order } = params.sort;
+ const include = params.include;
+ const query = {};
+ query['where'] = {...params.filter};
+ query['where'][params.target] = params.id;
+ if (field) query['order'] = [field + ' ' + order];
+ if (perPage > 0) {
+ query['limit'] = perPage;
+ if (page >= 0) query['skip'] = (page - 1) * perPage;
+ }
+ if (include) query['include'] = include;
+ url = `${apiUrl}/${resource}?${stringify({filter: JSON.stringify(query)})}`;
+ break;
+ }
+ case UPDATE:
+ url = `${apiUrl}/${resource}/${params.id}`;
+ options.method = 'PATCH';
+ options.body = JSON.stringify(params.data);
+ break;
+ case CREATE:
+ url = `${apiUrl}/${resource}`;
+ options.method = 'POST';
+ options.body = JSON.stringify(params.data);
+ break;
+ case DELETE:
+ url = `${apiUrl}/${resource}/${params.id}`;
+ options.method = 'DELETE';
+ break;
+ default:
+ throw new Error(`Unsupported fetch action type ${type}`);
+ }
+ return { url, options };
+ };
+
+ /**
+ * @param {Object} response HTTP response from fetch()
+ * @param {String} type One of the constants appearing at the top if this file, e.g. 'UPDATE'
+ * @param {String} resource Name of the resource to fetch, e.g. 'posts'
+ * @param {Object} params The data request params, depending on the type
+ * @returns {Object} Data response
+ */
+ const convertHTTPResponse = (response, type, resource, params) => {
+ const { headers, json } = response;
+ switch (type) {
+ case GET_LIST:
+ case GET_MANY_REFERENCE:
+ if (!headers.has('content-range')) {
+ throw new Error(
+ 'The Content-Range header is missing in the HTTP Response. The simple REST data provider expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?'
+ );
+ }
+ return {
+ data: json,
+ total: parseInt(
+ headers
+ .get('content-range')
+ .split('/')
+ .pop(),
+ 10
+ ),
+ };
+ case CREATE:
+ return { data: { ...params.data, id: json.id } };
+ case DELETE:
+ return { data: { ...json, id: params.id } };
+ default:
+ return { data: json };
+ }
+ };
+
+ /**
+ * @param {string} type Request type, e.g GET_LIST
+ * @param {string} resource Resource name, e.g. "posts"
+ * @param {Object} payload Request parameters. Depends on the request type
+ * @returns {Promise} the Promise for a data response
+ */
+ return (type, resource, params) => {
+ // simple-rest doesn't handle filters on UPDATE route, so we fallback to calling UPDATE n times instead
+ if (type === UPDATE_MANY) {
+ return Promise.all(
+ params.ids.map(id =>
+ httpClient(`${apiUrl}/${resource}/${id}`, {
+ method: 'PATCH',
+ body: JSON.stringify(params.data),
+ })
+ )
+ ).then(responses => ({
+ data: responses.map(response => response.json),
+ }));
+ }
+ // simple-rest doesn't handle filters on DELETE route, so we fallback to calling DELETE n times instead
+ if (type === DELETE_MANY) {
+ return Promise.all(
+ params.ids.map(id =>
+ httpClient(`${apiUrl}/${resource}/${id}`, {
+ method: 'DELETE',
+ })
+ )
+ ).then(responses => ({
+ data: responses.map(response => response.json),
+ }));
+ }
+
+ const { url, options } = convertDataRequestToHTTP(
+ type,
+ resource,
+ params
+ );
+
+ return httpClient(url, options).then(response =>
+ convertHTTPResponse(response, type, resource, params)
+ );
+ };
+};
\ No newline at end of file
diff --git a/src/storage.js b/src/storage.js
new file mode 100644
index 0000000..a94c4b3
--- /dev/null
+++ b/src/storage.js
@@ -0,0 +1,40 @@
+export default {
+ save : function(key, value, expirationSec){
+ if (typeof (Storage) === "undefined") { return false; }
+ var expirationMS = expirationSec * 1000;
+ var record = {value: value, timestamp: new Date().getTime() + expirationMS};
+ localStorage.setItem(key, JSON.stringify(record));
+
+ return value;
+ },
+ load : function(key){
+ if (typeof (Storage) === "undefined") { return false; }
+ try {
+ var record = JSON.parse(localStorage.getItem(key));
+ if (!record) {
+ return false;
+ }
+ return (new Date().getTime() < record.timestamp && record.value);
+ } catch (e) {
+ return false;
+ }
+ },
+ remove : function(key){
+ if (typeof (Storage) === "undefined") { return false; }
+ localStorage.removeItem(key);
+ },
+ update : function(key, value){
+ if (typeof (Storage) === "undefined") { return false; }
+ try {
+ var record = JSON.parse(localStorage.getItem(key));
+ if (!record) {
+ return false;
+ }
+ var updatedRecord = {value: value, timestamp: record.timestamp};
+ localStorage.setItem(key, JSON.stringify(updatedRecord));
+ return updatedRecord;
+ } catch (e) {
+ return false;
+ }
+ },
+};
\ No newline at end of file