Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
final providers refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
adr1enbe4udou1n committed Sep 12, 2020
1 parent 46e34cb commit 0290056
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 424 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ run-tutorial: ## run the tutorial example
run-api-platform: ## run the api-platform example
@cd ./examples/api-platform && yarn serve --open --port 8000

run-demo-retail: ## run the retail demo example
@cd ./examples/demo-retail && yarn serve --open

run-laravel: ## serve laravel tutorial
@cd ./examples/laravel && php artisan serve

Expand Down
2 changes: 1 addition & 1 deletion packages/admin/src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Vue.component("draggable", draggable);
* Include guess logger on dev only
*/
if (process.env.NODE_ENV === "development") {
import("./utils/guess-logger").then(({ logger }) => {
import("./utils/guessLogger").then(({ logger }) => {
Vue.prototype.$guessLogger = logger;
});
}
85 changes: 29 additions & 56 deletions packages/admin/src/providers/data/hydra.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,80 +17,53 @@ export default (httpClient) => {
httpClient = new FetchHydra(httpClient);
}

const fetchApi = async (type, resource, params = {}) => {
switch (type) {
case GET_LIST: {
const { pagination, sort, filter } = params;

let query = filter || {};

if (pagination) {
query = {
...query,
page: pagination.page,
itemsPerPage: pagination.perPage,
};
}

if (sort && sort.length) {
query.order = {};

sort.forEach((item) => {
let { by, desc } = item;

query.order[by] = desc ? "desc" : "asc";
});
}
return {
[GET_LIST]: (resource, params) => {
const { pagination, sort, filter } = params;

return httpClient.get(
`${resource}?${qs.stringify(query, { arrayFormat: "repeat" })}`
);
}
let query = filter || {};

case GET_ONE: {
return httpClient.get(`${resource}/${params.id}`);
if (pagination) {
query = {
...query,
page: pagination.page,
itemsPerPage: pagination.perPage,
};
}

case CREATE: {
return httpClient.post(resource, params.data);
}
if (sort && sort.length) {
query.order = {};

case UPDATE: {
return httpClient.put(`${resource}/${params.id}`, params.data);
}
sort.forEach((item) => {
let { by, desc } = item;

case DELETE: {
return httpClient.delete(`${resource}/${params.id}`);
query.order[by] = desc ? "desc" : "asc";
});
}

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

return {
[GET_LIST]: (resource, params) => fetchApi(GET_LIST, resource, params),
return httpClient.get(
`${resource}?${qs.stringify(query, { arrayFormat: "repeat" })}`
);
},
[GET_MANY]: (resource, params) =>
Promise.all(
params.ids.map((id) =>
fetchApi(GET_ONE, resource, {
id: id.substring(id.lastIndexOf("/") + 1),
})
httpClient.get(`${resource}/${id.substring(id.lastIndexOf("/") + 1)}`)
)
).then((responses) => ({ data: responses.map(({ data }) => data) })),
[GET_ONE]: (resource, params) => fetchApi(GET_ONE, resource, params),
[CREATE]: (resource, params) => fetchApi(CREATE, resource, params),
[UPDATE]: (resource, params) => fetchApi(UPDATE, resource, params),
[GET_ONE]: (resource, params) => httpClient.get(`${resource}/${params.id}`),
[CREATE]: (resource, params) => httpClient.post(resource, params.data),
[UPDATE]: (resource, params) =>
httpClient.put(`${resource}/${params.id}`, params.data),
[UPDATE_MANY]: (resource, params) =>
Promise.all(
params.ids.map((id) =>
fetchApi(UPDATE, resource, { id, data: params.data })
)
params.ids.map((id) => httpClient.put(`${resource}/${id}`, params.data))
).then(() => Promise.resolve()),
[DELETE]: (resource, params) => fetchApi(DELETE, resource, params),
[DELETE]: (resource, params) =>
httpClient.delete(`${resource}/${params.id}`),
[DELETE_MANY]: (resource, params) =>
Promise.all(
params.ids.map((id) => fetchApi(DELETE, resource, { id }))
params.ids.map((id) => httpClient.delete(`${resource}/${id}`))
).then(() => Promise.resolve()),
};
};
131 changes: 0 additions & 131 deletions packages/admin/src/providers/data/json-server.js

This file was deleted.

97 changes: 97 additions & 0 deletions packages/admin/src/providers/data/jsonServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {
GET_LIST,
GET_MANY,
GET_ONE,
CREATE,
UPDATE,
UPDATE_MANY,
DELETE,
DELETE_MANY,
} from "./actions";

import FetchJson from "../utils/fetchJson";
import qs from "qs";

export default (httpClient) => {
if (typeof httpClient === "string") {
httpClient = new FetchJson(httpClient);
}

const withInclude = (params) => {
let query = {};

if (params.include) {
let { embed, expand } = params.include;
query = {
_embed: embed,
_expand: expand,
};
}

return query;
};

return {
[GET_LIST]: async (resource, params) => {
const { pagination, sort, filter } = params;
let query = {
...withInclude(params),
...filter,
};

if (pagination) {
let { page, perPage } = pagination;

query = {
...query,
_start: (page - 1) * perPage,
_end: page * perPage,
};
}

if (sort && sort.length) {
query = {
...query,
_sort: sort.map((item) => item.by),
_order: sort.map((item) => (item.desc ? "desc" : "asc")),
};
}

let { data, headers } = await httpClient.get(
`${resource}?${qs.stringify(query, { arrayFormat: "repeat" })}`
);

return {
data,
total: parseInt(headers["x-total-count"], 10),
};
},
[GET_MANY]: (resource, params) =>
httpClient.get(
`${resource}?${qs.stringify(
{
...withInclude(params),
id: params.ids,
},
{ arrayFormat: "repeat" }
)}`
),
[GET_ONE]: (resource, params) =>
httpClient.get(
`${resource}/${params.id}?${qs.stringify(withInclude(params))}`
),
[CREATE]: (resource, params) => httpClient.post(resource, params.data),
[UPDATE]: (resource, params) =>
httpClient.put(`${resource}/${params.id}`, params.data),
[UPDATE_MANY]: (resource, params) =>
Promise.all(
params.ids.map((id) => httpClient.put(`${resource}/${id}`, params.data))
).then(() => Promise.resolve()),
[DELETE]: (resource, params) =>
httpClient.delete(`${resource}/${params.id}`),
[DELETE_MANY]: (resource, params) =>
Promise.all(
params.ids.map((id) => httpClient.delete(`${resource}/${id}`))
).then(() => Promise.resolve()),
};
};
Loading

0 comments on commit 0290056

Please sign in to comment.