Skip to content

Commit

Permalink
feat: Add ra-data-amplify-rest
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Sep 7, 2019
1 parent 4d29ae3 commit 9f7b024
Show file tree
Hide file tree
Showing 62 changed files with 1,633 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Expand Up @@ -2,6 +2,7 @@
/coverage


/amplify-material-ui/lib
/packages/amplify-material-ui/lib
/packages/ra-data-amplify-rest/lib

node_modules
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -3,8 +3,8 @@
"private": true,
"scripts": {
"storybook": "yarn workspace amplify-material-ui storybook",
"grooming:outdated": "yarn outdated",
"grooming:upgrade": "yarn upgrade-interactive --latest",
"all:outdated": "yarn outdated",
"all:upgrade": "yarn upgrade-interactive --latest",
"lint": "eslint '*/**/*.{js,ts,tsx}' --cache --report-unused-disable-directives",
"lint:ci": "eslint '*/**/*.{js,ts,tsx}' --report-unused-disable-directives",
"lint:fix": "eslint '*/**/*.{js,ts,tsx}' --cache --fix",
Expand All @@ -14,7 +14,7 @@
},
"workspaces": [
"e2e-tests",
"amplify-material-ui"
"packages/*"
],
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^1.10.2",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions packages/ra-data-amplify-rest/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019

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.
19 changes: 19 additions & 0 deletions packages/ra-data-amplify-rest/README.md
@@ -0,0 +1,19 @@
# ra-admin-amplify-rest

