Skip to content

Commit

Permalink
fix: ApiMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Sep 8, 2019
1 parent ec48703 commit 44d17e6
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions packages/ra-data-amplify-rest/src/build-data-provider.ts
Expand Up @@ -4,12 +4,6 @@ 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.bind(this)],
[HttpMethod.PUT, API.put.bind(this)],
[HttpMethod.POST, API.post.bind(this)],
[HttpMethod.DELETE, API.del.bind(this)],
]);
export interface BuildDataProviderOptions {
apiName: string;
buildDataRequest: typeof buildDataRequest;
Expand All @@ -26,11 +20,28 @@ export const buildDataProvider = (options: BuildDataProviderOptions) => {
) => {
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);
let response;

switch (method) {
case HttpMethod.GET: {
response = await API.get(apiName, path, init);
break;
}
case HttpMethod.PUT: {
response = await API.put(apiName, path, init);
break;
}
case HttpMethod.POST: {
response = await API.post(apiName, path, init);
break;
}
case HttpMethod.DELETE: {
response = await API.del(apiName, path, init);
break;
}
default:
throw new Error(`Unsupported http method ${method}`);
}

return parseResponse(response, type, resource, params);
};
Expand Down

0 comments on commit 44d17e6

Please sign in to comment.