> [aws amplify](https://github.com/aws-amplify/amplify-js) REST Data Provider for [react-admin](https://github.com/marmelab/react-admin), the frontend framework for building admin applications on top of REST/GraphQL services.
:warning: This is experimental and subject to breaking changes.

## Install

```sh
// with npm
npm install amplify-material-ui

// with yarn
yarn add amplify-material-ui
```

## License

[MIT](LICENSE)
35 changes: 35 additions & 0 deletions packages/ra-data-amplify-rest/package.json
@@ -0,0 +1,35 @@
{
"name": "ra-data-amplify-rest",
"version": "0.0.1",
"description": "aws amplify REST Data Provider for react-admin",
"license": "MIT",
"keywords": [
"react",
"aws amplify",
"amplify",
"react admin",
"rest",
"api"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib"
],
"scripts": {
"build": "rimraf ./lib && tsc",
"watch": "rimraf ./lib && tsc --watch",
"test": "jest",
"prepare": "npm run build"
},
"peerDependencies": {
"@aws-amplify/api": "^1.1.1",
"ra-core": "^3.0.0-alpha.4"
},
"devDependencies": {
"@aws-amplify/api": "^1.1.1",
"ra-core": "^3.0.0-alpha.4",
"rimraf": "^3.0.0",
"typescript": "^3.5.3"
}
}
38 changes: 38 additions & 0 deletions packages/ra-data-amplify-rest/src/build-data-provider.ts
@@ -0,0 +1,38 @@
import API from '@aws-amplify/api';

import { buildDataRequest } from './build-data-request';
import { parseResponse } from './parse-response';
import { RequestType, RequestParams, ApiCall, HttpMethod } from './types';

const apiCallMap = new Map<HttpMethod, ApiCall>([
[HttpMethod.GET, API.get],
[HttpMethod.PUT, API.put],
[HttpMethod.POST, API.post],
[HttpMethod.DELETE, API.del],
]);

export interface BuildDataProviderOptions {
apiName: string;
buildDataRequest: typeof buildDataRequest;
parseResponse: typeof parseResponse;
}

export const buildDataProvider = (options: BuildDataProviderOptions) => {
const { apiName, parseResponse, buildDataRequest } = options;

return async (
type: RequestType,
resource: string,
params: RequestParams,
) => {
const { path, method, init } = buildDataRequest(type, resource, params);

const apiCall = apiCallMap.get(method);

if (!apiCall) throw new Error(`Unsupported http method ${method}`);

const response = await apiCall(apiName, path, init);

return parseResponse(response, type, resource, params);
};
};
92 changes: 92 additions & 0 deletions packages/ra-data-amplify-rest/src/build-data-request.ts
@@ -0,0 +1,92 @@
import {
GET_LIST,
GET_ONE,
GET_MANY,
GET_MANY_REFERENCE,
CREATE,
UPDATE,
DELETE,
} from 'ra-core';

import { RequestType, RequestParams, Init, HttpMethod } from './types';

export const buildDataRequest = (
type: RequestType,
resource: string,
params: RequestParams,
) => {
let path = '';
let method: HttpMethod;
const init: Init = {};

switch (type) {
case GET_LIST: {
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
init.queryStringParameters = {
sort: [field, order],
range: [(page - 1) * perPage, page * perPage - 1],
filter: params.filter,
};
path = `/${resource}`;
method = HttpMethod.GET;
break;
}

case GET_ONE: {
path = `/${resource}/${params.id}`;
method = HttpMethod.GET;
break;
}

case GET_MANY: {
init.queryStringParameters = {
filter: { id: params.ids },
};
path = `/${resource}`;
method = HttpMethod.GET;
break;
}

case GET_MANY_REFERENCE: {
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
init.queryStringParameters = {
sort: [field, order],
range: [(page - 1) * perPage, page * perPage - 1],
filter: {
...params.filter,
[params.target]: params.id,
},
};
path = `/${resource}`;
method = HttpMethod.GET;
break;
}

case UPDATE: {
path = `/${resource}/${params.id}`;
method = HttpMethod.PUT;
init.body = params.data;
break;
}

case CREATE: {
path = `/${resource}`;
method = HttpMethod.POST;
init.body = params.data;
break;
}

case DELETE: {
path = `/${resource}/${params.id}`;
method = HttpMethod.DELETE;
break;
}

default:
throw new Error(`Unsupported fetch action type ${type}`);
}

return { path, method, init };
};
76 changes: 76 additions & 0 deletions packages/ra-data-amplify-rest/src/index.ts
@@ -0,0 +1,76 @@
import { UPDATE_MANY, DELETE_MANY, UPDATE, DELETE } from 'ra-core';

import { buildDataRequest } from './build-data-request';
import { parseResponse } from './parse-response';
import {
buildDataProvider,
BuildDataProviderOptions,
} from './build-data-provider';

import { RequestType, RequestParams } from './types';

const defaultOptions = {
buildDataRequest,
parseResponse,
};

export interface DataProviderOptions extends Partial<BuildDataProviderOptions> {
apiName: string;
}

export default (options: DataProviderOptions) => {
const dataProvider = buildDataProvider({ ...defaultOptions, ...options });

return async (
type: RequestType,
resource: string,
params: RequestParams,
) => {
if (type === UPDATE_MANY) {
const { ids, data, ...otherParams } = params;

if (!ids) return;

const responses = await Promise.all(
ids.map(id =>
dataProvider(UPDATE, resource, {
data: {
id,
...data,
},
...otherParams,
}),
),
);

return {
data: responses.map(response => response.data),
};
}

if (type === DELETE_MANY) {
const { ids, ...otherParams } = params;

if (!ids) return;

const responses = await Promise.all(
ids.map(id =>
dataProvider(DELETE, resource, {
id,
...otherParams,
}),
),
);

return {
data: responses.map(response => response.data),
};
}

return dataProvider(type, resource, params);
};
};

export { buildDataRequest, parseResponse, buildDataProvider };

export * from './types';
44 changes: 44 additions & 0 deletions packages/ra-data-amplify-rest/src/parse-response.ts
@@ -0,0 +1,44 @@
import { GET_LIST, GET_MANY_REFERENCE, CREATE, DELETE_MANY } from 'ra-core';

import { RequestType, RequestParams } from './types';

export const parseResponse = (
response: any,
type: RequestType,
resource: string,
params: RequestParams,
) => {
const { headers, data } = 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,
total: parseInt(
headers
.get('content-range')
.split('/')
.pop(),
10,
),
};
}

case CREATE: {
return { data: { ...params.data, id: data.id } };
}

case DELETE_MANY: {
return { data: data || [] };
}

default:
return { data };
}
};

0 comments on commit 9f7b024

Please sign in to comment.